File: reservedvector.hh

package info (click to toggle)
dune-common 2.11.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,048 kB
  • sloc: cpp: 54,403; python: 4,136; sh: 1,657; makefile: 17
file content (431 lines) | stat: -rw-r--r-- 12,358 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
// -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
// vi: set et ts=4 sw=2 sts=2:
// SPDX-FileCopyrightInfo: Copyright © DUNE Project contributors, see file LICENSE.md in module root
// SPDX-License-Identifier: LicenseRef-GPL-2.0-only-with-DUNE-exception
#ifndef DUNE_COMMON_RESERVEDVECTOR_HH
#define DUNE_COMMON_RESERVEDVECTOR_HH

/** \file
 * \brief An stl-compliant random-access container which stores everything on the stack
 */

#include <algorithm>
#include <array>
#include <cassert>
#include <iostream>
#include <iterator>
#include <cstddef>
#include <initializer_list>

#include <dune/common/hash.hh>
#include <dune/common/std/algorithm.hh>
#include <dune/common/std/compare.hh>

#ifdef CHECK_RESERVEDVECTOR
#define CHECKSIZE(X) assert(X)
#else
#define CHECKSIZE(X) {}
#endif

namespace Dune
{
  /**
     \brief A Vector class with statically reserved memory.

     ReservedVector is something between std::array and std::vector.
     It is a dynamically sized vector which can be extended and shrunk
     using methods like push_back and pop_back, but reserved memory is
     statically predefined.

     This implies that the vector cannot grow bigger than the predefined
     maximum size.

     \tparam T The value type ReservedVector stores.
     \tparam n The maximum number of objects the ReservedVector can store.

   */
  template<class T, int n>
  class ReservedVector
  {
    using storage_type = std::array<T,n>;

  public:

    /** @{ Typedefs */

    //! The type of object, T, stored in the vector.
    typedef typename storage_type::value_type value_type;
    //! Pointer to T.
    typedef typename storage_type::pointer pointer;
    //! Const pointer to T.
    typedef typename storage_type::const_pointer const_pointer;
    //! Reference to T
    typedef typename storage_type::reference reference;
    //! Const reference to T
    typedef typename storage_type::const_reference const_reference;
    //! An unsigned integral type.
    typedef typename storage_type::size_type size_type;
    //! A signed integral type.
    typedef typename storage_type::difference_type difference_type;
    //! Iterator used to iterate through a vector.
    typedef typename storage_type::iterator iterator;
    //! Const iterator used to iterate through a vector.
    typedef typename storage_type::const_iterator const_iterator;
    //! Reverse iterator
    typedef std::reverse_iterator<iterator> reverse_iterator;
    //! Const reverse iterator
    typedef std::reverse_iterator<const_iterator> const_reverse_iterator;

    /** @} */

    /** @{ Constructors */

    //! Constructs an empty vector
    constexpr ReservedVector()
          noexcept(std::is_nothrow_default_constructible_v<value_type>)
      : storage_()
      , size_(0)
    {}

    //! Constructs the vector with `count` elements that will be default-initialized.
    explicit constexpr ReservedVector(size_type count)
          noexcept(std::is_nothrow_default_constructible_v<value_type>)
      : storage_()
      , size_(count)
    {
      assert(count <= n);
    }

    //! Constructs the vector with `count` copies of elements with value `value`.
    constexpr ReservedVector(size_type count, const value_type& value)
          noexcept(std::is_nothrow_copy_assignable_v<value_type> &&
          noexcept(ReservedVector(count)))
      : ReservedVector(count)
    {
      for (size_type i=0; i<count; ++i)
        storage_[i] = value;
    }

    //! Constructs the vector from an iterator range `[first,last)`
    template<class InputIt,
      std::enable_if_t<std::is_convertible_v<typename std::iterator_traits<InputIt>::value_type, value_type>, int> = 0>
    constexpr ReservedVector(InputIt first, InputIt last)
          noexcept(std::is_nothrow_copy_assignable_v<value_type> &&
          noexcept(ReservedVector()))
      : ReservedVector()
    {
      for (size_type i=0; i<n && first!=last; ++i,++size_)
        storage_[i] = *first++;
      assert(first == last);
    }

    //! Constructs the vector from an initializer list
    constexpr ReservedVector(std::initializer_list<value_type> const& l)
          noexcept(std::is_nothrow_copy_assignable_v<value_type> &&
          noexcept(ReservedVector(l.begin(),l.end())))
      : ReservedVector(l.begin(),l.end())
    {}

    /** @} */

    /** @{ Comparison */

    //! Compares the values in the vector `this` with `that` for equality
    constexpr bool operator== (const ReservedVector& that) const noexcept
    {
      if (size() != that.size())
        return false;
      for (size_type i=0; i<size(); ++i)
        if (!(storage_[i]==that.storage_[i]))
          return false;
      return true;
    }

    //! Lexicographically compares the values in the vector `this` with `that`
    constexpr auto operator<=> (const ReservedVector& that) const noexcept
    {
      return Std::lexicographical_compare_three_way(begin(), end(), that.begin(), that.end());
    }

    /** @} */

    /** @{ Modifiers */

    //! Erases all elements.
    constexpr void clear() noexcept
    {
      size_ = 0;
    }

    //! Specifies a new size for the vector.
    constexpr void resize(size_type s) noexcept
    {
      CHECKSIZE(s<=n);
      size_ = s;
    }

    //! Appends an element to the end of a vector, up to the maximum size n, O(1) time.
    constexpr void push_back(const value_type& t)
          noexcept(std::is_nothrow_copy_assignable_v<value_type>)
    {
      CHECKSIZE(size_<n);
      storage_[size_++] = t;
    }

    //! Appends an element to the end of a vector by moving the value, up to the maximum size n, O(1) time.
    constexpr void push_back(value_type&& t)
          noexcept(std::is_nothrow_move_assignable_v<value_type>)
    {
      CHECKSIZE(size_<n);
      storage_[size_++] = std::move(t);
    }

    //! Appends an element to the end of a vector by constructing it in place
    template<class... Args>
    reference emplace_back(Args&&... args)
          noexcept(std::is_nothrow_constructible_v<value_type,decltype(args)...>)
    {
      CHECKSIZE(size_<n);
      value_type* p = &storage_[size_++];
      // first destroy any previously (default) constructed element at that location
      p->~value_type();
      // construct the value_type in place
      // NOTE: This is not an integral constant expression.
      // With c++20 we could use std::construct_at
      ::new (const_cast<void*>(static_cast<const volatile void*>(p)))
          value_type(std::forward<Args>(args)...);
      return *p;
    }

    //! Erases the last element of the vector, O(1) time.
    constexpr void pop_back() noexcept
    {
      if (! empty()) size_--;
    }

    /** @} */

    /** @{ Iterators  */

    //! Returns a iterator pointing to the beginning of the vector.
    constexpr iterator begin() noexcept
    {
      return storage_.begin();
    }

    //! Returns a const_iterator pointing to the beginning of the vector.
    constexpr const_iterator begin() const noexcept
    {
      return storage_.begin();
    }

    //! Returns a const_iterator pointing to the beginning of the vector.
    constexpr const_iterator cbegin() const noexcept
    {
      return storage_.cbegin();
    }

    //! Returns a const reverse-iterator pointing to the end of the vector.
    constexpr reverse_iterator rbegin() noexcept
    {
      return reverse_iterator{begin()+size()};
    }

    //! Returns a const reverse-iterator pointing to the end of the vector.
    constexpr const_reverse_iterator rbegin() const noexcept
    {
      return const_reverse_iterator{begin()+size()};
    }

    //! Returns a const reverse-iterator pointing to the end of the vector.
    constexpr const_reverse_iterator crbegin() const noexcept
    {
      return const_reverse_iterator{begin()+size()};
    }

    //! Returns an iterator pointing to the end of the vector.
    constexpr iterator end() noexcept
    {
      return storage_.begin()+size();
    }

    //! Returns a const_iterator pointing to the end of the vector.
    constexpr const_iterator end() const noexcept
    {
      return storage_.begin()+size();
    }

    //! Returns a const_iterator pointing to the end of the vector.
    constexpr const_iterator cend() const noexcept
    {
      return storage_.cbegin()+size();
    }

    //! Returns a const reverse-iterator pointing to the begin of the vector.
    constexpr reverse_iterator rend() noexcept
    {
      return reverse_iterator{begin()};
    }

    //! Returns a const reverse-iterator pointing to the begin of the vector.
    constexpr const_reverse_iterator rend() const noexcept
    {
      return const_reverse_iterator{begin()};
    }

    //! Returns a const reverse-iterator pointing to the begin of the vector.
    constexpr const_reverse_iterator crend() const noexcept
    {
      return const_reverse_iterator{begin()};
    }

    /** @} */

    /** @{ Element access  */

    //! Returns reference to the i'th element.
    constexpr reference at(size_type i)
    {
      if (!(i < size()))
        throw std::out_of_range("Index out of range");
      return storage_[i];
    }

    //! Returns a const reference to the i'th element.
    constexpr const_reference at(size_type i) const
    {
      if (!(i < size()))
        throw std::out_of_range("Index out of range");
      return storage_[i];
    }

    //! Returns reference to the i'th element.
    constexpr reference operator[] (size_type i) noexcept
    {
      CHECKSIZE(size_>i);
      return storage_[i];
    }

    //! Returns a const reference to the i'th element.
    constexpr const_reference operator[] (size_type i) const noexcept
    {
      CHECKSIZE(size_>i);
      return storage_[i];
    }

    //! Returns reference to first element of vector.
    constexpr reference front() noexcept
    {
      CHECKSIZE(size_>0);
      return storage_[0];
    }

    //! Returns const reference to first element of vector.
    constexpr const_reference front() const noexcept
    {
      CHECKSIZE(size_>0);
      return storage_[0];
    }

    //! Returns reference to last element of vector.
    constexpr reference back() noexcept
    {
      CHECKSIZE(size_>0);
      return storage_[size_-1];
    }

    //! Returns const reference to last element of vector.
    constexpr const_reference back() const noexcept
    {
      CHECKSIZE(size_>0);
      return storage_[size_-1];
    }

    //! Returns pointer to the underlying memory.
    constexpr pointer data() noexcept
    {
      return storage_.data();
    }

    //! Returns const pointer to the underlying memory.
    constexpr const_pointer data() const noexcept
    {
      return storage_.data();
    }

    /** @} */

    /** @{ Capacity */

    //! Returns number of elements in the vector.
    constexpr size_type size() const noexcept
    {
      return size_;
    }

    //! Returns true if vector has no elements.
    constexpr bool empty() const noexcept
    {
      return size_==0;
    }

    //! Returns current capacity (allocated memory) of the vector.
    static constexpr size_type capacity() noexcept
    {
      return n;
    }

    //! Returns the maximum length of the vector.
    static constexpr size_type max_size() noexcept
    {
      return n;
    }

    /** @} */

    /** @{ Operations */

    //! Fill the container with the value
    constexpr void fill(const value_type& value)
          noexcept(std::is_nothrow_copy_assignable_v<value_type>)
    {
      for (size_type i=0; i<size(); ++i)
        storage_[i] = value;
    }

    //! Swap the content with another vector
    void swap(ReservedVector& other)
          noexcept(std::is_nothrow_swappable_v<value_type>)
    {
      using std::swap;
      swap(storage_, other.storage_);
      swap(size_, other.size_);
    }

    /** @} */

    //! Send ReservedVector to an output stream
    friend std::ostream& operator<< (std::ostream& s, const ReservedVector& v)
    {
      for (size_type i=0; i<v.size(); i++)
        s << v[i] << "  ";
      return s;
    }

    inline friend std::size_t hash_value(const ReservedVector& v) noexcept
    {
      return hash_range(v.storage_.data(),v.storage_.data()+v.size_);
    }

  private:
    storage_type storage_;
    size_type size_;
  };

}

DUNE_DEFINE_HASH(DUNE_HASH_TEMPLATE_ARGS(typename T, int n),DUNE_HASH_TYPE(Dune::ReservedVector<T,n>))

#undef CHECKSIZE

#endif // DUNE_COMMON_RESERVEDVECTOR_HH