cindex.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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. if (Plus->cidx) {
  52. G_free(Plus->cidx);
  53. Plus->cidx = NULL;
  54. }
  55. Plus->a_cidx = 0;
  56. Plus->n_cidx = 0;
  57. Plus->cidx_up_to_date = 0;
  58. }
  59. /*
  60. * dig_cidx_add_cat ()
  61. * add new field - cat - line record, space is allocated if necessary
  62. *
  63. * returns 1 OK
  64. * 0 on error
  65. */
  66. int
  67. dig_cidx_add_cat(struct Plus_head *Plus, int field, int cat, int line,
  68. int type)
  69. {
  70. int i, si, found;
  71. struct Cat_index *ci;
  72. G_debug(3, "dig_cidx_add_cat(): field = %d cat = %d line = %d type = %d",
  73. field, cat, line, type);
  74. /* Find field or add new */
  75. si = -1;
  76. for (i = 0; i < Plus->n_cidx; i++) {
  77. if (Plus->cidx[i].field == field) {
  78. si = i;
  79. }
  80. }
  81. if (si == -1) { /* not found add new */
  82. if (Plus->n_cidx == Plus->a_cidx) {
  83. Plus->a_cidx += 10;
  84. Plus->cidx =
  85. (struct Cat_index *)G_realloc(Plus->cidx,
  86. Plus->a_cidx *
  87. sizeof(struct Cat_index));
  88. if (!Plus->cidx)
  89. return 0;
  90. }
  91. si = Plus->n_cidx;
  92. ci = &(Plus->cidx[si]);
  93. ci->field = field;
  94. ci->n_cats = ci->a_cats = 0;
  95. ci->cat = NULL;
  96. ci->n_types = 0;
  97. ci->offset = 0;
  98. Plus->n_cidx++;
  99. }
  100. /* Add new cat - line record */
  101. ci = &(Plus->cidx[si]);
  102. if (ci->n_cats == ci->a_cats) {
  103. ci->a_cats += 5000;
  104. ci->cat = G_realloc(ci->cat, ci->a_cats * 3 * sizeof(int));
  105. }
  106. ci->cat[ci->n_cats][0] = cat;
  107. ci->cat[ci->n_cats][1] = type;
  108. ci->cat[ci->n_cats][2] = line;
  109. ci->n_cats++;
  110. /* Add type */
  111. found = 0;
  112. for (i = 0; i < ci->n_types; i++) {
  113. if (ci->type[i][0] == type) {
  114. ci->type[i][1]++;
  115. found = 1;
  116. }
  117. }
  118. if (!found) {
  119. ci->type[ci->n_types][0] = type;
  120. ci->type[ci->n_types][1] = 1;
  121. ci->n_types++;
  122. }
  123. return 1;
  124. }
  125. /* Compare by cat */
  126. static int cmp_cat(const void *pa, const void *pb)
  127. {
  128. int *p1 = (int *)pa;
  129. int *p2 = (int *)pb;
  130. if (p1[0] < p2[0])
  131. return -1;
  132. if (p1[0] > p2[0])
  133. return 1;
  134. return 0;
  135. }
  136. /* Compare by field */
  137. static int cmp_field(const void *pa, const void *pb)
  138. {
  139. struct Cat_index *p1 = (struct Cat_index *)pa;
  140. struct Cat_index *p2 = (struct Cat_index *)pb;
  141. if (p1->field < p2->field)
  142. return -1;
  143. if (p1->field > p2->field)
  144. return 1;
  145. return 0;
  146. }
  147. /*
  148. * dig_cidx_add_cat_sorted ()
  149. * add new field - cat - line record to sorted category index, space is allocated if necessary
  150. *
  151. * returns 1 OK
  152. * 0 on error
  153. */
  154. int
  155. dig_cidx_add_cat_sorted(struct Plus_head *Plus, int field, int cat, int line,
  156. int type)
  157. {
  158. int i, si, found, position;
  159. struct Cat_index *ci;
  160. G_debug(3,
  161. "dig_cidx_add_cat_sorted(): field = %d cat = %d line = %d type = %d",
  162. field, cat, line, type);
  163. /* Find field or add new */
  164. si = -1;
  165. for (i = 0; i < Plus->n_cidx; i++) {
  166. if (Plus->cidx[i].field == field) {
  167. si = i;
  168. }
  169. }
  170. if (si == -1) { /* not found add new */
  171. if (Plus->n_cidx == Plus->a_cidx) {
  172. Plus->a_cidx += 10;
  173. Plus->cidx =
  174. (struct Cat_index *)G_realloc(Plus->cidx,
  175. Plus->a_cidx *
  176. sizeof(struct Cat_index));
  177. if (!Plus->cidx)
  178. return 0;
  179. }
  180. si = Plus->n_cidx;
  181. ci = &(Plus->cidx[si]);
  182. ci->field = field;
  183. ci->n_cats = ci->a_cats = 0;
  184. ci->cat = NULL;
  185. ci->n_types = 0;
  186. ci->offset = 0;
  187. Plus->n_cidx++;
  188. }
  189. /* Add new cat - line record */
  190. ci = &(Plus->cidx[si]);
  191. if (ci->n_cats == ci->a_cats) {
  192. ci->a_cats += 5000;
  193. ci->cat = G_realloc(ci->cat, ci->a_cats * 3 * sizeof(int));
  194. }
  195. /* Find position and move on the way */
  196. for (position = ci->n_cats; position > 0; position--) {
  197. if (ci->cat[position - 1][0] < cat ||
  198. (ci->cat[position - 1][0] == cat && ci->cat[position - 1][1] <= type)) {
  199. break;
  200. }
  201. ci->cat[position][0] = ci->cat[position - 1][0];
  202. ci->cat[position][1] = ci->cat[position - 1][1];
  203. ci->cat[position][2] = ci->cat[position - 1][2];
  204. }
  205. G_debug(4, "position = %d", position);
  206. ci->cat[position][0] = cat;
  207. ci->cat[position][1] = type;
  208. ci->cat[position][2] = line;
  209. ci->n_cats++;
  210. /* Add type */
  211. found = 0;
  212. for (i = 0; i < ci->n_types; i++) {
  213. if (ci->type[i][0] == type) {
  214. ci->type[i][1]++;
  215. found = 1;
  216. }
  217. }
  218. if (!found) {
  219. ci->type[ci->n_types][0] = type;
  220. ci->type[ci->n_types][1] = 1;
  221. ci->n_types++;
  222. }
  223. /* Sort by field */
  224. qsort(Plus->cidx, Plus->n_cidx, sizeof(struct Cat_index), cmp_field);
  225. G_debug(3, "Added new category to index");
  226. return 1;
  227. }
  228. /*
  229. * dig_cidx_del_cat ()
  230. * delete old field - cat - line record from _sorted_ category index
  231. *
  232. * returns 1 OK
  233. * 0 on error
  234. */
  235. int
  236. dig_cidx_del_cat(struct Plus_head *Plus, int field, int cat, int line,
  237. int type)
  238. {
  239. int i, position;
  240. struct Cat_index *ci;
  241. G_debug(3, "dig_cidx_del_cat(): field = %d cat = %d line = %d", field,
  242. cat, line);
  243. /* Find field or add new */
  244. ci = NULL;
  245. for (i = 0; i < Plus->n_cidx; i++) {
  246. if (Plus->cidx[i].field == field) {
  247. ci = &(Plus->cidx[i]);
  248. }
  249. }
  250. if (ci == NULL) { /* should not happen */
  251. G_warning("BUG: Category index not found for field %d.", field);
  252. return 0;
  253. }
  254. /* Find position */
  255. G_debug(3, "n_cats = %d", ci->n_cats);
  256. for (position = 0; position < ci->n_cats; position++) {
  257. if (ci->cat[position][0] == cat && ci->cat[position][1] == type &&
  258. ci->cat[position][2] == line) {
  259. break;
  260. }
  261. }
  262. G_debug(4, "position = %d", position);
  263. if (position == ci->n_cats) {
  264. G_warning("BUG: Category not found in category index.");
  265. return 0;
  266. }
  267. /* Delete */
  268. for (i = position; i < ci->n_cats - 1; i++) {
  269. ci->cat[i][0] = ci->cat[i + 1][0];
  270. ci->cat[i][1] = ci->cat[i + 1][1];
  271. ci->cat[i][2] = ci->cat[i + 1][2];
  272. }
  273. ci->n_cats--;
  274. for (i = 0; i < ci->n_types; i++) {
  275. if (ci->type[i][0] == type) {
  276. ci->type[i][1]--;
  277. }
  278. }
  279. G_debug(3, "Deleted from category index");
  280. return 1;
  281. }
  282. /*
  283. * dig_cidx_sort ()
  284. * sort all records in cat index
  285. *
  286. */
  287. void dig_cidx_sort(struct Plus_head *Plus)
  288. {
  289. int f;
  290. struct Cat_index *ci;
  291. G_debug(2, "dig_cidx_sort()");
  292. for (f = 0; f < Plus->n_cidx; f++) {
  293. int c, nucats = 0;
  294. ci = &(Plus->cidx[f]);
  295. /* Sort by category */
  296. qsort(ci->cat, ci->n_cats, 3 * sizeof(int), cmp_cat);
  297. /* Calculate number of unique cats */
  298. if (ci->n_cats > 0)
  299. nucats++;
  300. for (c = 1; c < ci->n_cats; c++) {
  301. if (ci->cat[c][0] != ci->cat[c - 1][0])
  302. nucats++;
  303. }
  304. ci->n_ucats = nucats;
  305. }
  306. /* Sort by field */
  307. qsort(Plus->cidx, Plus->n_cidx, sizeof(struct Cat_index), cmp_field);
  308. }