mn_fixed_vector.hpp
Go to the documentation of this file.
1 /*
2 *This file is part of the Mini Thread Library (https://github.com/RoseLeBlood/MiniThread ).
3 *Copyright (c) 2018-2021 Amber-Sophia Schroeck
4 *
5 *The Mini Thread Library is free software; you can redistribute it and/or modify
6 *it under the terms of the GNU Lesser General Public License as published by
7 *the Free Software Foundation, version 3, or (at your option) any later version.
8 
9 *The Mini Thread Library is distributed in the hope that it will be useful, but
10 *WITHOUT ANY WARRANTY; without even the implied warranty of
11 *MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 *General Public License for more details.
13 *
14 *You should have received a copy of the GNU Lesser General Public
15 *License along with the Mini Thread Library; if not, see
16 *<https://www.gnu.org/licenses/>.
17 */
18 #ifndef _MINLIB_f19367b0_5308_4f90_a8b9_5bb631bdb591_H_
19 #define _MINLIB_f19367b0_5308_4f90_a8b9_5bb631bdb591_H_
20 
21 #include "mn_vector.hpp"
22 
23 namespace mn {
24  namespace container {
25  template<typename T, class TAllocator, int TCapacity>
27  using allocator_type = TAllocator;
29  using value_type = T;
30  using pointer = value_type*;
34 
35  explicit fixed_vector_storage(const TAllocator& allocator)
36  : m_begin((pointer)&m_data[0]),
37  m_end(m_begin),
38  m_capacityEnd(m_begin + TCapacity),
39  m_allocator(allocator) ,
40  m_max_size(0) { }
41 
42  void reallocate(size_type newCapacity, size_type oldSize) {
43  assert(!"fixed_vector cannot grow");
44  }
45  void reallocate_discard_old(size_type newCapacity) {
46  assert(!"fixed_vector cannot grow");
47  }
48  inline void destroy(pointer ptr, size_type n) {
49  mn::destruct_n(ptr, n);
50  if ( (etype_t*)ptr != &m_data[0] )
51  m_allocator.free(ptr);
52  }
53  bool invariant() const {
54  return m_end >= m_begin;
55  }
56 
59  allocator_type m_data[(TCapacity * sizeof(T)) / sizeof(etype_t)];
61  TAllocator m_allocator;
63  };
64 
65  template<typename T, int TCapacity, class TAllocator = memory::default_allocator>
66  class basic_fixed_vector : public basic_vector<T, TAllocator, fixed_vector_storage<T, TAllocator, TCapacity> > {
68  public:
70  using value_type = T;
71  using pointer = value_type*;
74 
75  using iterator = pointer;
76  using const_iterator = const pointer;
77 
78  using allocator_type = TAllocator;
81 
82  explicit basic_fixed_vector(const allocator_type& allocator = allocator_type())
83  : base_type(allocator) { }
84 
85  explicit basic_fixed_vector(size_type initialSize, const allocator_type& allocator = allocator_type())
86  : base_type(initialSize, allocator) { }
87 
88  basic_fixed_vector(const pointer first, const pointer last, const allocator_type& allocator = allocator_type())
89  : base_type(first, last, allocator) { }
90 
91  basic_fixed_vector(const self_type& rhs, const allocator_type& allocator = allocator_type())
92  : base_type(rhs, allocator) { }
93 
94  using base_type::begin;
95  using base_type::end;
96  using base_type::size;
97  using base_type::empty;
98  using base_type::capacity;
99  using base_type::clear;
102 
104  if (&rhs != this) {
105  base_type::copy(rhs);
106  }
107  return *this;
108  }
109  };
110 
111  template<typename T, int TCapacity>
113  }
114 }
115 
116 #endif
Definition: mn_fixed_vector.hpp:66
pointer iterator
Definition: mn_fixed_vector.hpp:75
ptrdiff_t difference_type
Definition: mn_fixed_vector.hpp:73
basic_fixed_vector(const self_type &rhs, const allocator_type &allocator=allocator_type())
Definition: mn_fixed_vector.hpp:91
basic_fixed_vector(const allocator_type &allocator=allocator_type())
Definition: mn_fixed_vector.hpp:82
self_type & operator=(const self_type &rhs)
Definition: mn_fixed_vector.hpp:103
TAllocator allocator_type
Definition: mn_fixed_vector.hpp:78
value_type * pointer
Definition: mn_fixed_vector.hpp:71
basic_fixed_vector(size_type initialSize, const allocator_type &allocator=allocator_type())
Definition: mn_fixed_vector.hpp:85
const pointer const_iterator
Definition: mn_fixed_vector.hpp:76
basic_fixed_vector(const pointer first, const pointer last, const allocator_type &allocator=allocator_type())
Definition: mn_fixed_vector.hpp:88
Definition: mn_vector.hpp:117
iterator begin()
Definition: mn_vector.hpp:173
iterator end()
Definition: mn_vector.hpp:174
size_type capacity()
Definition: mn_vector.hpp:179
const allocator_type & get_allocator() const
Definition: mn_vector.hpp:377
void set_allocator(const allocator_type &allocator)
Definition: mn_vector.hpp:381
size_type size() const
Definition: mn_vector.hpp:176
void copy(const basic_vector &rhs)
Definition: mn_vector.hpp:161
bool empty() const
Definition: mn_vector.hpp:177
void clear()
Definition: mn_vector.hpp:343
Basic vector container This file is part of the Mini Thread Library (https://github....
struct mn::memory::detail::ptr_difference T
Definition: mn_atomic_singleton.hpp:38
Definition: mn_allocator_typetraits.hpp:25
void destruct_n(T *src, size_t n)
Definition: mn_algorithm.hpp:131
long ptrdiff_t
Definition: mn_def.hpp:49
typename internal::type_with_alignment< alignment_of< T >::res > res
Definition: mn_alignment.hpp:133
MN_THREAD_CONFIG_SIZE_TYPE size_t
Definition: mn_def.hpp:48
Definition: mn_fixed_vector.hpp:26
void destroy(pointer ptr, size_type n)
Definition: mn_fixed_vector.hpp:48
void reallocate_discard_old(size_type newCapacity)
Definition: mn_fixed_vector.hpp:45
mn::size_t size_type
Definition: mn_fixed_vector.hpp:32
pointer m_end
Definition: mn_fixed_vector.hpp:58
TAllocator allocator_type
Definition: mn_fixed_vector.hpp:27
TAllocator m_allocator
Definition: mn_fixed_vector.hpp:61
T value_type
Definition: mn_fixed_vector.hpp:29
typename aligned_as< value_type >::res etype_t
Definition: mn_fixed_vector.hpp:33
fixed_vector_storage(const TAllocator &allocator)
Definition: mn_fixed_vector.hpp:35
void reallocate(size_type newCapacity, size_type oldSize)
Definition: mn_fixed_vector.hpp:42
allocator_type m_data[(TCapacity *sizeof(T))/sizeof(etype_t)]
Definition: mn_fixed_vector.hpp:59
pointer m_begin
Definition: mn_fixed_vector.hpp:57
value_type & reference
Definition: mn_fixed_vector.hpp:31
pointer m_capacityEnd
Definition: mn_fixed_vector.hpp:60
value_type * pointer
Definition: mn_fixed_vector.hpp:30
bool invariant() const
Definition: mn_fixed_vector.hpp:53
size_type m_max_size
Definition: mn_fixed_vector.hpp:62
Definition: mn_iterator.hpp:36