list.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  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 val new item to append to the end of list
  242. *
  243. * \return 0 on success
  244. * \return 1 on error
  245. */
  246. int Vect_boxlist_append(struct boxlist *list, int id, struct bound_box box)
  247. {
  248. int i;
  249. size_t size;
  250. if (list == NULL)
  251. return 1;
  252. for (i = 0; i < list->n_values; i++) {
  253. if (id == list->id[i])
  254. return 0;
  255. }
  256. if (list->n_values == list->alloc_values) {
  257. size = (list->n_values + 1000) * sizeof(int);
  258. list->id = (int *)G_realloc((void *)list->id, size);
  259. if (list->have_boxes) {
  260. size = (list->n_values + 1000) * sizeof(struct bound_box);
  261. list->box = (struct bound_box *)G_realloc((void *)list->box, size);
  262. }
  263. list->alloc_values = list->n_values + 1000;
  264. }
  265. list->id[list->n_values] = id;
  266. if (list->have_boxes)
  267. list->box[list->n_values] = box;
  268. list->n_values++;
  269. return 0;
  270. }
  271. /**
  272. * \brief Append new items to the end of list if not yet present
  273. *
  274. * \param[in,out] alist pointer to boxlist structure where items will be appended
  275. * \param blist pointer to boxlist structure with new items
  276. *
  277. * \return 0 on success
  278. * \return 1 on error
  279. */
  280. int Vect_boxlist_append_boxlist(struct boxlist *alist, const struct boxlist *blist)
  281. {
  282. int i;
  283. if (alist == NULL || blist == NULL)
  284. return 1;
  285. if (blist->have_boxes) {
  286. for (i = 0; i < blist->n_values; i++)
  287. Vect_boxlist_append(alist, blist->id[i], blist->box[i]);
  288. }
  289. else {
  290. struct bound_box box;
  291. box.E = box.W = box.N = box.S = box.T = box.B = 0;
  292. for (i = 0; i < blist->n_values; i++)
  293. Vect_boxlist_append(alist, blist->id[i], box);
  294. }
  295. return 0;
  296. }
  297. /**
  298. * \brief Remove a given value (item) from list
  299. *
  300. * \param[in,out] list pointer to boxlist structure
  301. * \param val to remove
  302. *
  303. * \return 0 on success
  304. * \return 1 on error
  305. */
  306. int Vect_boxlist_delete(struct boxlist *list, int id)
  307. {
  308. int i, j;
  309. if (list == NULL)
  310. return 1;
  311. for (i = 0; i < list->n_values; i++) {
  312. if (id == list->id[i]) {
  313. for (j = i + 1; j < list->n_values; j++) {
  314. list->id[j - 1] = list->id[j];
  315. if (list->have_boxes)
  316. list->box[j - 1] = list->box[j];
  317. }
  318. list->n_values--;
  319. return 0;
  320. }
  321. }
  322. return 0;
  323. }
  324. /**
  325. * \brief Delete list from existing list
  326. *
  327. * \param[in,out] alist pointer to original boxlist structure,
  328. * \param blist pointer to boxlist structure with items to delete
  329. *
  330. * \return 0 on success
  331. * \return 1 on error
  332. */
  333. int Vect_boxlist_delete_boxlist(struct boxlist *alist, const struct boxlist *blist)
  334. {
  335. int i;
  336. if (alist == NULL || blist == NULL)
  337. return 1;
  338. for (i = 0; i < blist->n_values; i++)
  339. Vect_boxlist_delete(alist, blist->id[i]);
  340. return 0;
  341. }
  342. /**
  343. * \brief Find a given item in the list
  344. *
  345. * \param list pointer to boxlist structure
  346. * \param val value of item
  347. *
  348. * \return 1 if an item is found
  349. * \return 0 no found item in the list
  350. */
  351. int Vect_val_in_boxlist(const struct boxlist *list, int id)
  352. {
  353. int i;
  354. if (list == NULL)
  355. return 0;
  356. for (i = 0; i < list->n_values; i++) {
  357. if (id == list->id[i])
  358. return 1;
  359. }
  360. return 0;
  361. }