cindex.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. /*****************************************************************************
  2. *
  3. * MODULE: Vector library
  4. *
  5. * AUTHOR(S): Radim Blazek
  6. *
  7. * PURPOSE: Lower level functions for reading/writing/manipulating vectors.
  8. *
  9. * COPYRIGHT: (C) 2001 by the GRASS Development Team
  10. *
  11. * This program is free software under the GNU General Public
  12. * License (>=v2). Read the file COPYING that comes with GRASS
  13. * for details.
  14. *
  15. *****************************************************************************/
  16. #include <stdlib.h>
  17. #include <string.h>
  18. #include <grass/vector.h>
  19. /*!
  20. * \brief Initialize Plus_head structure (cidx)
  21. *
  22. * \param Plus pointer to Plus_head structure
  23. *
  24. * \return 1 OK
  25. * \return 0 on error
  26. */
  27. int dig_cidx_init(struct Plus_head *Plus)
  28. {
  29. G_debug(3, "dig_cidx_init()");
  30. Plus->n_cidx = 0;
  31. Plus->a_cidx = 5;
  32. Plus->cidx =
  33. (struct Cat_index *)G_malloc(Plus->a_cidx * sizeof(struct Cat_index));
  34. if (!Plus->cidx)
  35. return 0;
  36. Plus->cidx_up_to_date = 0;
  37. return 1;
  38. }
  39. /* Free category index */
  40. void dig_cidx_free(struct Plus_head *Plus)
  41. {
  42. int i;
  43. struct Cat_index *ci;
  44. G_debug(2, "dig_cidx_free()");
  45. for (i = 0; i < Plus->n_cidx; i++) {
  46. ci = &(Plus->cidx[0]);
  47. G_free(ci->cat);
  48. ci->cat = NULL;
  49. ci->field = ci->n_cats = ci->a_cats = ci->n_types = 0;
  50. }
  51. Plus->n_cidx = 0;
  52. Plus->cidx_up_to_date = 0;
  53. }
  54. /*
  55. * dig_cidx_add_cat ()
  56. * add new field - cat - line record, space is allocated if necessary
  57. *
  58. * returns 1 OK
  59. * 0 on error
  60. */
  61. int
  62. dig_cidx_add_cat(struct Plus_head *Plus, int field, int cat, int line,
  63. int type)
  64. {
  65. int i, si, found;
  66. struct Cat_index *ci;
  67. G_debug(3, "dig_cidx_add_cat(): field = %d cat = %d line = %d type = %d",
  68. field, cat, line, type);
  69. /* Find field or add new */
  70. si = -1;
  71. for (i = 0; i < Plus->n_cidx; i++) {
  72. if (Plus->cidx[i].field == field) {
  73. si = i;
  74. }
  75. }
  76. if (si == -1) { /* not found add new */
  77. if (Plus->n_cidx == Plus->a_cidx) {
  78. Plus->a_cidx += 10;
  79. Plus->cidx =
  80. (struct Cat_index *)G_realloc(Plus->cidx,
  81. Plus->a_cidx *
  82. sizeof(struct Cat_index));
  83. if (!Plus->cidx)
  84. return 0;
  85. }
  86. si = Plus->n_cidx;
  87. ci = &(Plus->cidx[si]);
  88. ci->field = field;
  89. ci->n_cats = ci->a_cats = 0;
  90. ci->cat = NULL;
  91. ci->n_types = 0;
  92. ci->offset = 0;
  93. Plus->n_cidx++;
  94. }
  95. /* Add new cat - line record */
  96. ci = &(Plus->cidx[si]);
  97. if (ci->n_cats == ci->a_cats) {
  98. ci->a_cats += 5000;
  99. ci->cat = G_realloc(ci->cat, ci->a_cats * 3 * sizeof(int));
  100. }
  101. ci->cat[ci->n_cats][0] = cat;
  102. ci->cat[ci->n_cats][1] = type;
  103. ci->cat[ci->n_cats][2] = line;
  104. ci->n_cats++;
  105. /* Add type */
  106. found = 0;
  107. for (i = 0; i < ci->n_types; i++) {
  108. if (ci->type[i][0] == type) {
  109. ci->type[i][1]++;
  110. found = 1;
  111. }
  112. }
  113. if (!found) {
  114. ci->type[ci->n_types][0] = type;
  115. ci->type[ci->n_types][1] = 1;
  116. ci->n_types++;
  117. }
  118. return 1;
  119. }
  120. /* Compare by cat */
  121. static int cmp_cat(const void *pa, const void *pb)
  122. {
  123. int *p1 = (int *)pa;
  124. int *p2 = (int *)pb;
  125. if (p1[0] < p2[0])
  126. return -1;
  127. if (p1[0] > p2[0])
  128. return 1;
  129. return 0;
  130. }
  131. /* Compare by field */
  132. static int cmp_field(const void *pa, const void *pb)
  133. {
  134. struct Cat_index *p1 = (struct Cat_index *)pa;
  135. struct Cat_index *p2 = (struct Cat_index *)pb;
  136. if (p1->field < p2->field)
  137. return -1;
  138. if (p1->field > p2->field)
  139. return 1;
  140. return 0;
  141. }
  142. /*
  143. * dig_cidx_add_cat_sorted ()
  144. * add new field - cat - line record to sorted category index, space is allocated if necessary
  145. *
  146. * returns 1 OK
  147. * 0 on error
  148. */
  149. int
  150. dig_cidx_add_cat_sorted(struct Plus_head *Plus, int field, int cat, int line,
  151. int type)
  152. {
  153. int i, si, found, position;
  154. struct Cat_index *ci;
  155. G_debug(3,
  156. "dig_cidx_add_cat_sorted(): field = %d cat = %d line = %d type = %d",
  157. field, cat, line, type);
  158. /* Find field or add new */
  159. si = -1;
  160. for (i = 0; i < Plus->n_cidx; i++) {
  161. if (Plus->cidx[i].field == field) {
  162. si = i;
  163. }
  164. }
  165. if (si == -1) { /* not found add new */
  166. if (Plus->n_cidx == Plus->a_cidx) {
  167. Plus->a_cidx += 10;
  168. Plus->cidx =
  169. (struct Cat_index *)G_realloc(Plus->cidx,
  170. Plus->a_cidx *
  171. sizeof(struct Cat_index));
  172. if (!Plus->cidx)
  173. return 0;
  174. }
  175. si = Plus->n_cidx;
  176. ci = &(Plus->cidx[si]);
  177. ci->field = field;
  178. ci->n_cats = ci->a_cats = 0;
  179. ci->cat = NULL;
  180. ci->n_types = 0;
  181. ci->offset = 0;
  182. Plus->n_cidx++;
  183. }
  184. /* Add new cat - line record */
  185. ci = &(Plus->cidx[si]);
  186. if (ci->n_cats == ci->a_cats) {
  187. ci->a_cats += 5000;
  188. ci->cat = G_realloc(ci->cat, ci->a_cats * 3 * sizeof(int));
  189. }
  190. /* Find position and move on the way */
  191. for (position = ci->n_cats; position > 0; position--) {
  192. if (ci->cat[position - 1][0] < cat ||
  193. (ci->cat[position - 1][0] == cat && ci->cat[position - 1][1] <= type)) {
  194. break;
  195. }
  196. ci->cat[position][0] = ci->cat[position - 1][0];
  197. ci->cat[position][1] = ci->cat[position - 1][1];
  198. ci->cat[position][2] = ci->cat[position - 1][2];
  199. }
  200. G_debug(4, "position = %d", position);
  201. ci->cat[position][0] = cat;
  202. ci->cat[position][1] = type;
  203. ci->cat[position][2] = line;
  204. ci->n_cats++;
  205. /* Add type */
  206. found = 0;
  207. for (i = 0; i < ci->n_types; i++) {
  208. if (ci->type[i][0] == type) {
  209. ci->type[i][1]++;
  210. found = 1;
  211. }
  212. }
  213. if (!found) {
  214. ci->type[ci->n_types][0] = type;
  215. ci->type[ci->n_types][1] = 1;
  216. ci->n_types++;
  217. }
  218. /* Sort by field */
  219. qsort(Plus->cidx, Plus->n_cidx, sizeof(struct Cat_index), cmp_field);
  220. G_debug(3, "Added new category to index");
  221. return 1;
  222. }
  223. /*
  224. * dig_cidx_del_cat ()
  225. * delete old field - cat - line record from _sorted_ category index
  226. *
  227. * returns 1 OK
  228. * 0 on error
  229. */
  230. int
  231. dig_cidx_del_cat(struct Plus_head *Plus, int field, int cat, int line,
  232. int type)
  233. {
  234. int i, position;
  235. struct Cat_index *ci;
  236. G_debug(3, "dig_cidx_del_cat(): field = %d cat = %d line = %d", field,
  237. cat, line);
  238. /* Find field or add new */
  239. ci = NULL;
  240. for (i = 0; i < Plus->n_cidx; i++) {
  241. if (Plus->cidx[i].field == field) {
  242. ci = &(Plus->cidx[i]);
  243. }
  244. }
  245. if (ci == NULL) { /* should not happen */
  246. G_warning("BUG: Category index not found for field %d.", field);
  247. return 0;
  248. }
  249. /* Find position */
  250. G_debug(3, "n_cats = %d", ci->n_cats);
  251. for (position = 0; position < ci->n_cats; position++) {
  252. if (ci->cat[position][0] == cat && ci->cat[position][1] == type &&
  253. ci->cat[position][2] == line) {
  254. break;
  255. }
  256. }
  257. G_debug(4, "position = %d", position);
  258. if (position == ci->n_cats) {
  259. G_warning("BUG: Category not found in category index.");
  260. return 0;
  261. }
  262. /* Delete */
  263. for (i = position; i < ci->n_cats - 1; i++) {
  264. ci->cat[i][0] = ci->cat[i + 1][0];
  265. ci->cat[i][1] = ci->cat[i + 1][1];
  266. ci->cat[i][2] = ci->cat[i + 1][2];
  267. }
  268. ci->n_cats--;
  269. for (i = 0; i < ci->n_types; i++) {
  270. if (ci->type[i][0] == type) {
  271. ci->type[i][1]--;
  272. }
  273. }
  274. G_debug(3, "Deleted from category index");
  275. return 1;
  276. }
  277. /*
  278. * dig_cidx_sort ()
  279. * sort all records in cat index
  280. *
  281. */
  282. void dig_cidx_sort(struct Plus_head *Plus)
  283. {
  284. int f;
  285. struct Cat_index *ci;
  286. G_debug(2, "dig_cidx_sort()");
  287. for (f = 0; f < Plus->n_cidx; f++) {
  288. int c, nucats = 0;
  289. ci = &(Plus->cidx[f]);
  290. /* Sort by category */
  291. qsort(ci->cat, ci->n_cats, 3 * sizeof(int), cmp_cat);
  292. /* Calculate number of unique cats */
  293. if (ci->n_cats > 0)
  294. nucats++;
  295. for (c = 1; c < ci->n_cats; c++) {
  296. if (ci->cat[c][0] != ci->cat[c - 1][0])
  297. nucats++;
  298. }
  299. ci->n_ucats = nucats;
  300. }
  301. /* Sort by field */
  302. qsort(Plus->cidx, Plus->n_cidx, sizeof(struct Cat_index), cmp_field);
  303. }