1 #ifndef MARISA_GRIMOIRE_TRIE_CONFIG_H_ 2 #define MARISA_GRIMOIRE_TRIE_CONFIG_H_ 3 4 #include "marisa/base.h" 5 6 namespace marisa { 7 namespace grimoire { 8 namespace trie { 9 10 class Config { 11 public: Config()12 Config() 13 : num_tries_(MARISA_DEFAULT_NUM_TRIES), 14 cache_level_(MARISA_DEFAULT_CACHE), 15 tail_mode_(MARISA_DEFAULT_TAIL), 16 node_order_(MARISA_DEFAULT_ORDER) {} 17 parse(int config_flags)18 void parse(int config_flags) { 19 Config temp; 20 temp.parse_(config_flags); 21 swap(temp); 22 } 23 flags()24 int flags() const { 25 return (int)num_tries_ | tail_mode_ | node_order_; 26 } 27 num_tries()28 std::size_t num_tries() const { 29 return num_tries_; 30 } cache_level()31 CacheLevel cache_level() const { 32 return cache_level_; 33 } tail_mode()34 TailMode tail_mode() const { 35 return tail_mode_; 36 } node_order()37 NodeOrder node_order() const { 38 return node_order_; 39 } 40 clear()41 void clear() { 42 Config().swap(*this); 43 } swap(Config & rhs)44 void swap(Config &rhs) { 45 marisa::swap(num_tries_, rhs.num_tries_); 46 marisa::swap(cache_level_, rhs.cache_level_); 47 marisa::swap(tail_mode_, rhs.tail_mode_); 48 marisa::swap(node_order_, rhs.node_order_); 49 } 50 51 private: 52 std::size_t num_tries_; 53 CacheLevel cache_level_; 54 TailMode tail_mode_; 55 NodeOrder node_order_; 56 parse_(int config_flags)57 void parse_(int config_flags) { 58 MARISA_THROW_IF((config_flags & ~MARISA_CONFIG_MASK) != 0, 59 MARISA_CODE_ERROR); 60 61 parse_num_tries(config_flags); 62 parse_cache_level(config_flags); 63 parse_tail_mode(config_flags); 64 parse_node_order(config_flags); 65 } 66 parse_num_tries(int config_flags)67 void parse_num_tries(int config_flags) { 68 const int num_tries = config_flags & MARISA_NUM_TRIES_MASK; 69 if (num_tries != 0) { 70 num_tries_ = static_cast<std::size_t>(num_tries); 71 } 72 } 73 parse_cache_level(int config_flags)74 void parse_cache_level(int config_flags) { 75 switch (config_flags & MARISA_CACHE_LEVEL_MASK) { 76 case 0: { 77 cache_level_ = MARISA_DEFAULT_CACHE; 78 break; 79 } 80 case MARISA_HUGE_CACHE: { 81 cache_level_ = MARISA_HUGE_CACHE; 82 break; 83 } 84 case MARISA_LARGE_CACHE: { 85 cache_level_ = MARISA_LARGE_CACHE; 86 break; 87 } 88 case MARISA_NORMAL_CACHE: { 89 cache_level_ = MARISA_NORMAL_CACHE; 90 break; 91 } 92 case MARISA_SMALL_CACHE: { 93 cache_level_ = MARISA_SMALL_CACHE; 94 break; 95 } 96 case MARISA_TINY_CACHE: { 97 cache_level_ = MARISA_TINY_CACHE; 98 break; 99 } 100 default: { 101 MARISA_THROW(MARISA_CODE_ERROR, "undefined cache level"); 102 } 103 } 104 } 105 parse_tail_mode(int config_flags)106 void parse_tail_mode(int config_flags) { 107 switch (config_flags & MARISA_TAIL_MODE_MASK) { 108 case 0: { 109 tail_mode_ = MARISA_DEFAULT_TAIL; 110 break; 111 } 112 case MARISA_TEXT_TAIL: { 113 tail_mode_ = MARISA_TEXT_TAIL; 114 break; 115 } 116 case MARISA_BINARY_TAIL: { 117 tail_mode_ = MARISA_BINARY_TAIL; 118 break; 119 } 120 default: { 121 MARISA_THROW(MARISA_CODE_ERROR, "undefined tail mode"); 122 } 123 } 124 } 125 parse_node_order(int config_flags)126 void parse_node_order(int config_flags) { 127 switch (config_flags & MARISA_NODE_ORDER_MASK) { 128 case 0: { 129 node_order_ = MARISA_DEFAULT_ORDER; 130 break; 131 } 132 case MARISA_LABEL_ORDER: { 133 node_order_ = MARISA_LABEL_ORDER; 134 break; 135 } 136 case MARISA_WEIGHT_ORDER: { 137 node_order_ = MARISA_WEIGHT_ORDER; 138 break; 139 } 140 default: { 141 MARISA_THROW(MARISA_CODE_ERROR, "undefined node order"); 142 } 143 } 144 } 145 146 // Disallows copy and assignment. 147 Config(const Config &); 148 Config &operator=(const Config &); 149 }; 150 151 } // namespace trie 152 } // namespace grimoire 153 } // namespace marisa 154 155 #endif // MARISA_GRIMOIRE_TRIE_CONFIG_H_ 156