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