1 #ifndef MARISA_KEYSET_H_ 2 #define MARISA_KEYSET_H_ 3 4 #include "marisa/key.h" 5 6 namespace marisa { 7 8 class Keyset { 9 public: 10 enum { 11 BASE_BLOCK_SIZE = 4096, 12 EXTRA_BLOCK_SIZE = 1024, 13 KEY_BLOCK_SIZE = 256 14 }; 15 16 Keyset(); 17 18 void push_back(const Key &key); 19 void push_back(const Key &key, char end_marker); 20 21 void push_back(const char *str); 22 void push_back(const char *ptr, std::size_t length, float weight = 1.0); 23 24 const Key &operator[](std::size_t i) const { 25 MARISA_DEBUG_IF(i >= size_, MARISA_BOUND_ERROR); 26 return key_blocks_[i / KEY_BLOCK_SIZE][i % KEY_BLOCK_SIZE]; 27 } 28 Key &operator[](std::size_t i) { 29 MARISA_DEBUG_IF(i >= size_, MARISA_BOUND_ERROR); 30 return key_blocks_[i / KEY_BLOCK_SIZE][i % KEY_BLOCK_SIZE]; 31 } 32 num_keys()33 std::size_t num_keys() const { 34 return size_; 35 } 36 empty()37 bool empty() const { 38 return size_ == 0; 39 } size()40 std::size_t size() const { 41 return size_; 42 } total_length()43 std::size_t total_length() const { 44 return total_length_; 45 } 46 47 void reset(); 48 49 void clear(); 50 void swap(Keyset &rhs); 51 52 private: 53 scoped_array<scoped_array<char> > base_blocks_; 54 std::size_t base_blocks_size_; 55 std::size_t base_blocks_capacity_; 56 scoped_array<scoped_array<char> > extra_blocks_; 57 std::size_t extra_blocks_size_; 58 std::size_t extra_blocks_capacity_; 59 scoped_array<scoped_array<Key> > key_blocks_; 60 std::size_t key_blocks_size_; 61 std::size_t key_blocks_capacity_; 62 char *ptr_; 63 std::size_t avail_; 64 std::size_t size_; 65 std::size_t total_length_; 66 67 char *reserve(std::size_t size); 68 69 void append_base_block(); 70 void append_extra_block(std::size_t size); 71 void append_key_block(); 72 73 // Disallows copy and assignment. 74 Keyset(const Keyset &); 75 Keyset &operator=(const Keyset &); 76 }; 77 78 } // namespace marisa 79 80 #endif // MARISA_KEYSET_H_ 81