• R/O
  • SSH
  • HTTPS

eirrepo: Commit


Commit MetaInfo

Revision451 (tree)
Time2022-02-23 19:23:23
Authorquiret

Log Message

- added MultiValueStore::Replace method
- added support for MultiValueStore in replaceInContainer & removeFromContainer
- extended the type-traits for MultiValueStore

Change Summary

Incremental Difference

--- common/sdk/MultiValueStore.h (revision 450)
+++ common/sdk/MultiValueStore.h (revision 451)
@@ -38,6 +38,14 @@
3838 {
3939 return;
4040 }
41+ template <typename subValueType, size_t N> requires ( std::convertible_to <subValueType, valueType> )
42+ inline MultiValueStore( std::array <subValueType, N> values )
43+ {
44+ for ( subValueType& value : values )
45+ {
46+ this->Insert( std::move( value ) );
47+ }
48+ }
4149 inline MultiValueStore( const MultiValueStore& ) = default;
4250 inline MultiValueStore( MultiValueStore&& ) = default;
4351
@@ -63,6 +71,7 @@
6371 }
6472 }
6573
74+ // Removes a single instance of the given value.
6675 template <ThreeWayComparableValues <valueType> queryType>
6776 inline void Remove( const queryType& value )
6877 {
@@ -84,7 +93,37 @@
8493 this->valrefs.RemoveNode( findNode );
8594 }
8695 }
96+ // Removes all instances of the given value.
97+ template <ThreeWayComparableValues <valueType> queryType>
98+ inline void RemoveAll( const queryType& value )
99+ {
100+ this->valrefs.Remove( value );
101+ }
87102
103+ template <ThreeWayComparableValues <valueType> queryType, std::convertible_to <valueType> convValueType>
104+ inline void Replace( const queryType& findval, convValueType&& replaceby )
105+ {
106+ if ( auto *findNode = this->valrefs.Find( findval ) )
107+ {
108+ if ( auto *appendNode = this->valrefs.Find( replaceby ) )
109+ {
110+ const valinfo& vinfo = appendNode->GetValue();
111+
112+ valinfo oldvinfo = this->valrefs.ExtractNode( findNode );
113+
114+ vinfo.refcnt += oldvinfo.refcnt;
115+ }
116+ else
117+ {
118+ valinfo& vinfo = this->valrefs.BeginNodeUpdate( findNode );
119+
120+ vinfo.value = std::forward <convValueType> ( replaceby );
121+
122+ this->valrefs.EndNodeUpdate( findNode );
123+ }
124+ }
125+ }
126+
88127 inline size_t GetValueCount( void ) const noexcept
89128 {
90129 return this->valrefs.GetValueCount();
@@ -211,6 +250,21 @@
211250 template <typename... mvsArgs>
212251 struct is_multivaluestore <MultiValueStore <mvsArgs...>> : std::true_type
213252 {};
253+template <typename... mvsArgs>
254+struct is_multivaluestore <const MultiValueStore <mvsArgs...>> : std::true_type
255+{};
256+template <typename... mvsArgs>
257+struct is_multivaluestore <MultiValueStore <mvsArgs...>&> : std::true_type
258+{};
259+template <typename... mvsArgs>
260+struct is_multivaluestore <const MultiValueStore <mvsArgs...>&> : std::true_type
261+{};
262+template <typename... mvsArgs>
263+struct is_multivaluestore <MultiValueStore <mvsArgs...>&&> : std::true_type
264+{};
265+template <typename... mvsArgs>
266+struct is_multivaluestore <const MultiValueStore <mvsArgs...>&&> : std::true_type
267+{};
214268
215269 } // namespace eir
216270
--- common/sdk/Set.h (revision 450)
+++ common/sdk/Set.h (revision 451)
@@ -217,8 +217,8 @@
217217 return;
218218 }
219219
220- template <typename subValueType, size_t N>
221- inline Set( std::array <subValueType, N> values ) requires std::convertible_to <subValueType, valueType>
220+ template <typename subValueType, size_t N> requires std::convertible_to <subValueType, valueType>
221+ inline Set( std::array <subValueType, N> values )
222222 {
223223 for ( subValueType& value : values )
224224 {
--- common/sdk/UniqueIterate.h (revision 450)
+++ common/sdk/UniqueIterate.h (revision 451)
@@ -364,6 +364,10 @@
364364 container.EndNodeUpdate( foundNode );
365365 }
366366 }
367+ else if constexpr ( is_multivaluestore <decltype(container)>::value )
368+ {
369+ container.Replace( oldval, newval );
370+ }
367371 else if constexpr ( is_vector <decltype(container)>::value )
368372 {
369373 container.ReplaceValues( oldval, newval );
@@ -387,12 +391,16 @@
387391 requires ( sizeof...( containerTypes ) > 0 )
388392 AINLINE void removeFromContainer( const itemType& findval, containerTypes&&... containers )
389393 {
390- auto _remove_lambda = [&]( auto& container )
394+ auto _remove_lambda = [&]( auto& container ) LAINLINE
391395 {
392396 if constexpr ( is_set <decltype(container)>::value )
393397 {
394398 container.Remove( findval );
395399 }
400+ else if constexpr ( is_multivaluestore <decltype(container)>::value )
401+ {
402+ container.RemoveAll( findval );
403+ }
396404 else if constexpr ( is_vector <decltype(container)>::value )
397405 {
398406 container.RemoveByValue( findval );
--- unittests/src/multivaluestore.cpp (revision 450)
+++ unittests/src/multivaluestore.cpp (revision 451)
@@ -121,4 +121,27 @@
121121 assert( cnt == 2 );
122122 }
123123 printf( "ok.\n" );
124+
125+ printf( "testing MultiValueStore replace..." );
126+ {
127+ eir::MultiValueStore <int, CRTHeapAllocator> mvs;
128+
129+ mvs.Insert( 5 );
130+ assert( mvs.Find( 5 ) == true );
131+
132+ mvs.Replace( 5, 1 );
133+ assert( mvs.Find( 5 ) == false );
134+ assert( mvs.Find( 1 ) == true );
135+
136+ mvs.Insert( 5 );
137+ mvs.Replace( 5, 1 );
138+ assert( mvs.Find( 5 ) == false );
139+ assert( mvs.Find( 1 ) == true );
140+
141+ mvs.Remove( 1 );
142+ assert( mvs.Find( 1 ) == true );
143+ mvs.Remove( 1 );
144+ assert( mvs.Find( 1 ) == false );
145+ }
146+ printf( "ok.\n" );
124147 }
\ No newline at end of file
--- unittests/src/uniqueiter.cpp (revision 450)
+++ unittests/src/uniqueiter.cpp (revision 451)
@@ -1,6 +1,7 @@
11 #include <sdk/UniqueIterate.h>
22 #include <sdk/Vector.h>
33 #include <sdk/Set.h>
4+#include <sdk/MultiValueStore.h>
45
56 #include <assert.h>
67
@@ -218,6 +219,28 @@
218219 }
219220 printf( "ok.\n" );
220221
222+ printf( "testing replaceInContainer (with multi-value-store)..." );
223+ {
224+ eir::MultiValueStore <int, CRTHeapAllocator> mvs = std::array{ 1, 1, 2, 2, 3, 3 };
225+ eir::MultiValueStore <int, CRTHeapAllocator> mvs2 = std::array{ 99 };
226+
227+ eir::replaceInContainer( 1, 7, mvs, mvs2 );
228+
229+ assert( mvs.GetRefCount( 7 ) == 2 );
230+ assert( mvs.GetRefCount( 1 ) == 0 );
231+
232+ eir::replaceInContainer( 2, 3, mvs, mvs2 );
233+
234+ assert( mvs.GetRefCount( 3 ) == 4 );
235+ assert( mvs.GetRefCount( 2 ) == 0 );
236+
237+ eir::replaceInContainer( 99, 0, mvs, mvs2 );
238+
239+ assert( mvs2.GetRefCount( 0 ) == 1 );
240+ assert( mvs2.GetRefCount( 99 ) == 0 );
241+ }
242+ printf( "ok.\n" );
243+
221244 printf( "testing removeFromContainer (with array)..." );
222245 {
223246 auto arr = std::array{ 1, 2, 3 };
@@ -253,4 +276,19 @@
253276 assert( set.GetValueCount() == 2 );
254277 }
255278 printf( "ok.\n" );
279+
280+ printf( "testing removeFromContainer (with multi-value-store)..." );
281+ {
282+ eir::MultiValueStore <int, CRTHeapAllocator> mvs = std::array{ 66, 99, 111 };
283+ eir::MultiValueStore <int, CRTHeapAllocator> mvs2 = std::array{ 1, 1, 2 };
284+
285+ eir::removeFromContainer( 1, mvs, mvs2 );
286+
287+ assert( mvs2.Find( 1 ) == false );
288+
289+ eir::removeFromContainer( 99, mvs, mvs2 );
290+
291+ assert( mvs.Find( 99 ) == false );
292+ }
293+ printf( "ok.\n" );
256294 }
\ No newline at end of file
Show on old repository browser