Containers (STL-compatible) StateMachines MessageBus and more for Embedded Systems. See www.etlcpp.com

Committer:
bobbery
Date:
Fri Mar 16 16:34:18 2018 +0000
Revision:
0:b47c2a7920c2
Works after using gcc_generic undef CAPACITY and replacing nullptr by std::nullptr

Who changed what in which revision?

UserRevisionLine numberNew contents of line
bobbery 0:b47c2a7920c2 1 ///\file
bobbery 0:b47c2a7920c2 2
bobbery 0:b47c2a7920c2 3 /******************************************************************************
bobbery 0:b47c2a7920c2 4 The MIT License(MIT)
bobbery 0:b47c2a7920c2 5
bobbery 0:b47c2a7920c2 6 Embedded Template Library.
bobbery 0:b47c2a7920c2 7 https://github.com/ETLCPP/etl
bobbery 0:b47c2a7920c2 8 http://www.etlcpp.com
bobbery 0:b47c2a7920c2 9
bobbery 0:b47c2a7920c2 10 Copyright(c) 2016 jwellbelove
bobbery 0:b47c2a7920c2 11
bobbery 0:b47c2a7920c2 12 Permission is hereby granted, free of charge, to any person obtaining a copy
bobbery 0:b47c2a7920c2 13 of this software and associated documentation files(the "Software"), to deal
bobbery 0:b47c2a7920c2 14 in the Software without restriction, including without limitation the rights
bobbery 0:b47c2a7920c2 15 to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
bobbery 0:b47c2a7920c2 16 copies of the Software, and to permit persons to whom the Software is
bobbery 0:b47c2a7920c2 17 furnished to do so, subject to the following conditions :
bobbery 0:b47c2a7920c2 18
bobbery 0:b47c2a7920c2 19 The above copyright notice and this permission notice shall be included in all
bobbery 0:b47c2a7920c2 20 copies or substantial portions of the Software.
bobbery 0:b47c2a7920c2 21
bobbery 0:b47c2a7920c2 22 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
bobbery 0:b47c2a7920c2 23 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
bobbery 0:b47c2a7920c2 24 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
bobbery 0:b47c2a7920c2 25 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
bobbery 0:b47c2a7920c2 26 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
bobbery 0:b47c2a7920c2 27 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
bobbery 0:b47c2a7920c2 28 SOFTWARE.
bobbery 0:b47c2a7920c2 29 ******************************************************************************/
bobbery 0:b47c2a7920c2 30
bobbery 0:b47c2a7920c2 31 #ifndef __ETL_PVOIDVECTOR__
bobbery 0:b47c2a7920c2 32 #define __ETL_PVOIDVECTOR__
bobbery 0:b47c2a7920c2 33
bobbery 0:b47c2a7920c2 34 #define __ETL_IN_PVOIDVECTOR__
bobbery 0:b47c2a7920c2 35
bobbery 0:b47c2a7920c2 36 #include <iterator>
bobbery 0:b47c2a7920c2 37 #include <algorithm>
bobbery 0:b47c2a7920c2 38 #include <functional>
bobbery 0:b47c2a7920c2 39 #include <stddef.h>
bobbery 0:b47c2a7920c2 40
bobbery 0:b47c2a7920c2 41 #include "../platform.h"
bobbery 0:b47c2a7920c2 42 #include "../algorithm.h"
bobbery 0:b47c2a7920c2 43 #include "vector_base.h"
bobbery 0:b47c2a7920c2 44 #include "../type_traits.h"
bobbery 0:b47c2a7920c2 45 #include "../error_handler.h"
bobbery 0:b47c2a7920c2 46
bobbery 0:b47c2a7920c2 47 #ifdef ETL_COMPILER_GCC
bobbery 0:b47c2a7920c2 48 #pragma GCC diagnostic ignored "-Wunused-variable"
bobbery 0:b47c2a7920c2 49 #endif
bobbery 0:b47c2a7920c2 50
bobbery 0:b47c2a7920c2 51 #ifdef ETL_COMPILER_MICROSOFT
bobbery 0:b47c2a7920c2 52 #undef min
bobbery 0:b47c2a7920c2 53 #endif
bobbery 0:b47c2a7920c2 54
bobbery 0:b47c2a7920c2 55 namespace etl
bobbery 0:b47c2a7920c2 56 {
bobbery 0:b47c2a7920c2 57 //***************************************************************************
bobbery 0:b47c2a7920c2 58 /// The base class for void* vectors.
bobbery 0:b47c2a7920c2 59 ///\ingroup vector
bobbery 0:b47c2a7920c2 60 //***************************************************************************
bobbery 0:b47c2a7920c2 61 class pvoidvector : public vector_base
bobbery 0:b47c2a7920c2 62 {
bobbery 0:b47c2a7920c2 63 public:
bobbery 0:b47c2a7920c2 64
bobbery 0:b47c2a7920c2 65 typedef void* value_type;
bobbery 0:b47c2a7920c2 66 typedef void*& reference;
bobbery 0:b47c2a7920c2 67 typedef const void*& const_reference;
bobbery 0:b47c2a7920c2 68 typedef void** pointer;
bobbery 0:b47c2a7920c2 69 typedef const void** const_pointer;
bobbery 0:b47c2a7920c2 70 typedef void** iterator;
bobbery 0:b47c2a7920c2 71 typedef const void** const_iterator;
bobbery 0:b47c2a7920c2 72 typedef std::reverse_iterator<iterator> reverse_iterator;
bobbery 0:b47c2a7920c2 73 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
bobbery 0:b47c2a7920c2 74 typedef size_t size_type;
bobbery 0:b47c2a7920c2 75 typedef std::iterator_traits<iterator>::difference_type difference_type;
bobbery 0:b47c2a7920c2 76
bobbery 0:b47c2a7920c2 77 public:
bobbery 0:b47c2a7920c2 78
bobbery 0:b47c2a7920c2 79 //*********************************************************************
bobbery 0:b47c2a7920c2 80 /// Returns an iterator to the beginning of the vector.
bobbery 0:b47c2a7920c2 81 ///\return An iterator to the beginning of the vector.
bobbery 0:b47c2a7920c2 82 //*********************************************************************
bobbery 0:b47c2a7920c2 83 iterator begin()
bobbery 0:b47c2a7920c2 84 {
bobbery 0:b47c2a7920c2 85 return p_buffer;
bobbery 0:b47c2a7920c2 86 }
bobbery 0:b47c2a7920c2 87
bobbery 0:b47c2a7920c2 88 //*********************************************************************
bobbery 0:b47c2a7920c2 89 /// Returns a const_iterator to the beginning of the vector.
bobbery 0:b47c2a7920c2 90 ///\return A const iterator to the beginning of the vector.
bobbery 0:b47c2a7920c2 91 //*********************************************************************
bobbery 0:b47c2a7920c2 92 const_iterator begin() const
bobbery 0:b47c2a7920c2 93 {
bobbery 0:b47c2a7920c2 94 return const_iterator(p_buffer);
bobbery 0:b47c2a7920c2 95 }
bobbery 0:b47c2a7920c2 96
bobbery 0:b47c2a7920c2 97 //*********************************************************************
bobbery 0:b47c2a7920c2 98 /// Returns an iterator to the end of the vector.
bobbery 0:b47c2a7920c2 99 ///\return An iterator to the end of the vector.
bobbery 0:b47c2a7920c2 100 //*********************************************************************
bobbery 0:b47c2a7920c2 101 iterator end()
bobbery 0:b47c2a7920c2 102 {
bobbery 0:b47c2a7920c2 103 return p_end;
bobbery 0:b47c2a7920c2 104 }
bobbery 0:b47c2a7920c2 105
bobbery 0:b47c2a7920c2 106 //*********************************************************************
bobbery 0:b47c2a7920c2 107 /// Returns a const_iterator to the end of the vector.
bobbery 0:b47c2a7920c2 108 ///\return A const iterator to the end of the vector.
bobbery 0:b47c2a7920c2 109 //*********************************************************************
bobbery 0:b47c2a7920c2 110 const_iterator end() const
bobbery 0:b47c2a7920c2 111 {
bobbery 0:b47c2a7920c2 112 return const_iterator(p_end);
bobbery 0:b47c2a7920c2 113 }
bobbery 0:b47c2a7920c2 114
bobbery 0:b47c2a7920c2 115 //*********************************************************************
bobbery 0:b47c2a7920c2 116 /// Returns a const_iterator to the beginning of the vector.
bobbery 0:b47c2a7920c2 117 ///\return A const iterator to the beginning of the vector.
bobbery 0:b47c2a7920c2 118 //*********************************************************************
bobbery 0:b47c2a7920c2 119 const_iterator cbegin() const
bobbery 0:b47c2a7920c2 120 {
bobbery 0:b47c2a7920c2 121 return const_iterator(p_buffer);
bobbery 0:b47c2a7920c2 122 }
bobbery 0:b47c2a7920c2 123
bobbery 0:b47c2a7920c2 124 //*********************************************************************
bobbery 0:b47c2a7920c2 125 /// Returns a const_iterator to the end of the vector.
bobbery 0:b47c2a7920c2 126 ///\return A const iterator to the end of the vector.
bobbery 0:b47c2a7920c2 127 //*********************************************************************
bobbery 0:b47c2a7920c2 128 const_iterator cend() const
bobbery 0:b47c2a7920c2 129 {
bobbery 0:b47c2a7920c2 130 return const_iterator(p_end);
bobbery 0:b47c2a7920c2 131 }
bobbery 0:b47c2a7920c2 132
bobbery 0:b47c2a7920c2 133 //*********************************************************************
bobbery 0:b47c2a7920c2 134 /// Returns an reverse iterator to the reverse beginning of the vector.
bobbery 0:b47c2a7920c2 135 ///\return Iterator to the reverse beginning of the vector.
bobbery 0:b47c2a7920c2 136 //*********************************************************************
bobbery 0:b47c2a7920c2 137 reverse_iterator rbegin()
bobbery 0:b47c2a7920c2 138 {
bobbery 0:b47c2a7920c2 139 return reverse_iterator(end());
bobbery 0:b47c2a7920c2 140 }
bobbery 0:b47c2a7920c2 141
bobbery 0:b47c2a7920c2 142 //*********************************************************************
bobbery 0:b47c2a7920c2 143 /// Returns a const reverse iterator to the reverse beginning of the vector.
bobbery 0:b47c2a7920c2 144 ///\return Const iterator to the reverse beginning of the vector.
bobbery 0:b47c2a7920c2 145 //*********************************************************************
bobbery 0:b47c2a7920c2 146 const_reverse_iterator rbegin() const
bobbery 0:b47c2a7920c2 147 {
bobbery 0:b47c2a7920c2 148 return const_reverse_iterator(end());
bobbery 0:b47c2a7920c2 149 }
bobbery 0:b47c2a7920c2 150
bobbery 0:b47c2a7920c2 151 //*********************************************************************
bobbery 0:b47c2a7920c2 152 /// Returns a reverse iterator to the end + 1 of the vector.
bobbery 0:b47c2a7920c2 153 ///\return Reverse iterator to the end + 1 of the vector.
bobbery 0:b47c2a7920c2 154 //*********************************************************************
bobbery 0:b47c2a7920c2 155 reverse_iterator rend()
bobbery 0:b47c2a7920c2 156 {
bobbery 0:b47c2a7920c2 157 return reverse_iterator(begin());
bobbery 0:b47c2a7920c2 158 }
bobbery 0:b47c2a7920c2 159
bobbery 0:b47c2a7920c2 160 //*********************************************************************
bobbery 0:b47c2a7920c2 161 /// Returns a const reverse iterator to the end + 1 of the vector.
bobbery 0:b47c2a7920c2 162 ///\return Const reverse iterator to the end + 1 of the vector.
bobbery 0:b47c2a7920c2 163 //*********************************************************************
bobbery 0:b47c2a7920c2 164 const_reverse_iterator rend() const
bobbery 0:b47c2a7920c2 165 {
bobbery 0:b47c2a7920c2 166 return const_reverse_iterator(begin());
bobbery 0:b47c2a7920c2 167 }
bobbery 0:b47c2a7920c2 168
bobbery 0:b47c2a7920c2 169 //*********************************************************************
bobbery 0:b47c2a7920c2 170 /// Returns a const reverse iterator to the reverse beginning of the vector.
bobbery 0:b47c2a7920c2 171 ///\return Const reverse iterator to the reverse beginning of the vector.
bobbery 0:b47c2a7920c2 172 //*********************************************************************
bobbery 0:b47c2a7920c2 173 const_reverse_iterator crbegin() const
bobbery 0:b47c2a7920c2 174 {
bobbery 0:b47c2a7920c2 175 return const_reverse_iterator(cend());
bobbery 0:b47c2a7920c2 176 }
bobbery 0:b47c2a7920c2 177
bobbery 0:b47c2a7920c2 178 //*********************************************************************
bobbery 0:b47c2a7920c2 179 /// Returns a const reverse iterator to the end + 1 of the vector.
bobbery 0:b47c2a7920c2 180 ///\return Const reverse iterator to the end + 1 of the vector.
bobbery 0:b47c2a7920c2 181 //*********************************************************************
bobbery 0:b47c2a7920c2 182 const_reverse_iterator crend() const
bobbery 0:b47c2a7920c2 183 {
bobbery 0:b47c2a7920c2 184 return const_reverse_iterator(cbegin());
bobbery 0:b47c2a7920c2 185 }
bobbery 0:b47c2a7920c2 186
bobbery 0:b47c2a7920c2 187 //*********************************************************************
bobbery 0:b47c2a7920c2 188 /// Resizes the vector.
bobbery 0:b47c2a7920c2 189 /// If asserts or exceptions are enabled and the new size is larger than the
bobbery 0:b47c2a7920c2 190 /// maximum then a vector_full is thrown.
bobbery 0:b47c2a7920c2 191 ///\param new_size The new size.
bobbery 0:b47c2a7920c2 192 //*********************************************************************
bobbery 0:b47c2a7920c2 193 void resize(size_t new_size)
bobbery 0:b47c2a7920c2 194 {
bobbery 0:b47c2a7920c2 195 ETL_ASSERT(new_size <= CAPACITY, ETL_ERROR(vector_full));
bobbery 0:b47c2a7920c2 196
bobbery 0:b47c2a7920c2 197 p_end = p_buffer + new_size;
bobbery 0:b47c2a7920c2 198 }
bobbery 0:b47c2a7920c2 199
bobbery 0:b47c2a7920c2 200 //*********************************************************************
bobbery 0:b47c2a7920c2 201 /// Resizes the vector.
bobbery 0:b47c2a7920c2 202 /// If asserts or exceptions are enabled and the new size is larger than the
bobbery 0:b47c2a7920c2 203 /// maximum then a vector_full is thrown.
bobbery 0:b47c2a7920c2 204 ///\param new_size The new size.
bobbery 0:b47c2a7920c2 205 ///\param value The value to fill new elements with. Default = default constructed value.
bobbery 0:b47c2a7920c2 206 //*********************************************************************
bobbery 0:b47c2a7920c2 207 void resize(size_t new_size, value_type value)
bobbery 0:b47c2a7920c2 208 {
bobbery 0:b47c2a7920c2 209 ETL_ASSERT(new_size <= CAPACITY, ETL_ERROR(vector_full));
bobbery 0:b47c2a7920c2 210
bobbery 0:b47c2a7920c2 211 pointer p_new_end = p_buffer + new_size;
bobbery 0:b47c2a7920c2 212
bobbery 0:b47c2a7920c2 213 // Size up if necessary.
bobbery 0:b47c2a7920c2 214 if (p_end < p_new_end)
bobbery 0:b47c2a7920c2 215 {
bobbery 0:b47c2a7920c2 216 std::fill(p_end, p_new_end, value);
bobbery 0:b47c2a7920c2 217 }
bobbery 0:b47c2a7920c2 218
bobbery 0:b47c2a7920c2 219 p_end = p_new_end;
bobbery 0:b47c2a7920c2 220 }
bobbery 0:b47c2a7920c2 221
bobbery 0:b47c2a7920c2 222 //*********************************************************************
bobbery 0:b47c2a7920c2 223 /// Returns a reference to the value at index 'i'
bobbery 0:b47c2a7920c2 224 ///\param i The index.
bobbery 0:b47c2a7920c2 225 ///\return A reference to the value at index 'i'
bobbery 0:b47c2a7920c2 226 //*********************************************************************
bobbery 0:b47c2a7920c2 227 reference operator [](size_t i)
bobbery 0:b47c2a7920c2 228 {
bobbery 0:b47c2a7920c2 229 return p_buffer[i];
bobbery 0:b47c2a7920c2 230 }
bobbery 0:b47c2a7920c2 231
bobbery 0:b47c2a7920c2 232 //*********************************************************************
bobbery 0:b47c2a7920c2 233 /// Returns a const reference to the value at index 'i'
bobbery 0:b47c2a7920c2 234 ///\param i The index.
bobbery 0:b47c2a7920c2 235 ///\return A const reference to the value at index 'i'
bobbery 0:b47c2a7920c2 236 //*********************************************************************
bobbery 0:b47c2a7920c2 237 const_reference operator [](size_t i) const
bobbery 0:b47c2a7920c2 238 {
bobbery 0:b47c2a7920c2 239 return const_reference(p_buffer[i]);
bobbery 0:b47c2a7920c2 240 }
bobbery 0:b47c2a7920c2 241
bobbery 0:b47c2a7920c2 242 //*********************************************************************
bobbery 0:b47c2a7920c2 243 /// Returns a reference to the value at index 'i'
bobbery 0:b47c2a7920c2 244 /// If asserts or exceptions are enabled, emits an etl::vector_out_of_bounds if the index is out of range.
bobbery 0:b47c2a7920c2 245 ///\param i The index.
bobbery 0:b47c2a7920c2 246 ///\return A reference to the value at index 'i'
bobbery 0:b47c2a7920c2 247 //*********************************************************************
bobbery 0:b47c2a7920c2 248 reference at(size_t i)
bobbery 0:b47c2a7920c2 249 {
bobbery 0:b47c2a7920c2 250 ETL_ASSERT(i < size(), ETL_ERROR(vector_out_of_bounds));
bobbery 0:b47c2a7920c2 251 return p_buffer[i];
bobbery 0:b47c2a7920c2 252 }
bobbery 0:b47c2a7920c2 253
bobbery 0:b47c2a7920c2 254 //*********************************************************************
bobbery 0:b47c2a7920c2 255 /// Returns a const reference to the value at index 'i'
bobbery 0:b47c2a7920c2 256 /// If asserts or exceptions are enabled, emits an etl::vector_out_of_bounds if the index is out of range.
bobbery 0:b47c2a7920c2 257 ///\param i The index.
bobbery 0:b47c2a7920c2 258 ///\return A const reference to the value at index 'i'
bobbery 0:b47c2a7920c2 259 //*********************************************************************
bobbery 0:b47c2a7920c2 260 const_reference at(size_t i) const
bobbery 0:b47c2a7920c2 261 {
bobbery 0:b47c2a7920c2 262 ETL_ASSERT(i < size(), ETL_ERROR(vector_out_of_bounds));
bobbery 0:b47c2a7920c2 263 return const_reference(p_buffer[i]);
bobbery 0:b47c2a7920c2 264 }
bobbery 0:b47c2a7920c2 265
bobbery 0:b47c2a7920c2 266 //*********************************************************************
bobbery 0:b47c2a7920c2 267 /// Returns a reference to the first element.
bobbery 0:b47c2a7920c2 268 ///\return A reference to the first element.
bobbery 0:b47c2a7920c2 269 //*********************************************************************
bobbery 0:b47c2a7920c2 270 reference front()
bobbery 0:b47c2a7920c2 271 {
bobbery 0:b47c2a7920c2 272 return p_buffer[0];
bobbery 0:b47c2a7920c2 273 }
bobbery 0:b47c2a7920c2 274
bobbery 0:b47c2a7920c2 275 //*********************************************************************
bobbery 0:b47c2a7920c2 276 /// Returns a const reference to the first element.
bobbery 0:b47c2a7920c2 277 ///\return A const reference to the first element.
bobbery 0:b47c2a7920c2 278 //*********************************************************************
bobbery 0:b47c2a7920c2 279 const_reference front() const
bobbery 0:b47c2a7920c2 280 {
bobbery 0:b47c2a7920c2 281 return const_reference(p_buffer[0]);
bobbery 0:b47c2a7920c2 282 }
bobbery 0:b47c2a7920c2 283
bobbery 0:b47c2a7920c2 284 //*********************************************************************
bobbery 0:b47c2a7920c2 285 /// Returns a reference to the last element.
bobbery 0:b47c2a7920c2 286 ///\return A reference to the last element.
bobbery 0:b47c2a7920c2 287 //*********************************************************************
bobbery 0:b47c2a7920c2 288 reference back()
bobbery 0:b47c2a7920c2 289 {
bobbery 0:b47c2a7920c2 290 return *(p_end -1);
bobbery 0:b47c2a7920c2 291 }
bobbery 0:b47c2a7920c2 292
bobbery 0:b47c2a7920c2 293 //*********************************************************************
bobbery 0:b47c2a7920c2 294 /// Returns a const reference to the last element.
bobbery 0:b47c2a7920c2 295 ///\return A const reference to the last element.
bobbery 0:b47c2a7920c2 296 //*********************************************************************
bobbery 0:b47c2a7920c2 297 const_reference back() const
bobbery 0:b47c2a7920c2 298 {
bobbery 0:b47c2a7920c2 299 return const_reference(*(p_end - 1));
bobbery 0:b47c2a7920c2 300 }
bobbery 0:b47c2a7920c2 301
bobbery 0:b47c2a7920c2 302 //*********************************************************************
bobbery 0:b47c2a7920c2 303 /// Returns a pointer to the beginning of the vector data.
bobbery 0:b47c2a7920c2 304 ///\return A pointer to the beginning of the vector data.
bobbery 0:b47c2a7920c2 305 //*********************************************************************
bobbery 0:b47c2a7920c2 306 pointer data()
bobbery 0:b47c2a7920c2 307 {
bobbery 0:b47c2a7920c2 308 return p_buffer;
bobbery 0:b47c2a7920c2 309 }
bobbery 0:b47c2a7920c2 310
bobbery 0:b47c2a7920c2 311 //*********************************************************************
bobbery 0:b47c2a7920c2 312 /// Returns a const pointer to the beginning of the vector data.
bobbery 0:b47c2a7920c2 313 ///\return A const pointer to the beginning of the vector data.
bobbery 0:b47c2a7920c2 314 //*********************************************************************
bobbery 0:b47c2a7920c2 315 const_pointer data() const
bobbery 0:b47c2a7920c2 316 {
bobbery 0:b47c2a7920c2 317 return const_pointer(p_buffer);
bobbery 0:b47c2a7920c2 318 }
bobbery 0:b47c2a7920c2 319
bobbery 0:b47c2a7920c2 320 //*********************************************************************
bobbery 0:b47c2a7920c2 321 /// Assigns values to the vector.
bobbery 0:b47c2a7920c2 322 /// If asserts or exceptions are enabled, emits vector_full if the vector does not have enough free space.
bobbery 0:b47c2a7920c2 323 /// If asserts or exceptions are enabled, emits vector_iterator if the iterators are reversed.
bobbery 0:b47c2a7920c2 324 ///\param first The iterator to the first element.
bobbery 0:b47c2a7920c2 325 ///\param last The iterator to the last element + 1.
bobbery 0:b47c2a7920c2 326 //*********************************************************************
bobbery 0:b47c2a7920c2 327 template <typename TIterator>
bobbery 0:b47c2a7920c2 328 void assign(TIterator first, TIterator last)
bobbery 0:b47c2a7920c2 329 {
bobbery 0:b47c2a7920c2 330 #if defined(ETL_DEBUG)
bobbery 0:b47c2a7920c2 331 difference_type d = std::distance(first, last);
bobbery 0:b47c2a7920c2 332 ETL_ASSERT(static_cast<size_t>(d) <= CAPACITY, ETL_ERROR(vector_full));
bobbery 0:b47c2a7920c2 333 #endif
bobbery 0:b47c2a7920c2 334
bobbery 0:b47c2a7920c2 335 initialise();
bobbery 0:b47c2a7920c2 336
bobbery 0:b47c2a7920c2 337 while (first != last)
bobbery 0:b47c2a7920c2 338 {
bobbery 0:b47c2a7920c2 339 *p_end++ = const_cast<void*>(*first++);
bobbery 0:b47c2a7920c2 340 }
bobbery 0:b47c2a7920c2 341 }
bobbery 0:b47c2a7920c2 342
bobbery 0:b47c2a7920c2 343 //*********************************************************************
bobbery 0:b47c2a7920c2 344 /// Assigns values to the vector.
bobbery 0:b47c2a7920c2 345 /// If asserts or exceptions are enabled, emits vector_full if the vector does not have enough free space.
bobbery 0:b47c2a7920c2 346 ///\param n The number of elements to add.
bobbery 0:b47c2a7920c2 347 ///\param value The value to insert for each element.
bobbery 0:b47c2a7920c2 348 //*********************************************************************
bobbery 0:b47c2a7920c2 349 void assign(size_t n, value_type value)
bobbery 0:b47c2a7920c2 350 {
bobbery 0:b47c2a7920c2 351 initialise();
bobbery 0:b47c2a7920c2 352
bobbery 0:b47c2a7920c2 353 ETL_ASSERT(n <= CAPACITY, ETL_ERROR(vector_full));
bobbery 0:b47c2a7920c2 354
bobbery 0:b47c2a7920c2 355 for (size_t current_size = 0; current_size < n; ++current_size)
bobbery 0:b47c2a7920c2 356 {
bobbery 0:b47c2a7920c2 357 *p_end++ = value;
bobbery 0:b47c2a7920c2 358 }
bobbery 0:b47c2a7920c2 359 }
bobbery 0:b47c2a7920c2 360
bobbery 0:b47c2a7920c2 361 //*************************************************************************
bobbery 0:b47c2a7920c2 362 /// Clears the vector.
bobbery 0:b47c2a7920c2 363 //*************************************************************************
bobbery 0:b47c2a7920c2 364 void clear()
bobbery 0:b47c2a7920c2 365 {
bobbery 0:b47c2a7920c2 366 initialise();
bobbery 0:b47c2a7920c2 367 }
bobbery 0:b47c2a7920c2 368
bobbery 0:b47c2a7920c2 369 //*************************************************************************
bobbery 0:b47c2a7920c2 370 /// Increases the size of the vector by one, but does not initialise the new element.
bobbery 0:b47c2a7920c2 371 /// If asserts or exceptions are enabled, throws a vector_full if the vector is already full.
bobbery 0:b47c2a7920c2 372 //*************************************************************************
bobbery 0:b47c2a7920c2 373 void push_back()
bobbery 0:b47c2a7920c2 374 {
bobbery 0:b47c2a7920c2 375 #if defined(ETL_CHECK_PUSH_POP)
bobbery 0:b47c2a7920c2 376 ETL_ASSERT(size() != CAPACITY, ETL_ERROR(vector_full));
bobbery 0:b47c2a7920c2 377 #endif
bobbery 0:b47c2a7920c2 378
bobbery 0:b47c2a7920c2 379 ++p_end;
bobbery 0:b47c2a7920c2 380 }
bobbery 0:b47c2a7920c2 381
bobbery 0:b47c2a7920c2 382 //*********************************************************************
bobbery 0:b47c2a7920c2 383 /// Inserts a value at the end of the vector.
bobbery 0:b47c2a7920c2 384 /// If asserts or exceptions are enabled, emits vector_full if the vector is already full.
bobbery 0:b47c2a7920c2 385 ///\param value The value to add.
bobbery 0:b47c2a7920c2 386 //*********************************************************************
bobbery 0:b47c2a7920c2 387 void push_back(value_type value)
bobbery 0:b47c2a7920c2 388 {
bobbery 0:b47c2a7920c2 389 #if defined(ETL_CHECK_PUSH_POP)
bobbery 0:b47c2a7920c2 390 ETL_ASSERT(size() != CAPACITY, ETL_ERROR(vector_full));
bobbery 0:b47c2a7920c2 391 #endif
bobbery 0:b47c2a7920c2 392 *p_end++ = value;
bobbery 0:b47c2a7920c2 393 }
bobbery 0:b47c2a7920c2 394
bobbery 0:b47c2a7920c2 395 //*************************************************************************
bobbery 0:b47c2a7920c2 396 /// Removes an element from the end of the vector.
bobbery 0:b47c2a7920c2 397 /// Does nothing if the vector is empty.
bobbery 0:b47c2a7920c2 398 //*************************************************************************
bobbery 0:b47c2a7920c2 399 void pop_back()
bobbery 0:b47c2a7920c2 400 {
bobbery 0:b47c2a7920c2 401 #if defined(ETL_CHECK_PUSH_POP)
bobbery 0:b47c2a7920c2 402 ETL_ASSERT(size() > 0, ETL_ERROR(vector_empty));
bobbery 0:b47c2a7920c2 403 #endif
bobbery 0:b47c2a7920c2 404 --p_end;
bobbery 0:b47c2a7920c2 405 }
bobbery 0:b47c2a7920c2 406
bobbery 0:b47c2a7920c2 407 //*********************************************************************
bobbery 0:b47c2a7920c2 408 /// Inserts a value to the vector.
bobbery 0:b47c2a7920c2 409 /// If asserts or exceptions are enabled, emits vector_full if the vector is already full.
bobbery 0:b47c2a7920c2 410 ///\param position The position to insert before.
bobbery 0:b47c2a7920c2 411 ///\param value The value to insert.
bobbery 0:b47c2a7920c2 412 //*********************************************************************
bobbery 0:b47c2a7920c2 413 iterator insert(iterator position, value_type value)
bobbery 0:b47c2a7920c2 414 {
bobbery 0:b47c2a7920c2 415 ETL_ASSERT(size() + 1 <= CAPACITY, ETL_ERROR(vector_full));
bobbery 0:b47c2a7920c2 416
bobbery 0:b47c2a7920c2 417 if (position != end())
bobbery 0:b47c2a7920c2 418 {
bobbery 0:b47c2a7920c2 419 ++p_end;
bobbery 0:b47c2a7920c2 420 std::copy_backward(position, end() - 1, end());
bobbery 0:b47c2a7920c2 421 *position = value;
bobbery 0:b47c2a7920c2 422 }
bobbery 0:b47c2a7920c2 423 else
bobbery 0:b47c2a7920c2 424 {
bobbery 0:b47c2a7920c2 425 *p_end++ = value;
bobbery 0:b47c2a7920c2 426 }
bobbery 0:b47c2a7920c2 427
bobbery 0:b47c2a7920c2 428 return position;
bobbery 0:b47c2a7920c2 429 }
bobbery 0:b47c2a7920c2 430
bobbery 0:b47c2a7920c2 431 //*********************************************************************
bobbery 0:b47c2a7920c2 432 /// Inserts 'n' values to the vector.
bobbery 0:b47c2a7920c2 433 /// If asserts or exceptions are enabled, emits vector_full if the vector does not have enough free space.
bobbery 0:b47c2a7920c2 434 ///\param position The position to insert before.
bobbery 0:b47c2a7920c2 435 ///\param n The number of elements to add.
bobbery 0:b47c2a7920c2 436 ///\param value The value to insert.
bobbery 0:b47c2a7920c2 437 //*********************************************************************
bobbery 0:b47c2a7920c2 438 void insert(iterator position, size_t n, value_type value)
bobbery 0:b47c2a7920c2 439 {
bobbery 0:b47c2a7920c2 440 ETL_ASSERT((size() + 1) <= CAPACITY, ETL_ERROR(vector_full));
bobbery 0:b47c2a7920c2 441
bobbery 0:b47c2a7920c2 442 std::copy_backward(position, p_end, p_end + n);
bobbery 0:b47c2a7920c2 443 std::fill_n(position, n, value);
bobbery 0:b47c2a7920c2 444
bobbery 0:b47c2a7920c2 445 p_end += n;
bobbery 0:b47c2a7920c2 446 }
bobbery 0:b47c2a7920c2 447
bobbery 0:b47c2a7920c2 448 //*********************************************************************
bobbery 0:b47c2a7920c2 449 /// Inserts a range of values to the vector.
bobbery 0:b47c2a7920c2 450 /// If asserts or exceptions are enabled, emits vector_full if the vector does not have enough free space.
bobbery 0:b47c2a7920c2 451 /// For fundamental and pointer types.
bobbery 0:b47c2a7920c2 452 ///\param position The position to insert before.
bobbery 0:b47c2a7920c2 453 ///\param first The first element to add.
bobbery 0:b47c2a7920c2 454 ///\param last The last + 1 element to add.
bobbery 0:b47c2a7920c2 455 //*********************************************************************
bobbery 0:b47c2a7920c2 456 template <typename TIterator>
bobbery 0:b47c2a7920c2 457 void insert(iterator position, TIterator first, TIterator last)
bobbery 0:b47c2a7920c2 458 {
bobbery 0:b47c2a7920c2 459 size_t count = std::distance(first, last);
bobbery 0:b47c2a7920c2 460
bobbery 0:b47c2a7920c2 461 ETL_ASSERT((size() + count) <= CAPACITY, ETL_ERROR(vector_full));
bobbery 0:b47c2a7920c2 462
bobbery 0:b47c2a7920c2 463 std::copy_backward(position, p_end, p_end + count);
bobbery 0:b47c2a7920c2 464 std::copy(first, last, position);
bobbery 0:b47c2a7920c2 465 p_end += count;
bobbery 0:b47c2a7920c2 466 }
bobbery 0:b47c2a7920c2 467
bobbery 0:b47c2a7920c2 468 //*********************************************************************
bobbery 0:b47c2a7920c2 469 /// Erases an element.
bobbery 0:b47c2a7920c2 470 ///\param i_element Iterator to the element.
bobbery 0:b47c2a7920c2 471 ///\return An iterator pointing to the element that followed the erased element.
bobbery 0:b47c2a7920c2 472 //*********************************************************************
bobbery 0:b47c2a7920c2 473 iterator erase(iterator i_element)
bobbery 0:b47c2a7920c2 474 {
bobbery 0:b47c2a7920c2 475 std::copy(i_element + 1, end(), i_element);
bobbery 0:b47c2a7920c2 476 --p_end;
bobbery 0:b47c2a7920c2 477
bobbery 0:b47c2a7920c2 478 return i_element;
bobbery 0:b47c2a7920c2 479 }
bobbery 0:b47c2a7920c2 480
bobbery 0:b47c2a7920c2 481 //*********************************************************************
bobbery 0:b47c2a7920c2 482 /// Erases a range of elements.
bobbery 0:b47c2a7920c2 483 /// The range includes all the elements between first and last, including the
bobbery 0:b47c2a7920c2 484 /// element pointed by first, but not the one pointed by last.
bobbery 0:b47c2a7920c2 485 ///\param first Iterator to the first element.
bobbery 0:b47c2a7920c2 486 ///\param last Iterator to the last element.
bobbery 0:b47c2a7920c2 487 ///\return An iterator pointing to the element that followed the erased element.
bobbery 0:b47c2a7920c2 488 //*********************************************************************
bobbery 0:b47c2a7920c2 489 iterator erase(iterator first, iterator last)
bobbery 0:b47c2a7920c2 490 {
bobbery 0:b47c2a7920c2 491 std::copy(last, end(), first);
bobbery 0:b47c2a7920c2 492 size_t n_delete = std::distance(first, last);
bobbery 0:b47c2a7920c2 493
bobbery 0:b47c2a7920c2 494 // Just adjust the count.
bobbery 0:b47c2a7920c2 495 p_end -= n_delete;
bobbery 0:b47c2a7920c2 496
bobbery 0:b47c2a7920c2 497 return first;
bobbery 0:b47c2a7920c2 498 }
bobbery 0:b47c2a7920c2 499
bobbery 0:b47c2a7920c2 500 //*************************************************************************
bobbery 0:b47c2a7920c2 501 /// Assignment operator.
bobbery 0:b47c2a7920c2 502 //*************************************************************************
bobbery 0:b47c2a7920c2 503 pvoidvector& operator = (const pvoidvector& rhs)
bobbery 0:b47c2a7920c2 504 {
bobbery 0:b47c2a7920c2 505 if (&rhs != this)
bobbery 0:b47c2a7920c2 506 {
bobbery 0:b47c2a7920c2 507 assign(rhs.cbegin(), rhs.cend());
bobbery 0:b47c2a7920c2 508 }
bobbery 0:b47c2a7920c2 509
bobbery 0:b47c2a7920c2 510 return *this;
bobbery 0:b47c2a7920c2 511 }
bobbery 0:b47c2a7920c2 512
bobbery 0:b47c2a7920c2 513 //*************************************************************************
bobbery 0:b47c2a7920c2 514 /// Gets the current size of the vector.
bobbery 0:b47c2a7920c2 515 ///\return The current size of the vector.
bobbery 0:b47c2a7920c2 516 //*************************************************************************
bobbery 0:b47c2a7920c2 517 size_type size() const
bobbery 0:b47c2a7920c2 518 {
bobbery 0:b47c2a7920c2 519 return size_t(p_end - p_buffer);
bobbery 0:b47c2a7920c2 520 }
bobbery 0:b47c2a7920c2 521
bobbery 0:b47c2a7920c2 522 //*************************************************************************
bobbery 0:b47c2a7920c2 523 /// Checks the 'empty' state of the vector.
bobbery 0:b47c2a7920c2 524 ///\return <b>true</b> if empty.
bobbery 0:b47c2a7920c2 525 //*************************************************************************
bobbery 0:b47c2a7920c2 526 bool empty() const
bobbery 0:b47c2a7920c2 527 {
bobbery 0:b47c2a7920c2 528 return (p_end == p_buffer);
bobbery 0:b47c2a7920c2 529 }
bobbery 0:b47c2a7920c2 530
bobbery 0:b47c2a7920c2 531 //*************************************************************************
bobbery 0:b47c2a7920c2 532 /// Checks the 'full' state of the vector.
bobbery 0:b47c2a7920c2 533 ///\return <b>true</b> if full.
bobbery 0:b47c2a7920c2 534 //*************************************************************************
bobbery 0:b47c2a7920c2 535 bool full() const
bobbery 0:b47c2a7920c2 536 {
bobbery 0:b47c2a7920c2 537 return size() == CAPACITY;
bobbery 0:b47c2a7920c2 538 }
bobbery 0:b47c2a7920c2 539
bobbery 0:b47c2a7920c2 540 //*************************************************************************
bobbery 0:b47c2a7920c2 541 /// Returns the remaining capacity.
bobbery 0:b47c2a7920c2 542 ///\return The remaining capacity.
bobbery 0:b47c2a7920c2 543 //*************************************************************************
bobbery 0:b47c2a7920c2 544 size_t available() const
bobbery 0:b47c2a7920c2 545 {
bobbery 0:b47c2a7920c2 546 return max_size() - size();
bobbery 0:b47c2a7920c2 547 }
bobbery 0:b47c2a7920c2 548
bobbery 0:b47c2a7920c2 549 protected:
bobbery 0:b47c2a7920c2 550
bobbery 0:b47c2a7920c2 551 //*********************************************************************
bobbery 0:b47c2a7920c2 552 /// Constructor.
bobbery 0:b47c2a7920c2 553 //*********************************************************************
bobbery 0:b47c2a7920c2 554 pvoidvector(void** p_buffer_, size_t MAX_SIZE)
bobbery 0:b47c2a7920c2 555 : vector_base(MAX_SIZE),
bobbery 0:b47c2a7920c2 556 p_buffer(p_buffer_),
bobbery 0:b47c2a7920c2 557 p_end(p_buffer_)
bobbery 0:b47c2a7920c2 558 {
bobbery 0:b47c2a7920c2 559 }
bobbery 0:b47c2a7920c2 560
bobbery 0:b47c2a7920c2 561 //*********************************************************************
bobbery 0:b47c2a7920c2 562 /// Initialise the vector.
bobbery 0:b47c2a7920c2 563 //*********************************************************************
bobbery 0:b47c2a7920c2 564 void initialise()
bobbery 0:b47c2a7920c2 565 {
bobbery 0:b47c2a7920c2 566 p_end = p_buffer;
bobbery 0:b47c2a7920c2 567 }
bobbery 0:b47c2a7920c2 568
bobbery 0:b47c2a7920c2 569 //*************************************************************************
bobbery 0:b47c2a7920c2 570 /// Fix the internal pointers after a low level memory copy.
bobbery 0:b47c2a7920c2 571 //*************************************************************************
bobbery 0:b47c2a7920c2 572 void repair(void** p_buffer_)
bobbery 0:b47c2a7920c2 573 {
bobbery 0:b47c2a7920c2 574 uintptr_t length = p_end - p_buffer;
bobbery 0:b47c2a7920c2 575
bobbery 0:b47c2a7920c2 576 p_buffer = p_buffer_;
bobbery 0:b47c2a7920c2 577 p_end = p_buffer_ + length;
bobbery 0:b47c2a7920c2 578 }
bobbery 0:b47c2a7920c2 579
bobbery 0:b47c2a7920c2 580 void** p_buffer;
bobbery 0:b47c2a7920c2 581 void** p_end;
bobbery 0:b47c2a7920c2 582
bobbery 0:b47c2a7920c2 583 private:
bobbery 0:b47c2a7920c2 584
bobbery 0:b47c2a7920c2 585 // Disable copy construction.
bobbery 0:b47c2a7920c2 586 pvoidvector(const pvoidvector&);
bobbery 0:b47c2a7920c2 587 };
bobbery 0:b47c2a7920c2 588
bobbery 0:b47c2a7920c2 589 bool operator ==(const etl::pvoidvector& lhs, const etl::pvoidvector& rhs);
bobbery 0:b47c2a7920c2 590 bool operator !=(const etl::pvoidvector& lhs, const etl::pvoidvector& rhs);
bobbery 0:b47c2a7920c2 591 bool operator <(const etl::pvoidvector& lhs, const etl::pvoidvector& rhs);
bobbery 0:b47c2a7920c2 592 bool operator >(const etl::pvoidvector& lhs, const etl::pvoidvector& rhs);
bobbery 0:b47c2a7920c2 593 bool operator <=(const etl::pvoidvector& lhs, const etl::pvoidvector& rhs);
bobbery 0:b47c2a7920c2 594 bool operator >=(const etl::pvoidvector& lhs, const etl::pvoidvector& rhs);
bobbery 0:b47c2a7920c2 595 }
bobbery 0:b47c2a7920c2 596
bobbery 0:b47c2a7920c2 597 #ifdef ETL_COMPILER_MICROSOFT
bobbery 0:b47c2a7920c2 598 #define min(a,b) (((a) < (b)) ? (a) : (b))
bobbery 0:b47c2a7920c2 599 #endif
bobbery 0:b47c2a7920c2 600
bobbery 0:b47c2a7920c2 601 #undef __ETL_IN_PVOIDVECTOR__
bobbery 0:b47c2a7920c2 602
bobbery 0:b47c2a7920c2 603 #endif
bobbery 0:b47c2a7920c2 604