xref: /aosp_15_r20/external/marisa-trie/include/marisa/base.h (revision ab8db090fce404b23716c4c9194221ee27efe31c)
1*ab8db090SAndroid Build Coastguard Worker #ifndef MARISA_BASE_H_
2*ab8db090SAndroid Build Coastguard Worker #define MARISA_BASE_H_
3*ab8db090SAndroid Build Coastguard Worker 
4*ab8db090SAndroid Build Coastguard Worker // Old Visual C++ does not provide stdint.h.
5*ab8db090SAndroid Build Coastguard Worker #ifndef _MSC_VER
6*ab8db090SAndroid Build Coastguard Worker  #include <stdint.h>
7*ab8db090SAndroid Build Coastguard Worker #endif  // _MSC_VER
8*ab8db090SAndroid Build Coastguard Worker 
9*ab8db090SAndroid Build Coastguard Worker #ifdef __cplusplus
10*ab8db090SAndroid Build Coastguard Worker  #include <cstddef>
11*ab8db090SAndroid Build Coastguard Worker #else  // __cplusplus
12*ab8db090SAndroid Build Coastguard Worker  #include <stddef.h>
13*ab8db090SAndroid Build Coastguard Worker #endif  // __cplusplus
14*ab8db090SAndroid Build Coastguard Worker 
15*ab8db090SAndroid Build Coastguard Worker #ifdef __cplusplus
16*ab8db090SAndroid Build Coastguard Worker extern "C" {
17*ab8db090SAndroid Build Coastguard Worker #endif  // __cplusplus
18*ab8db090SAndroid Build Coastguard Worker 
19*ab8db090SAndroid Build Coastguard Worker #ifdef _MSC_VER
20*ab8db090SAndroid Build Coastguard Worker typedef unsigned __int8  marisa_uint8;
21*ab8db090SAndroid Build Coastguard Worker typedef unsigned __int16 marisa_uint16;
22*ab8db090SAndroid Build Coastguard Worker typedef unsigned __int32 marisa_uint32;
23*ab8db090SAndroid Build Coastguard Worker typedef unsigned __int64 marisa_uint64;
24*ab8db090SAndroid Build Coastguard Worker #else  // _MSC_VER
25*ab8db090SAndroid Build Coastguard Worker typedef uint8_t  marisa_uint8;
26*ab8db090SAndroid Build Coastguard Worker typedef uint16_t marisa_uint16;
27*ab8db090SAndroid Build Coastguard Worker typedef uint32_t marisa_uint32;
28*ab8db090SAndroid Build Coastguard Worker typedef uint64_t marisa_uint64;
29*ab8db090SAndroid Build Coastguard Worker #endif  // _MSC_VER
30*ab8db090SAndroid Build Coastguard Worker 
31*ab8db090SAndroid Build Coastguard Worker #if defined(_WIN64) || defined(__amd64__) || defined(__x86_64__) || \
32*ab8db090SAndroid Build Coastguard Worker     defined(__ia64__) || defined(__ppc64__) || defined(__powerpc64__) || \
33*ab8db090SAndroid Build Coastguard Worker     defined(__sparc64__) || defined(__mips64__) || defined(__aarch64__) || \
34*ab8db090SAndroid Build Coastguard Worker     defined(__s390x__)
35*ab8db090SAndroid Build Coastguard Worker  #define MARISA_WORD_SIZE 64
36*ab8db090SAndroid Build Coastguard Worker #else  // defined(_WIN64), etc.
37*ab8db090SAndroid Build Coastguard Worker  #define MARISA_WORD_SIZE 32
38*ab8db090SAndroid Build Coastguard Worker #endif  // defined(_WIN64), etc.
39*ab8db090SAndroid Build Coastguard Worker 
40*ab8db090SAndroid Build Coastguard Worker //#define MARISA_WORD_SIZE  (sizeof(void *) * 8)
41*ab8db090SAndroid Build Coastguard Worker 
42*ab8db090SAndroid Build Coastguard Worker #define MARISA_UINT8_MAX  ((marisa_uint8)~(marisa_uint8)0)
43*ab8db090SAndroid Build Coastguard Worker #define MARISA_UINT16_MAX ((marisa_uint16)~(marisa_uint16)0)
44*ab8db090SAndroid Build Coastguard Worker #define MARISA_UINT32_MAX ((marisa_uint32)~(marisa_uint32)0)
45*ab8db090SAndroid Build Coastguard Worker #define MARISA_UINT64_MAX ((marisa_uint64)~(marisa_uint64)0)
46*ab8db090SAndroid Build Coastguard Worker #define MARISA_SIZE_MAX   ((size_t)~(size_t)0)
47*ab8db090SAndroid Build Coastguard Worker 
48*ab8db090SAndroid Build Coastguard Worker #define MARISA_INVALID_LINK_ID MARISA_UINT32_MAX
49*ab8db090SAndroid Build Coastguard Worker #define MARISA_INVALID_KEY_ID  MARISA_UINT32_MAX
50*ab8db090SAndroid Build Coastguard Worker #define MARISA_INVALID_EXTRA   (MARISA_UINT32_MAX >> 8)
51*ab8db090SAndroid Build Coastguard Worker 
52*ab8db090SAndroid Build Coastguard Worker // Error codes are defined as members of marisa_error_code. This library throws
53*ab8db090SAndroid Build Coastguard Worker // an exception with one of the error codes when an error occurs.
54*ab8db090SAndroid Build Coastguard Worker typedef enum marisa_error_code_ {
55*ab8db090SAndroid Build Coastguard Worker   // MARISA_OK means that a requested operation has succeeded. In practice, an
56*ab8db090SAndroid Build Coastguard Worker   // exception never has MARISA_OK because it is not an error.
57*ab8db090SAndroid Build Coastguard Worker   MARISA_OK           = 0,
58*ab8db090SAndroid Build Coastguard Worker 
59*ab8db090SAndroid Build Coastguard Worker   // MARISA_STATE_ERROR means that an object was not ready for a requested
60*ab8db090SAndroid Build Coastguard Worker   // operation. For example, an operation to modify a fixed vector throws an
61*ab8db090SAndroid Build Coastguard Worker   // exception with MARISA_STATE_ERROR.
62*ab8db090SAndroid Build Coastguard Worker   MARISA_STATE_ERROR  = 1,
63*ab8db090SAndroid Build Coastguard Worker 
64*ab8db090SAndroid Build Coastguard Worker   // MARISA_NULL_ERROR means that an invalid NULL pointer has been given.
65*ab8db090SAndroid Build Coastguard Worker   MARISA_NULL_ERROR   = 2,
66*ab8db090SAndroid Build Coastguard Worker 
67*ab8db090SAndroid Build Coastguard Worker   // MARISA_BOUND_ERROR means that an operation has tried to access an out of
68*ab8db090SAndroid Build Coastguard Worker   // range address.
69*ab8db090SAndroid Build Coastguard Worker   MARISA_BOUND_ERROR  = 3,
70*ab8db090SAndroid Build Coastguard Worker 
71*ab8db090SAndroid Build Coastguard Worker   // MARISA_RANGE_ERROR means that an out of range value has appeared in
72*ab8db090SAndroid Build Coastguard Worker   // operation.
73*ab8db090SAndroid Build Coastguard Worker   MARISA_RANGE_ERROR  = 4,
74*ab8db090SAndroid Build Coastguard Worker 
75*ab8db090SAndroid Build Coastguard Worker   // MARISA_CODE_ERROR means that an undefined code has appeared in operation.
76*ab8db090SAndroid Build Coastguard Worker   MARISA_CODE_ERROR   = 5,
77*ab8db090SAndroid Build Coastguard Worker 
78*ab8db090SAndroid Build Coastguard Worker   // MARISA_RESET_ERROR means that a smart pointer has tried to reset itself.
79*ab8db090SAndroid Build Coastguard Worker   MARISA_RESET_ERROR  = 6,
80*ab8db090SAndroid Build Coastguard Worker 
81*ab8db090SAndroid Build Coastguard Worker   // MARISA_SIZE_ERROR means that a size has exceeded a library limitation.
82*ab8db090SAndroid Build Coastguard Worker   MARISA_SIZE_ERROR   = 7,
83*ab8db090SAndroid Build Coastguard Worker 
84*ab8db090SAndroid Build Coastguard Worker   // MARISA_MEMORY_ERROR means that a memory allocation has failed.
85*ab8db090SAndroid Build Coastguard Worker   MARISA_MEMORY_ERROR = 8,
86*ab8db090SAndroid Build Coastguard Worker 
87*ab8db090SAndroid Build Coastguard Worker   // MARISA_IO_ERROR means that an I/O operation has failed.
88*ab8db090SAndroid Build Coastguard Worker   MARISA_IO_ERROR     = 9,
89*ab8db090SAndroid Build Coastguard Worker 
90*ab8db090SAndroid Build Coastguard Worker   // MARISA_FORMAT_ERROR means that input was in invalid format.
91*ab8db090SAndroid Build Coastguard Worker   MARISA_FORMAT_ERROR = 10,
92*ab8db090SAndroid Build Coastguard Worker } marisa_error_code;
93*ab8db090SAndroid Build Coastguard Worker 
94*ab8db090SAndroid Build Coastguard Worker // Min/max values, flags and masks for dictionary settings are defined below.
95*ab8db090SAndroid Build Coastguard Worker // Please note that unspecified settings will be replaced with the default
96*ab8db090SAndroid Build Coastguard Worker // settings. For example, 0 is equivalent to (MARISA_DEFAULT_NUM_TRIES |
97*ab8db090SAndroid Build Coastguard Worker // MARISA_DEFAULT_TRIE | MARISA_DEFAULT_TAIL | MARISA_DEFAULT_ORDER).
98*ab8db090SAndroid Build Coastguard Worker 
99*ab8db090SAndroid Build Coastguard Worker // A dictionary consists of 3 tries in default. Usually more tries make a
100*ab8db090SAndroid Build Coastguard Worker // dictionary space-efficient but time-inefficient.
101*ab8db090SAndroid Build Coastguard Worker typedef enum marisa_num_tries_ {
102*ab8db090SAndroid Build Coastguard Worker   MARISA_MIN_NUM_TRIES     = 0x00001,
103*ab8db090SAndroid Build Coastguard Worker   MARISA_MAX_NUM_TRIES     = 0x0007F,
104*ab8db090SAndroid Build Coastguard Worker   MARISA_DEFAULT_NUM_TRIES = 0x00003,
105*ab8db090SAndroid Build Coastguard Worker } marisa_num_tries;
106*ab8db090SAndroid Build Coastguard Worker 
107*ab8db090SAndroid Build Coastguard Worker // This library uses a cache technique to accelerate search functions. The
108*ab8db090SAndroid Build Coastguard Worker // following enumerated type marisa_cache_level gives a list of available cache
109*ab8db090SAndroid Build Coastguard Worker // size options. A larger cache enables faster search but takes a more space.
110*ab8db090SAndroid Build Coastguard Worker typedef enum marisa_cache_level_ {
111*ab8db090SAndroid Build Coastguard Worker   MARISA_HUGE_CACHE        = 0x00080,
112*ab8db090SAndroid Build Coastguard Worker   MARISA_LARGE_CACHE       = 0x00100,
113*ab8db090SAndroid Build Coastguard Worker   MARISA_NORMAL_CACHE      = 0x00200,
114*ab8db090SAndroid Build Coastguard Worker   MARISA_SMALL_CACHE       = 0x00400,
115*ab8db090SAndroid Build Coastguard Worker   MARISA_TINY_CACHE        = 0x00800,
116*ab8db090SAndroid Build Coastguard Worker   MARISA_DEFAULT_CACHE     = MARISA_NORMAL_CACHE
117*ab8db090SAndroid Build Coastguard Worker } marisa_cache_level;
118*ab8db090SAndroid Build Coastguard Worker 
119*ab8db090SAndroid Build Coastguard Worker // This library provides 2 kinds of TAIL implementations.
120*ab8db090SAndroid Build Coastguard Worker typedef enum marisa_tail_mode_ {
121*ab8db090SAndroid Build Coastguard Worker   // MARISA_TEXT_TAIL merges last labels as zero-terminated strings. So, it is
122*ab8db090SAndroid Build Coastguard Worker   // available if and only if the last labels do not contain a NULL character.
123*ab8db090SAndroid Build Coastguard Worker   // If MARISA_TEXT_TAIL is specified and a NULL character exists in the last
124*ab8db090SAndroid Build Coastguard Worker   // labels, the setting is automatically switched to MARISA_BINARY_TAIL.
125*ab8db090SAndroid Build Coastguard Worker   MARISA_TEXT_TAIL         = 0x01000,
126*ab8db090SAndroid Build Coastguard Worker 
127*ab8db090SAndroid Build Coastguard Worker   // MARISA_BINARY_TAIL also merges last labels but as byte sequences. It uses
128*ab8db090SAndroid Build Coastguard Worker   // a bit vector to detect the end of a sequence, instead of NULL characters.
129*ab8db090SAndroid Build Coastguard Worker   // So, MARISA_BINARY_TAIL requires a larger space if the average length of
130*ab8db090SAndroid Build Coastguard Worker   // labels is greater than 8.
131*ab8db090SAndroid Build Coastguard Worker   MARISA_BINARY_TAIL       = 0x02000,
132*ab8db090SAndroid Build Coastguard Worker 
133*ab8db090SAndroid Build Coastguard Worker   MARISA_DEFAULT_TAIL      = MARISA_TEXT_TAIL,
134*ab8db090SAndroid Build Coastguard Worker } marisa_tail_mode;
135*ab8db090SAndroid Build Coastguard Worker 
136*ab8db090SAndroid Build Coastguard Worker // The arrangement of nodes affects the time cost of matching and the order of
137*ab8db090SAndroid Build Coastguard Worker // predictive search.
138*ab8db090SAndroid Build Coastguard Worker typedef enum marisa_node_order_ {
139*ab8db090SAndroid Build Coastguard Worker   // MARISA_LABEL_ORDER arranges nodes in ascending label order.
140*ab8db090SAndroid Build Coastguard Worker   // MARISA_LABEL_ORDER is useful if an application needs to predict keys in
141*ab8db090SAndroid Build Coastguard Worker   // label order.
142*ab8db090SAndroid Build Coastguard Worker   MARISA_LABEL_ORDER       = 0x10000,
143*ab8db090SAndroid Build Coastguard Worker 
144*ab8db090SAndroid Build Coastguard Worker   // MARISA_WEIGHT_ORDER arranges nodes in descending weight order.
145*ab8db090SAndroid Build Coastguard Worker   // MARISA_WEIGHT_ORDER is generally a better choice because it enables faster
146*ab8db090SAndroid Build Coastguard Worker   // matching.
147*ab8db090SAndroid Build Coastguard Worker   MARISA_WEIGHT_ORDER      = 0x20000,
148*ab8db090SAndroid Build Coastguard Worker 
149*ab8db090SAndroid Build Coastguard Worker   MARISA_DEFAULT_ORDER     = MARISA_WEIGHT_ORDER,
150*ab8db090SAndroid Build Coastguard Worker } marisa_node_order;
151*ab8db090SAndroid Build Coastguard Worker 
152*ab8db090SAndroid Build Coastguard Worker typedef enum marisa_config_mask_ {
153*ab8db090SAndroid Build Coastguard Worker   MARISA_NUM_TRIES_MASK    = 0x0007F,
154*ab8db090SAndroid Build Coastguard Worker   MARISA_CACHE_LEVEL_MASK  = 0x00F80,
155*ab8db090SAndroid Build Coastguard Worker   MARISA_TAIL_MODE_MASK    = 0x0F000,
156*ab8db090SAndroid Build Coastguard Worker   MARISA_NODE_ORDER_MASK   = 0xF0000,
157*ab8db090SAndroid Build Coastguard Worker   MARISA_CONFIG_MASK       = 0xFFFFF
158*ab8db090SAndroid Build Coastguard Worker } marisa_config_mask;
159*ab8db090SAndroid Build Coastguard Worker 
160*ab8db090SAndroid Build Coastguard Worker #ifdef __cplusplus
161*ab8db090SAndroid Build Coastguard Worker }  // extern "C"
162*ab8db090SAndroid Build Coastguard Worker #endif  // __cplusplus
163*ab8db090SAndroid Build Coastguard Worker 
164*ab8db090SAndroid Build Coastguard Worker #ifdef __cplusplus
165*ab8db090SAndroid Build Coastguard Worker 
166*ab8db090SAndroid Build Coastguard Worker // `std::swap` is in <utility> since C++ 11 but in <algorithm> in C++ 98:
167*ab8db090SAndroid Build Coastguard Worker #if __cplusplus >= 201103L
168*ab8db090SAndroid Build Coastguard Worker  #include <utility>
169*ab8db090SAndroid Build Coastguard Worker #else
170*ab8db090SAndroid Build Coastguard Worker  #include <algorithm>
171*ab8db090SAndroid Build Coastguard Worker #endif
172*ab8db090SAndroid Build Coastguard Worker namespace marisa {
173*ab8db090SAndroid Build Coastguard Worker 
174*ab8db090SAndroid Build Coastguard Worker typedef ::marisa_uint8  UInt8;
175*ab8db090SAndroid Build Coastguard Worker typedef ::marisa_uint16 UInt16;
176*ab8db090SAndroid Build Coastguard Worker typedef ::marisa_uint32 UInt32;
177*ab8db090SAndroid Build Coastguard Worker typedef ::marisa_uint64 UInt64;
178*ab8db090SAndroid Build Coastguard Worker 
179*ab8db090SAndroid Build Coastguard Worker typedef ::marisa_error_code ErrorCode;
180*ab8db090SAndroid Build Coastguard Worker 
181*ab8db090SAndroid Build Coastguard Worker typedef ::marisa_cache_level CacheLevel;
182*ab8db090SAndroid Build Coastguard Worker typedef ::marisa_tail_mode TailMode;
183*ab8db090SAndroid Build Coastguard Worker typedef ::marisa_node_order NodeOrder;
184*ab8db090SAndroid Build Coastguard Worker 
185*ab8db090SAndroid Build Coastguard Worker using std::swap;
186*ab8db090SAndroid Build Coastguard Worker 
187*ab8db090SAndroid Build Coastguard Worker }  // namespace marisa
188*ab8db090SAndroid Build Coastguard Worker #endif  // __cplusplus
189*ab8db090SAndroid Build Coastguard Worker 
190*ab8db090SAndroid Build Coastguard Worker #ifdef __cplusplus
191*ab8db090SAndroid Build Coastguard Worker  #include "marisa/exception.h"
192*ab8db090SAndroid Build Coastguard Worker  #include "marisa/scoped-ptr.h"
193*ab8db090SAndroid Build Coastguard Worker  #include "marisa/scoped-array.h"
194*ab8db090SAndroid Build Coastguard Worker #endif  // __cplusplus
195*ab8db090SAndroid Build Coastguard Worker 
196*ab8db090SAndroid Build Coastguard Worker #endif  // MARISA_BASE_H_
197