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.h 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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 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. #ifndef _ACISM_H
  20. #define _ACISM_H
  21. #include <stdint.h>
  22. #include <stdlib.h> // malloc
  23. #include <string.h> // memcpy
  24. typedef int (*qsort_cmp)(const void *, const void *);
  25. #ifndef ACISM_SIZE
  26. # define ACISM_SIZE 4
  27. #endif
  28. #if ACISM_SIZE == 8
  29. typedef uint64_t TRAN, STATE, STRNO;
  30. # define SYM_BITS 9U
  31. # define SYM_MASK 511
  32. #else
  33. typedef uint32_t TRAN, STATE, STRNO;
  34. # define SYM_BITS psp->sym_bits
  35. # define SYM_MASK psp->sym_mask
  36. #endif
  37. typedef uint16_t SYMBOL;
  38. typedef unsigned _SYMBOL; // An efficient stacklocal SYMBOL
  39. #define IS_MATCH ((TRAN)1 << (8*sizeof(TRAN) - 1))
  40. #define IS_SUFFIX ((TRAN)1 << (8*sizeof(TRAN) - 2))
  41. #define T_FLAGS (IS_MATCH | IS_SUFFIX)
  42. typedef struct { STATE state; STRNO strno; } STRASH;
  43. typedef enum { BASE=2, USED=1 } USES;
  44. struct acism {
  45. TRAN* tranv;
  46. STRASH* hashv;
  47. unsigned flags;
  48. # define IS_MMAP 1
  49. #if ACISM_SIZE < 8
  50. TRAN sym_mask;
  51. unsigned sym_bits;
  52. #endif
  53. unsigned hash_mod; // search hashv starting at (state + sym) % hash_mod.
  54. unsigned hash_size; // #(hashv): hash_mod plus the overflows past [hash_mod-1]
  55. unsigned tran_size; // #(tranv)
  56. unsigned nsyms, nchars, nstrs, maxlen;
  57. SYMBOL symv[256];
  58. };
  59. #include "acism.h"
  60. // p_size: size of tranv + hashv
  61. static inline unsigned p_size(ac_trie_t const *psp)
  62. { return psp->hash_size * sizeof*psp->hashv
  63. + psp->tran_size * sizeof*psp->tranv; }
  64. static inline unsigned p_hash(ac_trie_t const *psp, STATE s)
  65. { return s * 107 % psp->hash_mod; }
  66. static inline void set_tranv(ac_trie_t *psp, void *mem)
  67. { psp->hashv = (STRASH*)&(psp->tranv = mem)[psp->tran_size]; }
  68. // TRAN accessors. For ACISM_SIZE=8, SYM_{BITS,MASK} do not use psp.
  69. static inline TRAN p_tran(ac_trie_t const *psp, STATE s, _SYMBOL sym)
  70. { return psp->tranv[s + sym] ^ sym; }
  71. static inline _SYMBOL t_sym(ac_trie_t const *psp, TRAN t)
  72. { (void)psp; return t & SYM_MASK; }
  73. static inline STATE t_next(ac_trie_t const *psp, TRAN t)
  74. { (void)psp; return (t & ~T_FLAGS) >> SYM_BITS; }
  75. static inline int t_isleaf(ac_trie_t const *psp, TRAN t)
  76. { return t_next(psp, t) >= psp->tran_size; }
  77. static inline int t_strno(ac_trie_t const *psp, TRAN t)
  78. { return t_next(psp, t) - psp->tran_size; }
  79. static inline _SYMBOL t_valid(ac_trie_t const *psp, TRAN t)
  80. { return !t_sym(psp, t); }
  81. #endif//_ACISM_H