vector.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. /*!
  2. Temelia - Vector interface.
  3. Copyright (C) 2008, 2009 Ceata (http://cod.ceata.org/proiecte/temelia).
  4. @author Dascalu Laurentiu
  5. This program is free software; you can redistribute it and
  6. modify it under the terms of the GNU General Public License
  7. as published by the Free Software Foundation; either version 3
  8. of the License, or (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  16. */
  17. #ifndef VECTOR_H_
  18. #define VECTOR_H_
  19. #include "platform.h"
  20. struct _vector_t;
  21. typedef struct _vector_t *vector_t;
  22. /*!
  23. * @brief Returns an empty vector with maximum capacity keys.
  24. * Default capacity increment step is capacity.
  25. * O(1) complexity
  26. *
  27. * @param Vector's capacity
  28. */
  29. DECLSPEC vector_t vector_new(int capacity);
  30. /*!
  31. * @brief Frees the memory allocated by the library for this vector.
  32. * O(1) complexity
  33. *
  34. * @param Vector
  35. */
  36. DECLSPEC void vector_delete(vector_t vector);
  37. /*!
  38. * @brief Clear method. Sets all internal pointers to NULL.
  39. * O(n) complexity
  40. *
  41. * @param Vector
  42. */
  43. DECLSPEC void vector_clear(vector_t vector);
  44. /*!
  45. * @brief Returns vector's capacity.
  46. * O(1) complexity
  47. *
  48. * @param Vector
  49. */
  50. DECLSPEC int vector_get_capacity(vector_t vector);
  51. /*!
  52. * @brief Returns vector's number of keys.
  53. * O(1) complexity
  54. *
  55. * @param Vector
  56. */
  57. DECLSPEC int vector_get_size(vector_t vector);
  58. /*!
  59. * @brief Returns vector's capacity increment step.
  60. * O(1) complexity
  61. *
  62. * @param Vector
  63. */
  64. DECLSPEC int vector_get_capacity_increment(vector_t vector);
  65. /*!
  66. * @brief Returns vector's data internal representation; cast it to void **.
  67. * O(1) complexity
  68. *
  69. * @param Vector
  70. */
  71. DECLSPEC void *vector_get_internal_representation(vector_t vector);
  72. /*!
  73. * @brief Sets vector's number of keys to a new value. If new number is bigger
  74. * then the old one, the last new vector keys will have NULL value; if not, then
  75. * you may loose some keys. The vector it's not adjusting it's capacity.
  76. * O(1) complexity
  77. *
  78. * @param Vector
  79. * @param New keys number.
  80. */
  81. DECLSPEC void vector_set_size(vector_t vector, int new_size);
  82. /*!
  83. * @brief Sets the capacity increment of the current vector.
  84. * O(1) complexity
  85. *
  86. * @param Vector
  87. * @param New capacity increment value
  88. */
  89. DECLSPEC void vector_set_capacity_increment(vector_t vector, int new_capacity_increment);
  90. /*!
  91. * @brief Sets an key, at specified position, to a value in vector.
  92. * O(1) complexity
  93. *
  94. * @param Vector
  95. * @param Position
  96. * @param Pointer to key
  97. */
  98. DECLSPEC void vector_set_key_at(vector_t vector, int position, void *key);
  99. /*!
  100. * @brief Returns component at a specified position in vector.
  101. * O(1) complexity
  102. *
  103. * @param Vector
  104. * @param Position
  105. */
  106. DECLSPEC void *vector_get_key_at(vector_t vector, int position);
  107. /*!
  108. * @brief Swaps the keys from given indexes.
  109. * O(1) complexity
  110. *
  111. * @param Vector
  112. * @param First index
  113. * @param Second index
  114. */
  115. DECLSPEC void vector_swap(vector_t vector, int index1, int index2);
  116. /*!
  117. * @brief Pushes key to the front of vector.
  118. * O(n) complexity
  119. *
  120. * @param Vector
  121. * @param key.
  122. */
  123. DECLSPEC void vector_push_front(vector_t vector, void *key);
  124. /*!
  125. * @brief Pushes key to the back of vector.
  126. * O(1) complexity
  127. *
  128. * @param Vector
  129. * @param key.
  130. */
  131. DECLSPEC void vector_push_back(vector_t vector, void *key);
  132. /*!
  133. * @brief Inserts before_key before key. If F is not found then E is pushed front.
  134. * O(n) complexity
  135. *
  136. * @param Address of pointer to vector.
  137. * @param Key about to add in
  138. * @param Pointer to searched key
  139. * @param Pointer to comparison function
  140. */
  141. DECLSPEC void vector_push_before(vector_t vector, void *new_key, void *key, int compare(
  142. void *x, void *y, void *context), void *context);
  143. /*!
  144. * @brief Inserts in vector an key after_key after another key key.
  145. * If key is not found then after_key is pushed back.
  146. * O(n) complexity
  147. *
  148. * @param Vector
  149. * @param Key about to add in
  150. * @param Pointer to searched key
  151. * @param Pointer to comparison function
  152. */
  153. DECLSPEC void vector_push_after(vector_t vector, void *new_key, void *key, int compare(
  154. void *x, void *y, void *context), void *context);
  155. /*!
  156. * @brief Removes the key from the vector.
  157. * O(n) complexity
  158. *
  159. * @param Vector
  160. * @param Key
  161. * @param Pointer to comparison function
  162. */
  163. DECLSPEC void *vector_remove_key(vector_t vector, void *key, int compare(void *x,
  164. void *y, void *context), void *context);
  165. /*!
  166. * @brief Removes key given by its index.
  167. * O(n) complexity
  168. *
  169. * @param Vector
  170. * @param Index
  171. */
  172. DECLSPEC void *vector_remove_key_at(vector_t vector, int index);
  173. /*!
  174. * @brief Returns and removes last key.
  175. * O(1) complexity
  176. *
  177. * @param Vector
  178. */
  179. DECLSPEC void *vector_pop_back(vector_t vector);
  180. /*!
  181. * @brief Returns and removes first key.
  182. * O(n) complexity
  183. *
  184. * @param Vector
  185. */
  186. DECLSPEC void *vector_pop_front(vector_t vector);
  187. /*!
  188. * @brief Finds an key in vector V and returns the position of the key, or -1
  189. * if the search failed.
  190. * O(n) complexity
  191. *
  192. * @param Vector
  193. * @param Key
  194. * @param Pointer to comparison function
  195. */
  196. DECLSPEC int vector_search(vector_t vector, void *key, int compare(void *x, void *y,
  197. void *context), void *context);
  198. /*!
  199. * @brief Tests if vector is empty.
  200. * O(1) complexity
  201. *
  202. * @param Vector
  203. */
  204. DECLSPEC char vector_is_empty(vector_t vector);
  205. /*!
  206. * @brief Tests if vector is full.
  207. * O(1) complexity
  208. *
  209. * @param Vector
  210. */
  211. DECLSPEC char vector_is_full(vector_t vector);
  212. /*!
  213. * @brief Sorts the vector's keys using randomized quicksort.
  214. * O(n*log(n)) complexity
  215. *
  216. * @param Vector
  217. * @param Pointer to comparison function
  218. */
  219. DECLSPEC void vector_sort(vector_t vector, int compare(void *x, void *y, void *context),
  220. void *context);
  221. /*!
  222. * @brief Iterates over the keys of current vector.
  223. * O(n) complexity
  224. *
  225. * @param Vector
  226. * @param Pointer to iterating function
  227. * @param Pointer to context - you may store here a structure with a FILE pointer if
  228. * you want to print the vector's keys
  229. */
  230. DECLSPEC void vector_iterate(vector_t vector,
  231. void (*key_handler)(void *key, void *context), void *context);
  232. /*!
  233. * @brief Returns a reference to the minimum key in vector.
  234. * O(n) complexity
  235. *
  236. * @param Vector
  237. * @param Pointer to comparison function
  238. */
  239. DECLSPEC void *vector_min(vector_t vector, int compare(void *x, void *y, void *context),
  240. void *context);
  241. /*!
  242. * @brief Returns a reference to the maximum key in vector.
  243. * O(n) complexity
  244. *
  245. * @param Vector
  246. * @param Pointer to comparison function
  247. */
  248. DECLSPEC void *vector_max(vector_t vector, int compare(void *x, void *y, void *context),
  249. void *context);
  250. #endif /* VECTOR_H */