1 #ifndef MARISA_TAIL_H_ 2 #define MARISA_TAIL_H_ 3 4 #include "marisa-string.h" 5 #include "vector.h" 6 7 namespace marisa { 8 9 class Tail { 10 public: 11 Tail(); 12 13 void build(const Vector<String> &keys, 14 Vector<UInt32> *offsets, int mode); 15 16 void mmap(Mapper *mapper, const char *filename, 17 long offset = 0, int whence = SEEK_SET); 18 void map(const void *ptr, std::size_t size); 19 void map(Mapper &mapper); 20 21 void load(const char *filename, 22 long offset = 0, int whence = SEEK_SET); 23 void fread(::FILE *file); 24 void read(int fd); 25 void read(std::istream &stream); 26 void read(Reader &reader); 27 28 void save(const char *filename, bool trunc_flag = true, 29 long offset = 0, int whence = SEEK_SET) const; 30 void fwrite(::FILE *file) const; 31 void write(int fd) const; 32 void write(std::ostream &stream) const; 33 void write(Writer &writer) const; 34 35 const UInt8 *operator[](std::size_t offset) const { 36 MARISA_DEBUG_IF(offset >= buf_.size(), MARISA_PARAM_ERROR); 37 return &buf_[offset]; 38 } 39 mode()40 int mode() const { 41 return (buf_.front() == '\0') ? MARISA_BINARY_TAIL : MARISA_TEXT_TAIL; 42 } empty()43 bool empty() const { 44 return buf_.empty(); 45 } size()46 std::size_t size() const { 47 return buf_.size(); 48 } total_size()49 std::size_t total_size() const { 50 return buf_.total_size(); 51 } 52 53 void clear(); 54 void swap(Tail *rhs); 55 56 private: 57 Vector<UInt8> buf_; 58 59 void build_binary_tail(const Vector<String> &keys, 60 Vector<UInt32> *offsets); 61 bool build_text_tail(const Vector<String> &keys, 62 Vector<UInt32> *offsets); 63 void build_empty_tail(Vector<UInt32> *offsets); 64 65 // Disallows copy and assignment. 66 Tail(const Tail &); 67 Tail &operator=(const Tail &); 68 }; 69 70 } // namespace marisa 71 72 #endif // MARISA_TAIL_H_ 73