list.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. /**
  2. * \file list.c
  3. *
  4. * \brief Vector library - list definition
  5. *
  6. * Higher level functions for reading/writing/manipulating vectors.
  7. *
  8. * (C) 2001-2008 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. * Update to GRASS 5.7 Radim Blazek and David D. Gray
  16. *
  17. * \date 2001-2008
  18. */
  19. #include <stdlib.h>
  20. #include <grass/Vect.h>
  21. #include <grass/gis.h>
  22. /**
  23. * \brief Creates and initializes a struct ilist.
  24. *
  25. * This structure is used as container for integer values. The
  26. * library routines handle all memory allocation.
  27. *
  28. * \return pointer to struct ilist
  29. * \return NULL on error
  30. */
  31. struct ilist *Vect_new_list(void)
  32. {
  33. struct ilist *p;
  34. p = (struct ilist *)G_malloc(sizeof(struct ilist));
  35. if (p) {
  36. p->value = NULL;
  37. p->n_values = 0;
  38. p->alloc_values = 0;
  39. }
  40. return p;
  41. }
  42. /**
  43. * \brief Reset ilist structure.
  44. *
  45. * To make sure ilist structure is clean to be re-used. List must have
  46. * previously been created with Vect_new_list().
  47. *
  48. * \param[in,out] list pointer to struct ilist
  49. *
  50. * \return 0
  51. */
  52. int Vect_reset_list(struct ilist *list)
  53. {
  54. list->n_values = 0;
  55. return 0;
  56. }
  57. /**
  58. * \brief Frees all memory associated with a struct ilist, including
  59. * the struct itself
  60. *
  61. * \param[in,out] list pointer to ilist structure
  62. *
  63. * \return 0
  64. */
  65. int Vect_destroy_list(struct ilist *list)
  66. {
  67. if (list) { /* probably a moot test */
  68. if (list->alloc_values) {
  69. G_free((void *)list->value);
  70. }
  71. G_free((void *)list);
  72. }
  73. list = NULL;
  74. return 0;
  75. }
  76. /**
  77. * \brief Append new item to the end of list if not yet present
  78. *
  79. * \param[in,out] list pointer to ilist structure
  80. * \param[in] val new item to append to the end of list
  81. *
  82. * \return 0 on success
  83. * \return 1 on error
  84. */
  85. int Vect_list_append(struct ilist *list, int val)
  86. {
  87. int i;
  88. size_t size;
  89. if (list == NULL)
  90. return 1;
  91. for (i = 0; i < list->n_values; i++) {
  92. if (val == list->value[i])
  93. return 0;
  94. }
  95. if (list->n_values == list->alloc_values) {
  96. size = (list->n_values + 1000) * sizeof(int);
  97. list->value = (int *)G_realloc((void *)list->value, size);
  98. list->alloc_values = list->n_values + 1000;
  99. }
  100. list->value[list->n_values] = val;
  101. list->n_values++;
  102. return 0;
  103. }
  104. /**
  105. * \brief Append new items to the end of list if not yet present
  106. *
  107. * \param[in,out] alist pointer to ilist structure where items will be appended
  108. * \param[in] blist pointer to ilist structure with new items
  109. *
  110. * \return 0 on success
  111. * \return 1 on error
  112. */
  113. int Vect_list_append_list(struct ilist *alist, struct ilist *blist)
  114. {
  115. int i;
  116. if (alist == NULL || blist == NULL)
  117. return 1;
  118. for (i = 0; i < blist->n_values; i++)
  119. Vect_list_append(alist, blist->value[i]);
  120. return 0;
  121. }
  122. /**
  123. * \brief Remove a given value (item) from list
  124. *
  125. * \param[in,out] list pointer to ilist structure
  126. * \param[in] val to remove
  127. *
  128. * \return 0 on success
  129. * \return 1 on error
  130. */
  131. int Vect_list_delete(struct ilist *list, int val)
  132. {
  133. int i, j;
  134. if (list == NULL)
  135. return 1;
  136. for (i = 0; i < list->n_values; i++) {
  137. if (val == list->value[i]) {
  138. for (j = i + 1; j < list->n_values; j++)
  139. list->value[j - 1] = list->value[j];
  140. list->n_values--;
  141. return 0;
  142. }
  143. }
  144. return 0;
  145. }
  146. /**
  147. * \brief Delete list from existing list
  148. *
  149. * \param[in,out] alist pointer to original ilist structure,
  150. * \param[in] blist pointer to ilist structure with items to delete
  151. *
  152. * \return 0 on success
  153. * \return 1 on error
  154. */
  155. int Vect_list_delete_list(struct ilist *alist, struct ilist *blist)
  156. {
  157. int i;
  158. if (alist == NULL || blist == NULL)
  159. return 1;
  160. for (i = 0; i < blist->n_values; i++)
  161. Vect_list_delete(alist, blist->value[i]);
  162. return 0;
  163. }
  164. /**
  165. * \brief Find a given item in the list
  166. *
  167. * \param[in] list pointer to ilist structure
  168. * \param[in] val value of item
  169. *
  170. * \return 1 if an item is found
  171. * \return 0 no found item in the list
  172. */
  173. int Vect_val_in_list(struct ilist *list, int val)
  174. {
  175. int i;
  176. if (list == NULL)
  177. return 0;
  178. for (i = 0; i < list->n_values; i++) {
  179. if (val == list->value[i])
  180. return 1;
  181. }
  182. return 0;
  183. }