Wayverb
vector.h
1 #pragma once
2 
3 #include "combined/model/member.h"
4 
5 #include "utilities/event.h"
6 #include "utilities/mapping_iterator_adapter.h"
7 
8 #include <memory>
9 
10 namespace wayverb {
11 namespace combined {
12 namespace model {
13 
14 template <typename T>
15 class vector final : public basic_member<vector<T>> {
16 public:
18 
20  using value_type = T;
21 
22  vector(size_t elements, const T& t) {
23  reserve(elements);
24  for (auto i = 0u; i != elements; ++i) {
25  data_.emplace_back(*this, t);
26  }
27  }
28 
29  explicit vector(size_t elements)
30  : vector{elements, T{}} {}
31 
32  vector() = default;
33 
34  template <typename It>
35  vector(It b, It e) {
36  reserve(std::distance(b, e));
37  std::copy(b, e, std::back_inserter(*this));
38  }
39 
40  template <size_t N>
41  explicit vector(const T (&arr)[N])
42  : vector{std::begin(arr), std::end(arr)} {}
43 
44  vector(const vector& other)
45  : vector{make_item_extractor_iterator(std::begin(other)),
46  make_item_extractor_iterator(std::end(other))} {}
47 
49  vector& operator=(const vector& other) {
50  clear();
51  reserve(other.size());
52  std::copy(make_item_extractor_iterator(std::begin(other)),
53  make_item_extractor_iterator(std::end(other)),
54  std::back_inserter(*this));
55  this->notify();
56  return *this;
57  }
58 
59  const auto& operator[](size_t index) const { return data_[index]; }
60  auto& operator[](size_t index) { return data_[index]; }
61 
62  auto& front() { return data_.front(); }
63  const auto& front() const { return data_.front(); }
64 
65  auto& back() { return data_.back(); }
66  const auto& back() const { return data_.back(); }
67 
68  auto cbegin() const { return data_.cbegin(); }
69  auto begin() const { return data_.begin(); }
70  auto begin() { return data_.begin(); }
71 
72  auto cend() const { return data_.cend(); }
73  auto end() const { return data_.end(); }
74  auto end() { return data_.end(); }
75 
76  void reserve(size_t items) { data_.reserve(items); }
77 
78  template <typename It>
79  auto insert(It it, const T& t) {
80  const auto i = data_.emplace(std::move(it), *this, t);
81  this->notify();
82  return i;
83  }
84 
85  void push_back(const value_type& t) {
86  data_.emplace_back(*this, t);
87  this->notify();
88  }
89 
90  void pop_back() {
91  data_.pop_back();
92  this->notify();
93  }
94 
95  template <typename It>
96  auto erase(It it) {
97  const auto i = data_.erase(std::move(it));
98  this->notify();
99  return i;
100  }
101 
102  void resize(size_t new_size, const value_type& t) {
103  reserve(new_size);
104  while (new_size < size()) {
105  pop_back();
106  }
107  while (size() < new_size) {
108  push_back(t);
109  }
110  }
111 
112  void resize(size_t new_size) { resize(new_size, value_type{}); }
113 
114  auto size() const { return data_.size(); }
115  auto empty() const { return data_.empty(); }
116 
117  void clear() {
118  data_.clear();
119  this->notify();
120  }
121 
122  template <typename Archive>
123  void serialize(Archive& archive) {
125 
126  cereal::size_type size = this->size();
127  archive(cereal::make_size_tag(size));
128  resize(size);
129  for (const auto& i : *this) {
130  archive(*i);
131  }
132  }
133 
134  bool operator==(const vector& x) const {
135  // Two vectors are equal if their values are equal, *not* their
136  // memory layout.
137  return std::equal(
138  cbegin(),
139  cend(),
140  x.cbegin(),
141  x.cend(),
142  [](const auto& a, const auto& b) { return *a == *b; });
143  }
144  bool operator!=(const vector& x) const { return !operator==(x); }
145 
146 private:
147  std::vector<data_member> data_;
148 };
149 
150 } // namespace model
151 } // namespace combined
152 } // namespace wayverb
Definition: vector.h:15
vector & operator=(const vector &other)
Not strongly exception safe.
Definition: vector.h:49
Definition: capsule_base.h:9
void serialize(Archive &archive)
Definition: vector.h:123
T value_type
So that back_inserter works.
Definition: vector.h:20