• R/O
  • SSH
  • HTTPS

eirrepo: Commit


Commit MetaInfo

Revision455 (tree)
Time2022-02-25 01:52:09
Authorquiret

Log Message

- 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

Change Summary

Incremental Difference

--- common/sdk/UniqueIterate.h (revision 454)
+++ common/sdk/UniqueIterate.h (revision 455)
@@ -214,7 +214,7 @@
214214
215215 auto iterate_container = [&]( const auto& container ) LAINLINE -> bool
216216 {
217- if constexpr ( std::same_as <decltype(container), const itemType&> )
217+ if constexpr ( std::is_assignable <itemType&, decltype(container)>::value )
218218 {
219219 if ( has_found_prev_yet == true )
220220 {
@@ -424,7 +424,13 @@
424424 ( _replace_lambda( containers ), ... );
425425 }
426426
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>
428434 requires ( sizeof...( containerTypes ) > 0 && std::is_const <itemType>::value == false && ( std::is_const <containerTypes>::value || ... ) == false )
429435 AINLINE void removeFromContainer( const itemType& findval, containerTypes&&... containers )
430436 {
@@ -438,8 +444,9 @@
438444 {
439445 container.RemoveAll( findval );
440446 }
441- else if constexpr ( is_vector <decltype(container)>::value )
447+ else if constexpr ( is_vector <decltype(container)>::value && vecRemPolicy == eUniqueVectorRemovalPolicy::REMOVE )
442448 {
449+ // If the policy is not equal to REMOVE, then we fall down to the replacement logic below.
443450 container.RemoveByValue( findval );
444451 }
445452 else if constexpr ( std::same_as <decltype(container), itemType&> )
--- unittests/src/uniqueiter.cpp (revision 454)
+++ unittests/src/uniqueiter.cpp (revision 455)
@@ -123,6 +123,37 @@
123123 }
124124 printf( "ok.\n" );
125125
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+
126157 printf( "testing uniqueIterate (single container)..." );
127158 {
128159 size_t cnt = 0;
@@ -349,4 +380,17 @@
349380 assert( c == 0 );
350381 }
351382 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" );
352396 }
\ No newline at end of file
Show on old repository browser