list.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  1. /*!
  2. * \file lib/vector/Vlib/list.c
  3. *
  4. * \brief Vector library - list definition
  5. *
  6. * Higher level functions for reading/writing/manipulating vectors.
  7. *
  8. * (C) 2001-2009 by the GRASS Development Team
  9. *
  10. * This program is free software under the GNU General Public
  11. * License (>=v2). Read the file COPYING that comes with GRASS
  12. * for details.
  13. *
  14. * \author Original author CERL, probably Dave Gerdes or Mike Higgins.
  15. * \author Update to GRASS 5.7 Radim Blazek and David D. Gray
  16. * \author Update to GRASS 7 Markus Metz
  17. */
  18. #include <stdlib.h>
  19. #include <grass/vector.h>
  20. /**
  21. * \brief Creates and initializes a struct ilist.
  22. *
  23. * This structure is used as container for integer values. The
  24. * library routines handle all memory allocation.
  25. *
  26. * \return pointer to struct ilist
  27. * \return NULL on error
  28. */
  29. struct ilist *Vect_new_list(void)
  30. {
  31. struct ilist *p;
  32. p = (struct ilist *)G_malloc(sizeof(struct ilist));
  33. if (p) {
  34. p->value = NULL;
  35. p->n_values = 0;
  36. p->alloc_values = 0;
  37. }
  38. return p;
  39. }
  40. /**
  41. * \brief Reset ilist structure.
  42. *
  43. * To make sure ilist structure is clean to be re-used. List must have
  44. * previously been created with Vect_new_list().
  45. *
  46. * \param[in,out] list pointer to struct ilist
  47. *
  48. * \return 0
  49. */
  50. int Vect_reset_list(struct ilist *list)
  51. {
  52. list->n_values = 0;
  53. return 0;
  54. }
  55. /**
  56. * \brief Frees all memory associated with a struct ilist, including
  57. * the struct itself
  58. *
  59. * \param[in,out] list pointer to ilist structure
  60. */
  61. void Vect_destroy_list(struct ilist *list)
  62. {
  63. if (list) { /* probably a moot test */
  64. if (list->alloc_values) {
  65. G_free((void *)list->value);
  66. }
  67. G_free((void *)list);
  68. }
  69. list = NULL;
  70. }
  71. /**
  72. * \brief Append new item to the end of list if not yet present
  73. *
  74. * \param[in,out] list pointer to ilist structure
  75. * \param val new item to append to the end of list
  76. *
  77. * \return 0 on success
  78. * \return 1 on error
  79. */
  80. int Vect_list_append(struct ilist *list, int val)
  81. {
  82. int i;
  83. size_t size;
  84. if (list == NULL)
  85. return 1;
  86. for (i = 0; i < list->n_values; i++) {
  87. if (val == list->value[i])
  88. return 0;
  89. }
  90. if (list->n_values == list->alloc_values) {
  91. size = (list->n_values + 1000) * sizeof(int);
  92. list->value = (int *)G_realloc((void *)list->value, size);
  93. list->alloc_values = list->n_values + 1000;
  94. }
  95. list->value[list->n_values] = val;
  96. list->n_values++;
  97. return 0;
  98. }
  99. /**
  100. * \brief Append new items to the end of list if not yet present
  101. *
  102. * \param[in,out] alist pointer to ilist structure where items will be appended
  103. * \param blist pointer to ilist structure with new items
  104. *
  105. * \return 0 on success
  106. * \return 1 on error
  107. */
  108. int Vect_list_append_list(struct ilist *alist, const struct ilist *blist)
  109. {
  110. int i;
  111. if (alist == NULL || blist == NULL)
  112. return 1;
  113. for (i = 0; i < blist->n_values; i++)
  114. Vect_list_append(alist, blist->value[i]);
  115. return 0;
  116. }
  117. /**
  118. * \brief Remove a given value (item) from list
  119. *
  120. * \param[in,out] list pointer to ilist structure
  121. * \param val to remove
  122. *
  123. * \return 0 on success
  124. * \return 1 on error
  125. */
  126. int Vect_list_delete(struct ilist *list, int val)
  127. {
  128. int i, j;
  129. if (list == NULL)
  130. return 1;
  131. for (i = 0; i < list->n_values; i++) {
  132. if (val == list->value[i]) {
  133. for (j = i + 1; j < list->n_values; j++)
  134. list->value[j - 1] = list->value[j];
  135. list->n_values--;
  136. return 0;
  137. }
  138. }
  139. return 0;
  140. }
  141. /**
  142. * \brief Delete list from existing list
  143. *
  144. * \param[in,out] alist pointer to original ilist structure,
  145. * \param blist pointer to ilist structure with items to delete
  146. *
  147. * \return 0 on success
  148. * \return 1 on error
  149. */
  150. int Vect_list_delete_list(struct ilist *alist, const struct ilist *blist)
  151. {
  152. int i;
  153. if (alist == NULL || blist == NULL)
  154. return 1;
  155. for (i = 0; i < blist->n_values; i++)
  156. Vect_list_delete(alist, blist->value[i]);
  157. return 0;
  158. }
  159. /**
  160. * \brief Find a given item in the list
  161. *
  162. * \param list pointer to ilist structure
  163. * \param val value of item
  164. *
  165. * \return 1 if an item is found
  166. * \return 0 no found item in the list
  167. */
  168. int Vect_val_in_list(const struct ilist *list, int val)
  169. {
  170. int i;
  171. if (list == NULL)
  172. return 0;
  173. for (i = 0; i < list->n_values; i++) {
  174. if (val == list->value[i])
  175. return 1;
  176. }
  177. return 0;
  178. }
  179. /* box list routines */
  180. /**
  181. * \brief Creates and initializes a struct boxlist.
  182. *
  183. * This structure is used as container for bounding boxes with id. The
  184. * library routines handle all memory allocation.
  185. *
  186. * \param have_boxes if set to 0, the list will hold only ids and no boxes
  187. *
  188. * \return pointer to struct boxlist
  189. * \return NULL on error
  190. */
  191. struct boxlist *Vect_new_boxlist(int have_boxes)
  192. {
  193. struct boxlist *p;
  194. p = (struct boxlist *)G_malloc(sizeof(struct boxlist));
  195. if (p) {
  196. p->id = NULL;
  197. p->box = NULL;
  198. p->have_boxes = have_boxes != 0;
  199. p->n_values = 0;
  200. p->alloc_values = 0;
  201. }
  202. return p;
  203. }
  204. /**
  205. * \brief Reset boxlist structure.
  206. *
  207. * To make sure boxlist structure is clean to be re-used. List must have
  208. * previously been created with Vect_new_boxlist().
  209. *
  210. * \param[in,out] list pointer to struct boxlist
  211. *
  212. * \return 0
  213. */
  214. int Vect_reset_boxlist(struct boxlist *list)
  215. {
  216. list->n_values = 0;
  217. return 0;
  218. }
  219. /**
  220. * \brief Frees all memory associated with a struct boxlist, including
  221. * the struct itself
  222. *
  223. * \param[in,out] list pointer to ilist structure
  224. */
  225. void Vect_destroy_boxlist(struct boxlist *list)
  226. {
  227. if (list) { /* probably a moot test */
  228. if (list->alloc_values) {
  229. G_free((void *)list->id);
  230. if (list->box)
  231. G_free((void *)list->box);
  232. }
  233. G_free((void *)list);
  234. }
  235. list = NULL;
  236. }
  237. /**
  238. * \brief Append new item to the end of list if not yet present
  239. *
  240. * \param[in,out] list pointer to ilist structure
  241. * \param id new item to append to the end of list
  242. * \param box bounding box
  243. *
  244. * \return 0 on success
  245. * \return 1 on error
  246. */
  247. int Vect_boxlist_append(struct boxlist *list, int id, const struct bound_box *box)
  248. {
  249. int i;
  250. size_t size;
  251. if (list == NULL)
  252. return 1;
  253. for (i = 0; i < list->n_values; i++) {
  254. if (id == list->id[i])
  255. return 0;
  256. }
  257. if (list->n_values == list->alloc_values) {
  258. size = (list->n_values + 1000) * sizeof(int);
  259. list->id = (int *)G_realloc((void *)list->id, size);
  260. if (list->have_boxes) {
  261. size = (list->n_values + 1000) * sizeof(struct bound_box);
  262. list->box = (struct bound_box *)G_realloc((void *)list->box, size);
  263. }
  264. list->alloc_values = list->n_values + 1000;
  265. }
  266. list->id[list->n_values] = id;
  267. if (list->have_boxes)
  268. list->box[list->n_values] = *box;
  269. list->n_values++;
  270. return 0;
  271. }
  272. /**
  273. * \brief Append new items to the end of list if not yet present
  274. *
  275. * \param[in,out] alist pointer to boxlist structure where items will be appended
  276. * \param blist pointer to boxlist structure with new items
  277. *
  278. * \return 0 on success
  279. * \return 1 on error
  280. */
  281. int Vect_boxlist_append_boxlist(struct boxlist *alist, const struct boxlist *blist)
  282. {
  283. int i;
  284. if (alist == NULL || blist == NULL)
  285. return 1;
  286. if (blist->have_boxes) {
  287. for (i = 0; i < blist->n_values; i++)
  288. Vect_boxlist_append(alist, blist->id[i], &blist->box[i]);
  289. }
  290. else {
  291. struct bound_box box;
  292. box.E = box.W = box.N = box.S = box.T = box.B = 0;
  293. for (i = 0; i < blist->n_values; i++)
  294. Vect_boxlist_append(alist, blist->id[i], &box);
  295. }
  296. return 0;
  297. }
  298. /**
  299. * \brief Remove a given value (item) from list
  300. *
  301. * \param[in,out] list pointer to boxlist structure
  302. * \param id to remove
  303. *
  304. * \return 0 on success
  305. * \return 1 on error
  306. */
  307. int Vect_boxlist_delete(struct boxlist *list, int id)
  308. {
  309. int i, j;
  310. if (list == NULL)
  311. return 1;
  312. for (i = 0; i < list->n_values; i++) {
  313. if (id == list->id[i]) {
  314. for (j = i + 1; j < list->n_values; j++) {
  315. list->id[j - 1] = list->id[j];
  316. if (list->have_boxes)
  317. list->box[j - 1] = list->box[j];
  318. }
  319. list->n_values--;
  320. return 0;
  321. }
  322. }
  323. return 0;
  324. }
  325. /**
  326. * \brief Delete list from existing list
  327. *
  328. * \param[in,out] alist pointer to original boxlist structure,
  329. * \param blist pointer to boxlist structure with items to delete
  330. *
  331. * \return 0 on success
  332. * \return 1 on error
  333. */
  334. int Vect_boxlist_delete_boxlist(struct boxlist *alist, const struct boxlist *blist)
  335. {
  336. int i;
  337. if (alist == NULL || blist == NULL)
  338. return 1;
  339. for (i = 0; i < blist->n_values; i++)
  340. Vect_boxlist_delete(alist, blist->id[i]);
  341. return 0;
  342. }
  343. /**
  344. * \brief Find a given item in the list
  345. *
  346. * \param list pointer to boxlist structure
  347. * \param id value of item
  348. *
  349. * \return 1 if an item is found
  350. * \return 0 no found item in the list
  351. */
  352. int Vect_val_in_boxlist(const struct boxlist *list, int id)
  353. {
  354. int i;
  355. if (list == NULL)
  356. return 0;
  357. for (i = 0; i < list->n_values; i++) {
  358. if (id == list->id[i])
  359. return 1;
  360. }
  361. return 0;
  362. }