collections.pxd 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. from libc.stdint cimport uint32_t
  2. ctypedef void* (*mem_alloc_ft)(size_t size)
  3. ctypedef void* (*mem_calloc_ft)(size_t blocks, size_t size)
  4. ctypedef void (*mem_free_ft)(void* block)
  5. ctypedef size_t (*hash_ft)(const void* key, int l, uint32_t seed)
  6. ctypedef int (*key_compare_ft)(const void* key1, const void* key2)
  7. cdef extern from "common.h":
  8. enum cc_stat:
  9. CC_OK
  10. CC_ERR_ALLOC
  11. CC_ERR_INVALID_CAPACITY
  12. CC_ERR_INVALID_RANGE
  13. CC_ERR_MAX_CAPACITY
  14. CC_ERR_KEY_NOT_FOUND
  15. CC_ERR_VALUE_NOT_FOUND
  16. CC_ERR_OUT_OF_RANGE
  17. CC_ITER_END
  18. key_compare_ft CC_CMP_STRING
  19. key_compare_ft CC_CMP_POINTER
  20. #
  21. # int cc_common_cmp_str(const void* key1, const void* key2)
  22. #
  23. # int cc_common_cmp_ptr(const void* key1, const void* key2)
  24. cdef extern from "array.h":
  25. ctypedef struct Array:
  26. pass
  27. ctypedef struct ArrayConf:
  28. size_t capacity
  29. float exp_factor
  30. mem_alloc_ft mem_alloc
  31. mem_calloc_ft mem_calloc
  32. mem_free_ft mem_free
  33. ctypedef struct ArrayIter:
  34. Array* ar
  35. size_t index
  36. bint last_removed
  37. # ctypedef struct ArrayZipIter:
  38. # Array* ar1
  39. # Array* ar2
  40. # size_t index
  41. # bint last_removed
  42. #
  43. cc_stat array_new(Array** out)
  44. cc_stat array_new_conf(ArrayConf* conf, Array** out)
  45. void array_conf_init(ArrayConf* conf)
  46. void array_destroy(Array* ar)
  47. # ctypedef void (*_array_destroy_cb_cb_ft)(void*)
  48. #
  49. # void array_destroy_cb(Array* ar, _array_destroy_cb_cb_ft cb)
  50. #
  51. cc_stat array_add(Array* ar, void* element)
  52. #
  53. # #cc_stat array_add_at(Array* ar, void* element, size_t index)
  54. #
  55. # cc_stat array_replace_at(Array* ar, void* element, size_t index, void** out)
  56. #
  57. # cc_stat array_swap_at(Array* ar, size_t index1, size_t index2)
  58. #
  59. # cc_stat array_remove(Array* ar, void* element, void** out)
  60. #
  61. # cc_stat array_remove_at(Array* ar, size_t index, void** out)
  62. #
  63. # cc_stat array_remove_last(Array* ar, void** out)
  64. #
  65. # void array_remove_all(Array* ar)
  66. #
  67. # void array_remove_all_free(Array* ar)
  68. #
  69. # cc_stat array_get_at(Array* ar, size_t index, void** out)
  70. #
  71. # cc_stat array_get_last(Array* ar, void** out)
  72. #
  73. # cc_stat array_subarray(Array* ar, size_t from_, size_t to, Array** out)
  74. #
  75. # cc_stat array_copy_shallow(Array* ar, Array** out)
  76. #
  77. # ctypedef void* (*_array_copy_deep_cp_ft)(void*)
  78. #
  79. # cc_stat array_copy_deep(Array* ar, _array_copy_deep_cp_ft cp, Array** out)
  80. #
  81. # void array_reverse(Array* ar)
  82. #
  83. # cc_stat array_trim_capacity(Array* ar)
  84. #
  85. # size_t array_contains(Array* ar, void* element)
  86. #
  87. # ctypedef int (*_array_contains_value_cmp_ft)(void*, void*)
  88. #
  89. # size_t array_contains_value(Array* ar, void* element, _array_contains_value_cmp_ft cmp)
  90. #
  91. # size_t array_size(Array* ar)
  92. #
  93. # size_t array_capacity(Array* ar)
  94. #
  95. # cc_stat array_index_of(Array* ar, void* element, size_t* index)
  96. #
  97. # ctypedef int (*_array_sort_cmp_ft)(void*, void*)
  98. #
  99. # void array_sort(Array* ar, _array_sort_cmp_ft cmp)
  100. #
  101. # ctypedef void (*_array_map_fn_ft)(void*)
  102. #
  103. # void array_map(Array* ar, _array_map_fn_ft fn)
  104. #
  105. # ctypedef void (*_array_reduce_fn_ft)(void*, void*, void*)
  106. #
  107. # void array_reduce(Array* ar, _array_reduce_fn_ft fn, void* result)
  108. #
  109. # ctypedef bint (*_array_filter_mut_predicate_ft)(void*)
  110. #
  111. # cc_stat array_filter_mut(Array* ar, _array_filter_mut_predicate_ft predicate)
  112. #
  113. # ctypedef bint (*_array_filter_predicate_ft)(void*)
  114. #
  115. # cc_stat array_filter(Array* ar, _array_filter_predicate_ft predicate, Array** out)
  116. #
  117. void array_iter_init(ArrayIter* iter, Array* ar)
  118. cc_stat array_iter_next(ArrayIter* iter, void** out)
  119. #
  120. # cc_stat array_iter_remove(ArrayIter* iter, void** out)
  121. #
  122. # cc_stat array_iter_add(ArrayIter* iter, void* element)
  123. #
  124. # cc_stat array_iter_replace(ArrayIter* iter, void* element, void** out)
  125. #
  126. # size_t array_iter_index(ArrayIter* iter)
  127. #
  128. # void array_zip_iter_init(ArrayZipIter* iter, Array* a1, Array* a2)
  129. #
  130. # cc_stat array_zip_iter_next(ArrayZipIter* iter, void** out1, void** out2)
  131. #
  132. # cc_stat array_zip_iter_add(ArrayZipIter* iter, void* e1, void* e2)
  133. #
  134. # cc_stat array_zip_iter_remove(ArrayZipIter* iter, void** out1, void** out2)
  135. #
  136. # cc_stat array_zip_iter_replace(ArrayZipIter* iter, void* e1, void* e2, void** out1, void** out2)
  137. #
  138. # size_t array_zip_iter_index(ArrayZipIter* iter)
  139. #
  140. # void** array_get_buffer(Array* ar)
  141. cdef extern from "hashtable.h":
  142. ctypedef struct TableEntry:
  143. void* key
  144. void* value
  145. size_t hash
  146. TableEntry* next
  147. ctypedef struct HashTable:
  148. pass
  149. ctypedef struct HashTableConf:
  150. float load_factor
  151. size_t initial_capacity
  152. int key_length
  153. uint32_t hash_seed
  154. hash_ft hash
  155. key_compare_ft key_compare
  156. mem_alloc_ft mem_alloc
  157. mem_calloc_ft mem_calloc
  158. mem_free_ft mem_free
  159. ctypedef struct HashTableIter:
  160. HashTable* table
  161. size_t bucket_index
  162. TableEntry* prev_entry
  163. TableEntry* next_entry
  164. hash_ft GENERAL_HASH
  165. hash_ft STRING_HASH
  166. hash_ft POINTER_HASH
  167. # size_t get_table_index(HashTable *table, void *key)
  168. #
  169. # void hashtable_conf_init(HashTableConf* conf)
  170. #
  171. # cc_stat hashtable_new(HashTable** out)
  172. #
  173. # cc_stat hashtable_new_conf(HashTableConf* conf, HashTable** out)
  174. #
  175. # void hashtable_destroy(HashTable* table)
  176. #
  177. # cc_stat hashtable_add(HashTable* table, void* key, void* val)
  178. #
  179. # cc_stat hashtable_get(HashTable* table, void* key, void** out)
  180. #
  181. # cc_stat hashtable_remove(HashTable* table, void* key, void** out)
  182. #
  183. # void hashtable_remove_all(HashTable* table)
  184. #
  185. # bint hashtable_contains_key(HashTable* table, void* key)
  186. #
  187. # size_t hashtable_size(HashTable* table)
  188. #
  189. # size_t hashtable_capacity(HashTable* table)
  190. #
  191. # cc_stat hashtable_get_keys(HashTable* table, Array** out)
  192. #
  193. # cc_stat hashtable_get_values(HashTable* table, Array** out)
  194. #
  195. size_t hashtable_hash_string(void* key, int len, uint32_t seed)
  196. size_t hashtable_hash(void* key, int len, uint32_t seed)
  197. size_t hashtable_hash_ptr(void* key, int len, uint32_t seed)
  198. #
  199. # ctypedef void (*_hashtable_foreach_key_op_ft)(void*)
  200. #
  201. # void hashtable_foreach_key(HashTable* table, _hashtable_foreach_key_op_ft op)
  202. #
  203. # ctypedef void (*_hashtable_foreach_value_op_ft)(void*)
  204. #
  205. # void hashtable_foreach_value(HashTable* table, _hashtable_foreach_value_op_ft op)
  206. #
  207. # void hashtable_iter_init(HashTableIter* iter, HashTable* table)
  208. #
  209. # cc_stat hashtable_iter_next(HashTableIter* iter, TableEntry** out)
  210. #
  211. # cc_stat hashtable_iter_remove(HashTableIter* iter, void** out)
  212. cdef extern from "hashset.h":
  213. ctypedef struct HashSet:
  214. pass
  215. ctypedef HashTableConf HashSetConf
  216. ctypedef struct HashSetIter:
  217. HashTableIter iter
  218. void hashset_conf_init(HashSetConf* conf)
  219. cc_stat hashset_new(HashSet** hs)
  220. cc_stat hashset_new_conf(HashSetConf* conf, HashSet** hs)
  221. void hashset_destroy(HashSet* set)
  222. cc_stat hashset_add(HashSet* set, void* element)
  223. cc_stat hashset_add_or_get(HashSet* set, void** element)
  224. cc_stat hashset_remove(HashSet* set, void* element, void** out)
  225. void hashset_remove_all(HashSet* set)
  226. bint hashset_contains(HashSet* set, void* element)
  227. cc_stat hashset_get(HashSet *set, void **element)
  228. size_t hashset_size(HashSet* set)
  229. size_t hashset_capacity(HashSet* set)
  230. ctypedef void (*_hashset_foreach_op_ft)(void*)
  231. void hashset_foreach(HashSet* set, _hashset_foreach_op_ft op)
  232. void hashset_iter_init(HashSetIter* iter, HashSet* set)
  233. cc_stat hashset_iter_next(HashSetIter* iter, void** out)
  234. cc_stat hashset_iter_remove(HashSetIter* iter, void** out)