1 #ifndef MARISA_GRIMOIRE_TRIE_ENTRY_H_ 2 #define MARISA_GRIMOIRE_TRIE_ENTRY_H_ 3 4 #include "marisa/base.h" 5 6 namespace marisa { 7 namespace grimoire { 8 namespace trie { 9 10 class Entry { 11 public: Entry()12 Entry() : ptr_(NULL), length_(0), id_(0) {} Entry(const Entry & entry)13 Entry(const Entry &entry) 14 : ptr_(entry.ptr_), length_(entry.length_), id_(entry.id_) {} 15 16 Entry &operator=(const Entry &entry) { 17 ptr_ = entry.ptr_; 18 length_ = entry.length_; 19 id_ = entry.id_; 20 return *this; 21 } 22 23 char operator[](std::size_t i) const { 24 MARISA_DEBUG_IF(i >= length_, MARISA_BOUND_ERROR); 25 return *(ptr_ - i); 26 } 27 set_str(const char * ptr,std::size_t length)28 void set_str(const char *ptr, std::size_t length) { 29 MARISA_DEBUG_IF((ptr == NULL) && (length != 0), MARISA_NULL_ERROR); 30 MARISA_DEBUG_IF(length > MARISA_UINT32_MAX, MARISA_SIZE_ERROR); 31 ptr_ = ptr + length - 1; 32 length_ = (UInt32)length; 33 } set_id(std::size_t id)34 void set_id(std::size_t id) { 35 MARISA_DEBUG_IF(id > MARISA_UINT32_MAX, MARISA_SIZE_ERROR); 36 id_ = (UInt32)id; 37 } 38 ptr()39 const char *ptr() const { 40 return ptr_ - length_ + 1; 41 } length()42 std::size_t length() const { 43 return length_; 44 } id()45 std::size_t id() const { 46 return id_; 47 } 48 49 class StringComparer { 50 public: operator()51 bool operator()(const Entry &lhs, const Entry &rhs) const { 52 for (std::size_t i = 0; i < lhs.length(); ++i) { 53 if (i == rhs.length()) { 54 return true; 55 } 56 if (lhs[i] != rhs[i]) { 57 return (UInt8)lhs[i] > (UInt8)rhs[i]; 58 } 59 } 60 return lhs.length() > rhs.length(); 61 } 62 }; 63 64 class IDComparer { 65 public: operator()66 bool operator()(const Entry &lhs, const Entry &rhs) const { 67 return lhs.id_ < rhs.id_; 68 } 69 }; 70 71 private: 72 const char *ptr_; 73 UInt32 length_; 74 UInt32 id_; 75 }; 76 77 } // namespace trie 78 } // namespace grimoire 79 } // namespace marisa 80 81 #endif // MARISA_GRIMOIRE_TRIE_ENTRY_H_ 82