osi_order_list.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. /* Copyright (C) 2018 RDA Technologies Limited and/or its affiliates("RDA").
  2. * All rights reserved.
  3. *
  4. * This software is supplied "AS IS" without any warranties.
  5. * RDA assumes no responsibility or liability for the use of the software,
  6. * conveys no license or title under any patent, copyright, or mask work
  7. * right to the product. RDA reserves the right to make changes in the
  8. * software without notification. RDA also make no representation or
  9. * warranty that such application will be suitable for the specified use
  10. * without further testing or modification.
  11. */
  12. #ifndef _OSI_ORDER_LIST_H_
  13. #define _OSI_ORDER_LIST_H_
  14. #include <stdint.h>
  15. #include <stdbool.h>
  16. #include <stddef.h>
  17. #ifdef __cplusplus
  18. extern "C" {
  19. #endif
  20. /**
  21. * \brief opaque data struct for order list
  22. *
  23. * <em>order list</em> is a generic data struct for:
  24. * - Fixed memory size, allocated at creation.
  25. * - Use contiguous memory. So, the pointer of first can be casted into array.
  26. * - Record will be sorted at insert (similar to \p std::map).
  27. * - Record is unique (similar to \p std::map).
  28. * - User shall provide \p compar function (similar to \p bsearch) for sort.
  29. * - Provide API similar to \p std::lower_bound and \p std::upper_bound.
  30. * - Provide API to find EQ, LT, GT.
  31. * - Optionally replace smallest record on full.
  32. *
  33. * Typical usage is to keep a sorted history, and it is needed to search from
  34. * the history.
  35. */
  36. typedef struct osiOrderList osiOrderList_t;
  37. /**
  38. * \brief create order list
  39. *
  40. * Signature of \p compar is the same as \p bsearch and \p qsort.
  41. *
  42. * \param size size of each record
  43. * \param compar record comparison function, return -1, 0, 1
  44. * \param max_count maximum record count, can't be zero
  45. * \param replace whether to replace smallest on full
  46. * \return
  47. * - the created order list
  48. * - NULL if out of memory, or invalid parameter
  49. */
  50. osiOrderList_t *osiOrderListCreate(unsigned size, int (*compar)(const void *, const void *),
  51. unsigned max_count, bool replace);
  52. /**
  53. * \brief delete the order list
  54. *
  55. * \param d the order list instance
  56. */
  57. void osiOrderListDelete(osiOrderList_t *d);
  58. /**
  59. * \brief clear all records in order list
  60. *
  61. * \param d the order list instance
  62. */
  63. void osiOrderListClear(osiOrderList_t *d);
  64. /**
  65. * \brief record count of order list
  66. *
  67. * \param d the order list instance
  68. * \return
  69. * - the record count
  70. */
  71. unsigned osiOrderListCount(osiOrderList_t *d);
  72. /**
  73. * \brief insert a record into order list
  74. *
  75. * When there exists a record which will match \p val, that is \p compar
  76. * will return 0, the existed record will be updated, and the existed
  77. * record will be returned.
  78. *
  79. * When record count reaches \p max_count, there are 2 strategies at
  80. * inserting new record:
  81. * - When \p replace is true, the smallest one will be dropped;
  82. * - When \p replace is false, return false;
  83. *
  84. * \param d the order list instance
  85. * \param val value to be inserted
  86. * \return
  87. * - the inserted record
  88. * - NULL at full
  89. */
  90. const void *osiOrderListInsert(osiOrderList_t *d, const void *val);
  91. /**
  92. * \brief the fist record in order list
  93. *
  94. * \param d the order list instance
  95. * \return
  96. * - the first (smallest) record
  97. * - NULL if the order list is empty
  98. */
  99. const void *osiOrderListFirst(osiOrderList_t *d);
  100. /**
  101. * \brief the last record in order list
  102. *
  103. * \param d the order list instance
  104. * \return
  105. * - the last (largest) record
  106. * - NULL if the order list is empty
  107. */
  108. const void *osiOrderListLast(osiOrderList_t *d);
  109. /**
  110. * \brief find record in order list
  111. *
  112. * Find the record: <tt>record >= key</tt>. When all records are less than
  113. * \p val, the last record will be return. In this case,
  114. * <tt>record < key</tt>.
  115. *
  116. * Refer to <tt>std::lower_bound</tt>.
  117. *
  118. * \param d the order list instance
  119. * \param val record for find
  120. * \return
  121. * - the lower bound record
  122. * - NULL if the order list is empty
  123. */
  124. const void *osiOrderListLowerBound(osiOrderList_t *d, const void *val);
  125. /**
  126. * \brief find record in order list
  127. *
  128. * Find the record: <tt>record > key</tt>. When all records are less than
  129. * \p val, the last record will be return. In this case,
  130. * <tt>record <= key</tt>.
  131. *
  132. * Refer to <tt>std::upper_bound</tt>.
  133. *
  134. * \param d the order list instance
  135. * \param val key for find
  136. * \return
  137. * - the upper bound record
  138. * - NULL if the order list is empty
  139. */
  140. const void *osiOrderListUpperBound(osiOrderList_t *d, const void *val);
  141. /**
  142. * \brief find record in order list
  143. *
  144. * Find the record: <tt>record == key</tt>.
  145. *
  146. * \param d the order list instance
  147. * \param val key for find
  148. * \return
  149. * - the match record
  150. * - NULL if the order list is empty, or no record matches
  151. */
  152. const void *osiOrderListFind(osiOrderList_t *d, const void *val);
  153. /**
  154. * \brief find record in order list
  155. *
  156. * Find the record: <tt>record < key</tt>. When all records are greater or
  157. * equal than \p val, return NULL.
  158. *
  159. * \param d the order list instance
  160. * \param val key for find
  161. * \return
  162. * - the upper bound record
  163. * - NULL if the order list is empty, or no record matches
  164. */
  165. const void *osiOrderListFindLT(osiOrderList_t *d, const void *val);
  166. /**
  167. * \brief find record in order list
  168. *
  169. * Find the record: <tt>record > key</tt>. When all records are smaller or
  170. * equal than \p val, return NULL.
  171. *
  172. * \param d the order list instance
  173. * \param val key for find
  174. * \return
  175. * - the upper bound record
  176. * - NULL if the order list is empty, or no record matches
  177. */
  178. const void *osiOrderListFindGT(osiOrderList_t *d, const void *val);
  179. /**
  180. * \brief find record in order list
  181. *
  182. * Find the record: <tt>record <= key</tt>. When all records are greater or
  183. * equal than \p val, return NULL.
  184. *
  185. * \param d the order list instance
  186. * \param val key for find
  187. * \return
  188. * - the upper bound record
  189. * - NULL if the order list is empty, or no record matches
  190. */
  191. const void *osiOrderListFindLE(osiOrderList_t *d, const void *val);
  192. /**
  193. * \brief find record in order list
  194. *
  195. * Find the record: <tt>record >= key</tt>. When all records are smaller or
  196. * equal than \p val, return NULL.
  197. *
  198. * \param d the order list instance
  199. * \param val key for find
  200. * \return
  201. * - the upper bound record
  202. * - NULL if the order list is empty, or no record matches
  203. */
  204. const void *osiOrderListFindGE(osiOrderList_t *d, const void *val);
  205. /**
  206. * \brief helper function for unsigned integer compare
  207. *
  208. * Both \p a and \p b should be able to be casted into pointer of \p uint32_t,
  209. * and return the comparition of \p uint32_t.
  210. *
  211. * \param a comparison parameter
  212. * \param b comparison parameter
  213. * \return
  214. * - 1 if <tt>*(const uint32_t *)a > *(const uint32_t *)b</tt>
  215. * - 0 if <tt>*(const uint32_t *)a == *(const uint32_t *)b</tt>
  216. * - -1 if <tt>*(const uint32_t *)a < *(const uint32_t *)b</tt>
  217. */
  218. int osiUintCompare(const void *a, const void *b);
  219. /**
  220. * \brief helper function for signed integer compare
  221. *
  222. * Both \p a and \p b should be able to be casted into pointer of \p int32_t,
  223. * and return the comparition of \p int32_t.
  224. *
  225. * \param a comparison parameter
  226. * \param b comparison parameter
  227. * \return
  228. * - 1 if <tt>*(const int32_t *)a > *(const int32_t *)b</tt>
  229. * - 0 if <tt>*(const int32_t *)a == *(const int32_t *)b</tt>
  230. * - -1 if <tt>*(const int32_t *)a < *(const int32_t *)b</tt>
  231. */
  232. int osiIntCompare(const void *a, const void *b);
  233. #ifdef __cplusplus
  234. }
  235. #endif
  236. #endif