1 #ifndef MARISA_ALPHA_TAIL_H_
2 #define MARISA_ALPHA_TAIL_H_
3 
4 #include "marisa-string.h"
5 #include "vector.h"
6 
7 namespace marisa_alpha {
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_ALPHA_DEBUG_IF(offset >= buf_.size(), MARISA_ALPHA_PARAM_ERROR);
37     return &buf_[offset];
38   }
39 
mode()40   int mode() const {
41     return (buf_.front() == '\0') ?
42         MARISA_ALPHA_BINARY_TAIL : MARISA_ALPHA_TEXT_TAIL;
43   }
empty()44   bool empty() const {
45     return buf_.empty();
46   }
size()47   std::size_t size() const {
48     return buf_.size();
49   }
total_size()50   std::size_t total_size() const {
51     return buf_.total_size();
52   }
53 
54   void clear();
55   void swap(Tail *rhs);
56 
57  private:
58   Vector<UInt8> buf_;
59 
60   void build_binary_tail(const Vector<String> &keys,
61       Vector<UInt32> *offsets);
62   bool build_text_tail(const Vector<String> &keys,
63       Vector<UInt32> *offsets);
64   void build_empty_tail(Vector<UInt32> *offsets);
65 
66   // Disallows copy and assignment.
67   Tail(const Tail &);
68   Tail &operator=(const Tail &);
69 };
70 
71 }  // namespace marisa_alpha
72 
73 #endif  // MARISA_ALPHA_TAIL_H_
74