ztsdb
valuevector.hpp
1 // (C) 2016 Leonardo Silvestri
2 //
3 // This file is part of ztsdb.
4 //
5 // ztsdb is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // ztsdb is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with ztsdb. If not, see <http://www.gnu.org/licenses/>.
17 
18 
19 #ifndef VALUE_VECTOR_HPP
20 #define VALUE_VECTOR_HPP
21 
22 
23 #include <cassert>
24 #include "valuevar.hpp"
25 #include "vector.hpp"
26 #include "index.hpp"
27 
28 
29 namespace arr {
30 
31  template<typename O>
32  struct Vector<val::Value, O> {
33  typedef O comparator;
34  typedef val::Value value_type;
35 
36  friend struct vector_iterator<val::Value, O>;
37  friend struct vector_const_iterator<val::Value, O>;
38  // friend bool operator== <>(const Vector<val::Value>& v1, const Vector<val::Value>& v2);
39 
40  template <typename U, typename UO>
41  friend void setv(Vector<U,UO>& v, size_t i, const U& t);
42  template <typename U, typename UO>
43  friend void setv_checkbefore(Vector<U,UO>& v, size_t i, const U& t);
44 
45  // constructors --------------------------------------------
46 
48  Vector(Vector<val::Value>&& v) : c(std::move(v.c)) { }
49 
50  // copy constructor.
51  Vector(const Vector<val::Value>& v,
52  std::unique_ptr<baseallocator>&& alloc_p = nullptr) : c(v.c) { }
53 
55  Vector(size_t n=0,
56  const val::Value& value=getInitValue<val::Value>(),
57  std::unique_ptr<baseallocator>&& alloc_p = nullptr) : c(n, value) { }
58 
60  Vector(rsv_t, size_t n, std::unique_ptr<baseallocator>&& alloc_p = nullptr) {
61  c.reserve(n);
62  }
63 
67  std::unique_ptr<baseallocator>&& alloc_p = nullptr) : c(b, e) { }
68 
70  template <class InputIterator>
71  Vector(const InputIterator& b,
72  const InputIterator& e,
73  std::unique_ptr<baseallocator>&& alloc_p = nullptr) : c(b, e) { }
74 
75  Vector(std::initializer_list<val::Value> l,
76  std::unique_ptr<baseallocator>&& alloc_p = nullptr) : c(l) { }
77 
79  Vector(std::unique_ptr<baseallocator>&& alloc_p) {
80  assert(false);
81  }
82 
83  void swap(Vector<val::Value>& o) {
84  std::swap(c, o.c);
85  }
86 
87  Vector& operator=(Vector<val::Value> other) {
88  swap(other);
89  return *this;
90  }
91 
92  bool isOrdered() { return false; }
93 
94  void at(arr::idx_type i, const val::Value& v) {
95  if (i > size() - 1) {
96  throw std::out_of_range("subscript out of bounds");
97  }
98  c[i] = v;
99  }
100  val::Value& operator[](size_t i) {
101  if (i > size() - 1) {
102  throw std::out_of_range("subscript out of bounds");
103  }
104  return c[i];
105  }
106  const val::Value& operator[](size_t i) const {
107  if (i > size() - 1) {
108  throw std::out_of_range("subscript out of bounds");
109  }
110  return c[i];
111  }
112 
113  void push_back(const val::Value& value) {
114  c.push_back(value);
115  }
116 
117  template <class InputIterator>
119  InputIterator first,
120  InputIterator last) {
121  auto diff = last - first;
122  if (diff <= 0) {
123  return position;
124  }
125  auto oldbegin = begin();
126  auto oldend = end();
127  resize(size() + static_cast<size_t>(diff));
128  // move down everything that is at and beyond position:
129  if (oldbegin != oldend && oldend - 1 - position > 0) {
130  for (auto iter = end(); iter != position; --iter) {
131  *(iter) = *(iter - diff);
132  }
133  }
134  // insert the elements
135  for (auto iter = first; iter != last; ++iter) {
136  *position = *iter;
137  ++position;
138  }
139  return position;
140  }
141 
143  c.erase(c.begin() + size_t(position));
144  return position;
145  }
146 
148  const vector_iterator<val::Value,O>& last) {
149  c.erase(c.begin() + size_t(first), c.begin() + size_t(last));
150  return first;
151  }
152 
153  explicit operator std::vector<val::Value>() const {
154  return c;
155  }
156 
157  const val::Value& front() const {
158  if (c.empty()) {
159  throw std::range_error("front on empty Vector");
160  }
161  return c[0];
162  }
163 
164  const val::Value& back() const {
165  if (c.empty()) {
166  throw std::range_error("back on empty Vector");
167  }
168  return c[c.size()-1];
169  }
170 
171  size_t size() const { return c.size(); }
172  void forceUnOrdered() { ; } // only here for template compatibility reasons
173 
174  Vector<val::Value>& assign(size_t count, const val::Value& value) {
175  c.assign(count, value);
176  return *this;
177  }
178 
179  Vector<val::Value>& resize(size_t n, size_t from=0) {
180  if (from > 0) {
181  throw std::out_of_range("valuevector can't resize with 'from' > 0");
182  }
183  c.resize(n);
184  return *this;
185  }
186 
187  Vector<val::Value>& resize(size_t n, const value_type& v) {
188  c.resize(n, v);
189  return *this;
190  }
191 
192  bool operator==(const Vector<val::Value,O>& v1) const {
193  return c == v1.c;
194  }
195 
196  template <typename AO=O>
197  Vector& sort() { return *this; } // only for template reasons
198 
199  template<typename U, typename AO=O>
200  Vector<U> sort_idx(size_t base) { return Vector<U>(); } // only for template reasons
201 
202  void deallocate() { }
203 
204  vector_iterator<val::Value,O> begin() { return vector_iterator<val::Value,O>(*this, 0); }
205  vector_iterator<val::Value,O> end() { return vector_iterator<val::Value,O>(*this, c.size()); }
206  vector_const_iterator<val::Value,O> begin() const {
207  return vector_const_iterator<val::Value,O>(*this, 0); }
209  return vector_const_iterator<val::Value,O>(*this, c.size()); }
210  vector_const_iterator<val::Value,O> cbegin() const {
211  return vector_const_iterator<val::Value,O>(*this, 0); }
213  return vector_const_iterator<val::Value,O>(*this, c.size()); }
214 
215  ~Vector() { }
216 
217  private:
218  val::Value& at(size_t i) { return c[i]; }
219  vector<val::Value> c;
220  };
221 
222 
223  // we redefine these so as not to take into account ordering:
224  template <>
225  inline void setv(Vector<val::Value>& v, size_t i, const val::Value& t) {
226  if (i >= v.size()) throw std::range_error("subscript out of bounds");
227  v.at(i, t);
228  }
229  template <>
230  inline void setv_checkbefore(Vector<val::Value>& v, size_t i, const val::Value& t) {
231  if (i >= v.size()) throw std::range_error("subscript out of bounds");
232  v.at(i, t);
233  }
234 
235 }
236 
237 
238 
239 #endif
arr::Vector::Vector
Vector(Vector< T, O > &&v)
move constructor.
Definition: vector_base.hpp:124
arr::Vector< val::Value, O >::Vector
Vector(const vector_const_iterator< val::Value, O > &b, const vector_const_iterator< val::Value, O > &e, std::unique_ptr< baseallocator > &&alloc_p=nullptr)
vector_iterator constructor.
Definition: valuevector.hpp:65
arr::Vector
Definition: vector_base.hpp:103
arr::Vector< val::Value, O >
Definition: valuevector.hpp:32
val
Contains the types that constitute the output of an evaluation by the interpreter.
Definition: display.hpp:31
arr::vector_const_iterator
Definition: vector_base.hpp:56
arr::vector_iterator
Definition: vector_base.hpp:55
arr::Vector< val::Value, O >::Vector
Vector(const InputIterator &b, const InputIterator &e, std::unique_ptr< baseallocator > &&alloc_p=nullptr)
std::vector iterator constructor.
Definition: valuevector.hpp:71
arr::Vector< val::Value, O >::Vector
Vector(std::unique_ptr< baseallocator > &&alloc_p)
constructor from a mmapallocator.
Definition: valuevector.hpp:79
arr::rsv_t
Definition: vector_base.hpp:59
arr
Contains the classes and functions that implement a multidimentional array type.
Definition: allocator.hpp:29
arr::Vector< val::Value, O >::Vector
Vector(size_t n=0, const val::Value &value=getInitValue< val::Value >(), std::unique_ptr< baseallocator > &&alloc_p=nullptr)
basic constructor with conditional initial value.
Definition: valuevector.hpp:55
arr::Vector< val::Value, O >::Vector
Vector(rsv_t, size_t n, std::unique_ptr< baseallocator > &&alloc_p=nullptr)
basic constructor leaving the vector allocated but unininitialized.
Definition: valuevector.hpp:60