list.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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/vector.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. void Vect_destroy_list(struct ilist *list)
  63. {
  64. if (list) { /* probably a moot test */
  65. if (list->alloc_values) {
  66. G_free((void *)list->value);
  67. }
  68. G_free((void *)list);
  69. }
  70. list = NULL;
  71. }
  72. /**
  73. * \brief Append new item to the end of list if not yet present
  74. *
  75. * \param[in,out] list pointer to ilist structure
  76. * \param val new item to append to the end of list
  77. *
  78. * \return 0 on success
  79. * \return 1 on error
  80. */
  81. int Vect_list_append(struct ilist *list, int val)
  82. {
  83. int i;
  84. size_t size;
  85. if (list == NULL)
  86. return 1;
  87. for (i = 0; i < list->n_values; i++) {
  88. if (val == list->value[i])
  89. return 0;
  90. }
  91. if (list->n_values == list->alloc_values) {
  92. size = (list->n_values + 1000) * sizeof(int);
  93. list->value = (int *)G_realloc((void *)list->value, size);
  94. list->alloc_values = list->n_values + 1000;
  95. }
  96. list->value[list->n_values] = val;
  97. list->n_values++;
  98. return 0;
  99. }
  100. /**
  101. * \brief Append new items to the end of list if not yet present
  102. *
  103. * \param[in,out] alist pointer to ilist structure where items will be appended
  104. * \param blist pointer to ilist structure with new items
  105. *
  106. * \return 0 on success
  107. * \return 1 on error
  108. */
  109. int Vect_list_append_list(struct ilist *alist, const struct ilist *blist)
  110. {
  111. int i;
  112. if (alist == NULL || blist == NULL)
  113. return 1;
  114. for (i = 0; i < blist->n_values; i++)
  115. Vect_list_append(alist, blist->value[i]);
  116. return 0;
  117. }
  118. /**
  119. * \brief Remove a given value (item) from list
  120. *
  121. * \param[in,out] list pointer to ilist structure
  122. * \param val to remove
  123. *
  124. * \return 0 on success
  125. * \return 1 on error
  126. */
  127. int Vect_list_delete(struct ilist *list, int val)
  128. {
  129. int i, j;
  130. if (list == NULL)
  131. return 1;
  132. for (i = 0; i < list->n_values; i++) {
  133. if (val == list->value[i]) {
  134. for (j = i + 1; j < list->n_values; j++)
  135. list->value[j - 1] = list->value[j];
  136. list->n_values--;
  137. return 0;
  138. }
  139. }
  140. return 0;
  141. }
  142. /**
  143. * \brief Delete list from existing list
  144. *
  145. * \param[in,out] alist pointer to original ilist structure,
  146. * \param blist pointer to ilist structure with items to delete
  147. *
  148. * \return 0 on success
  149. * \return 1 on error
  150. */
  151. int Vect_list_delete_list(struct ilist *alist, const struct ilist *blist)
  152. {
  153. int i;
  154. if (alist == NULL || blist == NULL)
  155. return 1;
  156. for (i = 0; i < blist->n_values; i++)
  157. Vect_list_delete(alist, blist->value[i]);
  158. return 0;
  159. }
  160. /**
  161. * \brief Find a given item in the list
  162. *
  163. * \param list pointer to ilist structure
  164. * \param val value of item
  165. *
  166. * \return 1 if an item is found
  167. * \return 0 no found item in the list
  168. */
  169. int Vect_val_in_list(const struct ilist *list, int val)
  170. {
  171. int i;
  172. if (list == NULL)
  173. return 0;
  174. for (i = 0; i < list->n_values; i++) {
  175. if (val == list->value[i])
  176. return 1;
  177. }
  178. return 0;
  179. }