23 */ |
23 */ |
24 |
24 |
25 #ifndef SHARE_LIBADT_VECTSET_HPP |
25 #ifndef SHARE_LIBADT_VECTSET_HPP |
26 #define SHARE_LIBADT_VECTSET_HPP |
26 #define SHARE_LIBADT_VECTSET_HPP |
27 |
27 |
28 #include "libadt/set.hpp" |
28 #include "memory/allocation.hpp" |
29 |
29 |
30 #define BITS_IN_BYTE_ARRAY_SIZE 256 |
30 // Vector Sets |
31 |
|
32 // Vector Sets - An Abstract Data Type |
|
33 //INTERFACE |
|
34 |
31 |
35 // These sets can grow or shrink, based on the initial size and the largest |
32 // These sets can grow or shrink, based on the initial size and the largest |
36 // element currently in them. Slow and bulky for sparse sets, these sets |
33 // element currently in them. |
37 // are super for dense sets. They are fast and compact when dense. |
|
38 |
|
39 // TIME: |
|
40 // O(1) - Insert, Delete, Member, Sort |
|
41 // O(max_element) - Create, Clear, Size, Copy, Union, Intersect, Difference, |
|
42 // Equal, ChooseMember, Forall |
|
43 |
|
44 // SPACE: (max_element)/(8*sizeof(int)) |
|
45 |
|
46 |
34 |
47 //------------------------------VectorSet-------------------------------------- |
35 //------------------------------VectorSet-------------------------------------- |
48 class VectorSet : public Set { |
36 class VectorSet : public ResourceObj { |
49 friend class VectorSetI; // Friendly iterator class |
37 private: |
50 protected: |
|
51 uint size; // Size of data IN LONGWORDS (32bits) |
38 uint size; // Size of data IN LONGWORDS (32bits) |
52 uint32_t* data; // The data, bit packed |
39 uint32_t* data; // The data, bit packed |
|
40 Arena *_set_arena; |
53 |
41 |
54 void slamin( const VectorSet& s ); // Initialize one set with another |
42 void grow(uint newsize); // Grow vector to required bitsize |
55 int compare(const VectorSet &s) const; // Compare set contents |
|
56 void grow(uint newsize); // Grow vector to required bitsize |
|
57 |
43 |
58 public: |
44 public: |
59 VectorSet(Arena *arena); // Creates a new, empty set. |
45 VectorSet(Arena *arena); |
60 VectorSet(const VectorSet &s) : Set(s._set_arena) {slamin(s);} // Set clone; deep-copy guts |
|
61 Set &operator =(const Set &s); // Set clone; deep-copy guts |
|
62 VectorSet &operator =(const VectorSet &s) // Set clone; deep-copy guts |
|
63 { if( &s != this ) { slamin(s); } return *this; } |
|
64 ~VectorSet() {} |
46 ~VectorSet() {} |
65 Set &clone(void) const { return *(new VectorSet(*this)); } |
47 void insert(uint elem); |
66 |
48 |
67 Set &operator <<=(uint elem); // Add member to set |
49 void clear(); |
68 VectorSet operator << (uint elem) // Add member to new set |
50 bool is_empty() const; |
69 { VectorSet foo(*this); foo <<= elem; return foo; } |
51 int hash() const; |
70 Set &operator >>=(uint elem); // Delete member from set |
52 void reset() { |
71 VectorSet operator >> (uint elem) // Delete member from new set |
53 memset(data, 0, size*sizeof(uint32_t)); |
72 { VectorSet foo(*this); foo >>= elem; return foo; } |
|
73 |
|
74 VectorSet &operator &=(const VectorSet &s); // Intersect sets into first set |
|
75 Set &operator &=(const Set &s); // Intersect sets into first set |
|
76 VectorSet operator & (const VectorSet &s) const |
|
77 { VectorSet foo(*this); foo &= s; return foo; } |
|
78 |
|
79 VectorSet &operator |=(const VectorSet &s); // Intersect sets into first set |
|
80 Set &operator |=(const Set &s); // Intersect sets into first set |
|
81 VectorSet operator | (const VectorSet &s) const |
|
82 { VectorSet foo(*this); foo |= s; return foo; } |
|
83 |
|
84 VectorSet &operator -=(const VectorSet &s); // Intersect sets into first set |
|
85 Set &operator -=(const Set &s); // Intersect sets into first set |
|
86 VectorSet operator - (const VectorSet &s) const |
|
87 { VectorSet foo(*this); foo -= s; return foo; } |
|
88 |
|
89 int operator ==(const VectorSet &s) const; // True if sets are equal |
|
90 int operator ==(const Set &s) const; // True if sets are equal |
|
91 int operator < (const VectorSet &s) const; // True if strict subset |
|
92 int operator < (const Set &s) const; // True if strict subset |
|
93 int operator <=(const VectorSet &s) const; // True if subset relation holds. |
|
94 int operator <=(const Set &s) const; // True if subset relation holds. |
|
95 int disjoint (const Set &s) const; // True if sets are disjoint |
|
96 |
|
97 int operator [](uint elem) const; // Test for membership |
|
98 void Clear(void); // Clear a set |
|
99 uint Size(void) const; // Number of elements in the Set. |
|
100 void Sort(void); // Sort before iterating |
|
101 int hash() const; // Hash function |
|
102 void Reset(void) { // Reset a set |
|
103 memset( data, 0, size*sizeof(uint32_t) ); |
|
104 } |
54 } |
105 |
|
106 /* Removed for MCC BUG |
|
107 operator const VectorSet* (void) const { return this; } */ |
|
108 const VectorSet *asVectorSet() const { return this; } |
|
109 |
55 |
110 // Expose internals for speed-critical fast iterators |
56 // Expose internals for speed-critical fast iterators |
111 uint word_size() const { return size; } |
57 uint word_size() const { return size; } |
112 |
58 |
113 // Fast inlined "test and set". Replaces the idiom: |
59 // Fast inlined "test and set". Replaces the idiom: |
114 // if( visited[idx] ) return; |
60 // if (visited.test(idx)) return; |
115 // visited <<= idx; |
61 // visited.set(idx); |
116 // With: |
62 // With: |
117 // if( visited.test_set(idx) ) return; |
63 // if (visited.test_set(idx)) return; |
118 // |
64 // |
119 int test_set( uint elem ) { |
65 int test_set(uint elem) { |
120 uint word = elem >> 5; // Get the longword offset |
66 uint word = elem >> 5; // Get the longword offset |
121 if( word >= size ) // Beyond the last? |
67 if (word >= size) { |
122 return test_set_grow(elem); // Then grow; set; return 0; |
68 // Then grow; set; return 0; |
|
69 this->insert(elem); |
|
70 return 0; |
|
71 } |
123 uint32_t mask = 1L << (elem & 31); // Get bit mask |
72 uint32_t mask = 1L << (elem & 31); // Get bit mask |
124 uint32_t datum = data[word] & mask;// Get bit |
73 uint32_t datum = data[word] & mask;// Get bit |
125 data[word] |= mask; // Set bit |
74 data[word] |= mask; // Set bit |
126 return datum; // Return bit |
75 return datum; // Return bit |
127 } |
76 } |
128 int test_set_grow( uint elem ) { // Insert & return 0; |
77 |
129 (*this) <<= elem; // Insert into set |
78 // Fast inlined test |
130 return 0; // Return 0! |
79 int test(uint elem) const { |
|
80 uint word = elem >> 5; |
|
81 if (word >= size) { |
|
82 return 0; |
|
83 } |
|
84 uint32_t mask = 1L << (elem & 31); |
|
85 return data[word] & mask; |
131 } |
86 } |
132 |
87 |
133 // Fast inlined test |
88 void remove(uint elem) { |
134 int test( uint elem ) const { |
89 uint word = elem >> 5; |
135 uint word = elem >> 5; // Get the longword offset |
90 if (word >= size) { |
136 if( word >= size ) return 0; // Beyond the last? |
91 return; |
137 uint32_t mask = 1L << (elem & 31); // Get bit mask |
92 } |
138 return data[word] & mask; // Get bit |
93 uint32_t mask = 1L << (elem & 31); |
|
94 data[word] &= ~mask; // Clear bit |
139 } |
95 } |
140 |
96 |
141 // Fast inlined set |
97 // Fast inlined set |
142 void set( uint elem ) { |
98 void set(uint elem) { |
143 uint word = elem >> 5; // Get the longword offset |
99 uint word = elem >> 5; |
144 if( word >= size ) { // Beyond the last? |
100 if (word >= size) { |
145 test_set_grow(elem); // Then grow and set |
101 this->insert(elem); |
146 } else { |
102 } else { |
147 uint32_t mask = 1L << (elem & 31); // Get bit mask |
103 uint32_t mask = 1L << (elem & 31); |
148 data[word] |= mask; // Set bit |
104 data[word] |= mask; |
149 } |
105 } |
150 } |
106 } |
151 |
|
152 |
|
153 private: |
|
154 SetI_ *iterate(uint&) const; |
|
155 }; |
|
156 |
|
157 //------------------------------Iteration-------------------------------------- |
|
158 // Loop thru all elements of the set, setting "elem" to the element numbers |
|
159 // in random order. Inserted or deleted elements during this operation may |
|
160 // or may not be iterated over; untouched elements will be affected once. |
|
161 // Usage: for( VectorSetI i(s); i.test(); i++ ) { body = i.elem; } |
|
162 |
|
163 class VectorSetI : public StackObj { |
|
164 friend class VectorSet; |
|
165 const VectorSet *s; |
|
166 uint i, j; |
|
167 uint32_t mask; |
|
168 uint next(void); |
|
169 |
|
170 public: |
|
171 uint elem; // The publically accessible element |
|
172 |
|
173 VectorSetI( const VectorSet *vset ) : |
|
174 s(vset), |
|
175 i((uint)-1L), |
|
176 j((uint)-1L), |
|
177 mask((unsigned)(1L<<31)) { |
|
178 elem = next(); |
|
179 } |
|
180 |
|
181 void operator ++(void) { elem = next(); } |
|
182 int test(void) { return i < s->size; } |
|
183 }; |
107 }; |
184 |
108 |
185 #endif // SHARE_LIBADT_VECTSET_HPP |
109 #endif // SHARE_LIBADT_VECTSET_HPP |