/lib/unictype/3level.h

https://github.com/rofl0r/gnulib · C Header · 327 lines · 239 code · 30 blank · 58 comment · 61 complexity · 4dda6fd89a7a151d43dc614bd9fa02fb MD5 · raw file

  1. /* Copyright (C) 2000-2001, 2009-2011 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. Contributed by Bruno Haible <haible@clisp.cons.org>, 2000.
  4. NOTE: The canonical source of this file is maintained with the GNU C Library.
  5. Bugs can be reported to bug-glibc@gnu.org.
  6. This program is free software; you can redistribute it and/or modify it
  7. under the terms of the GNU General Public License as published by the
  8. Free Software Foundation; either version 2, or (at your option) any
  9. later version.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with this program; if not, write to the Free Software
  16. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
  17. USA. */
  18. /* Construction of sparse 3-level tables.
  19. See wchar-lookup.h or coll-lookup.h for their structure and the
  20. meaning of p and q.
  21. Before including this file, set
  22. TABLE to the name of the structure to be defined
  23. ELEMENT to the type of every entry
  24. DEFAULT to the default value for empty entries
  25. ITERATE if you want the TABLE_iterate function to be defined
  26. NO_FINALIZE if you don't want the TABLE_finalize function to be defined
  27. This will define
  28. struct TABLE;
  29. void TABLE_init (struct TABLE *t);
  30. ELEMENT TABLE_get (struct TABLE *t, uint32_t wc);
  31. void TABLE_add (struct TABLE *t, uint32_t wc, ELEMENT value);
  32. void TABLE_iterate (struct TABLE *t,
  33. void (*fn) (uint32_t wc, ELEMENT value));
  34. void TABLE_finalize (struct TABLE *t);
  35. */
  36. #define CONCAT(a,b) CONCAT1(a,b)
  37. #define CONCAT1(a,b) a##b
  38. struct TABLE
  39. {
  40. /* Parameters. */
  41. unsigned int p;
  42. unsigned int q;
  43. /* Working representation. */
  44. size_t level1_alloc;
  45. size_t level1_size;
  46. uint32_t *level1;
  47. size_t level2_alloc;
  48. size_t level2_size;
  49. uint32_t *level2;
  50. size_t level3_alloc;
  51. size_t level3_size;
  52. ELEMENT *level3;
  53. /* Compressed representation. */
  54. size_t result_size;
  55. char *result;
  56. };
  57. /* Initialize. Assumes t->p and t->q have already been set. */
  58. static inline void
  59. CONCAT(TABLE,_init) (struct TABLE *t)
  60. {
  61. t->level1 = NULL;
  62. t->level1_alloc = t->level1_size = 0;
  63. t->level2 = NULL;
  64. t->level2_alloc = t->level2_size = 0;
  65. t->level3 = NULL;
  66. t->level3_alloc = t->level3_size = 0;
  67. }
  68. /* Marker for an empty slot. This has the value 0xFFFFFFFF, regardless
  69. whether 'int' is 16 bit, 32 bit, or 64 bit. */
  70. #define EMPTY ((uint32_t) ~0)
  71. /* Retrieve an entry. */
  72. static inline ELEMENT
  73. CONCAT(TABLE,_get) (struct TABLE *t, uint32_t wc)
  74. {
  75. uint32_t index1 = wc >> (t->q + t->p);
  76. if (index1 < t->level1_size)
  77. {
  78. uint32_t lookup1 = t->level1[index1];
  79. if (lookup1 != EMPTY)
  80. {
  81. uint32_t index2 = ((wc >> t->p) & ((1 << t->q) - 1))
  82. + (lookup1 << t->q);
  83. uint32_t lookup2 = t->level2[index2];
  84. if (lookup2 != EMPTY)
  85. {
  86. uint32_t index3 = (wc & ((1 << t->p) - 1))
  87. + (lookup2 << t->p);
  88. ELEMENT lookup3 = t->level3[index3];
  89. return lookup3;
  90. }
  91. }
  92. }
  93. return DEFAULT;
  94. }
  95. /* Add one entry. */
  96. static void
  97. CONCAT(TABLE,_add) (struct TABLE *t, uint32_t wc, ELEMENT value)
  98. {
  99. uint32_t index1 = wc >> (t->q + t->p);
  100. uint32_t index2 = (wc >> t->p) & ((1 << t->q) - 1);
  101. uint32_t index3 = wc & ((1 << t->p) - 1);
  102. size_t i, i1, i2;
  103. if (value == CONCAT(TABLE,_get) (t, wc))
  104. return;
  105. if (index1 >= t->level1_size)
  106. {
  107. if (index1 >= t->level1_alloc)
  108. {
  109. size_t alloc = 2 * t->level1_alloc;
  110. if (alloc <= index1)
  111. alloc = index1 + 1;
  112. t->level1 = (uint32_t *) xrealloc ((char *) t->level1,
  113. alloc * sizeof (uint32_t));
  114. t->level1_alloc = alloc;
  115. }
  116. while (index1 >= t->level1_size)
  117. t->level1[t->level1_size++] = EMPTY;
  118. }
  119. if (t->level1[index1] == EMPTY)
  120. {
  121. if (t->level2_size == t->level2_alloc)
  122. {
  123. size_t alloc = 2 * t->level2_alloc + 1;
  124. t->level2 = (uint32_t *) xrealloc ((char *) t->level2,
  125. (alloc << t->q) * sizeof (uint32_t));
  126. t->level2_alloc = alloc;
  127. }
  128. i1 = t->level2_size << t->q;
  129. i2 = (t->level2_size + 1) << t->q;
  130. for (i = i1; i < i2; i++)
  131. t->level2[i] = EMPTY;
  132. t->level1[index1] = t->level2_size++;
  133. }
  134. index2 += t->level1[index1] << t->q;
  135. if (t->level2[index2] == EMPTY)
  136. {
  137. if (t->level3_size == t->level3_alloc)
  138. {
  139. size_t alloc = 2 * t->level3_alloc + 1;
  140. t->level3 = (ELEMENT *) xrealloc ((char *) t->level3,
  141. (alloc << t->p) * sizeof (ELEMENT));
  142. t->level3_alloc = alloc;
  143. }
  144. i1 = t->level3_size << t->p;
  145. i2 = (t->level3_size + 1) << t->p;
  146. for (i = i1; i < i2; i++)
  147. t->level3[i] = DEFAULT;
  148. t->level2[index2] = t->level3_size++;
  149. }
  150. index3 += t->level2[index2] << t->p;
  151. t->level3[index3] = value;
  152. }
  153. #ifdef ITERATE
  154. /* Apply a function to all entries in the table. */
  155. static void
  156. CONCAT(TABLE,_iterate) (struct TABLE *t,
  157. void (*fn) (uint32_t wc, ELEMENT value))
  158. {
  159. uint32_t index1;
  160. for (index1 = 0; index1 < t->level1_size; index1++)
  161. {
  162. uint32_t lookup1 = t->level1[index1];
  163. if (lookup1 != EMPTY)
  164. {
  165. uint32_t lookup1_shifted = lookup1 << t->q;
  166. uint32_t index2;
  167. for (index2 = 0; index2 < (1 << t->q); index2++)
  168. {
  169. uint32_t lookup2 = t->level2[index2 + lookup1_shifted];
  170. if (lookup2 != EMPTY)
  171. {
  172. uint32_t lookup2_shifted = lookup2 << t->p;
  173. uint32_t index3;
  174. for (index3 = 0; index3 < (1 << t->p); index3++)
  175. {
  176. ELEMENT lookup3 = t->level3[index3 + lookup2_shifted];
  177. if (lookup3 != DEFAULT)
  178. fn ((((index1 << t->q) + index2) << t->p) + index3,
  179. lookup3);
  180. }
  181. }
  182. }
  183. }
  184. }
  185. }
  186. #endif
  187. #ifndef NO_FINALIZE
  188. /* Finalize and shrink. */
  189. static void
  190. CONCAT(TABLE,_finalize) (struct TABLE *t)
  191. {
  192. size_t i, j, k;
  193. uint32_t reorder3[t->level3_size];
  194. uint32_t reorder2[t->level2_size];
  195. uint32_t level1_offset, level2_offset, level3_offset, last_offset;
  196. /* Uniquify level3 blocks. */
  197. k = 0;
  198. for (j = 0; j < t->level3_size; j++)
  199. {
  200. for (i = 0; i < k; i++)
  201. if (memcmp (&t->level3[i << t->p], &t->level3[j << t->p],
  202. (1 << t->p) * sizeof (ELEMENT)) == 0)
  203. break;
  204. /* Relocate block j to block i. */
  205. reorder3[j] = i;
  206. if (i == k)
  207. {
  208. if (i != j)
  209. memcpy (&t->level3[i << t->p], &t->level3[j << t->p],
  210. (1 << t->p) * sizeof (ELEMENT));
  211. k++;
  212. }
  213. }
  214. t->level3_size = k;
  215. for (i = 0; i < (t->level2_size << t->q); i++)
  216. if (t->level2[i] != EMPTY)
  217. t->level2[i] = reorder3[t->level2[i]];
  218. /* Uniquify level2 blocks. */
  219. k = 0;
  220. for (j = 0; j < t->level2_size; j++)
  221. {
  222. for (i = 0; i < k; i++)
  223. if (memcmp (&t->level2[i << t->q], &t->level2[j << t->q],
  224. (1 << t->q) * sizeof (uint32_t)) == 0)
  225. break;
  226. /* Relocate block j to block i. */
  227. reorder2[j] = i;
  228. if (i == k)
  229. {
  230. if (i != j)
  231. memcpy (&t->level2[i << t->q], &t->level2[j << t->q],
  232. (1 << t->q) * sizeof (uint32_t));
  233. k++;
  234. }
  235. }
  236. t->level2_size = k;
  237. for (i = 0; i < t->level1_size; i++)
  238. if (t->level1[i] != EMPTY)
  239. t->level1[i] = reorder2[t->level1[i]];
  240. /* Create and fill the resulting compressed representation. */
  241. last_offset =
  242. 5 * sizeof (uint32_t)
  243. + t->level1_size * sizeof (uint32_t)
  244. + (t->level2_size << t->q) * sizeof (uint32_t)
  245. + (t->level3_size << t->p) * sizeof (ELEMENT);
  246. t->result_size = (last_offset + 3) & ~3ul;
  247. t->result = (char *) xmalloc (t->result_size);
  248. level1_offset =
  249. 5 * sizeof (uint32_t);
  250. level2_offset =
  251. 5 * sizeof (uint32_t)
  252. + t->level1_size * sizeof (uint32_t);
  253. level3_offset =
  254. 5 * sizeof (uint32_t)
  255. + t->level1_size * sizeof (uint32_t)
  256. + (t->level2_size << t->q) * sizeof (uint32_t);
  257. ((uint32_t *) t->result)[0] = t->q + t->p;
  258. ((uint32_t *) t->result)[1] = t->level1_size;
  259. ((uint32_t *) t->result)[2] = t->p;
  260. ((uint32_t *) t->result)[3] = (1 << t->q) - 1;
  261. ((uint32_t *) t->result)[4] = (1 << t->p) - 1;
  262. for (i = 0; i < t->level1_size; i++)
  263. ((uint32_t *) (t->result + level1_offset))[i] =
  264. (t->level1[i] == EMPTY
  265. ? 0
  266. : (t->level1[i] << t->q) * sizeof (uint32_t) + level2_offset);
  267. for (i = 0; i < (t->level2_size << t->q); i++)
  268. ((uint32_t *) (t->result + level2_offset))[i] =
  269. (t->level2[i] == EMPTY
  270. ? 0
  271. : (t->level2[i] << t->p) * sizeof (ELEMENT) + level3_offset);
  272. for (i = 0; i < (t->level3_size << t->p); i++)
  273. ((ELEMENT *) (t->result + level3_offset))[i] = t->level3[i];
  274. if (last_offset < t->result_size)
  275. memset (t->result + last_offset, 0, t->result_size - last_offset);
  276. if (t->level1_alloc > 0)
  277. free (t->level1);
  278. if (t->level2_alloc > 0)
  279. free (t->level2);
  280. if (t->level3_alloc > 0)
  281. free (t->level3);
  282. }
  283. #endif
  284. #undef EMPTY
  285. #undef TABLE
  286. #undef ELEMENT
  287. #undef DEFAULT
  288. #undef ITERATE
  289. #undef NO_FINALIZE