ModErn Text Analysis
META Enumerates Textual Applications
sparse_vector.h
Go to the documentation of this file.
1 
10 #ifndef META_UTIL_SPARSE_VECTOR_H_
11 #define META_UTIL_SPARSE_VECTOR_H_
12 
13 #include <cstdint>
14 #include <utility>
15 #include <vector>
16 
17 namespace meta
18 {
19 namespace util
20 {
21 
27 template <class Index, class Value>
29 {
30  public:
31  using pair_type = std::pair<Index, Value>;
32  using container_type = std::vector<pair_type>;
33  using iterator = typename container_type::iterator;
34  using const_iterator = typename container_type::const_iterator;
35 
39  sparse_vector() = default;
40 
45  sparse_vector(uint64_t size);
46 
52  template <class Iter>
53  sparse_vector(Iter begin, Iter end);
54 
63  Value& operator[](const Index& index);
64 
74  Value at(const Index& index) const;
75 
81  const_iterator find(const Index& index) const;
82 
93  template <class... Ts>
94  void emplace_back(Ts&&... ts);
95 
100  void reserve(uint64_t size);
101 
105  void clear();
106 
110  void shrink_to_fit();
111 
116  void condense();
117 
121  uint64_t size() const;
122 
126  bool empty() const;
127 
131  const container_type& contents() const;
132 
136  void contents(container_type cont);
137 
141  iterator begin();
142 
146  const_iterator begin() const;
147 
151  const_iterator cbegin() const;
152 
156  iterator end();
157 
161  const_iterator end() const;
162 
166  const_iterator cend() const;
167 
168  private:
172  container_type storage_;
173 };
174 }
175 }
176 
177 #include "util/sparse_vector.tcc"
178 #endif
iterator end()
Definition: sparse_vector.tcc:172
void emplace_back(Ts &&...ts)
Adds a new element to the vector, placing it at the back.
Definition: sparse_vector.tcc:86
const_iterator find(const Index &index) const
Definition: sparse_vector.tcc:69
const container_type & contents() const
Definition: sparse_vector.tcc:137
Value at(const Index &index) const
Gets the value associated with a given index in the vector.
Definition: sparse_vector.tcc:55
iterator begin()
Definition: sparse_vector.tcc:154
bool empty() const
Definition: sparse_vector.tcc:131
uint64_t size() const
Definition: sparse_vector.tcc:125
Represents a sparse vector, indexed by type Index and storing values of type Value.
Definition: sparse_vector.h:28
const_iterator cend() const
Definition: sparse_vector.tcc:184
void shrink_to_fit()
Optimises the memory usage of the vector.
Definition: sparse_vector.tcc:104
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
const_iterator cbegin() const
Definition: sparse_vector.tcc:166
sparse_vector()=default
Creates an empty sparse_vector.
void clear()
Empties the vector.
Definition: sparse_vector.tcc:98
void condense()
Condenses the vector to include only non-zero (value initialized) entries.
Definition: sparse_vector.tcc:110
container_type storage_
Internal storage for the sparse vector: a sorted vector of pairs.
Definition: sparse_vector.h:172
Value & operator[](const Index &index)
Gets the value associated with a given index in the vector, adding it with a value-initialized Value ...
Definition: sparse_vector.tcc:30
void reserve(uint64_t size)
Reserves space for elements.
Definition: sparse_vector.tcc:92