| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
#ifndef TSETASTREE_H |
| 21 |
#define TSETASTREE_H |
| 22 |
|
| 23 |
#include <set> |
| 24 |
|
| 25 |
|
| 26 |
class TSetAsTree |
| 27 |
{ |
| 28 |
protected: |
| 29 |
|
| 30 |
typedef std::set<unsigned int> BaseType; |
| 31 |
|
| 32 |
protected: |
| 33 |
|
| 34 |
BaseType Base; |
| 35 |
|
| 36 |
unsigned int nElems; |
| 37 |
|
| 38 |
public: |
| 39 |
|
| 40 |
explicit TSetAsTree ( unsigned int size ) : nElems(size) {} |
| 41 |
|
| 42 |
TSetAsTree ( const TSetAsTree& ) = default; |
| 43 |
|
| 44 |
TSetAsTree ( TSetAsTree&& ) = default; |
| 45 |
|
| 46 |
TSetAsTree& operator= ( const TSetAsTree& ) = default; |
| 47 |
|
| 48 |
TSetAsTree& operator= ( TSetAsTree&& ) = default; |
| 49 |
|
| 50 |
~TSetAsTree() = default; |
| 51 |
|
| 52 |
|
| 53 |
void insert ( unsigned int i ) |
| 54 |
{ |
| 55 |
# ifdef ENABLE_CHECKING |
| 56 |
fpp_assert ( i > 0 ); |
| 57 |
# endif |
| 58 |
Base.insert(i); |
| 59 |
} |
| 60 |
|
| 61 |
void completeSet ( void ) |
| 62 |
{ |
| 63 |
for ( unsigned int i = 1; i < nElems; ++i ) |
| 64 |
Base.insert(i); |
| 65 |
} |
| 66 |
|
| 67 |
TSetAsTree& operator |= ( const TSetAsTree& is ) |
| 68 |
{ |
| 69 |
Base.insert ( is.Base.begin(), is.Base.end() ); |
| 70 |
return *this; |
| 71 |
} |
| 72 |
|
| 73 |
void clear ( void ) { Base.clear(); } |
| 74 |
|
| 75 |
|
| 76 |
bool empty ( void ) const { return Base.empty(); } |
| 77 |
|
| 78 |
bool contains ( unsigned int i ) const { return Base.find(i) != Base.end(); } |
| 79 |
|
| 80 |
bool intersects ( const TSetAsTree& is ) const |
| 81 |
{ |
| 82 |
if ( Base.empty() || is.Base.empty() ) |
| 83 |
return false; |
| 84 |
|
| 85 |
BaseType::const_iterator p1 = Base.begin(), p1_end = Base.end(), p2 = is.Base.begin(), p2_end = is.Base.end(); |
| 86 |
while ( p1 != p1_end && p2 != p2_end ) |
| 87 |
if ( *p1 == *p2 ) |
| 88 |
return true; |
| 89 |
else if ( *p1 < *p2 ) |
| 90 |
++p1; |
| 91 |
else |
| 92 |
++p2; |
| 93 |
|
| 94 |
return false; |
| 95 |
} |
| 96 |
|
| 97 |
void print ( std::ostream& o ) const |
| 98 |
{ |
| 99 |
o << "{"; |
| 100 |
if ( !empty() ) |
| 101 |
{ |
| 102 |
BaseType::const_iterator p = Base.begin(), p_end = Base.end(); |
| 103 |
o << *p; |
| 104 |
while ( ++p != p_end ) |
| 105 |
o << ',' << *p; |
| 106 |
} |
| 107 |
o << "}"; |
| 108 |
} |
| 109 |
typedef BaseType::const_iterator const_iterator; |
| 110 |
const_iterator begin ( void ) const { return Base.begin(); } |
| 111 |
const_iterator end ( void ) const { return Base.end(); } |
| 112 |
|
| 113 |
|
| 114 |
size_t size ( void ) const { return Base.size(); } |
| 115 |
|
| 116 |
unsigned int maxSize ( void ) const { return nElems; } |
| 117 |
}; |
| 118 |
|
| 119 |
#endif |