list.c 4.2 KB

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