• R/O
  • SSH
  • HTTPS

eirrepo: Commit


Commit MetaInfo

Revision166 (tree)
Time2018-12-13 07:02:09
Authorquiret

Log Message

- added standard for-each walking to eir::Set and eir::Map + unit tests for it

Change Summary

Incremental Difference

--- common/sdk/Map.h (revision 165)
+++ common/sdk/Map.h (revision 166)
@@ -395,6 +395,57 @@
395395 }
396396 }
397397
398+ // Support for the standard C++ for-each walking.
399+ struct end_std_iterator {};
400+ struct std_iterator
401+ {
402+ AINLINE std_iterator( iterator&& right ) : iter( std::move( right ) )
403+ {
404+ return;
405+ }
406+
407+ AINLINE bool operator != ( const end_std_iterator& right ) const { return iter.IsEnd() == false; }
408+
409+ AINLINE std_iterator& operator ++ ( void )
410+ {
411+ iter.Increment();
412+ return *this;
413+ }
414+ AINLINE Node* operator * ( void )
415+ {
416+ return iter.Resolve();
417+ }
418+
419+ private:
420+ iterator iter;
421+ };
422+ AINLINE std_iterator begin( void ) { return std_iterator( iterator( *this ) ); }
423+ AINLINE end_std_iterator end( void ) const { return end_std_iterator(); }
424+
425+ struct const_std_iterator
426+ {
427+ AINLINE const_std_iterator( const_iterator&& right ) : iter( std::move( right ) )
428+ {
429+ return;
430+ }
431+
432+ AINLINE bool operator != ( const end_std_iterator& right ) const { return iter.IsEnd() == false; }
433+
434+ AINLINE const_std_iterator& operator ++ ( void )
435+ {
436+ iter.Increment();
437+ return *this;
438+ }
439+ AINLINE const Node* operator * ( void )
440+ {
441+ return iter.Resolve();
442+ }
443+
444+ private:
445+ const_iterator iter;
446+ };
447+ AINLINE const_std_iterator begin( void ) const { return const_std_iterator( const_iterator( *this ) ); }
448+
398449 // Nice helpers using operators.
399450 inline valueType& operator [] ( const keyType& key )
400451 {
--- common/sdk/Set.h (revision 165)
+++ common/sdk/Set.h (revision 166)
@@ -391,6 +391,30 @@
391391 }
392392 }
393393
394+ // Support for standard C++ container for-each loop.
395+ struct end_std_iterator {};
396+ struct std_iterator
397+ {
398+ AINLINE std_iterator( const_iterator&& right ) : iter( std::move( right ) )
399+ {
400+ return;
401+ }
402+
403+ AINLINE bool operator != ( const end_std_iterator& ) const { return iter.IsEnd() == false; }
404+
405+ AINLINE std_iterator& operator ++ ( void )
406+ {
407+ iter.Increment();
408+ return *this;
409+ }
410+ AINLINE const valueType& operator * ( void ) { return iter.Resolve()->GetValue(); }
411+
412+ private:
413+ const_iterator iter;
414+ };
415+ AINLINE std_iterator begin( void ) const { return std_iterator( const_iterator( *this ) ); }
416+ AINLINE end_std_iterator end( void ) const { return end_std_iterator(); }
417+
394418 // TODO: create a common method between Map and Set so they reuse maximum amount of code.
395419 template <typename otherAllocatorType>
396420 inline bool operator == ( const Set <valueType, otherAllocatorType, comparatorType>& right ) const
--- unittests/src/map.cpp (revision 165)
+++ unittests/src/map.cpp (revision 166)
@@ -97,6 +97,29 @@
9797 }
9898 printf( "ok.\n" );
9999
100+ printf( "testing map standard for-each..." );
101+ {
102+ eir::Map <int, int, EirHeapAllocator> map;
103+
104+ map[ -1 ] = 77;
105+ map[ 64 ] = 100;
106+
107+ for ( eir::Map <int, int, EirHeapAllocator>::Node *node : map )
108+ {
109+ assert( node->GetKey() == -1 || node->GetKey() == 64 );
110+ assert( node->GetValue() == 77 || node->GetValue() == 100 );
111+ }
112+
113+ const eir::Map <int, int, EirHeapAllocator>& const_map = map;
114+
115+ for ( const eir::Map <int, int, EirHeapAllocator>::Node *node : const_map )
116+ {
117+ assert( node->GetKey() == -1 || node->GetKey() == 64 );
118+ assert( node->GetValue() == 77 || node->GetValue() == 100 );
119+ }
120+ }
121+ printf( "ok.\n" );
122+
100123 printf( "testing map clear..." );
101124 {
102125 MapWithHeap map;
--- unittests/src/set.cpp (revision 165)
+++ unittests/src/set.cpp (revision 166)
@@ -124,6 +124,20 @@
124124 }
125125 printf( "ok.\n" );
126126
127+ printf( "testing set standard for-each..." );
128+ {
129+ eir::Set <int, EirHeapAllocator> set;
130+
131+ set.Insert( 5 );
132+ set.Insert( 99 );
133+
134+ for ( const int& val : set )
135+ {
136+ assert( val == 5 || val == 99 );
137+ }
138+ }
139+ printf( "ok.\n" );
140+
127141 printf( "testing set clear..." );
128142 {
129143 eir::Set <int, EirHeapAllocator> set;
--- unittests/src/vector.cpp (revision 165)
+++ unittests/src/vector.cpp (revision 166)
@@ -331,6 +331,28 @@
331331 }
332332 printf( "ok.\n" );
333333
334+ printf( "testing vector standard for-each..." );
335+ {
336+ VectorWithHeap vec;
337+
338+ int values[] = { 99, 51, 3124 };
339+
340+ for ( int val : values )
341+ {
342+ vec.AddToBack( val );
343+ }
344+
345+ size_t idx = 0;
346+
347+ for ( long long& val : vec )
348+ {
349+ assert( values[idx] == val );
350+
351+ idx++;
352+ }
353+ }
354+ printf( "ok.\n" );
355+
334356 printf( "testing vector with custom allocator link..." );
335357 {
336358 eir::Vector <long long, EirHeapLinkAllocator> customVec( nullptr, 0, &globalHeapAlloc );
Show on old repository browser