- added removal policy support for removeFromContainer (eUniqueVectorRemovalPolicy with REMOVE and REPLACE_WITH_DEFAULT)
- added more unit-tests
- fixed a small thing inside of eir::uniqueFetchNextTuple so that convertible values are accepted as single-value containers
@@ -214,7 +214,7 @@ | ||
214 | 214 | |
215 | 215 | auto iterate_container = [&]( const auto& container ) LAINLINE -> bool |
216 | 216 | { |
217 | - if constexpr ( std::same_as <decltype(container), const itemType&> ) | |
217 | + if constexpr ( std::is_assignable <itemType&, decltype(container)>::value ) | |
218 | 218 | { |
219 | 219 | if ( has_found_prev_yet == true ) |
220 | 220 | { |
@@ -424,7 +424,13 @@ | ||
424 | 424 | ( _replace_lambda( containers ), ... ); |
425 | 425 | } |
426 | 426 | |
427 | -template <typename itemType, typename... containerTypes> | |
427 | +enum class eUniqueVectorRemovalPolicy | |
428 | +{ | |
429 | + REMOVE, | |
430 | + REPLACE_WITH_DEFAULT | |
431 | +}; | |
432 | + | |
433 | +template <typename itemType, eUniqueVectorRemovalPolicy vecRemPolicy = eUniqueVectorRemovalPolicy::REMOVE, typename... containerTypes> | |
428 | 434 | requires ( sizeof...( containerTypes ) > 0 && std::is_const <itemType>::value == false && ( std::is_const <containerTypes>::value || ... ) == false ) |
429 | 435 | AINLINE void removeFromContainer( const itemType& findval, containerTypes&&... containers ) |
430 | 436 | { |
@@ -438,8 +444,9 @@ | ||
438 | 444 | { |
439 | 445 | container.RemoveAll( findval ); |
440 | 446 | } |
441 | - else if constexpr ( is_vector <decltype(container)>::value ) | |
447 | + else if constexpr ( is_vector <decltype(container)>::value && vecRemPolicy == eUniqueVectorRemovalPolicy::REMOVE ) | |
442 | 448 | { |
449 | + // If the policy is not equal to REMOVE, then we fall down to the replacement logic below. | |
443 | 450 | container.RemoveByValue( findval ); |
444 | 451 | } |
445 | 452 | else if constexpr ( std::same_as <decltype(container), itemType&> ) |
@@ -123,6 +123,37 @@ | ||
123 | 123 | } |
124 | 124 | printf( "ok.\n" ); |
125 | 125 | |
126 | + printf( "testing uniqueFetchNextPtr (stopped by nullptr)..." ); | |
127 | + { | |
128 | + int a, b; | |
129 | + | |
130 | + assert( eir::uniqueFetchNextPtr <int*> ( nullptr, &a, nullptr, &b ) == &a ); | |
131 | + assert( eir::uniqueFetchNextPtr <int*> ( &a, &a, nullptr, &b ) == nullptr ); | |
132 | + } | |
133 | + printf( "ok.\n" ); | |
134 | + | |
135 | + printf( "testing uniqueFetchNextPtrTuple (skip nullptr)..." ); | |
136 | + { | |
137 | + int a, b; | |
138 | + | |
139 | + assert( eir::uniqueFetchNextPtrTuple <int*> ( nullptr, std::tuple( &a, nullptr, &b ), std::tuple( nullptr ) ) == &a ); | |
140 | + assert( eir::uniqueFetchNextPtrTuple <int*> ( &a, std::tuple( &a, nullptr, &b ), std::tuple( nullptr ) ) == &b ); | |
141 | + assert( eir::uniqueFetchNextPtrTuple <int*> ( &b, std::tuple( &a, nullptr, &b ), std::tuple( nullptr ) ) == nullptr ); | |
142 | + } | |
143 | + printf( "ok.\n" ); | |
144 | + | |
145 | + printf( "testing uniqueFextNextPtrTuple (with references)..." ); | |
146 | + { | |
147 | + int tmp1, tmp2; | |
148 | + | |
149 | + int *a = &tmp1, *b = &tmp2; | |
150 | + | |
151 | + assert( eir::uniqueFetchNextPtrTuple <int*> ( nullptr, std::tie( a, b ), std::tuple( nullptr ) ) == &tmp1 ); | |
152 | + assert( eir::uniqueFetchNextPtrTuple <int*> ( a, std::tie( a, b ), std::tuple( nullptr ) ) == &tmp2 ); | |
153 | + assert( eir::uniqueFetchNextPtrTuple <int*> ( b, std::tie( a, b ), std::tuple( nullptr ) ) == nullptr ); | |
154 | + } | |
155 | + printf( "ok.\n" ); | |
156 | + | |
126 | 157 | printf( "testing uniqueIterate (single container)..." ); |
127 | 158 | { |
128 | 159 | size_t cnt = 0; |
@@ -349,4 +380,17 @@ | ||
349 | 380 | assert( c == 0 ); |
350 | 381 | } |
351 | 382 | printf( "ok.\n" ); |
383 | + | |
384 | + printf( "testing removeFromContainer (replace-default removal policy)..." ); | |
385 | + { | |
386 | + eir::Vector <int, CRTHeapAllocator> vec = std::array{ 1, 2, 3 }; | |
387 | + | |
388 | + eir::removeFromContainer <int, eir::eUniqueVectorRemovalPolicy::REPLACE_WITH_DEFAULT> ( 2, vec ); | |
389 | + | |
390 | + assert( vec.GetCount() == 3 ); | |
391 | + assert( vec[0] == 1 ); | |
392 | + assert( vec[1] == 0 ); | |
393 | + assert( vec[2] == 3 ); | |
394 | + } | |
395 | + printf( "ok.\n" ); | |
352 | 396 | } |
\ No newline at end of file |