Wayverb
min_size_vector.h
1 #pragma once
2 
3 #include "combined/model/vector.h"
4 
5 namespace wayverb {
6 namespace combined {
7 namespace model {
8 
10 template <typename T, size_t MinSize>
12  : public owning_member<min_size_vector<T, MinSize>, vector<T>> {
13 public:
15 
18  min_size_vector(size_t extra, const T& t)
19  : base_type{vector<T>{MinSize + extra, t}} {}
20 
21  min_size_vector(size_t extra)
22  : min_size_vector{extra, T{}} {}
23 
25  : min_size_vector{0} {}
26 
27  template <size_t N>
28  explicit min_size_vector(const T (&arr)[N])
29  : base_type{vector<T>{arr}} {
30  static_assert(MinSize <= N,
31  "Must be at least MinSize elements in initializer list.");
32  }
33 
34  // Extra functionality over a normal vector.
35 
36  bool can_erase() const { return MinSize < size(); }
37 
38  // Forwarding methods to impl.
39 
40  const auto& operator[](size_t index) const { return vec()[index]; }
41  auto& operator[](size_t index) { return vec()[index]; }
42 
43  auto& front() { return vec().front(); }
44  const auto& front() const { return vec().front(); }
45 
46  auto& back() { return vec().back(); }
47  const auto& back() const { return vec().back(); }
48 
49  auto cbegin() const { return vec().cbegin(); }
50  auto begin() const { return vec().begin(); }
51  auto begin() { return vec().begin(); }
52 
53  auto cend() const { return vec().cend(); }
54  auto end() const { return vec().end(); }
55  auto end() { return vec().end(); }
56 
57  void reserve(size_t items) { vec().reserve(items); }
58 
59  template <typename It>
60  auto insert(It it, const T& t) {
61  return vec().insert(std::move(it), t);
62  }
63 
64  void push_back(const T& t) { vec().push_back(t); }
65 
66  void pop_back() { vec().pop_back(); }
67 
68  // TODO could return std::optional iterator depending on whether or not
69  // erase was successful.
70  template <typename It>
71  void erase(It it) {
72  if (can_erase()) {
73  vec().erase(std::move(it));
74  }
75  }
76 
78  void resize(size_t new_size, const T& t) {
79  vec().resize(std::max(MinSize, new_size), t);
80  }
81 
82  void resize(size_t new_size) { resize(new_size, T{}); }
83 
84  auto size() const { return vec().size(); }
85  auto empty() const { return vec().empty(); }
86 
87  void clear() { vec().clear(); }
88 
89  NOTIFYING_COPY_ASSIGN_DECLARATION(min_size_vector)
90 private:
91  inline void swap(min_size_vector&) noexcept { using std::swap; };
92 
93  auto& vec() { return *this->template get<0>(); }
94  const auto& vec() const { return *this->template get<0>(); }
95 };
96 
97 } // namespace model
98 } // namespace combined
99 } // namespace wayverb
Definition: vector.h:15
void resize(size_t new_size, const T &t)
Will resize to std::max(MinSize, new_size)
Definition: min_size_vector.h:78
Definition: capsule_base.h:9
A vector which must hold some minimum number of elements.
Definition: min_size_vector.h:11
min_size_vector(size_t extra, const T &t)
Definition: min_size_vector.h:18