ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
FlatMap.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------------*/
2/*----------------------------- File FlatMap.h -----------------------------*/
3/*--------------------------------------------------------------------------*/
33/*--------------------------------------------------------------------------*/
34/*----------------------------- DEFINITIONS --------------------------------*/
35/*--------------------------------------------------------------------------*/
36
37#ifndef __ADS_interop_std_FlatMap
38 #define __ADS_interop_std_FlatMap
39/* self-identification: #endif constant */
40
41/*--------------------------------------------------------------------------*/
42/*------------------------------ INCLUDES ----------------------------------*/
43/*--------------------------------------------------------------------------*/
44
46
47#include <cstddef>
48#include <functional>
49#include <iterator>
50#include <utility>
51#include <version>
52#if defined( __cpp_lib_flat_map )
53 #include <flat_map>
54#endif
55#if defined( __cpp_lib_flat_set )
56 #include <flat_set>
57#endif
58
59/*--------------------------------------------------------------------------*/
60/*--------------------------- NAMESPACE ------------------------------------*/
61/*--------------------------------------------------------------------------*/
62
63namespace ADS
64{
65
66/*--------------------------------------------------------------------------*/
67/*--------------------------- CLASS StdSequence ----------------------------*/
68/*--------------------------------------------------------------------------*/
69
73template< class S >
74 requires( std::random_access_iterator< typename S::iterator > &&
75 requires( S & s , std::size_t i , const typename S::value_type & x )
76{
77 s.insert( i , x );
78 s.erase( i );
79 } )
80class StdSequence : public S
81{
82
83 public:
84 using value_type = typename S::value_type;
86 using const_reference = const value_type &;
87 using size_type = std::size_t;
88 using difference_type = std::ptrdiff_t;
89 using iterator = typename S::iterator;
90 using const_iterator = typename S::const_iterator;
91
92 using S::S;
93 using S::begin;
94 using S::end;
95
97 StdSequence( void ) = default;
98
100 template< std::input_iterator I , std::sentinel_for< I > E >
101 StdSequence( I first , E last ) {
102 insert( cend() , first , last );
103 }
104
106 [[nodiscard]] const_iterator cbegin( void ) const { return( begin() ); }
108 [[nodiscard]] const_iterator cend( void ) const { return( end() ); }
109
112 const std::size_t i = rankOf( p );
113 S::insert( i , x );
114 return( begin() + difference_type( i ) );
115 }
116
119 template< std::input_iterator I , std::sentinel_for< I > E >
120 iterator insert( const_iterator p , I first , E last ) {
121 const std::size_t i0 = rankOf( p );
122 for( std::size_t i = i0 ; first != last ; ++first )
123 S::insert( i++ , value_type( *first ) );
124 return( begin() + difference_type( i0 ) );
125 }
126
128 template< class... A >
129 iterator emplace( const_iterator p , A &&... a ) {
130 return( insert( p , value_type( std::forward< A >( a )... ) ) );
131 }
132
134 template< class... A >
135 reference emplace_back( A &&... a ) {
136 return( *insert( cend() , value_type( std::forward< A >( a )... ) ) );
137 }
138
141 const std::size_t i = rankOf( p );
142 S::erase( i );
143 return( begin() + difference_type( i ) );
144 }
145
149 const std::size_t i = rankOf( first );
150 for( std::size_t k = rankOf( last ) - i ; k > 0 ; --k )
151 S::erase( i );
152 return( begin() + difference_type( i ) );
153 }
154
155 private:
157 std::size_t rankOf( const_iterator p ) const {
158 return( std::size_t( p - cbegin() ) );
159 }
160
161 }; // end( class StdSequence )
162
163/*--------------------------------------------------------------------------*/
164/*------------------------ THE FLAT CONTAINERS -----------------------------*/
165/*--------------------------------------------------------------------------*/
166
167#if defined( __cpp_lib_flat_set )
169template< class K , class C = std::less< K > >
170using TieredFlatSet = std::flat_set< K , C , TieredVector< K > >;
171#endif
172
173#if defined( __cpp_lib_flat_map )
175template< class K , class V , class C = std::less< K > >
176using TieredFlatMap =
177 std::flat_map< K , V , C , TieredVector< K > , TieredVector< V > >;
178#endif
179
180/*--------------------------------------------------------------------------*/
181
182} // end( namespace ADS )
183
184/*--------------------------------------------------------------------------*/
185
186#endif /* FlatMap.h included */
187
188/*--------------------------------------------------------------------------*/
189/*---------------------------- End File FlatMap.h --------------------------*/
190/*--------------------------------------------------------------------------*/
The tiered vector of Goodrich and Kloss, beyond the book: a sequence with access by index in time,...
iterator emplace(const_iterator p, A &&... a)
an item built from before the item of
Definition FlatMap.h:129
iterator erase(const_iterator first, const_iterator last)
the items of removed; an iterator on the item after them
Definition FlatMap.h:148
const_iterator cbegin(void) const
the constant iterator on the first item
Definition FlatMap.h:106
typename S::value_type value_type
the items
Definition FlatMap.h:84
reference emplace_back(A &&... a)
an item built from at the end
Definition FlatMap.h:135
StdSequence(void)=default
the empty sequence
std::size_t size_type
the sizes
Definition FlatMap.h:87
StdSequence(I first, E last)
the items of , in their order
Definition FlatMap.h:101
typename S::iterator iterator
the iterator
Definition FlatMap.h:89
iterator erase(const_iterator p)
the item of removed; an iterator on the item after it
Definition FlatMap.h:140
typename S::const_iterator const_iterator
the constant one
Definition FlatMap.h:90
std::ptrdiff_t difference_type
the distances
Definition FlatMap.h:88
const_iterator cend(void) const
the constant iterator past the last item
Definition FlatMap.h:108
iterator insert(const_iterator p, I first, E last)
the items of before the item of , in their order; an iterator on the first of them
Definition FlatMap.h:120
iterator insert(const_iterator p, const value_type &x)
before the item of ; an iterator on
Definition FlatMap.h:111
value_type & reference
a reference to an item
Definition FlatMap.h:85
const value_type & const_reference
to a constant item
Definition FlatMap.h:86
a sequence with access in and insertion and removal anywhere in , in circular blocks of items
Definition TieredVector.h:107
Definition ADT.h:96