1 #ifndef MARISA_QUERY_H_ 2 #define MARISA_QUERY_H_ 3 4 #include "marisa/base.h" 5 6 namespace marisa { 7 8 class Query { 9 public: Query()10 Query() : ptr_(NULL), length_(0), id_(0) {} Query(const Query & query)11 Query(const Query &query) 12 : ptr_(query.ptr_), length_(query.length_), id_(query.id_) {} 13 14 Query &operator=(const Query &query) { 15 ptr_ = query.ptr_; 16 length_ = query.length_; 17 id_ = query.id_; 18 return *this; 19 } 20 21 char operator[](std::size_t i) const { 22 MARISA_DEBUG_IF(i >= length_, MARISA_BOUND_ERROR); 23 return ptr_[i]; 24 } 25 set_str(const char * str)26 void set_str(const char *str) { 27 MARISA_DEBUG_IF(str == NULL, MARISA_NULL_ERROR); 28 std::size_t length = 0; 29 while (str[length] != '\0') { 30 ++length; 31 } 32 ptr_ = str; 33 length_ = length; 34 } set_str(const char * ptr,std::size_t length)35 void set_str(const char *ptr, std::size_t length) { 36 MARISA_DEBUG_IF((ptr == NULL) && (length != 0), MARISA_NULL_ERROR); 37 ptr_ = ptr; 38 length_ = length; 39 } set_id(std::size_t id)40 void set_id(std::size_t id) { 41 id_ = id; 42 } 43 ptr()44 const char *ptr() const { 45 return ptr_; 46 } length()47 std::size_t length() const { 48 return length_; 49 } id()50 std::size_t id() const { 51 return id_; 52 } 53 clear()54 void clear() { 55 Query().swap(*this); 56 } swap(Query & rhs)57 void swap(Query &rhs) { 58 marisa::swap(ptr_, rhs.ptr_); 59 marisa::swap(length_, rhs.length_); 60 marisa::swap(id_, rhs.id_); 61 } 62 63 private: 64 const char *ptr_; 65 std::size_t length_; 66 std::size_t id_; 67 }; 68 69 } // namespace marisa 70 71 #endif // MARISA_QUERY_H_ 72