1 #ifndef GEOS_COMMON_STD_CONTAINER_WRAPPERS_HPP
2 #define GEOS_COMMON_STD_CONTAINER_WRAPPERS_HPP
6 #include <unordered_map>
14 #ifdef GEOS_USE_BOUNDS_CHECK
15 #define USE_STD_CONTAINER_BOUNDS_CHECKING true
17 #define USE_STD_CONTAINER_BOUNDS_CHECKING false
30 template<
typename T >
31 using DefaultAllocator = std::allocator< T >;
42 typename Allocator = DefaultAllocator< T >,
43 bool USE_BOUNDS_CHECKING =
false >
44 class StdVectorWrapper :
public std::vector< T, Allocator >
48 using Base = std::vector< T, Allocator >;
52 StdVectorWrapper() noexcept(noexcept(Allocator())): Base( Allocator()) {}
54 explicit StdVectorWrapper(
const Allocator & alloc ) noexcept: Base( alloc ) {}
56 explicit StdVectorWrapper(
size_t count,
const Allocator & alloc = Allocator())
57 : Base( count, alloc ) {}
59 explicit StdVectorWrapper(
size_t count,
const T & value,
60 const Allocator & alloc = Allocator())
61 : Base( count, value, alloc ) {}
63 template<
class InputIt >
64 StdVectorWrapper( InputIt first, InputIt last,
65 const Allocator & alloc = Allocator())
66 : Base( first, last, alloc ) {}
68 StdVectorWrapper(
const Base & other ): Base( other ) {}
70 StdVectorWrapper( Base && other ): Base( other ) {}
72 StdVectorWrapper(
const Base & other,
const Allocator & alloc )
73 : Base( other, alloc ) {}
75 StdVectorWrapper( Base && other,
const Allocator & alloc )
76 : Base( other, alloc ) {}
78 StdVectorWrapper( std::initializer_list< T > init,
79 const Allocator & alloc = Allocator())
80 : Base( init, alloc ) {}
92 T
const & operator[](
size_t const index )
const
94 if constexpr (USE_BOUNDS_CHECKING)
96 if( index >= this->size() )
98 std::cout<<
"Index out of bounds in StdVectorWrapper::operator[]: index = " + std::to_string( index ) +
", size = " + std::to_string( this->size());
100 return Base::at( index );
104 return Base::operator[]( index );
114 T & operator[](
size_t const index )
116 if constexpr (USE_BOUNDS_CHECKING)
118 if( index >= this->size() )
120 std::cout<<
"Index out of bounds in StdVectorWrapper::operator[]: index = " + std::to_string( index ) +
", size = " + std::to_string( this->size());
122 return Base::at( index );
126 return Base::operator[]( index );
137 template<
typename T,
typename Allocator = std::allocator< T > >
138 using stdVector = internal::StdVectorWrapper< T, Allocator, USE_STD_CONTAINER_BOUNDS_CHECKING >;
152 template<
typename MapType,
153 bool USE_BOUNDS_CHECKING =
false >
154 class StdMapWrapper :
public MapType
158 using Base = MapType;
159 using KeyType =
typename Base::key_type;
160 using MappedType =
typename Base::mapped_type;
161 using ValueType =
typename Base::value_type;
173 MappedType & operator[]( KeyType
const & key )
175 if constexpr (USE_BOUNDS_CHECKING)
177 return this->at( key );
181 return Base::operator[]( key );
192 MappedType
const & operator[]( KeyType
const & key )
const
194 if constexpr (USE_BOUNDS_CHECKING)
196 return this->at( key );
200 return Base::operator[]( key );
214 template<
typename Key,
216 typename Compare = std::less< Key >,
217 typename Allocator = std::allocator< std::pair< const Key, T > > >
218 using stdMap = internal::StdMapWrapper< std::map< Key, T, Compare, Allocator >,
219 USE_STD_CONTAINER_BOUNDS_CHECKING >;
229 template<
typename Key,
231 typename Hash = std::hash< Key >,
232 typename KeyEqual = std::equal_to< Key >,
233 typename Allocator = std::allocator< std::pair< const Key, T > > >
234 using stdUnorderedMap = internal::StdMapWrapper< std::unordered_map< Key, T, Hash, KeyEqual, Allocator >,
235 USE_STD_CONTAINER_BOUNDS_CHECKING >;
255 template<
typename TKEY,
typename TVAL,
typename SORTED >
260 template<
typename TKEY,
typename TVAL >
261 class mapType< TKEY, TVAL, std::integral_constant< bool, true > > :
public stdMap< TKEY, TVAL >
266 template<
typename TKEY,
typename TVAL >
267 class mapType< TKEY, TVAL, std::integral_constant< bool, false > > :
public stdUnorderedMap< TKEY, TVAL >
269 using stdUnorderedMap< TKEY, TVAL >::stdUnorderedMap;
286 template<
typename TKEY,
typename TVAL,
typename SORTED >
291 template<
typename TKEY,
typename TVAL >
292 class mapBase< TKEY, TVAL, std::integral_constant< bool, true > > :
public std::map< TKEY, TVAL >
297 template<
typename TKEY,
typename TVAL >
298 class mapBase< TKEY, TVAL, std::integral_constant< bool, false > > :
public std::unordered_map< TKEY, TVAL >
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.
internal::StdMapWrapper< std::map< Key, T, Compare, Allocator >, USE_STD_CONTAINER_BOUNDS_CHECKING > stdMap
mapBase< TKEY, TVAL, std::integral_constant< bool, true > > map
Ordered map type.
internal::StdVectorWrapper< T, Allocator, USE_STD_CONTAINER_BOUNDS_CHECKING > stdVector