1#ifndef MARISA_VECTOR_H_
2#define MARISA_VECTOR_H_
3
4#include "io.h"
5
6namespace marisa {
7
8template <typename T>
9class Vector {
10 public:
11  Vector();
12  ~Vector();
13
14  void mmap(Mapper *mapper, const char *filename,
15      long offset = 0, int whence = SEEK_SET);
16  void map(const void *ptr, std::size_t size);
17  void map(Mapper &mapper);
18
19  void load(const char *filename,
20      long offset = 0, int whence = SEEK_SET);
21  void fread(std::FILE *file);
22  void read(int fd);
23  void read(std::istream &stream);
24  void read(Reader &reader);
25
26  void save(const char *filename, bool trunc_flag = false,
27      long offset = 0, int whence = SEEK_SET) const;
28  void fwrite(std::FILE *file) const;
29  void write(int fd) const;
30  void write(std::ostream &stream) const;
31  void write(Writer &writer) const;
32
33  void push_back(const T &x);
34  void pop_back();
35
36  void resize(std::size_t size);
37  void resize(std::size_t size, const T &x);
38  void reserve(std::size_t capacity);
39  void shrink();
40
41  void fix();
42
43  const T *begin() const {
44    return objs_;
45  }
46  const T *end() const {
47    return objs_ + size_;
48  }
49  const T &operator[](std::size_t i) const {
50    MARISA_DEBUG_IF(i > size_, MARISA_PARAM_ERROR);
51    return objs_[i];
52  }
53  const T &front() const {
54    MARISA_DEBUG_IF(size_ == 0, MARISA_STATE_ERROR);
55    return objs_[0];
56  }
57  const T &back() const {
58    MARISA_DEBUG_IF(size_ == 0, MARISA_STATE_ERROR);
59    return objs_[size_ - 1];
60  }
61
62  T *begin() {
63    MARISA_DEBUG_IF(fixed_, MARISA_STATE_ERROR);
64    return buf_;
65  }
66  T *end() {
67    MARISA_DEBUG_IF(fixed_, MARISA_STATE_ERROR);
68    return buf_ + size_;
69  }
70  T &operator[](std::size_t i) {
71    MARISA_DEBUG_IF(fixed_, MARISA_STATE_ERROR);
72    MARISA_DEBUG_IF(i > size_, MARISA_PARAM_ERROR);
73    return buf_[i];
74  }
75  T &front() {
76    MARISA_DEBUG_IF(fixed_ || (size_ == 0), MARISA_STATE_ERROR);
77    return buf_[0];
78  }
79  T &back() {
80    MARISA_DEBUG_IF(fixed_ || (size_ == 0), MARISA_STATE_ERROR);
81    return buf_[size_ - 1];
82  }
83
84  bool empty() const {
85    return size_ == 0;
86  }
87  std::size_t size() const {
88    return size_;
89  }
90  std::size_t capacity() const {
91    return capacity_;
92  }
93  bool fixed() const {
94    return fixed_;
95  }
96  std::size_t total_size() const {
97    return (sizeof(T) * size_) + sizeof(size_);
98  }
99
100  void clear() {
101    Vector().swap(this);
102  }
103  void swap(Vector *rhs);
104
105  static std::size_t max_size() {
106    return MARISA_UINT32_MAX;
107  }
108
109 private:
110  T *buf_;
111  const T *objs_;
112  UInt32 size_;
113  UInt32 capacity_;
114  bool fixed_;
115
116  void realloc(std::size_t new_capacity);
117
118  // Disallows copy and assignment.
119  Vector(const Vector &);
120  Vector &operator=(const Vector &);
121};
122
123}  // namespace marisa
124
125#include "vector-inline.h"
126
127#endif  // MARISA_VECTOR_H_
128