You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

acism_create.c 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. /*
  2. ** Copyright (C) 2009-2014 Mischa Sandberg <mischasan@gmail.com>
  3. **
  4. ** This program is free software; you can redistribute it and/or modify
  5. ** it under the terms of the GNU Lesser General Public License Version 3 as
  6. ** published by the Free Software Foundation. You may not use, modify or
  7. ** distribute this program under any other version of the GNU Lesser General
  8. ** Public License.
  9. **
  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 Lesser General Public License for more details.
  14. **
  15. ** You should have received a copy of the GNU Lesser General Public License
  16. ** along with this program; if not, write to the Free Software
  17. ** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. */
  19. #include "_acism.h"
  20. typedef enum { BASE=2, USED=1 } USES;
  21. typedef struct tnode {
  22. struct tnode *child, *next, *back;
  23. // nrefs was used in "prune_backlinks".
  24. // It will be used again in "curtail".
  25. unsigned nrefs;
  26. STATE state;
  27. STRNO match;
  28. SYMBOL sym;
  29. char is_suffix; // "bool"
  30. } TNODE;
  31. //--------------|---------------------------------------------
  32. // bitwid: 1+floor(log2(u))
  33. static inline int bitwid(unsigned u)
  34. {
  35. int ret = !!u;
  36. if (u & 0xFFFF0000) u >>= 16, ret += 16;
  37. if (u & 0x0000FF00) u >>= 8, ret += 8;
  38. if (u & 0x000000F0) u >>= 4, ret += 4;
  39. if (u & 0x0000000C) u >>= 2, ret += 2;
  40. if (u & 0x00000002) ret++;
  41. return ret;
  42. }
  43. static void fill_symv(ACISM*, MEMREF const*, int ns);
  44. static int create_tree(TNODE*, SYMBOL const*symv, MEMREF const*strv, int nstrs);
  45. static void add_backlinks(TNODE*, TNODE**, TNODE**);
  46. static int interleave(TNODE*, int nnodes, int nsyms, TNODE**, TNODE**);
  47. static void fill_tranv(ACISM*, TNODE const*);
  48. static void fill_hashv(ACISM*, TNODE const*, int nn);
  49. static TNODE* find_child(TNODE*, SYMBOL);
  50. // (ns) is either a STATE, or a (STRNO + tran_size)
  51. static inline void
  52. set_tran(ACISM *psp, STATE s, SYMBOL sym, int match, int suffix, TRAN ns)
  53. {
  54. psp->tranv[s + sym] = sym | (match ? IS_MATCH : 0)
  55. | (suffix ? IS_SUFFIX : 0)
  56. | (ns << SYM_BITS);
  57. }
  58. // Track statistics for construction
  59. #ifdef ACISM_STATS
  60. typedef struct { long long val; const char *name; } PSSTAT;
  61. extern PSSTAT psstat[];
  62. # define NOTE(n) (psstat[__LINE__] = (PSSTAT) {n, #n})
  63. # define HIT(id) (psstat[__LINE__].val++, psstat[__LINE__].name = id)
  64. #else
  65. # define NOTE(n) (void)0
  66. # define HIT(id) (void)0
  67. #endif //ACISM_STATS
  68. //--------------|---------------------------------------------
  69. ACISM*
  70. acism_create(MEMREF const* strv, int nstrs)
  71. {
  72. TNODE **v1 = NULL, **v2 = NULL;
  73. ACISM *psp = g_malloc0(sizeof*psp);
  74. fill_symv(psp, strv, nstrs);
  75. TNODE *troot = g_malloc0((psp->nchars + 1) * sizeof(*troot));
  76. int nnodes = create_tree(troot, psp->symv, strv, nstrs);
  77. NOTE(nnodes);
  78. // v1, v2: breadth-first work vectors for add_backlink and interleave.
  79. int i = (nstrs + 1) * sizeof(TNODE);
  80. add_backlinks(troot, v1 = g_malloc0(i), v2 = g_malloc0(i));
  81. int nhash = 0;
  82. TNODE* tp = troot + nnodes;
  83. while (--tp > troot)
  84. nhash += tp->match && tp->child;
  85. // Calculate each node's offset in tranv[]:
  86. psp->tran_size = interleave(troot, nnodes, psp->nsyms, v1, v2);
  87. if (bitwid(psp->tran_size + nstrs - 1) + SYM_BITS > sizeof(TRAN)*8 - 2)
  88. goto FAIL;
  89. if (nhash) {
  90. // Hash table is for match info of non-leaf nodes (only).
  91. // Set hash_size for p_size(psp):
  92. psp->hash_mod = nhash * 5 / 4 + 1;
  93. // Initially oversize the table for overflows without wraparound.
  94. psp->hash_size = psp->hash_mod + nhash;
  95. }
  96. set_tranv(psp, g_malloc0(p_size(psp) + sizeof(TRAN)));
  97. if (!psp->tranv) goto FAIL;
  98. fill_tranv(psp, troot);
  99. // The root state (0) must not look like a valid backref.
  100. // Any symbol value other than (0) in tranv[0] ensures that.
  101. psp->tranv[0] = 1;
  102. if (nhash) {
  103. fill_hashv(psp, troot, nnodes);
  104. // Adjust hash_size to include trailing overflows
  105. // but trim trailing empty slots.
  106. psp->hash_size = psp->hash_mod;
  107. while ( psp->hashv[psp->hash_size].state) ++psp->hash_size;
  108. while (!psp->hashv[psp->hash_size - 1].state) --psp->hash_size;
  109. set_tranv(psp, g_realloc(psp->tranv, p_size(psp)));
  110. }
  111. // Diagnostics/statistics only:
  112. psp->nstrs = nstrs;
  113. for (i = psp->maxlen = 0; i < nstrs; ++i)
  114. if (psp->maxlen < strv[i].len) psp->maxlen = strv[i].len;
  115. goto DONE;
  116. FAIL: acism_destroy(psp), psp = NULL;
  117. DONE: free(troot), free(v1), free(v2);
  118. return psp;
  119. }
  120. typedef struct { int freq, rank; } FRANK;
  121. static int frcmp(FRANK*a, FRANK*b) { return a->freq - b->freq; }
  122. static void
  123. fill_symv(ACISM *psp, MEMREF const *strv, int nstrs)
  124. {
  125. int i, j;
  126. FRANK frv[256];
  127. for (i = 0; i < 256; ++i) frv[i] = (FRANK){0,i};
  128. for (i = 0; i < nstrs; ++i)
  129. for (psp->nchars += j = strv[i].len; --j >= 0;)
  130. frv[(uint8_t)strv[i].ptr[j]].freq++;
  131. qsort(frv, 256, sizeof*frv, (qsort_cmp)frcmp);
  132. for (i = 256; --i >= 0 && frv[i].freq;)
  133. psp->symv[frv[i].rank] = ++psp->nsyms;
  134. ++psp->nsyms;
  135. #if ACISM_SIZE < 8
  136. psp->sym_bits = bitwid(psp->nsyms);
  137. psp->sym_mask = ~((~0u) << psp->sym_bits);
  138. #endif
  139. }
  140. static int
  141. create_tree(TNODE *Tree, SYMBOL const *symv, MEMREF const *strv, int nstrs)
  142. {
  143. int i, j;
  144. TNODE *nextp = Tree + 1;
  145. for (i = 0; i < nstrs; ++i) {
  146. TNODE *tp = Tree;
  147. for (j = 0; tp->child && j < (int)strv[i].len; ++j) {
  148. SYMBOL sym = symv[(uint8_t)strv[i].ptr[j]];
  149. if (sym < tp->child->sym) {
  150. // Prep to insert new node before tp->child
  151. nextp->next = tp->child;
  152. break;
  153. }
  154. tp = tp->child;
  155. while (tp->next && sym >= tp->next->sym)
  156. tp = tp->next;
  157. // Insert new sibling after tp
  158. if (sym > tp->sym) {
  159. nextp->next = tp->next;
  160. tp = tp->next = nextp++;
  161. tp->sym = sym;
  162. tp->back = Tree;
  163. }
  164. }
  165. for (; j < (int) strv[i].len; ++j) {
  166. tp = tp->child = nextp++;
  167. tp->sym = symv[(uint8_t)strv[i].ptr[j]];
  168. tp->back = Tree;
  169. }
  170. tp->match = i + 1; // Encode strno as nonzero
  171. }
  172. return nextp - Tree;
  173. }
  174. static void
  175. add_backlinks(TNODE *troot, TNODE **v1, TNODE **v2)
  176. {
  177. TNODE *tp, **tmp;
  178. for (tp = troot->child, tmp = v1; tp; tp = tp->next)
  179. *tmp++ = tp;
  180. *tmp = NULL;
  181. while (*v1) {
  182. TNODE **spp = v1, **dpp = v2, *srcp, *dstp;
  183. while ((srcp = *spp++)) {
  184. for (dstp = srcp->child; dstp; dstp = dstp->next) {
  185. TNODE *bp = NULL;
  186. if (dstp->child)
  187. *dpp++ = dstp;
  188. // Go through the parent (srcp) node's backlink chain,
  189. // looking for a useful backlink for the child (dstp).
  190. // If the parent (srcp) has a backlink to (tp),
  191. // and (tp) has a child matching the transition sym
  192. // for (srcp -> dstp), then it is a useful backlink
  193. // for the child (dstp).
  194. // Note that backlinks do not point at the suffix match;
  195. // they point at the PARENT of that match.
  196. for (tp = srcp->back; tp; tp = tp->back)
  197. if ((bp = find_child(tp, dstp->sym)))
  198. break;
  199. if (!bp)
  200. bp = troot;
  201. dstp->back = dstp->child ? bp : tp ? tp : troot;
  202. dstp->back->nrefs++;
  203. dstp->is_suffix = bp->match || bp->is_suffix;
  204. }
  205. }
  206. *dpp = 0;
  207. tmp = v1; v1 = v2; v2 = tmp;
  208. }
  209. }
  210. static int
  211. interleave(TNODE *troot, int nnodes, int nsyms, TNODE **v1, TNODE **v2)
  212. {
  213. unsigned usev_size = nnodes + nsyms;
  214. char *usev = g_malloc0(usev_size * sizeof(*usev));
  215. STATE last_trans = 0, startv[257][2];
  216. TNODE *cp, **tmp;
  217. memset(startv, 0, nsyms * sizeof*startv);
  218. // Iterate through one level of the Tree at a time.
  219. // That srsly improves locality (L1-cache use).
  220. v1[0] = troot, v1[1] = NULL;
  221. for (; *v1; tmp = v1, v1 = v2, v2 = tmp) {
  222. TNODE **srcp = v1, **dstp = v2, *tp;
  223. while ((tp = *srcp++)) {
  224. if (!tp->child) continue;
  225. HIT("nonleaf");
  226. if (tp->back == troot) tp->back = NULL; // simplify tests.
  227. cp = tp->child;
  228. STATE pos, *startp = &startv[cp->sym][!!tp->back];
  229. while ((cp = cp->next)) {
  230. STATE *newp = &startv[cp->sym][!!tp->back];
  231. if (*startp < *newp) startp = newp;
  232. }
  233. // If (tp) has a backref, we need a slot at offset 0
  234. // that is free as a base AND to be used (filled in).
  235. char need = tp->back ? BASE|USED : BASE;
  236. for (pos = *startp;; ++pos) {
  237. if (usev[pos] & need) {
  238. HIT("inner loop");
  239. continue;
  240. }
  241. for (cp = tp->child; cp; cp = cp->next) {
  242. HIT("child loop");
  243. if (usev[pos + cp->sym] & USED) break;
  244. }
  245. // No child needs an in-use slot? We're done.
  246. if (!cp) break;
  247. }
  248. tp->state = pos;
  249. // Mark node's base and children as used:
  250. usev[pos] |= need;
  251. STATE last = 0; // Make compiler happy
  252. int nkids = 0;
  253. for (cp = tp->child; cp; *dstp++ = cp, cp = cp->next, ++nkids)
  254. usev[last = pos + cp->sym] |= USED;
  255. // This is a HEURISTIC for advancing search for other nodes
  256. *startp += (pos - *startp) / nkids;
  257. if (last_trans < last) {
  258. last_trans = last;
  259. if (last + nsyms >= usev_size) {
  260. usev = g_realloc(usev, usev_size << 1);
  261. memset(usev + usev_size, 0, usev_size);
  262. usev_size <<= 1;
  263. }
  264. }
  265. }
  266. *dstp = NULL;
  267. }
  268. free(usev);
  269. return last_trans + 1;
  270. }
  271. static void
  272. fill_hashv(ACISM *psp, TNODE const treev[], int nnodes)
  273. {
  274. STRASH *sv = g_malloc0(psp->hash_mod * sizeof*sv), *sp = sv;
  275. int i;
  276. // First pass: insert without resolving collisions.
  277. for (i = 0; i < nnodes; ++i) {
  278. STATE base = treev[i].state;
  279. TNODE const *tp;
  280. for (tp = treev[i].child; tp; tp = tp->next) {
  281. if (tp->match && tp->child) {
  282. STATE state = base + tp->sym;
  283. STRASH *hp = &psp->hashv[p_hash(psp, state)];
  284. *(hp->state ? sp++ : hp) = (STRASH){state, tp->match - 1};
  285. }
  286. }
  287. }
  288. while (--sp >= sv) {
  289. HIT("hash collisions");
  290. for (i = p_hash(psp, sp->state); psp->hashv[i].state; ++i)
  291. HIT("hash displacements");
  292. psp->hashv[i] = *sp;
  293. }
  294. free(sv);
  295. }
  296. static void
  297. fill_tranv(ACISM *psp, TNODE const*tp)
  298. {
  299. TNODE const *cp = tp->child;
  300. if (cp && tp->back)
  301. set_tran(psp, tp->state, 0, 0, 0, tp->back->state);
  302. for (; cp; cp = cp->next) {
  303. //NOTE: cp->match is (strno+1) so that !cp->match means "no match".
  304. set_tran(psp, tp->state, cp->sym, cp->match, cp->is_suffix,
  305. cp->child ? cp->state : cp->match - 1 + psp->tran_size);
  306. if (cp->child)
  307. fill_tranv(psp, cp);
  308. }
  309. }
  310. static TNODE *
  311. find_child(TNODE *tp, SYMBOL sym)
  312. {
  313. for (tp = tp->child; tp && tp->sym < sym; tp = tp->next);
  314. return tp && tp->sym == sym ? tp : NULL;
  315. }
  316. #ifdef ACISM_STATS
  317. PSSTAT psstat[__LINE__] = {{__LINE__,0}};
  318. #endif//ACISM_STATS
  319. //EOF