tkArray.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610
  1. /*
  2. * tkArray.h --
  3. *
  4. * An array is a sequence of items, stored in a contiguous memory region.
  5. * Random access to any item is very fast. New items can be either appended
  6. * or prepended. An array may be traversed in the forward or backward direction.
  7. *
  8. * Copyright (c) 2018-2019 by Gregor Cramer.
  9. *
  10. * See the file "license.terms" for information on usage and redistribution of
  11. * this file, and for a DISCLAIMER OF ALL WARRANTIES.
  12. */
  13. /*
  14. * Note that this file will not be included in header files, it is the purpose
  15. * of this file to be included in source files only. Thus we are not using the
  16. * prefix "Tk_" here for functions, because all the functions have private scope.
  17. */
  18. /*
  19. * -------------------------------------------------------------------------------
  20. * Use the array in the following way:
  21. * -------------------------------------------------------------------------------
  22. * typedef struct { int key, value; } Pair;
  23. * TK_PTR_ARRAY_DEFINE(MyArray, Pair);
  24. * MyArray *arr = NULL;
  25. * if (MyArray_IsEmpty(arr)) {
  26. * MyArray_Append(&arr, MakePair(1, 2));
  27. * MyArray_Append(&arr, MakePair(2, 3));
  28. * for (i = 0; i < MyArray_Size(arr); ++i) {
  29. * Pair *p = MyArray_Get(arr, i);
  30. * printf("%d -> %d\n", p->key, p->value);
  31. * ckfree(p);
  32. * }
  33. * MyArray_Free(&arr);
  34. * assert(arr == NULL);
  35. * }
  36. * -------------------------------------------------------------------------------
  37. * Or with aggregated elements:
  38. * -------------------------------------------------------------------------------
  39. * typedef struct { int key, value; } Pair;
  40. * TK_ARRAY_DEFINE(MyArray, Pair);
  41. * Pair p1 = { 1, 2 };
  42. * Pair p2 = { 2, 3 };
  43. * MyArray *arr = NULL;
  44. * if (MyArray_IsEmpty(arr)) {
  45. * MyArray_Append(&arr, p1);
  46. * MyArray_Append(&arr, p2);
  47. * for (i = 0; i < MyArray_Size(arr); ++i) {
  48. * const Pair *p = MyArray_Get(arr, i);
  49. * printf("%d -> %d\n", p->key, p->value);
  50. * }
  51. * MyArray_Free(&arr);
  52. * assert(arr == NULL);
  53. * }
  54. * -------------------------------------------------------------------------------
  55. */
  56. /*************************************************************************/
  57. /*
  58. * Two array types will be provided:
  59. * Use TK_ARRAY_DEFINE if your array is aggregating the elements. Use
  60. * TK_PTR_ARRAY_DEFINE if your array contains pointers to elements. But
  61. * in latter case the array is not responsible for the lifetime of the
  62. * elements.
  63. */
  64. /*************************************************************************/
  65. /*
  66. * Array_ElemSize: Returns the memory size for one array element.
  67. */
  68. /*************************************************************************/
  69. /*
  70. * Array_BufferSize: Returns the memory size for given number of elements.
  71. */
  72. /*************************************************************************/
  73. /*
  74. * Array_IsEmpty: Array is empty?
  75. */
  76. /*************************************************************************/
  77. /*
  78. * Array_Size: Number of elements in array.
  79. */
  80. /*************************************************************************/
  81. /*
  82. * Array_Capacity: Capacity of given array. This is the maximal number of
  83. * elements fitting into current array memory without resizing the buffer.
  84. */
  85. /*************************************************************************/
  86. /*
  87. * Array_SetSize: Set array size, new size must not exceed the capacity of
  88. * the array. This function has to be used with care when increasing the
  89. * array size.
  90. */
  91. /*************************************************************************/
  92. /*
  93. * Array_First: Returns position of first element in array. Given array
  94. * may be NULL.
  95. */
  96. /*************************************************************************/
  97. /*
  98. * Array_Last: Returns position after last element in array. Given array
  99. * may be empty.
  100. */
  101. /*************************************************************************/
  102. /*
  103. * Array_Front: Returns first element in array. Given array must not be
  104. * empty.
  105. */
  106. /*************************************************************************/
  107. /*
  108. * Array_Back: Returns last element in array. Given array must not be
  109. * empty.
  110. */
  111. /*************************************************************************/
  112. /*
  113. * Array_Resize: Resize buffer of array for given number of elements. The
  114. * array may grow or shrink. Note that this function is not initializing
  115. * the increased buffer.
  116. */
  117. /*************************************************************************/
  118. /*
  119. * Array_ResizeAndClear: Resize buffer of array for given number of
  120. * elements. The array may grow or shrink. The increased memory will be
  121. * filled with zeroes.
  122. */
  123. /*************************************************************************/
  124. /*
  125. * Array_Clear: Fill specified range with zeroes.
  126. */
  127. /*************************************************************************/
  128. /*
  129. * Array_Free: Resize array to size zero. This function will release the
  130. * array buffer.
  131. */
  132. /*************************************************************************/
  133. /*
  134. * Array_Append: Insert given element after end of array.
  135. */
  136. /*************************************************************************/
  137. /*
  138. * Array_PopBack: Shrink array by one element. Given array must not be
  139. * empty.
  140. */
  141. /*************************************************************************/
  142. /*
  143. * Array_Get: Random access to array element at given position. The given
  144. * index must not exceed current array size.
  145. */
  146. /*************************************************************************/
  147. /*
  148. * Array_Set: Replace array element at given position with new value. The
  149. * given index must not exceed current array size.
  150. */
  151. /*************************************************************************/
  152. /*
  153. * Array_Find: Return index position of element which matches given
  154. * argument. If not found then -1 will be returned.
  155. */
  156. /*************************************************************************/
  157. #ifndef TK_ARRAY_DEFINED
  158. #define TK_ARRAY_DEFINED
  159. #include "tkInt.h"
  160. #if defined(__GNUC__) || defined(__clang__)
  161. # define __TK_ARRAY_UNUSED __attribute__((unused))
  162. #else
  163. # define __TK_ARRAY_UNUSED
  164. #endif
  165. #define TK_ARRAY_DEFINE(AT, ElemType) /* AT = type of array */ \
  166. /* ------------------------------------------------------------------------- */ \
  167. typedef struct AT { \
  168. size_t size; \
  169. size_t capacity; \
  170. ElemType buf[1]; \
  171. } AT; \
  172. \
  173. __TK_ARRAY_UNUSED \
  174. static void \
  175. AT##_Init(AT *arr) \
  176. { \
  177. assert(arr); \
  178. arr->size = 0; \
  179. arr->capacity = 0; \
  180. } \
  181. \
  182. __TK_ARRAY_UNUSED \
  183. static size_t \
  184. AT##_ElemSize() \
  185. { \
  186. return sizeof(ElemType); \
  187. } \
  188. \
  189. __TK_ARRAY_UNUSED \
  190. static size_t \
  191. AT##_BufferSize(size_t numElems) \
  192. { \
  193. return numElems*sizeof(ElemType); \
  194. } \
  195. \
  196. __TK_ARRAY_UNUSED \
  197. static int \
  198. AT##_IsEmpty(const AT *arr) \
  199. { \
  200. assert(!arr || arr->size != 0xdeadbeef); \
  201. return !arr || arr->size == 0u; \
  202. } \
  203. \
  204. __TK_ARRAY_UNUSED \
  205. static size_t \
  206. AT##_Size(const AT *arr) \
  207. { \
  208. assert(!arr || arr->size != 0xdeadbeef); \
  209. return arr ? arr->size : 0u; \
  210. } \
  211. \
  212. __TK_ARRAY_UNUSED \
  213. static size_t \
  214. AT##_Capacity(const AT *arr) \
  215. { \
  216. assert(!arr || arr->size != 0xdeadbeef); \
  217. return arr ? arr->capacity : 0u; \
  218. } \
  219. \
  220. __TK_ARRAY_UNUSED \
  221. static ElemType * \
  222. AT##_First(AT *arr) \
  223. { \
  224. assert(!arr || arr->size != 0xdeadbeef); \
  225. return arr ? arr->buf : NULL; \
  226. } \
  227. \
  228. __TK_ARRAY_UNUSED \
  229. static ElemType * \
  230. AT##_Last(AT *arr) \
  231. { \
  232. assert(!arr || arr->size != 0xdeadbeef); \
  233. return arr ? arr->buf + arr->size : NULL; \
  234. } \
  235. \
  236. __TK_ARRAY_UNUSED \
  237. static ElemType * \
  238. AT##_Front(AT *arr) \
  239. { \
  240. assert(arr); \
  241. assert(arr->size != 0xdeadbeef); \
  242. assert(!AT##_IsEmpty(arr)); \
  243. return &arr->buf[0]; \
  244. } \
  245. \
  246. __TK_ARRAY_UNUSED \
  247. static ElemType * \
  248. AT##_Back(AT *arr) \
  249. { \
  250. assert(arr); \
  251. assert(arr->size != 0xdeadbeef); \
  252. assert(!AT##_IsEmpty(arr)); \
  253. return &arr->buf[arr->size - 1]; \
  254. } \
  255. \
  256. __TK_ARRAY_UNUSED \
  257. static void \
  258. AT##_Resize(AT **arrp, size_t newSize) \
  259. { \
  260. assert(arrp); \
  261. assert(!*arrp || (*arrp)->size != 0xdeadbeef); \
  262. if (newSize == 0) { \
  263. assert(!*arrp || ((*arrp)->size = 0xdeadbeef)); \
  264. ckfree(*arrp); \
  265. *arrp = NULL; \
  266. } else { \
  267. int init = *arrp == NULL; \
  268. size_t memSize = AT##_BufferSize(newSize - 1) + sizeof(AT); \
  269. *arrp = ckrealloc(*arrp, memSize); \
  270. if (init) { \
  271. (*arrp)->size = 0; \
  272. } else if (newSize < (*arrp)->size) { \
  273. (*arrp)->size = newSize; \
  274. } \
  275. (*arrp)->capacity = newSize; \
  276. } \
  277. } \
  278. \
  279. __TK_ARRAY_UNUSED \
  280. static void \
  281. AT##_Clear(AT *arr, size_t from, size_t to) \
  282. { \
  283. assert(arr); \
  284. assert(arr->size != 0xdeadbeef); \
  285. assert(to <= AT##_Capacity(arr)); \
  286. assert(from <= to); \
  287. memset(arr->buf + from, 0, AT##_BufferSize(to - from)); \
  288. } \
  289. \
  290. __TK_ARRAY_UNUSED \
  291. static void \
  292. AT##_ResizeAndClear(AT **arrp, size_t newSize) \
  293. { \
  294. size_t oldCapacity; \
  295. assert(arrp); \
  296. oldCapacity = *arrp ? (*arrp)->capacity : 0; \
  297. AT##_Resize(arrp, newSize); \
  298. if (newSize > oldCapacity) { \
  299. AT##_Clear(*arrp, oldCapacity, newSize); \
  300. } \
  301. } \
  302. \
  303. __TK_ARRAY_UNUSED \
  304. static void \
  305. AT##_SetSize(AT *arr, size_t newSize) \
  306. { \
  307. assert(newSize <= AT##_Capacity(arr)); \
  308. assert(!arr || arr->size != 0xdeadbeef); \
  309. if (arr) { \
  310. arr->size = newSize; \
  311. } \
  312. } \
  313. \
  314. __TK_ARRAY_UNUSED \
  315. static void \
  316. AT##_Append(AT **arrp, ElemType *elem) \
  317. { \
  318. assert(arrp); \
  319. if (!*arrp) { \
  320. AT##_Resize(arrp, 1); \
  321. } else if ((*arrp)->size == (*arrp)->capacity) { \
  322. AT##_Resize(arrp, (*arrp)->capacity + ((*arrp)->capacity + 1)/2); \
  323. } \
  324. (*arrp)->buf[(*arrp)->size++] = *elem; \
  325. } \
  326. \
  327. __TK_ARRAY_UNUSED \
  328. static size_t \
  329. AT##_PopBack(AT *arr) \
  330. { \
  331. assert(!AT##_IsEmpty(arr)); \
  332. return arr->size -= 1; \
  333. } \
  334. \
  335. __TK_ARRAY_UNUSED \
  336. static ElemType * \
  337. AT##_Get(const AT *arr, size_t at) \
  338. { \
  339. assert(arr); \
  340. assert(at < AT##_Size(arr)); \
  341. return (ElemType *) &arr->buf[at]; \
  342. } \
  343. \
  344. __TK_ARRAY_UNUSED \
  345. static void \
  346. AT##_Set(AT *arr, size_t at, ElemType *elem) \
  347. { \
  348. assert(arr); \
  349. assert(at < AT##_Size(arr)); \
  350. arr->buf[at] = *elem; \
  351. } \
  352. \
  353. __TK_ARRAY_UNUSED \
  354. static void \
  355. AT##_Free(AT **arrp) \
  356. { \
  357. AT##_Resize(arrp, 0); \
  358. } \
  359. \
  360. __TK_ARRAY_UNUSED \
  361. static int \
  362. AT##_Find(const AT *arr, const ElemType *elem) \
  363. { \
  364. assert(!arr || arr->size != 0xdeadbeef); \
  365. if (arr) { \
  366. const ElemType *buf = arr->buf; \
  367. size_t i; \
  368. for (i = 0; i < arr->size; ++i) { \
  369. if (memcmp(&buf[i], elem, sizeof(ElemType)) == 0) { \
  370. return (int) i; \
  371. } \
  372. } \
  373. } \
  374. return -1; \
  375. } \
  376. \
  377. __TK_ARRAY_UNUSED \
  378. static int \
  379. AT##_Contains(const AT *arr, const ElemType *elem) \
  380. { \
  381. return AT##_Find(arr, elem) != -1; \
  382. } \
  383. /* ------------------------------------------------------------------------- */
  384. #define TK_PTR_ARRAY_DEFINE(AT, ElemType) /* AT = type of array */ \
  385. /* ------------------------------------------------------------------------- */ \
  386. typedef struct AT { \
  387. size_t size; \
  388. size_t capacity; \
  389. ElemType *buf[1]; \
  390. } AT; \
  391. \
  392. __TK_ARRAY_UNUSED \
  393. static size_t \
  394. AT##_ElemSize() \
  395. { \
  396. return sizeof(ElemType); \
  397. } \
  398. \
  399. __TK_ARRAY_UNUSED \
  400. static size_t \
  401. AT##_BufferSize(size_t numElems) \
  402. { \
  403. return numElems*sizeof(ElemType *); \
  404. } \
  405. \
  406. __TK_ARRAY_UNUSED \
  407. static int \
  408. AT##_IsEmpty(const AT *arr) \
  409. { \
  410. assert(!arr || arr->size != 0xdeadbeef); \
  411. return !arr || arr->size == 0; \
  412. } \
  413. \
  414. __TK_ARRAY_UNUSED \
  415. static ElemType ** \
  416. AT##_First(AT *arr) \
  417. { \
  418. assert(!arr || arr->size != 0xdeadbeef); \
  419. return arr ? arr->buf : NULL; \
  420. } \
  421. \
  422. __TK_ARRAY_UNUSED \
  423. static ElemType ** \
  424. AT##_Last(AT *arr) \
  425. { \
  426. assert(!arr || arr->size != 0xdeadbeef); \
  427. return arr ? arr->buf + arr->size : NULL; \
  428. } \
  429. \
  430. __TK_ARRAY_UNUSED \
  431. static ElemType * \
  432. AT##_Front(AT *arr) \
  433. { \
  434. assert(arr); \
  435. assert(arr->size != 0xdeadbeef); \
  436. assert(!AT##_IsEmpty(arr)); \
  437. return arr->buf[0]; \
  438. } \
  439. \
  440. __TK_ARRAY_UNUSED \
  441. static ElemType * \
  442. AT##_Back(AT *arr) \
  443. { \
  444. assert(arr); \
  445. assert(arr->size != 0xdeadbeef); \
  446. assert(!AT##_IsEmpty(arr)); \
  447. return arr->buf[arr->size - 1]; \
  448. } \
  449. \
  450. __TK_ARRAY_UNUSED \
  451. static size_t \
  452. AT##_Size(const AT *arr) \
  453. { \
  454. assert(!arr || arr->size != 0xdeadbeef); \
  455. return arr ? arr->size : 0; \
  456. } \
  457. \
  458. __TK_ARRAY_UNUSED \
  459. static size_t \
  460. AT##_Capacity(const AT *arr) \
  461. { \
  462. assert(!arr || arr->size != 0xdeadbeef); \
  463. return arr ? arr->capacity : 0; \
  464. } \
  465. \
  466. __TK_ARRAY_UNUSED \
  467. static void \
  468. AT##_Resize(AT **arrp, size_t newCapacity) \
  469. { \
  470. assert(arrp); \
  471. assert(!*arrp || (*arrp)->size != 0xdeadbeef); \
  472. if (newCapacity == 0) { \
  473. assert(!*arrp || ((*arrp)->size = 0xdeadbeef)); \
  474. ckfree(*arrp); \
  475. *arrp = NULL; \
  476. } else { \
  477. int init = *arrp == NULL; \
  478. size_t memSize = AT##_BufferSize(newCapacity - 1) + sizeof(AT); \
  479. *arrp = ckrealloc(*arrp, memSize); \
  480. if (init) { \
  481. (*arrp)->size = 0; \
  482. } else if (newCapacity < (*arrp)->size) { \
  483. (*arrp)->size = newCapacity; \
  484. } \
  485. (*arrp)->capacity = newCapacity; \
  486. } \
  487. } \
  488. \
  489. __TK_ARRAY_UNUSED \
  490. static void \
  491. AT##_Clear(AT *arr, size_t from, size_t to) \
  492. { \
  493. assert(arr); \
  494. assert(arr->size != 0xdeadbeef); \
  495. assert(to <= AT##_Capacity(arr)); \
  496. assert(from <= to); \
  497. memset(arr->buf + from, 0, AT##_BufferSize(to - from)); \
  498. } \
  499. \
  500. __TK_ARRAY_UNUSED \
  501. static void \
  502. AT##_ResizeAndClear(AT **arrp, size_t newCapacity) \
  503. { \
  504. size_t oldCapacity; \
  505. assert(arrp); \
  506. oldCapacity = *arrp ? (*arrp)->capacity : 0; \
  507. AT##_Resize(arrp, newCapacity); \
  508. if (newCapacity > oldCapacity) { \
  509. AT##_Clear(*arrp, oldCapacity, newCapacity); \
  510. } \
  511. } \
  512. \
  513. __TK_ARRAY_UNUSED \
  514. static void \
  515. AT##_SetSize(AT *arr, size_t newSize) \
  516. { \
  517. assert(arr); \
  518. assert(newSize <= AT##_Capacity(arr)); \
  519. arr->size = newSize; \
  520. } \
  521. \
  522. __TK_ARRAY_UNUSED \
  523. static void \
  524. AT##_Append(AT **arrp, ElemType *elem) \
  525. { \
  526. assert(arrp); \
  527. if (!*arrp) { \
  528. AT##_Resize(arrp, 1); \
  529. } else if ((*arrp)->size == (*arrp)->capacity) { \
  530. AT##_Resize(arrp, (*arrp)->capacity + ((*arrp)->capacity + 1)/2); \
  531. } \
  532. (*arrp)->buf[(*arrp)->size++] = elem; \
  533. } \
  534. \
  535. __TK_ARRAY_UNUSED \
  536. static size_t \
  537. AT##_PopBack(AT *arr) \
  538. { \
  539. assert(!AT##_IsEmpty(arr)); \
  540. return arr->size -= 1; \
  541. } \
  542. \
  543. __TK_ARRAY_UNUSED \
  544. static ElemType * \
  545. AT##_Get(const AT *arr, size_t at) \
  546. { \
  547. assert(arr); \
  548. assert(at < AT##_Size(arr)); \
  549. return arr->buf[at]; \
  550. } \
  551. \
  552. __TK_ARRAY_UNUSED \
  553. static void \
  554. AT##_Set(AT *arr, size_t at, ElemType *elem) \
  555. { \
  556. assert(arr); \
  557. assert(at < AT##_Size(arr)); \
  558. arr->buf[at] = elem; \
  559. } \
  560. \
  561. __TK_ARRAY_UNUSED \
  562. static void \
  563. AT##_Free(AT **arrp) \
  564. { \
  565. AT##_Resize(arrp, 0); \
  566. } \
  567. \
  568. __TK_ARRAY_UNUSED \
  569. static int \
  570. AT##_Find(const AT *arr, const ElemType *elem) \
  571. { \
  572. assert(!arr || arr->size != 0xdeadbeef); \
  573. if (arr) { \
  574. ElemType *const *buf = arr->buf; \
  575. size_t i; \
  576. for (i = 0; i < arr->size; ++i) { \
  577. if (buf[i] == elem) { \
  578. return (int) i; \
  579. } \
  580. } \
  581. } \
  582. return -1; \
  583. } \
  584. \
  585. __TK_ARRAY_UNUSED \
  586. static int \
  587. AT##_Contains(const AT *arr, const ElemType *elem) \
  588. { \
  589. return AT##_Find(arr, elem) != -1; \
  590. } \
  591. /* ------------------------------------------------------------------------- */
  592. #endif /* TK_ARRAY_DEFINED */
  593. /*
  594. * Local Variables:
  595. * mode: c
  596. * c-basic-offset: 4
  597. * fill-column: 105
  598. * End:
  599. * vi:set ts=8 sw=4:
  600. */