1 #ifndef _STATIC_VECTOR_H_
2 #define _STATIC_VECTOR_H_
7 #include "ndb_type_traits.h"
9 template <typename T, size_t StaticSize = SMALL_SIZE_VEC>
12 static const bool is_trivially_destructible =
13 private_::is_trivially_destructible<T>::value;
15 // std::is_trivially_copyable not supported in g++-4.7
16 static const bool is_trivially_copyable = std::is_scalar<T>::value;
21 typedef T & reference;
22 typedef const T & const_reference;
23 typedef size_t size_type;
25 inline static_vector() : n(0) {}
26 inline ~static_vector() { clear(); }
28 inline static_vector(const static_vector &that)
34 // not efficient, don't use in performance critical parts
35 static_vector(std::initializer_list<T> l)
43 operator=(const static_vector &that)
65 INVARIANT(n <= StaticSize);
69 inline const_reference
72 return const_cast<static_vector *>(this)->front();
79 INVARIANT(n <= StaticSize);
83 inline const_reference
86 return const_cast<static_vector *>(this)->back();
93 if (!is_trivially_destructible)
99 push_back(const T &obj)
107 emplace_back(std::move(obj));
110 // C++11 goodness- a strange syntax this is
112 template <class... Args>
114 emplace_back(Args &&... args)
116 INVARIANT(n < StaticSize);
117 new (&(ptr()[n++])) T(std::forward<Args>(args)...);
126 inline const_reference
127 operator[](int i) const
129 return const_cast<static_vector *>(this)->operator[](i);
135 if (!is_trivially_destructible)
136 for (size_t i = 0; i < n; i++)
147 resize(size_t n, value_type val = value_type())
149 INVARIANT(n <= StaticSize);
153 new (&ptr()[this->n++]) T(val);
154 } else if (n < this->n) {
156 while (this->n > n) {
157 if (!is_trivially_destructible)
158 ptr()[this->n - 1].~T();
165 inline bool is_small_type() const { return true; }
167 template <typename Compare = std::less<T>>
169 sort(Compare c = Compare())
171 std::sort(begin(), end(), c);
176 template <typename ObjType>
177 class iterator_ : public std::iterator<std::bidirectional_iterator_tag, ObjType> {
178 friend class static_vector;
180 inline iterator_() : p(0) {}
182 template <typename O>
183 inline iterator_(const iterator_<O> &other)
200 operator==(const iterator_ &o) const
206 operator!=(const iterator_ &o) const
208 return !operator==(o);
212 operator<(const iterator_ &o) const
218 operator>=(const iterator_ &o) const
220 return !operator<(o);
224 operator>(const iterator_ &o) const
230 operator<=(const iterator_ &o) const
232 return !operator>(o);
250 operator+(int n) const
252 iterator_ cpy = *this;
257 operator-(int n) const
259 iterator_ cpy = *this;
264 operator-(const iterator_ &o) const
279 iterator_ cur = *this;
294 iterator_ cur = *this;
300 inline iterator_(ObjType *p) : p(p) {}
308 typedef iterator_<T> iterator;
309 typedef iterator_<const T> const_iterator;
311 typedef std::reverse_iterator<iterator> reverse_iterator;
312 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
317 return iterator(ptr());
320 inline const_iterator
323 return const_iterator(ptr());
329 return iterator(endptr());
332 inline const_iterator
335 return const_iterator(endptr());
338 inline reverse_iterator
341 return reverse_iterator(end());
344 inline const_reverse_iterator
347 return const_reverse_iterator(end());
350 inline reverse_iterator
353 return reverse_iterator(begin());
356 inline const_reverse_iterator
359 return const_reverse_iterator(begin());
364 assignFrom(const static_vector &that)
366 if (unlikely(this == &that))
369 INVARIANT(that.n <= StaticSize);
370 if (is_trivially_copyable) {
371 NDB_MEMCPY(ptr(), that.ptr(), that.n * sizeof(T));
373 for (size_t i = 0; i < that.n; i++)
374 new (&(ptr()[i])) T(that.ptr()[i]);
379 inline ALWAYS_INLINE T *
382 return reinterpret_cast<T *>(&elems_buf[0]);
385 inline ALWAYS_INLINE const T *
388 return reinterpret_cast<const T *>(&elems_buf[0]);
391 inline ALWAYS_INLINE T *
397 inline ALWAYS_INLINE const T *
404 char elems_buf[sizeof(T) * StaticSize];
407 #endif /* _STATIC_VECTOR_H_ */