cindex.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  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 */
  191. for (position = 0; position < ci->n_cats; position++) {
  192. if (ci->cat[position][0] >= cat) {
  193. break;
  194. }
  195. }
  196. G_debug(4, "position = %d", position);
  197. /* Move */
  198. for (i = ci->n_cats; i > position; i--) {
  199. ci->cat[i][0] = ci->cat[i - 1][0];
  200. ci->cat[i][1] = ci->cat[i - 1][1];
  201. ci->cat[i][2] = ci->cat[i - 1][2];
  202. }
  203. ci->cat[position][0] = cat;
  204. ci->cat[position][1] = type;
  205. ci->cat[position][2] = line;
  206. ci->n_cats++;
  207. /* Add type */
  208. found = 0;
  209. for (i = 0; i < ci->n_types; i++) {
  210. if (ci->type[i][0] == type) {
  211. ci->type[i][1]++;
  212. found = 1;
  213. }
  214. }
  215. if (!found) {
  216. ci->type[ci->n_types][0] = type;
  217. ci->type[ci->n_types][1] = 1;
  218. ci->n_types++;
  219. }
  220. /* Sort by field */
  221. qsort(Plus->cidx, Plus->n_cidx, sizeof(struct Cat_index), cmp_field);
  222. G_debug(3, "Added new category to index");
  223. return 1;
  224. }
  225. /*
  226. * dig_cidx_del_cat ()
  227. * delete old field - cat - line record from _sorted_ category index
  228. *
  229. * returns 1 OK
  230. * 0 on error
  231. */
  232. int
  233. dig_cidx_del_cat(struct Plus_head *Plus, int field, int cat, int line,
  234. int type)
  235. {
  236. int i, position;
  237. struct Cat_index *ci;
  238. G_debug(3, "dig_cidx_del_cat(): field = %d cat = %d line = %d", field,
  239. cat, line);
  240. /* Find field or add new */
  241. ci = NULL;
  242. for (i = 0; i < Plus->n_cidx; i++) {
  243. if (Plus->cidx[i].field == field) {
  244. ci = &(Plus->cidx[i]);
  245. }
  246. }
  247. if (ci == NULL) { /* should not happen */
  248. G_warning("BUG: Category index not found for field %d.", field);
  249. return 0;
  250. }
  251. /* Find position */
  252. G_debug(3, "n_cats = %d", ci->n_cats);
  253. for (position = 0; position < ci->n_cats; position++) {
  254. if (ci->cat[position][0] == cat && ci->cat[position][1] == type &&
  255. ci->cat[position][2] == line) {
  256. break;
  257. }
  258. }
  259. G_debug(4, "position = %d", position);
  260. if (position == ci->n_cats) {
  261. G_warning("BUG: Category not found in category index.");
  262. return 0;
  263. }
  264. /* Delete */
  265. for (i = position; i < ci->n_cats - 1; i++) {
  266. ci->cat[i][0] = ci->cat[i + 1][0];
  267. ci->cat[i][1] = ci->cat[i + 1][1];
  268. ci->cat[i][2] = ci->cat[i + 1][2];
  269. }
  270. ci->n_cats--;
  271. for (i = 0; i < ci->n_types; i++) {
  272. if (ci->type[i][0] == type) {
  273. ci->type[i][1]--;
  274. }
  275. }
  276. G_debug(3, "Deleted from category index");
  277. return 1;
  278. }
  279. /*
  280. * dig_cidx_sort ()
  281. * sort all records in cat index
  282. *
  283. */
  284. void dig_cidx_sort(struct Plus_head *Plus)
  285. {
  286. int f;
  287. struct Cat_index *ci;
  288. G_debug(2, "dig_cidx_sort()");
  289. for (f = 0; f < Plus->n_cidx; f++) {
  290. int c, nucats = 0;
  291. ci = &(Plus->cidx[f]);
  292. /* Sort by category */
  293. qsort(ci->cat, ci->n_cats, 3 * sizeof(int), cmp_cat);
  294. /* Calculate number of unique cats */
  295. if (ci->n_cats > 0)
  296. nucats++;
  297. for (c = 1; c < ci->n_cats; c++) {
  298. if (ci->cat[c][0] != ci->cat[c - 1][0])
  299. nucats++;
  300. }
  301. ci->n_ucats = nucats;
  302. }
  303. /* Sort by field */
  304. qsort(Plus->cidx, Plus->n_cidx, sizeof(struct Cat_index), cmp_field);
  305. }