1 #ifndef GEOS_COMMON_STD_CONTAINER_WRAPPERS_HPP
2 #define GEOS_COMMON_STD_CONTAINER_WRAPPERS_HPP
8 #include <unordered_map>
20 #ifdef GEOS_USE_BOUNDS_CHECK
21 #define USE_STD_CONTAINER_BOUNDS_CHECKING true
23 #define USE_STD_CONTAINER_BOUNDS_CHECKING false
54 template<
typename T >
55 using DefaultAllocator = std::allocator< T >;
66 typename Allocator = DefaultAllocator< T >,
67 bool USE_BOUNDS_CHECKING =
false >
68 class StdVectorWrapper :
public std::vector< T, Allocator >
72 using Base = std::vector< T, Allocator >;
76 StdVectorWrapper() noexcept(noexcept(Allocator())): Base( Allocator()) {}
78 explicit StdVectorWrapper(
const Allocator & alloc ) noexcept: Base( alloc ) {}
80 explicit StdVectorWrapper(
size_t count,
const Allocator & alloc = Allocator())
81 : Base( count, alloc ) {}
83 explicit StdVectorWrapper(
size_t count,
const T & value,
84 const Allocator & alloc = Allocator())
85 : Base( count, value, alloc ) {}
87 template<
class InputIt >
88 StdVectorWrapper( InputIt first, InputIt last,
89 const Allocator & alloc = Allocator())
90 : Base( first, last, alloc ) {}
92 StdVectorWrapper(
const Base & other ): Base( other ) {}
94 StdVectorWrapper( Base && other ): Base( other ) {}
96 StdVectorWrapper(
const Base & other,
const Allocator & alloc )
97 : Base( other, alloc ) {}
99 StdVectorWrapper( Base && other,
const Allocator & alloc )
100 : Base( other, alloc ) {}
102 StdVectorWrapper( std::initializer_list< T > init,
103 const Allocator & alloc = Allocator())
104 : Base( init, alloc ) {}
116 T
const & operator[](
size_t const index )
const
118 if constexpr (USE_BOUNDS_CHECKING)
120 if( index >= this->size() )
122 std::cout<<
"Index out of bounds in StdVectorWrapper::operator[]: index = " + std::to_string( index ) +
", size = " + std::to_string( this->size());
124 return Base::at( index );
128 return Base::operator[]( index );
138 T & operator[](
size_t const index )
140 if constexpr (USE_BOUNDS_CHECKING)
142 if( index >= this->size() )
144 std::cout<<
"Index out of bounds in StdVectorWrapper::operator[]: index = " + std::to_string( index ) +
", size = " + std::to_string( this->size());
146 return Base::at( index );
150 return Base::operator[]( index );
161 template<
typename T,
typename Allocator = std::allocator< T > >
162 using stdVector = internal::StdVectorWrapper< T, Allocator, USE_STD_CONTAINER_BOUNDS_CHECKING >;
176 template<
typename MapType,
177 bool USE_BOUNDS_CHECKING =
false >
178 class StdMapWrapper :
public MapType
182 using Base = MapType;
183 using KeyType =
typename Base::key_type;
184 using MappedType =
typename Base::mapped_type;
185 using ValueType =
typename Base::value_type;
197 MappedType & operator[]( KeyType
const & key )
199 if constexpr (USE_BOUNDS_CHECKING)
201 return this->at( key );
205 return Base::operator[]( key );
216 MappedType
const & operator[]( KeyType
const & key )
const
218 if constexpr (USE_BOUNDS_CHECKING)
220 return this->at( key );
224 return Base::operator[]( key );
234 auto & get_inserted( KeyType && k )
236 return this->try_emplace( k ).first->second;
245 auto & get_inserted( KeyType
const & k )
247 return this->try_emplace( k ).first->second;
261 template<
typename Key,
263 typename Compare = std::less< Key >,
264 typename Allocator = std::allocator< std::pair< const Key, T > > >
265 using stdMap = internal::StdMapWrapper< std::map< Key, T, Compare, Allocator >,
276 template<
typename Key,
278 typename Hash = std::hash< Key >,
279 typename KeyEqual = std::equal_to< Key >,
280 typename Allocator = std::allocator< std::pair< const Key, T > > >
281 using stdUnorderedMap = internal::StdMapWrapper< std::unordered_map< Key, T, Hash, KeyEqual, Allocator >,
302 template<
typename TKEY,
typename TVAL,
typename SORTED >
307 template<
typename TKEY,
typename TVAL >
308 class mapType< TKEY, TVAL, std::integral_constant< bool, true > > :
public stdMap< TKEY, TVAL >
313 template<
typename TKEY,
typename TVAL >
314 class mapType< TKEY, TVAL, std::integral_constant< bool, false > > :
public stdUnorderedMap< TKEY, TVAL >
333 template<
typename TKEY,
typename TVAL,
typename SORTED >
338 template<
typename TKEY,
typename TVAL >
339 class mapBase< TKEY, TVAL, std::integral_constant< bool, true > > :
public std::map< TKEY, TVAL >
344 template<
typename TKEY,
typename TVAL >
345 class mapBase< TKEY, TVAL, std::integral_constant< bool, false > > :
public std::unordered_map< TKEY, TVAL >
363 bool USE_BOUNDS_CHECKING =
false >
364 struct StdArrayWrapper :
public std::array< T, N >
368 using Base = std::array< T, N >;
377 constexpr T & operator[](
size_t const index )
379 if constexpr (USE_BOUNDS_CHECKING)
381 return this->at( index );
385 return Base::operator[]( index );
396 constexpr T
const & operator[](
size_t const index )
const
398 if constexpr (USE_BOUNDS_CHECKING)
400 return this->at( index );
404 return Base::operator[]( index );
416 template<
class T, std::
size_t N >
417 struct stdArray :
public internal::StdArrayWrapper< T, N, true >
429 template<
typename _Tp,
typename ... _Up >
431 ->stdArray< std::enable_if_t< (std::is_same_v< _Tp, _Up > && ...), _Tp >,
432 1 +
sizeof...(_Up) >;
445 return {{arr[Is] ...}};
456 template<
typename T, std::
size_t N >
#define USE_STD_CONTAINER_BOUNDS_CHECKING
Compile-time flag that enables or disables runtime bounds checking in GEOS container wrappers.
Base template for ordered and unordered maps.
Base template for ordered and unordered maps.
internal::StdMapWrapper< std::unordered_map< Key, T, Hash, KeyEqual, Allocator >, USE_STD_CONTAINER_BOUNDS_CHECKING > stdUnorderedMap
mapBase< TKEY, TVAL, std::integral_constant< bool, false > > unordered_map
Unordered map type.
constexpr stdArray< T, N > to_stdArray_impl(std::array< T, N > const &arr, std::index_sequence< Is... >)
Helper function that convert a std::array into a stdArray by expanding its elements.
constexpr stdArray< T, N > to_stdArray(std::array< T, N > const &arr)
Convert an std::array to an stdArray.
stdArray(_Tp, _Up ...) -> stdArray< std::enable_if_t<(std::is_same_v< _Tp, _Up > &&...), _Tp >, 1+sizeof...(_Up) >
Deduction guide for stdArray Allows the element type and array size to be automatically deduced from ...
internal::StdMapWrapper< std::map< Key, T, Compare, Allocator >, USE_STD_CONTAINER_BOUNDS_CHECKING > stdMap
std::size_t size_t
Unsigned size type.
mapBase< TKEY, TVAL, std::integral_constant< bool, true > > map
Ordered map type.
internal::StdVectorWrapper< T, Allocator, USE_STD_CONTAINER_BOUNDS_CHECKING > stdVector