Embedded Template Library 1.0
Loading...
Searching...
No Matches
flat_map.h
Go to the documentation of this file.
1
2
3/******************************************************************************
4The MIT License(MIT)
5
6Embedded Template Library.
7https://github.com/ETLCPP/etl
8https://www.etlcpp.com
9
10Copyright(c) 2015 John Wellbelove
11
12Permission is hereby granted, free of charge, to any person obtaining a copy
13of this software and associated documentation files(the "Software"), to deal
14in the Software without restriction, including without limitation the rights
15to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
16copies of the Software, and to permit persons to whom the Software is
17furnished to do so, subject to the following conditions :
18
19The above copyright notice and this permission notice shall be included in all
20copies or substantial portions of the Software.
21
22THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
25AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28SOFTWARE.
29******************************************************************************/
30
31#ifndef ETL_FLAT_MAP_INCLUDED
32#define ETL_FLAT_MAP_INCLUDED
33
34#include "platform.h"
35#include "initializer_list.h"
36#include "nth_type.h"
37#include "placement_new.h"
38#include "pool.h"
39#include "reference_flat_map.h"
40#include "type_traits.h"
41#include "utility.h"
42
44
45//*****************************************************************************
53//*****************************************************************************
54
55namespace etl
56{
57 //***************************************************************************
62 //***************************************************************************
63 template <typename TKey, typename TMapped, typename TKeyCompare = etl::less<TKey> >
64 class iflat_map : private etl::ireference_flat_map<TKey, TMapped, TKeyCompare>
65 {
66 private:
67
69 typedef typename refmap_t::lookup_t lookup_t;
70 typedef etl::ipool storage_t;
71
72 public:
73
74 typedef ETL_OR_STD::pair<const TKey, TMapped> value_type;
75 typedef TKey key_type;
76 typedef TMapped mapped_type;
77 typedef TKeyCompare key_compare;
78 typedef value_type& reference;
79 typedef const value_type& const_reference;
80#if ETL_USING_CPP11
81 typedef value_type&& rvalue_reference;
82#endif
83 typedef value_type* pointer;
84 typedef const value_type* const_pointer;
85 typedef size_t size_type;
86
87 typedef const key_type& const_key_reference;
88#if ETL_USING_CPP11
89 typedef key_type&& rvalue_key_reference;
90#endif
91 typedef mapped_type& mapped_reference;
92 typedef const mapped_type& const_mapped_reference;
93
94 typedef typename refmap_t::iterator iterator;
95 typedef typename refmap_t::const_iterator const_iterator;
96
97 typedef ETL_OR_STD::reverse_iterator<iterator> reverse_iterator;
98 typedef ETL_OR_STD::reverse_iterator<const_iterator> const_reverse_iterator;
99 typedef typename etl::iterator_traits<iterator>::difference_type difference_type;
100
101 private:
102
103 //*********************************************************************
105 //*********************************************************************
106 class compare
107 {
108 public:
109
110 bool operator()(const value_type& element, key_type key) const
111 {
112 return comp(element.first, key);
113 }
114
115 bool operator()(key_type key, const value_type& element) const
116 {
117 return comp(key, element.first);
118 }
119
120 key_compare comp;
121 };
122
123 public:
124
125 //*********************************************************************
128 //*********************************************************************
129 iterator begin()
130 {
131 return refmap_t::begin();
132 }
133
134 //*********************************************************************
137 //*********************************************************************
138 const_iterator begin() const
139 {
140 return refmap_t::begin();
141 }
142
143 //*********************************************************************
146 //*********************************************************************
147 iterator end()
148 {
149 return refmap_t::end();
150 }
151
152 //*********************************************************************
155 //*********************************************************************
156 const_iterator end() const
157 {
158 return refmap_t::end();
159 }
160
161 //*********************************************************************
164 //*********************************************************************
165 const_iterator cbegin() const
166 {
167 return refmap_t::cbegin();
168 }
169
170 //*********************************************************************
173 //*********************************************************************
174 const_iterator cend() const
175 {
176 return refmap_t::cend();
177 }
178
179 //*********************************************************************
182 //*********************************************************************
183 reverse_iterator rbegin()
184 {
185 return refmap_t::rbegin();
186 }
187
188 //*********************************************************************
192 //*********************************************************************
193 const_reverse_iterator rbegin() const
194 {
195 return refmap_t::rbegin();
196 }
197
198 //*********************************************************************
201 //*********************************************************************
202 reverse_iterator rend()
203 {
204 return refmap_t::rend();
205 }
206
207 //*********************************************************************
210 //*********************************************************************
211 const_reverse_iterator rend() const
212 {
213 return refmap_t::rend();
214 }
215
216 //*********************************************************************
220 //*********************************************************************
221 const_reverse_iterator crbegin() const
222 {
223 return refmap_t::crbegin();
224 }
225
226 //*********************************************************************
229 //*********************************************************************
230 const_reverse_iterator crend() const
231 {
232 return refmap_t::crend();
233 }
234
235#if ETL_USING_CPP11
236 //*********************************************************************
240 //*********************************************************************
241 mapped_reference operator[](rvalue_key_reference key)
242 {
243 iterator i_element = lower_bound(key);
244
245 // Doesn't already exist?
246 if ((i_element == end()) || compare(key, i_element->first))
247 {
248 insert_default_value(i_element, etl::move(key));
249 }
250
251 return i_element->second;
252 }
253#endif
254
255 //*********************************************************************
259 //*********************************************************************
260 mapped_reference operator[](const_key_reference key)
261 {
262 iterator i_element = lower_bound(key);
263
264 // Doesn't already exist?
265 if ((i_element == end()) || compare(key, i_element->first))
266 {
267 insert_default_value(i_element, key);
268 }
269
270 return i_element->second;
271 }
272
273 //*********************************************************************
279 //*********************************************************************
280 mapped_reference at(const_key_reference key)
281 {
282 return refmap_t::at(key);
283 }
284
285#if ETL_USING_CPP11
286 //*********************************************************************
287 template <typename K, typename KC = TKeyCompare, etl::enable_if_t<comparator_is_transparent<KC>::value, int> = 0>
288 mapped_reference at(const K& key)
289 {
290 return refmap_t::at(key);
291 }
292#endif
293
294 //*********************************************************************
300 //*********************************************************************
301 const_mapped_reference at(const_key_reference key) const
302 {
303 return refmap_t::at(key);
304 }
305
306#if ETL_USING_CPP11
307 //*********************************************************************
308 template <typename K, typename KC = TKeyCompare, etl::enable_if_t<comparator_is_transparent<KC>::value, int> = 0>
309 const_mapped_reference at(const K& key) const
310 {
311 return refmap_t::at(key);
312 }
313#endif
314
315 //*********************************************************************
323 //*********************************************************************
324 template <typename TIterator>
325 void assign(TIterator first, TIterator last)
326 {
327#if ETL_IS_DEBUG_BUILD
328 difference_type d = etl::distance(first, last);
329 ETL_ASSERT(d <= difference_type(capacity()), ETL_ERROR(flat_map_full));
330#endif
331
332 clear();
333
334 while (first != last)
335 {
336 insert(*first);
337 ++first;
338 }
339 }
340
341 //*********************************************************************
346 //*********************************************************************
347 ETL_OR_STD::pair<iterator, bool> insert(const_reference value)
348 {
349 iterator i_element = lower_bound(value.first);
350
351 ETL_OR_STD::pair<iterator, bool> result(i_element, false);
352
353 // Doesn't already exist?
354 if ((i_element == end()) || compare(value.first, i_element->first))
355 {
356 result = insert_value(i_element, value);
357 }
358
359 return result;
360 }
361
362#if ETL_USING_CPP11
363 //*********************************************************************
368 //*********************************************************************
369 ETL_OR_STD::pair<iterator, bool> insert(rvalue_reference value)
370 {
371 iterator i_element = lower_bound(value.first);
372
373 ETL_OR_STD::pair<iterator, bool> result(i_element, false);
374
375 // Doesn't already exist?
376 if ((i_element == end()) || compare(value.first, i_element->first))
377 {
378 // result = insert_value(i_element, etl::move(value.first),
379 // etl::move(value.second));
380 result = insert_value(i_element, etl::move(value));
381 }
382
383 return result;
384 }
385#endif
386
387 //*********************************************************************
393 //*********************************************************************
394 iterator insert(const_iterator /*position*/, const_reference value)
395 {
396 return insert(value).first;
397 }
398
399#if ETL_USING_CPP11
400 //*********************************************************************
406 //*********************************************************************
407 iterator insert(const_iterator /*position*/, rvalue_reference value)
408 {
409 return insert(etl::move(value)).first;
410 }
411#endif
412
413 //*********************************************************************
420 //*********************************************************************
421 template <class TIterator>
422 void insert(TIterator first, TIterator last)
423 {
424 while (first != last)
425 {
426 insert(*first);
427 ++first;
428 }
429 }
430
431 //*************************************************************************
433 //*************************************************************************
434 ETL_OR_STD::pair<iterator, bool> emplace(const value_type& value)
435 {
436 return emplace(value.first, value.second);
437 }
438
439#if ETL_USING_CPP11 && ETL_NOT_USING_STLPORT
440 //*************************************************************************
442 //*************************************************************************
443 template <typename... Args>
444 ETL_OR_STD::pair<iterator, bool> emplace(const_key_reference key, Args&&... args)
445 {
446 ETL_ASSERT(!full(), ETL_ERROR(flat_map_full));
447
448 // Create it.
449 value_type* pvalue = storage.allocate<value_type>();
450 ::new ((void*)etl::addressof(pvalue->first)) key_type(key);
451 ::new ((void*)etl::addressof(pvalue->second)) mapped_type(etl::forward<Args>(args)...);
452
453 iterator i_element = lower_bound(key);
454
455 ETL_OR_STD::pair<iterator, bool> result(i_element, false);
456
457 // Doesn't already exist?
458 if ((i_element == end()) || compare(key, i_element->first))
459 {
460 ETL_INCREMENT_DEBUG_COUNT;
461 result = refmap_t::insert_at(i_element, *pvalue);
462 }
463 else
464 {
465 pvalue->~value_type();
466 storage.release(pvalue);
467 }
468
469 return result;
470 }
471
472#else
473
474 //*************************************************************************
476 //*************************************************************************
477 template <typename T1>
478 ETL_OR_STD::pair<iterator, bool> emplace(const_key_reference key, const T1& value1)
479 {
480 ETL_ASSERT(!full(), ETL_ERROR(flat_map_full));
481
482 // Create it.
483 value_type* pvalue = storage.allocate<value_type>();
484 ::new ((void*)etl::addressof(pvalue->first)) key_type(key);
485 ::new ((void*)etl::addressof(pvalue->second)) mapped_type(value1);
486
487 iterator i_element = lower_bound(key);
488
489 ETL_OR_STD::pair<iterator, bool> result(i_element, false);
490
491 // Doesn't already exist?
492 if ((i_element == end()) || compare(key, i_element->first))
493 {
494 ETL_INCREMENT_DEBUG_COUNT;
495 result = refmap_t::insert_at(i_element, *pvalue);
496 }
497 else
498 {
499 pvalue->~value_type();
500 storage.release(pvalue);
501 }
502
503 return result;
504 }
505
506 //*************************************************************************
508 //*************************************************************************
509 template <typename T1, typename T2>
510 ETL_OR_STD::pair<iterator, bool> emplace(const_key_reference key, const T1& value1, const T2& value2)
511 {
512 ETL_ASSERT(!full(), ETL_ERROR(flat_map_full));
513
514 // Create it.
515 value_type* pvalue = storage.allocate<value_type>();
516 ::new ((void*)etl::addressof(pvalue->first)) key_type(key);
517 ::new ((void*)etl::addressof(pvalue->second)) mapped_type(value1, value2);
518
519 iterator i_element = lower_bound(key);
520
521 ETL_OR_STD::pair<iterator, bool> result(i_element, false);
522
523 // Doesn't already exist?
524 if ((i_element == end()) || compare(key, i_element->first))
525 {
526 ETL_INCREMENT_DEBUG_COUNT;
527 result = refmap_t::insert_at(i_element, *pvalue);
528 }
529 else
530 {
531 pvalue->~value_type();
532 storage.release(pvalue);
533 }
534
535 return result;
536 }
537
538 //*************************************************************************
540 //*************************************************************************
541 template <typename T1, typename T2, typename T3>
542 ETL_OR_STD::pair<iterator, bool> emplace(const_key_reference key, const T1& value1, const T2& value2, const T3& value3)
543 {
544 ETL_ASSERT(!full(), ETL_ERROR(flat_map_full));
545
546 // Create it.
547 value_type* pvalue = storage.allocate<value_type>();
548 ::new ((void*)etl::addressof(pvalue->first)) key_type(key);
549 ::new ((void*)etl::addressof(pvalue->second)) mapped_type(value1, value2, value3);
550
551 iterator i_element = lower_bound(key);
552
553 ETL_OR_STD::pair<iterator, bool> result(i_element, false);
554
555 // Doesn't already exist?
556 if ((i_element == end()) || compare(key, i_element->first))
557 {
558 ETL_INCREMENT_DEBUG_COUNT;
559 result = refmap_t::insert_at(i_element, *pvalue);
560 }
561 else
562 {
563 pvalue->~value_type();
564 storage.release(pvalue);
565 }
566
567 return result;
568 }
569
570 //*************************************************************************
572 //*************************************************************************
573 template <typename T1, typename T2, typename T3, typename T4>
574 ETL_OR_STD::pair<iterator, bool> emplace(const_key_reference key, const T1& value1, const T2& value2, const T3& value3, const T4& value4)
575 {
576 ETL_ASSERT(!full(), ETL_ERROR(flat_map_full));
577
578 // Create it.
579 value_type* pvalue = storage.allocate<value_type>();
580 ::new ((void*)etl::addressof(pvalue->first)) key_type(key);
581 ::new ((void*)etl::addressof(pvalue->second)) mapped_type(value1, value2, value3, value4);
582
583 iterator i_element = lower_bound(key);
584
585 ETL_OR_STD::pair<iterator, bool> result(i_element, false);
586
587 // Doesn't already exist?
588 if ((i_element == end()) || compare(key, i_element->first))
589 {
590 ETL_INCREMENT_DEBUG_COUNT;
591 result = refmap_t::insert_at(i_element, *pvalue);
592 }
593 else
594 {
595 pvalue->~value_type();
596 storage.release(pvalue);
597 }
598
599 return result;
600 }
601
602#endif
603
604 //*********************************************************************
608 //*********************************************************************
609 size_t erase(const_key_reference key)
610 {
611 iterator i_element = find(key);
612
613 if (i_element == end())
614 {
615 return 0;
616 }
617 else
618 {
619 i_element->~value_type();
620 storage.release(etl::addressof(*i_element));
621 refmap_t::erase(i_element);
622 ETL_DECREMENT_DEBUG_COUNT;
623 return 1;
624 }
625 }
626
627#if ETL_USING_CPP11
628 //*********************************************************************
629 template <typename K, typename KC = TKeyCompare, etl::enable_if_t<comparator_is_transparent<KC>::value, int> = 0>
630 size_t erase(K&& key)
631 {
632 iterator i_element = find(etl::forward<K>(key));
633
634 if (i_element == end())
635 {
636 return 0;
637 }
638 else
639 {
640 i_element->~value_type();
641 storage.release(etl::addressof(*i_element));
642 refmap_t::erase(i_element);
643 ETL_DECREMENT_DEBUG_COUNT;
644 return 1;
645 }
646 }
647#endif
648
649 //*********************************************************************
652 //*********************************************************************
653 iterator erase(iterator i_element)
654 {
655 i_element->~value_type();
656 storage.release(etl::addressof(*i_element));
657 ETL_DECREMENT_DEBUG_COUNT;
658 return refmap_t::erase(i_element);
659 }
660
661 //*********************************************************************
664 //*********************************************************************
665 iterator erase(const_iterator i_element)
666 {
667 i_element->~value_type();
668 storage.release(etl::addressof(*i_element));
669 ETL_DECREMENT_DEBUG_COUNT;
670 return refmap_t::erase(i_element);
671 }
672
673 //*********************************************************************
679 //*********************************************************************
680 iterator erase(const_iterator first, const_iterator last)
681 {
682 const_iterator itr = first;
683
684 while (itr != last)
685 {
686 itr->~value_type();
687 storage.release(etl::addressof(*itr));
688 ++itr;
689 ETL_DECREMENT_DEBUG_COUNT;
690 }
691
692 return refmap_t::erase(first, last);
693 }
694
695 //*************************************************************************
697 //*************************************************************************
698 void clear()
699 {
700 if ETL_IF_CONSTEXPR (etl::is_trivially_destructible<value_type>::value)
701 {
702 storage.release_all();
703 }
704 else
705 {
706 iterator itr = begin();
707
708 while (itr != end())
709 {
710 itr->~value_type();
711 storage.release(etl::addressof(*itr));
712 ++itr;
713 }
714 }
715
716 ETL_RESET_DEBUG_COUNT;
718 }
719
720 //*********************************************************************
724 //*********************************************************************
725 iterator find(const_key_reference key)
726 {
727 return refmap_t::find(key);
728 }
729
730#if ETL_USING_CPP11
731 //*********************************************************************
732 template <typename K, typename KC = TKeyCompare, etl::enable_if_t<comparator_is_transparent<KC>::value, int> = 0>
733 iterator find(const K& key)
734 {
735 return refmap_t::find(key);
736 }
737#endif
738
739 //*********************************************************************
743 //*********************************************************************
744 const_iterator find(const_key_reference key) const
745 {
746 return refmap_t::find(key);
747 }
748
749#if ETL_USING_CPP11
750 //*********************************************************************
751 template <typename K, typename KC = TKeyCompare, etl::enable_if_t<comparator_is_transparent<KC>::value, int> = 0>
752 const_iterator find(const K& key) const
753 {
754 return refmap_t::find(key);
755 }
756#endif
757
758 //*********************************************************************
762 //*********************************************************************
763 size_t count(const_key_reference key) const
764 {
765 return refmap_t::count(key);
766 }
767
768#if ETL_USING_CPP11
769 //*********************************************************************
770 template <typename K, typename KC = TKeyCompare, etl::enable_if_t<comparator_is_transparent<KC>::value, int> = 0>
771 size_t count(const K& key) const
772 {
773 return refmap_t::count(key);
774 }
775#endif
776
777 //*********************************************************************
781 //*********************************************************************
782 iterator lower_bound(const_key_reference key)
783 {
784 return refmap_t::lower_bound(key);
785 }
786
787#if ETL_USING_CPP11
788 //*********************************************************************
789 template <typename K, typename KC = TKeyCompare, etl::enable_if_t<comparator_is_transparent<KC>::value, int> = 0>
790 iterator lower_bound(const K& key)
791 {
792 return refmap_t::lower_bound(key);
793 }
794#endif
795
796 //*********************************************************************
800 //*********************************************************************
801 const_iterator lower_bound(const_key_reference key) const
802 {
803 return refmap_t::lower_bound(key);
804 }
805
806#if ETL_USING_CPP11
807 //*********************************************************************
808 template <typename K, typename KC = TKeyCompare, etl::enable_if_t<comparator_is_transparent<KC>::value, int> = 0>
809 const_iterator lower_bound(const K& key) const
810 {
811 return refmap_t::lower_bound(key);
812 }
813#endif
814
815 //*********************************************************************
819 //*********************************************************************
820 iterator upper_bound(const_key_reference key)
821 {
822 return refmap_t::upper_bound(key);
823 }
824
825#if ETL_USING_CPP11
826 //*********************************************************************
827 template <typename K, typename KC = TKeyCompare, etl::enable_if_t<comparator_is_transparent<KC>::value, int> = 0>
828 iterator upper_bound(const K& key)
829 {
830 return refmap_t::upper_bound(key);
831 }
832#endif
833
834 //*********************************************************************
838 //*********************************************************************
839 const_iterator upper_bound(const_key_reference key) const
840 {
841 return refmap_t::upper_bound(key);
842 }
843
844#if ETL_USING_CPP11
845 //*********************************************************************
846 template <typename K, typename KC = TKeyCompare, etl::enable_if_t<comparator_is_transparent<KC>::value, int> = 0>
847 const_iterator upper_bound(const K& key) const
848 {
849 return refmap_t::upper_bound(key);
850 }
851#endif
852
853 //*********************************************************************
857 //*********************************************************************
858 ETL_OR_STD::pair<iterator, iterator> equal_range(const_key_reference key)
859 {
860 return refmap_t::equal_range(key);
861 }
862
863#if ETL_USING_CPP11
864 //*********************************************************************
865 template <typename K, typename KC = TKeyCompare, etl::enable_if_t<comparator_is_transparent<KC>::value, int> = 0>
866 ETL_OR_STD::pair<iterator, iterator> equal_range(const K& key)
867 {
868 return refmap_t::equal_range(key);
869 }
870#endif
871
872 //*********************************************************************
876 //*********************************************************************
877 ETL_OR_STD::pair<const_iterator, const_iterator> equal_range(const_key_reference key) const
878 {
879 return refmap_t::equal_range(key);
880 }
881
882#if ETL_USING_CPP11
883 //*********************************************************************
884 template <typename K, typename KC = TKeyCompare, etl::enable_if_t<comparator_is_transparent<KC>::value, int> = 0>
885 ETL_OR_STD::pair<const_iterator, const_iterator> equal_range(const K& key) const
886 {
887 return refmap_t::equal_range(key);
888 }
889#endif
890
891 //*************************************************************************
893 //*************************************************************************
894 bool contains(const_key_reference key) const
895 {
896 return find(key) != end();
897 }
898
899#if ETL_USING_CPP11
900 //*************************************************************************
901 template <typename K, typename KC = TKeyCompare, etl::enable_if_t<comparator_is_transparent<KC>::value, int> = 0>
902 bool contains(const K& k) const
903 {
904 return find(k) != end();
905 }
906#endif
907
908 //*************************************************************************
910 //*************************************************************************
912 {
913 if (&rhs != this)
914 {
915 assign(rhs.cbegin(), rhs.cend());
916 }
917
918 return *this;
919 }
920
921#if ETL_USING_CPP11
922 //*************************************************************************
924 //*************************************************************************
926 {
927 move_container(etl::move(rhs));
928
929 return *this;
930 }
931#endif
932
933 //*************************************************************************
936 //*************************************************************************
937 size_type size() const
938 {
939 return refmap_t::size();
940 }
941
942 //*************************************************************************
945 //*************************************************************************
946 bool empty() const
947 {
948 return refmap_t::empty();
949 }
950
951 //*************************************************************************
954 //*************************************************************************
955 bool full() const
956 {
957 return refmap_t::full();
958 }
959
960 //*************************************************************************
963 //*************************************************************************
964 size_type capacity() const
965 {
966 return refmap_t::capacity();
967 }
968
969 //*************************************************************************
972 //*************************************************************************
973 size_type max_size() const
974 {
975 return refmap_t::max_size();
976 }
977
978 //*************************************************************************
981 //*************************************************************************
982 size_t available() const
983 {
984 return refmap_t::available();
985 }
986
987 protected:
988
989 //*********************************************************************
991 //*********************************************************************
992 iflat_map(lookup_t& lookup_, storage_t& storage_)
993 : refmap_t(lookup_)
994 , storage(storage_)
995 {
996 }
997
998#if ETL_USING_CPP11
999 //*************************************************************************
1002 //*************************************************************************
1003 void move_container(iflat_map&& rhs)
1004 {
1005 if (&rhs != this)
1006 {
1007 this->clear();
1008
1009 etl::iflat_map<TKey, TMapped, TKeyCompare>::iterator first = rhs.begin();
1010 etl::iflat_map<TKey, TMapped, TKeyCompare>::iterator last = rhs.end();
1011
1012 // Move all of the elements.
1013 while (first != last)
1014 {
1015 typename etl::iflat_map<TKey, TMapped, TKeyCompare>::iterator temp = first;
1016 ++temp;
1017
1018 this->insert(etl::move(*first));
1019 first = temp;
1020 }
1021 }
1022 }
1023#endif
1024
1025 private:
1026
1027 // Disable copy construction.
1028 iflat_map(const iflat_map&);
1029
1030 storage_t& storage;
1031
1032 TKeyCompare compare;
1033
1035 ETL_DECLARE_DEBUG_COUNT;
1036
1037#if ETL_USING_CPP11
1038 //*************************************************************************
1039 template <typename TValueType>
1040 ETL_OR_STD::pair<iterator, bool> insert_value(iterator i_element, TValueType&& value)
1041 {
1042 ETL_ASSERT(!refmap_t::full(), ETL_ERROR(flat_map_full));
1043
1044 value_type* pvalue = storage.allocate<value_type>();
1045 ::new (pvalue) value_type(etl::forward<TValueType>(value));
1046 ETL_INCREMENT_DEBUG_COUNT;
1047 return refmap_t::insert_at(i_element, *pvalue);
1048 }
1049#else
1050 //*************************************************************************
1051 ETL_OR_STD::pair<iterator, bool> insert_value(iterator i_element, const_reference value)
1052 {
1053 ETL_ASSERT(!refmap_t::full(), ETL_ERROR(flat_map_full));
1054
1055 value_type* pvalue = storage.allocate<value_type>();
1056 ::new (pvalue) value_type(value_type(value));
1057 ETL_INCREMENT_DEBUG_COUNT;
1058 return refmap_t::insert_at(i_element, *pvalue);
1059 }
1060#endif
1061
1062#if ETL_USING_CPP11
1063 //*************************************************************************
1064 ETL_OR_STD::pair<iterator, bool> insert_default_value(iterator i_element, rvalue_key_reference key)
1065 {
1066 ETL_ASSERT(!refmap_t::full(), ETL_ERROR(flat_map_full));
1067
1068 value_type* pvalue = storage.allocate<value_type>();
1069 ::new ((void*)etl::addressof(pvalue->first)) key_type(etl::move(key));
1070 ::new ((void*)etl::addressof(pvalue->second)) mapped_type();
1071 ETL_INCREMENT_DEBUG_COUNT;
1072
1073 return refmap_t::insert_at(i_element, *pvalue);
1074 }
1075#endif
1076
1077 //*************************************************************************
1078 ETL_OR_STD::pair<iterator, bool> insert_default_value(iterator i_element, const_key_reference key)
1079 {
1080 ETL_ASSERT(!refmap_t::full(), ETL_ERROR(flat_map_full));
1081
1082 value_type* pvalue = storage.allocate<value_type>();
1083 ::new ((void*)etl::addressof(pvalue->first)) key_type(key);
1084 ::new ((void*)etl::addressof(pvalue->second)) mapped_type();
1085 ETL_INCREMENT_DEBUG_COUNT;
1086
1087 return refmap_t::insert_at(i_element, *pvalue);
1088 }
1089
1090 //*************************************************************************
1092 //*************************************************************************
1093#if defined(ETL_POLYMORPHIC_FLAT_MAP) || defined(ETL_POLYMORPHIC_CONTAINERS)
1094
1095 public:
1096
1097 virtual ~iflat_map() {}
1098#else
1099
1100 protected:
1101
1103#endif
1104 };
1105
1106 //***************************************************************************
1112 //***************************************************************************
1113 template <typename TKey, typename TMapped, typename TKeyCompare>
1115 {
1116 return (lhs.size() == rhs.size()) && etl::equal(lhs.begin(), lhs.end(), rhs.begin());
1117 }
1118
1119 //***************************************************************************
1125 //***************************************************************************
1126 template <typename TKey, typename TMapped, typename TKeyCompare>
1128 {
1129 return !(lhs == rhs);
1130 }
1131
1132 //***************************************************************************
1139 //***************************************************************************
1140 template <typename TKey, typename TValue, const size_t MAX_SIZE_, typename TCompare = etl::less<TKey> >
1141 class flat_map : public etl::iflat_map<TKey, TValue, TCompare>
1142 {
1143 public:
1144
1145 static ETL_CONSTANT size_t MAX_SIZE = MAX_SIZE_;
1146
1147 //*************************************************************************
1149 //*************************************************************************
1151 : etl::iflat_map<TKey, TValue, TCompare>(lookup, storage)
1152 {
1153 }
1154
1155 //*************************************************************************
1157 //*************************************************************************
1158 flat_map(const flat_map& other)
1159 : etl::iflat_map<TKey, TValue, TCompare>(lookup, storage)
1160 {
1161 this->assign(other.cbegin(), other.cend());
1162 }
1163
1164#if ETL_USING_CPP11
1165 //*************************************************************************
1167 //*************************************************************************
1168 flat_map(flat_map&& other)
1169 : etl::iflat_map<TKey, TValue, TCompare>(lookup, storage)
1170 {
1171 if (&other != this)
1172 {
1173 this->move_container(etl::move(other));
1174 }
1175 }
1176#endif
1177
1178 //*************************************************************************
1183 //*************************************************************************
1184 template <typename TIterator>
1185 flat_map(TIterator first, TIterator last)
1186 : etl::iflat_map<TKey, TValue, TCompare>(lookup, storage)
1187 {
1188 this->assign(first, last);
1189 }
1190
1191#if ETL_HAS_INITIALIZER_LIST
1192 //*************************************************************************
1194 //*************************************************************************
1195 flat_map(std::initializer_list< typename etl::iflat_map<TKey, TValue, TCompare>::value_type> init)
1196 : etl::iflat_map<TKey, TValue, TCompare>(lookup, storage)
1197 {
1198 this->assign(init.begin(), init.end());
1199 }
1200#endif
1201
1202 //*************************************************************************
1204 //*************************************************************************
1206 {
1207 this->clear();
1208 }
1209
1210 //*************************************************************************
1212 //*************************************************************************
1214 {
1215 if (&rhs != this)
1216 {
1217 this->assign(rhs.cbegin(), rhs.cend());
1218 }
1219
1220 return *this;
1221 }
1222
1223#if ETL_USING_CPP11
1224 //*************************************************************************
1226 //*************************************************************************
1227 flat_map& operator=(flat_map&& rhs)
1228 {
1229 if (&rhs != this)
1230 {
1231 this->move_container(etl::move(rhs));
1232 }
1233
1234 return *this;
1235 }
1236#endif
1237
1238 private:
1239
1240 typedef typename etl::iflat_map<TKey, TValue, TCompare>::value_type node_t;
1241
1243 etl::pool<node_t, MAX_SIZE> storage;
1244
1246 etl::vector<node_t*, MAX_SIZE> lookup;
1247 };
1248
1249 template <typename TKey, typename TValue, const size_t MAX_SIZE_, typename TCompare>
1250 ETL_CONSTANT size_t flat_map<TKey, TValue, MAX_SIZE_, TCompare>::MAX_SIZE;
1251
1252 //*************************************************************************
1254 //*************************************************************************
1255#if ETL_USING_CPP17 && ETL_HAS_INITIALIZER_LIST
1256 template <typename... TPairs>
1257 flat_map(TPairs...)
1258 -> flat_map<typename etl::nth_type_t<0, TPairs...>::first_type, typename etl::nth_type_t<0, TPairs...>::second_type, sizeof...(TPairs)>;
1259#endif
1260
1261 //*************************************************************************
1263 //*************************************************************************
1264#if ETL_USING_CPP11 && ETL_HAS_INITIALIZER_LIST
1265 template <typename TKey, typename TMapped, typename TKeyCompare = etl::less<TKey>, typename... TPairs>
1266 constexpr auto make_flat_map(TPairs&&... pairs) -> etl::flat_map<TKey, TMapped, sizeof...(TPairs), TKeyCompare>
1267 {
1268 return {etl::forward<TPairs>(pairs)...};
1269 }
1270#endif
1271} // namespace etl
1272
1273#endif
#define ETL_ASSERT(b, e)
Definition error_handler.h:511
ETL_OR_STD::pair< iterator, bool > emplace(const_key_reference key, const T1 &value1, const T2 &value2)
Emplaces a value to the map.
Definition flat_map.h:510
const_iterator lower_bound(const_key_reference key) const
Definition flat_map.h:801
size_t count(const_key_reference key) const
Definition flat_map.h:763
size_type max_size() const
Definition flat_map.h:973
reverse_iterator rbegin()
Definition flat_map.h:183
iterator begin()
Definition flat_map.h:129
ETL_OR_STD::pair< iterator, iterator > equal_range(const_key_reference key)
Definition flat_map.h:858
~flat_map()
Destructor.
Definition flat_map.h:1205
void clear()
Clears the flat_map.
Definition flat_map.h:698
iflat_map(lookup_t &lookup_, storage_t &storage_)
Constructor.
Definition flat_map.h:992
size_t available() const
Definition flat_map.h:982
iterator find(const_key_reference key)
Definition flat_map.h:725
const_reverse_iterator rbegin() const
Definition flat_map.h:193
const_iterator upper_bound(const_key_reference key) const
Definition flat_map.h:839
mapped_reference at(const_key_reference key)
Definition flat_map.h:280
iterator erase(const_iterator i_element)
\ param i_element Iterator to the element.
Definition flat_map.h:665
~iflat_map()
Destructor.
Definition flat_map.h:1102
iterator upper_bound(const_key_reference key)
Definition flat_map.h:820
ETL_OR_STD::pair< iterator, bool > emplace(const value_type &value)
Emplaces a value to the map.
Definition flat_map.h:434
const_iterator cend() const
Definition flat_map.h:174
const_reverse_iterator crbegin() const
Definition flat_map.h:221
const_mapped_reference at(const_key_reference key) const
Definition flat_map.h:301
flat_map & operator=(const flat_map &rhs)
Assignment operator.
Definition flat_map.h:1213
reverse_iterator rend()
Definition flat_map.h:202
size_type capacity() const
Definition flat_map.h:964
flat_map()
Constructor.
Definition flat_map.h:1150
iterator erase(iterator i_element)
Definition flat_map.h:653
ETL_OR_STD::pair< iterator, bool > insert(const_reference value)
Definition flat_map.h:347
ETL_OR_STD::pair< iterator, bool > emplace(const_key_reference key, const T1 &value1)
Emplaces a value to the map.
Definition flat_map.h:478
const_iterator cbegin() const
Definition flat_map.h:165
size_t erase(const_key_reference key)
Definition flat_map.h:609
bool contains(const_key_reference key) const
Check if the map contains the key.
Definition flat_map.h:894
iflat_map & operator=(const iflat_map &rhs)
Assignment operator.
Definition flat_map.h:911
ETL_OR_STD::pair< iterator, bool > emplace(const_key_reference key, const T1 &value1, const T2 &value2, const T3 &value3, const T4 &value4)
Emplaces a value to the map.
Definition flat_map.h:574
const_iterator begin() const
Definition flat_map.h:138
const_iterator end() const
Definition flat_map.h:156
bool full() const
Definition flat_map.h:955
const_reverse_iterator rend() const
Definition flat_map.h:211
iterator end()
Definition flat_map.h:147
bool empty() const
Definition flat_map.h:946
flat_map(const flat_map &other)
Copy constructor.
Definition flat_map.h:1158
iterator erase(const_iterator first, const_iterator last)
Definition flat_map.h:680
const_iterator find(const_key_reference key) const
Definition flat_map.h:744
void insert(TIterator first, TIterator last)
Definition flat_map.h:422
void assign(TIterator first, TIterator last)
Definition flat_map.h:325
iterator insert(const_iterator, const_reference value)
Definition flat_map.h:394
const_reverse_iterator crend() const
Definition flat_map.h:230
flat_map(TIterator first, TIterator last)
Definition flat_map.h:1185
ETL_OR_STD::pair< iterator, bool > emplace(const_key_reference key, const T1 &value1, const T2 &value2, const T3 &value3)
Emplaces a value to the map.
Definition flat_map.h:542
iterator lower_bound(const_key_reference key)
Definition flat_map.h:782
size_type size() const
Definition flat_map.h:937
mapped_reference operator[](const_key_reference key)
Definition flat_map.h:260
ETL_OR_STD::pair< const_iterator, const_iterator > equal_range(const_key_reference key) const
Definition flat_map.h:877
Definition flat_map.h:1142
Definition flat_map.h:65
ETL_CONSTEXPR17 etl::enable_if<!etl::is_same< T, etl::nullptr_t >::value, T >::type * addressof(T &t)
Definition addressof.h:52
T * allocate()
Definition ipool.h:333
void release(const void *const p_object)
Definition ipool.h:460
Definition ipool.h:109
mapped_type & at(key_parameter_t key)
Definition reference_flat_map.h:474
iterator begin()
Definition reference_flat_map.h:356
void clear()
Definition reference_flat_map.h:668
ETL_OR_STD::pair< iterator, bool > insert_at(iterator i_element, value_type &value)
Definition reference_flat_map.h:994
const_reverse_iterator crbegin() const
Definition reference_flat_map.h:452
reverse_iterator rend()
Definition reference_flat_map.h:431
size_t count(key_parameter_t key) const
Definition reference_flat_map.h:772
iterator end()
Definition reference_flat_map.h:374
const_iterator cbegin() const
Definition reference_flat_map.h:392
ETL_OR_STD::pair< iterator, iterator > equal_range(key_parameter_t key)
Definition reference_flat_map.h:867
size_t available() const
Definition reference_flat_map.h:974
const_reverse_iterator crend() const
Definition reference_flat_map.h:462
size_type max_size() const
Definition reference_flat_map.h:965
bool empty() const
Definition reference_flat_map.h:938
iterator lower_bound(key_parameter_t key)
Definition reference_flat_map.h:791
reverse_iterator rbegin()
Definition reference_flat_map.h:411
iterator upper_bound(key_parameter_t key)
Definition reference_flat_map.h:829
size_type size() const
Definition reference_flat_map.h:929
iterator find(key_parameter_t key)
Definition reference_flat_map.h:678
const_iterator cend() const
Definition reference_flat_map.h:401
size_t erase(key_parameter_t key)
Definition reference_flat_map.h:601
bool full() const
Definition reference_flat_map.h:947
size_type capacity() const
Definition reference_flat_map.h:956
Definition reference_flat_map.h:79
Definition reference_flat_map.h:110
bitset_ext
Definition absolute.h:40
ETL_CONSTEXPR14 bool operator==(const etl::array< T, SIZE > &lhs, const etl::array< T, SIZE > &rhs)
Definition array.h:1081
ETL_CONSTEXPR14 bool operator!=(const etl::array< T, SIZE > &lhs, const etl::array< T, SIZE > &rhs)
Definition array.h:1093
ETL_CONSTEXPR TContainer::iterator end(TContainer &container)
Definition iterator.h:997
Definition compare.h:51
iterator
Definition iterator.h:424
pair holds two objects of arbitrary type
Definition utility.h:176