ModErn Text Analysis
META Enumerates Textual Applications
disk_vector.h
Go to the documentation of this file.
1 
10 #ifndef META_DISK_VECTOR_H_
11 #define META_DISK_VECTOR_H_
12 
13 #include <type_traits>
14 #include <string>
15 #include <fcntl.h>
16 #include <stdio.h>
17 #include <sys/mman.h>
18 #include <unistd.h>
19 #include "meta.h"
20 
21 namespace meta
22 {
23 namespace util
24 {
25 
30 template <class T>
31 class disk_vector
32 {
33  static_assert(std::is_integral<T>::value || std::is_floating_point<T>::value
34  || std::is_base_of<util::numeric, T>::value,
35  "disk_vector templated types must be integral types");
36 
37  public:
45  disk_vector(const std::string& path, uint64_t size = 0);
46 
51 
56 
60  ~disk_vector();
61 
67  T& operator[](uint64_t idx);
68 
74  const T& operator[](uint64_t idx) const;
75 
85  T& at(uint64_t idx);
86 
96  const T& at(uint64_t idx) const;
97 
101  uint64_t size() const;
102 
106  class iterator : public std::iterator<std::random_access_iterator_tag, T>
107  {
109  friend disk_vector;
110 
111  private:
113  uint64_t idx_;
114 
116  T* data_;
117 
123  iterator(uint64_t idx, T* data) : idx_{idx}, data_{data}
124  {
125  /* nothing */
126  }
127 
128  public:
130  iterator() : idx_{0}, data_{nullptr}
131  {
132  /* nothing */
133  }
134 
136  iterator(const iterator& other) : idx_{other.idx_}, data_{other.data_}
137  {
138  /* nothing */
139  }
140 
143  {
144  std::swap(*this, other);
145  return *this;
146  }
147 
150  {
151  ++idx_;
152  return *this;
153  }
154 
157  {
158  iterator save{*this};
159  ++idx_;
160  return save;
161  }
162 
165  {
166  --idx_;
167  return *this;
168  }
169 
172  {
173  iterator save{*this};
174  --idx_;
175  return *this;
176  }
177 
179  bool operator==(const iterator& other)
180  {
181  return other.idx_ == idx_ && other.data_ == data_;
182  }
183 
185  bool operator!=(const iterator& other)
186  {
187  return !(*this == other);
188  }
189 
192  {
193  return data_[idx_];
194  }
195 
197  const T* operator->()
198  {
199  return &data_[idx_];
200  }
201 
203  bool operator<(const iterator& other) const
204  {
205  return idx_ < other.idx_;
206  }
207 
209  bool operator>(const iterator& other) const
210  {
211  return idx_ > other.idx_;
212  }
213 
215  bool operator<=(const iterator& other) const
216  {
217  return idx_ <= other.idx_;
218  }
219 
221  bool operator>=(const iterator& other) const
222  {
223  return idx_ >= other.idx_;
224  }
225  };
226 
230  iterator begin() const;
231 
235  iterator end() const;
236 
237  private:
239  std::string path_;
240 
242  T* start_;
243 
245  uint64_t size_;
246 
249 
250  public:
254  class disk_vector_exception : public std::runtime_error
255  {
256  public:
257  using std::runtime_error::runtime_error;
258  };
259 };
260 }
261 }
262 
263 #include "disk_vector.tcc"
264 #endif
friend disk_vector
Need to access disk_vector representation.
Definition: disk_vector.h:109
iterator(uint64_t idx, T *data)
Constructor for disk_vector to use.
Definition: disk_vector.h:123
Contains top-level namespace documentation for the META toolkit.
iterator & operator=(iterator other)
assignment operator.
Definition: disk_vector.h:142
iterator operator--(int)
Post-decrement.
Definition: disk_vector.h:171
iterator(const iterator &other)
Copy constructor.
Definition: disk_vector.h:136
const T * operator->()
Arrow operator.
Definition: disk_vector.h:197
bool operator>(const iterator &other) const
Operator>.
Definition: disk_vector.h:209
disk_vector(const std::string &path, uint64_t size=0)
Definition: disk_vector.tcc:14
iterator end() const
Definition: disk_vector.tcc:127
Provides iterator functionality for the disk_vector class.
Definition: disk_vector.h:106
int file_desc_
the file descriptor used to open and close the mmap file
Definition: disk_vector.h:248
iterator & operator++()
Pre-increment.
Definition: disk_vector.h:149
iterator()
Constructor.
Definition: disk_vector.h:130
T & at(uint64_t idx)
Definition: disk_vector.tcc:99
T * data_
The current element this iterator is at.
Definition: disk_vector.h:116
bool operator==(const iterator &other)
Equality.
Definition: disk_vector.h:179
uint64_t size() const
Definition: disk_vector.tcc:115
iterator begin() const
Definition: disk_vector.tcc:121
iterator & operator--()
Pre-decrement.
Definition: disk_vector.h:164
The ModErn Text Analysis toolkit is a suite of natural language processing, classification, information retreival, data mining, and other applications of text processing.
Definition: analyzer.h:24
bool operator<=(const iterator &other) const
Operator<=.
Definition: disk_vector.h:215
~disk_vector()
Destructor.
Definition: disk_vector.tcc:78
T & operator[](uint64_t idx)
Definition: disk_vector.tcc:87
bool operator!=(const iterator &other)
Inequality.
Definition: disk_vector.h:185
disk_vector & operator=(disk_vector &&)
Move assignment operator.
Definition: disk_vector.tcc:59
iterator operator++(int)
Post-increment.
Definition: disk_vector.h:156
uint64_t size_
this size of the memory-mapped file (in regards to T objects)
Definition: disk_vector.h:245
Basic exception for disk_vector.
Definition: disk_vector.h:254
std::string path_
the path to the file this disk_vector uses for storage
Definition: disk_vector.h:239
T & operator*()
Dereference operator.
Definition: disk_vector.h:191
uint64_t idx_
The current index this iterator is at.
Definition: disk_vector.h:113
bool operator<(const iterator &other) const
Operator<.
Definition: disk_vector.h:203
T * start_
the beginning of where the storage file is memory mapped
Definition: disk_vector.h:242
bool operator>=(const iterator &other) const
Operator>=.
Definition: disk_vector.h:221