Embedded Template Library  1.0
pvoidvector.h
Go to the documentation of this file.
1 
3 /******************************************************************************
4 The MIT License(MIT)
5 
6 Embedded Template Library.
7 https://github.com/ETLCPP/etl
8 https://www.etlcpp.com
9 
10 Copyright(c) 2016 jwellbelove
11 
12 Permission is hereby granted, free of charge, to any person obtaining a copy
13 of this software and associated documentation files(the "Software"), to deal
14 in the Software without restriction, including without limitation the rights
15 to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
16 copies of the Software, and to permit persons to whom the Software is
17 furnished to do so, subject to the following conditions :
18 
19 The above copyright notice and this permission notice shall be included in all
20 copies or substantial portions of the Software.
21 
22 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
25 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28 SOFTWARE.
29 ******************************************************************************/
30 
31 #ifndef ETL_PVOIDVECTOR_INCLUDED
32 #define ETL_PVOIDVECTOR_INCLUDED
33 
34 #define ETL_IN_PVOIDVECTOR
35 
36 #include <stddef.h>
37 
38 #include "../platform.h"
39 #include "../algorithm.h"
40 #include "vector_base.h"
41 #include "../type_traits.h"
42 #include "../error_handler.h"
43 
44 #include "../functional.h"
45 #include "../iterator.h"
46 
47 #ifdef ETL_COMPILER_GCC
48 #pragma GCC diagnostic ignored "-Wunused-variable"
49 #endif
50 
51 #include "minmax_push.h"
52 
53 namespace etl
54 {
55  //***************************************************************************
58  //***************************************************************************
59  class pvoidvector : public vector_base
60  {
61  public:
62 
63  typedef void* value_type;
64  typedef value_type& reference;
65  typedef const value_type& const_reference;
66  typedef value_type* pointer;
67  typedef const value_type* const_pointer;
68  typedef value_type* iterator;
69  typedef const value_type* const_iterator;
70  typedef ETL_OR_STD::reverse_iterator<iterator> reverse_iterator;
71  typedef ETL_OR_STD::reverse_iterator<const_iterator> const_reverse_iterator;
72  typedef size_t size_type;
73  typedef etl::iterator_traits<iterator>::difference_type difference_type;
74 
75  public:
76 
77  //*********************************************************************
80  //*********************************************************************
81  iterator begin()
82  {
83  return p_buffer;
84  }
85 
86  //*********************************************************************
89  //*********************************************************************
90  const_iterator begin() const
91  {
92  return const_iterator(p_buffer);
93  }
94 
95  //*********************************************************************
98  //*********************************************************************
99  iterator end()
100  {
101  return p_end;
102  }
103 
104  //*********************************************************************
107  //*********************************************************************
108  const_iterator end() const
109  {
110  return const_iterator(p_end);
111  }
112 
113  //*********************************************************************
116  //*********************************************************************
117  const_iterator cbegin() const
118  {
119  return const_iterator(p_buffer);
120  }
121 
122  //*********************************************************************
125  //*********************************************************************
126  const_iterator cend() const
127  {
128  return const_iterator(p_end);
129  }
130 
131  //*********************************************************************
134  //*********************************************************************
135  reverse_iterator rbegin()
136  {
137  return reverse_iterator(end());
138  }
139 
140  //*********************************************************************
143  //*********************************************************************
144  const_reverse_iterator rbegin() const
145  {
146  return const_reverse_iterator(end());
147  }
148 
149  //*********************************************************************
152  //*********************************************************************
153  reverse_iterator rend()
154  {
155  return reverse_iterator(begin());
156  }
157 
158  //*********************************************************************
161  //*********************************************************************
162  const_reverse_iterator rend() const
163  {
164  return const_reverse_iterator(begin());
165  }
166 
167  //*********************************************************************
170  //*********************************************************************
171  const_reverse_iterator crbegin() const
172  {
173  return const_reverse_iterator(cend());
174  }
175 
176  //*********************************************************************
179  //*********************************************************************
180  const_reverse_iterator crend() const
181  {
182  return const_reverse_iterator(cbegin());
183  }
184 
185  //*********************************************************************
190  //*********************************************************************
191  void resize(size_t new_size)
192  {
193  ETL_ASSERT(new_size <= CAPACITY, ETL_ERROR(vector_full));
194 
195  p_end = p_buffer + new_size;
196  }
197 
198  //*********************************************************************
204  //*********************************************************************
205  void resize(size_t new_size, value_type value)
206  {
207  ETL_ASSERT(new_size <= CAPACITY, ETL_ERROR(vector_full));
208 
209  pointer p_new_end = p_buffer + new_size;
210 
211  // Size up if necessary.
212  if (p_end < p_new_end)
213  {
214  etl::fill(p_end, p_new_end, value);
215  }
216 
217  p_end = p_new_end;
218  }
219 
220  //*********************************************************************
224  //*********************************************************************
225  reference operator [](size_t i)
226  {
227  return p_buffer[i];
228  }
229 
230  //*********************************************************************
234  //*********************************************************************
235  const_reference operator [](size_t i) const
236  {
237  return p_buffer[i];
238  }
239 
240  //*********************************************************************
245  //*********************************************************************
246  reference at(size_t i)
247  {
248  ETL_ASSERT(i < size(), ETL_ERROR(vector_out_of_bounds));
249  return p_buffer[i];
250  }
251 
252  //*********************************************************************
257  //*********************************************************************
258  const_reference at(size_t i) const
259  {
260  ETL_ASSERT(i < size(), ETL_ERROR(vector_out_of_bounds));
261  return p_buffer[i];
262  }
263 
264  //*********************************************************************
267  //*********************************************************************
268  reference front()
269  {
270  return p_buffer[0];
271  }
272 
273  //*********************************************************************
276  //*********************************************************************
277  const_reference front() const
278  {
279  return p_buffer[0];
280  }
281 
282  //*********************************************************************
285  //*********************************************************************
286  reference back()
287  {
288  return *(p_end - 1);
289  }
290 
291  //*********************************************************************
294  //*********************************************************************
295  const_reference back() const
296  {
297  return *(p_end - 1);
298  }
299 
300  //*********************************************************************
303  //*********************************************************************
304  pointer data()
305  {
306  return p_buffer;
307  }
308 
309  //*********************************************************************
312  //*********************************************************************
313  const_pointer data() const
314  {
315  return p_buffer;
316  }
317 
318  //*********************************************************************
324  //*********************************************************************
325  template <typename TIterator>
327  assign(TIterator first, TIterator last)
328  {
329 #if defined(ETL_DEBUG)
330  difference_type d = etl::distance(first, last);
331  ETL_ASSERT(static_cast<size_t>(d) <= CAPACITY, ETL_ERROR(vector_full));
332 #endif
333 
334  initialise();
335 
336  while (first != last)
337  {
338  *p_end++ = (void*)(*first++);
339  }
340  }
341 
342  //*********************************************************************
348  //*********************************************************************
349  template <typename TIterator>
351  assign(TIterator first, TIterator last)
352  {
353 #if defined(ETL_DEBUG)
354  difference_type d = etl::distance(first, last);
355  ETL_ASSERT(static_cast<size_t>(d) <= CAPACITY, ETL_ERROR(vector_full));
356 #endif
357 
358  initialise();
359 
360  void** p_first = (void**)(first);
361  void** p_last = (void**)(last);
362 
363  p_end = etl::copy(p_first, p_last, p_buffer);;
364  }
365 
366  //*********************************************************************
371  //*********************************************************************
372  void assign(size_t n, value_type value)
373  {
374  ETL_ASSERT(n <= CAPACITY, ETL_ERROR(vector_full));
375 
376  initialise();
377 
378  p_end = etl::fill_n(p_buffer, n, value);
379  }
380 
381  //*************************************************************************
383  //*************************************************************************
384  void clear()
385  {
386  initialise();
387  }
388 
389  //*********************************************************************
393  //*********************************************************************
394  void push_back(value_type value)
395  {
396 #if defined(ETL_CHECK_PUSH_POP)
397  ETL_ASSERT(size() != CAPACITY, ETL_ERROR(vector_full));
398 #endif
399  *p_end++ = value;
400  }
401 
402  //*********************************************************************
406  //*********************************************************************
407  void emplace_back(value_type value)
408  {
409 #if defined(ETL_CHECK_PUSH_POP)
410  ETL_ASSERT(size() != CAPACITY, ETL_ERROR(vector_full));
411 #endif
412  * p_end++ = value;
413  }
414 
415  //*************************************************************************
418  //*************************************************************************
419  void pop_back()
420  {
421 #if defined(ETL_CHECK_PUSH_POP)
422  ETL_ASSERT(size() > 0, ETL_ERROR(vector_empty));
423 #endif
424  --p_end;
425  }
426 
427  //*********************************************************************
432  //*********************************************************************
433  iterator insert(iterator position, value_type value)
434  {
435  ETL_ASSERT(size() + 1 <= CAPACITY, ETL_ERROR(vector_full));
436 
437  if (position != end())
438  {
439  ++p_end;
440  etl::copy_backward(position, end() - 1, end());
441  *position = value;
442  }
443  else
444  {
445  *p_end++ = value;
446  }
447 
448  return position;
449  }
450 
451 
452  //*************************************************************************
455  //*************************************************************************
456  iterator emplace(iterator position, value_type value)
457  {
458  ETL_ASSERT(size() + 1 <= CAPACITY, ETL_ERROR(vector_full));
459 
460  if (position != end())
461  {
462  ++p_end;
463  etl::copy_backward(position, end() - 1, end());
464  *position = value;
465  }
466  else
467  {
468  *p_end++ = value;
469  }
470 
471  return position;
472  }
473 
474  //*********************************************************************
480  //*********************************************************************
481  void insert(iterator position, size_t n, value_type value)
482  {
483  ETL_ASSERT((size() + 1) <= CAPACITY, ETL_ERROR(vector_full));
484 
485  etl::copy_backward(position, p_end, p_end + n);
486  etl::fill_n(position, n, value);
487 
488  p_end += n;
489  }
490 
491  //*********************************************************************
498  //*********************************************************************
499  template <typename TIterator>
500  void insert(iterator position, TIterator first, TIterator last)
501  {
502  size_t count = etl::distance(first, last);
503 
504  ETL_ASSERT((size() + count) <= CAPACITY, ETL_ERROR(vector_full));
505 
506  etl::copy_backward(position, p_end, p_end + count);
507  etl::copy(first, last, position);
508  p_end += count;
509  }
510 
511  //*********************************************************************
515  //*********************************************************************
516  iterator erase(iterator i_element)
517  {
518  etl::copy(i_element + 1, end(), i_element);
519  --p_end;
520 
521  return i_element;
522  }
523 
524  //*********************************************************************
531  //*********************************************************************
532  iterator erase(iterator first, iterator last)
533  {
534  etl::copy(last, end(), first);
535  size_t n_delete = etl::distance(first, last);
536 
537  // Just adjust the count.
538  p_end -= n_delete;
539 
540  return first;
541  }
542 
543  //*************************************************************************
545  //*************************************************************************
547  {
548  if (&rhs != this)
549  {
550  this->initialise();
551  this->resize(rhs.size());
552  etl::copy_n(rhs.data(), rhs.size(), this->data());
553  }
554 
555  return *this;
556  }
557 
558 #if ETL_CPP11_SUPPORTED
559  //*************************************************************************
561  //*************************************************************************
563  {
564  if (&rhs != this)
565  {
566  this->initialise();
567  this->resize(rhs.size());
568  etl::copy_n(rhs.data(), rhs.size(), this->data());
569  rhs.initialise();
570  }
571 
572  return *this;
573  }
574 #endif
575 
576  //*************************************************************************
579  //*************************************************************************
580  size_type size() const
581  {
582  return size_t(p_end - p_buffer);
583  }
584 
585  //*************************************************************************
588  //*************************************************************************
589  bool empty() const
590  {
591  return (p_end == p_buffer);
592  }
593 
594  //*************************************************************************
597  //*************************************************************************
598  bool full() const
599  {
600  return size() == CAPACITY;
601  }
602 
603  //*************************************************************************
606  //*************************************************************************
607  size_t available() const
608  {
609  return max_size() - size();
610  }
611 
612  protected:
613 
614  //*********************************************************************
616  //*********************************************************************
617  pvoidvector(void** p_buffer_, size_t MAX_SIZE)
618  : vector_base(MAX_SIZE)
619  , p_buffer(p_buffer_)
620  , p_end(p_buffer_)
621  {
622  }
623 
624  //*********************************************************************
626  //*********************************************************************
627  void initialise()
628  {
629  p_end = p_buffer;
630  }
631 
632  //*************************************************************************
634  //*************************************************************************
635  void repair_buffer(void** p_buffer_)
636  {
637  uintptr_t length = p_end - p_buffer;
638 
639  p_buffer = p_buffer_;
640  p_end = p_buffer_ + length;
641  }
642 
643  void** p_buffer;
644  void** p_end;
645 
646  private:
647 
648  // Disable copy construction.
649  pvoidvector(const pvoidvector&);
650  };
651 
652  //***************************************************************************
658  //***************************************************************************
659  inline bool operator ==(const etl::pvoidvector& lhs, const etl::pvoidvector& rhs)
660  {
661  return (lhs.size() == rhs.size()) && etl::equal(lhs.begin(), lhs.end(), rhs.begin());
662  }
663 
664  //***************************************************************************
670  //***************************************************************************
671  inline bool operator !=(const etl::pvoidvector& lhs, const etl::pvoidvector& rhs)
672  {
673  return !(lhs == rhs);
674  }
675 
676  //***************************************************************************
682  //***************************************************************************
683  inline bool operator <(const etl::pvoidvector& lhs, const etl::pvoidvector& rhs)
684  {
685  return etl::lexicographical_compare(lhs.begin(), lhs.end(), rhs.begin(), rhs.end());
686  }
687 
688  //***************************************************************************
694  //***************************************************************************
695  inline bool operator >(const etl::pvoidvector& lhs, const etl::pvoidvector& rhs)
696  {
697  return (rhs < lhs);
698  }
699 
700  //***************************************************************************
706  //***************************************************************************
707  inline bool operator <=(const etl::pvoidvector& lhs, const etl::pvoidvector& rhs)
708  {
709  return !(lhs > rhs);
710  }
711 
712  //***************************************************************************
718  //***************************************************************************
719  inline bool operator >=(const etl::pvoidvector& lhs, const etl::pvoidvector& rhs)
720  {
721  return !(lhs < rhs);
722  }
723 }
724 
725 #include "minmax_pop.h"
726 
727 #undef ETL_IN_PVOIDVECTOR
728 
729 #endif
#define ETL_ASSERT(b, e)
Definition: error_handler.h:290
enable_if
Definition: type_traits_generator.h:1228
bool full() const
Definition: pvoidvector.h:598
const_reference at(size_t i) const
Definition: pvoidvector.h:258
iterator insert(iterator position, value_type value)
Definition: pvoidvector.h:433
iterator begin()
Definition: pvoidvector.h:81
void emplace_back(value_type value)
Definition: pvoidvector.h:407
iterator erase(iterator i_element)
Definition: pvoidvector.h:516
pointer data()
Definition: pvoidvector.h:304
size_type max_size() const
Definition: vector_base.h:143
const_reverse_iterator rend() const
Definition: pvoidvector.h:162
reference operator[](size_t i)
Definition: pvoidvector.h:225
void initialise()
Initialise the vector.
Definition: pvoidvector.h:627
bool empty() const
Definition: pvoidvector.h:589
const_iterator end() const
Definition: pvoidvector.h:108
const size_type CAPACITY
The maximum number of elements in the vector.
Definition: vector_base.h:165
reverse_iterator rend()
Definition: pvoidvector.h:153
etl::enable_if< etl::is_pointer< TIterator >::value, void >::type assign(TIterator first, TIterator last)
Definition: pvoidvector.h:351
void clear()
Clears the vector.
Definition: pvoidvector.h:384
void assign(size_t n, value_type value)
Definition: pvoidvector.h:372
void resize(size_t new_size)
Definition: pvoidvector.h:191
void insert(iterator position, size_t n, value_type value)
Definition: pvoidvector.h:481
void pop_back()
Definition: pvoidvector.h:419
const_reverse_iterator crend() const
Definition: pvoidvector.h:180
const_reference front() const
Definition: pvoidvector.h:277
void repair_buffer(void **p_buffer_)
Fix the internal pointers after a low level memory copy.
Definition: pvoidvector.h:635
reference back()
Definition: pvoidvector.h:286
iterator end()
Definition: pvoidvector.h:99
void insert(iterator position, TIterator first, TIterator last)
Definition: pvoidvector.h:500
etl::enable_if<!etl::is_pointer< TIterator >::value, void >::type assign(TIterator first, TIterator last)
Definition: pvoidvector.h:327
const_pointer data() const
Definition: pvoidvector.h:313
reference front()
Definition: pvoidvector.h:268
iterator emplace(iterator position, value_type value)
Definition: pvoidvector.h:456
iterator erase(iterator first, iterator last)
Definition: pvoidvector.h:532
pvoidvector(void **p_buffer_, size_t MAX_SIZE)
Constructor.
Definition: pvoidvector.h:617
etl::pvoidvector & operator=(const etl::pvoidvector &rhs)
Assignment operator.
Definition: pvoidvector.h:546
void push_back(value_type value)
Definition: pvoidvector.h:394
const_reference back() const
Definition: pvoidvector.h:295
size_t available() const
Definition: pvoidvector.h:607
const_iterator cend() const
Definition: pvoidvector.h:126
const_iterator begin() const
Definition: pvoidvector.h:90
const_iterator cbegin() const
Definition: pvoidvector.h:117
size_type size() const
Definition: pvoidvector.h:580
const_reverse_iterator crbegin() const
Definition: pvoidvector.h:171
reverse_iterator rbegin()
Definition: pvoidvector.h:135
reference at(size_t i)
Definition: pvoidvector.h:246
void resize(size_t new_size, value_type value)
Definition: pvoidvector.h:205
const_reverse_iterator rbegin() const
Definition: pvoidvector.h:144
Definition: pvoidvector.h:60
Definition: vector_base.h:125
Definition: vector_base.h:83
Definition: vector_base.h:69
Definition: vector_base.h:97
Definition: absolute.h:37
bool operator>(const etl::array< T, SIZE > &lhs, const etl::array< T, SIZE > &rhs)
Definition: array.h:633
bool operator>=(const etl::array< T, SIZE > &lhs, const etl::array< T, SIZE > &rhs)
Definition: array.h:645
bool operator!=(const etl::array< T, SIZE > &lhs, const etl::array< T, SIZE > &rhs)
Definition: array.h:594
bool operator==(const etl::array< T, SIZE > &lhs, const etl::array< T, SIZE > &rhs)
Definition: array.h:582
bool operator<(const etl::array< T, SIZE > &lhs, const etl::array< T, SIZE > &rhs)
Definition: array.h:606
bool operator<=(const etl::array< T, SIZE > &lhs, const etl::array< T, SIZE > &rhs)
Definition: array.h:621