cgma
|
00001 //------------------------------------------------------------------------- 00002 // Filename : OrderedSet.hpp 00003 // 00004 // Purpose : A DLIList that does not allow duplicates and is faster 00005 // at inserting uniquely than DLIList. 00006 // 00007 // Creator : Matt Staten 00008 // 00009 // Creation Date : 09/22/2005 00010 // 00011 // Owner : Matt Staten 00012 //------------------------------------------------------------------------- 00013 00014 #ifndef OrderedSet_HPP 00015 #define OrderedSet_HPP 00016 00017 #include <set> 00018 #include <vector> 00019 #include "DLIList.hpp" 00020 00021 template<class X> class OrderedSet 00022 { 00023 private: 00024 DLIList<X> mList; 00025 std::set<X> mSet; 00026 00027 public: 00028 00029 OrderedSet( void ) {}; 00030 OrderedSet( const DLIList<X> list ) 00031 { 00032 int i; 00033 for ( i = 0; i < list.size(); i++ ) 00034 { 00035 append( list[i] ); 00036 } 00037 }; 00038 00039 OrderedSet( const std::vector<X> list ) 00040 { 00041 int i; 00042 for ( i = 0; i < list.size(); i++ ) 00043 { 00044 append( list[i] ); 00045 } 00046 }; 00047 00048 CubitBoolean is_in_list( X item ) const 00049 { 00050 if ( mSet.find( item ) == mSet.end() ) 00051 { 00052 return CUBIT_FALSE; 00053 } 00054 return CUBIT_TRUE; 00055 }; 00056 00057 int where_is_item( X item ) const 00058 { 00059 CubitBoolean stat = is_in_list( item ); 00060 if ( stat == CUBIT_TRUE ) 00061 { 00062 return mList.where_is_item( item ); 00063 } 00064 return -1; 00065 }; 00066 00067 // true if it could be replaced. false if old_item is not in the list 00068 bool replace_item( X old_item, X new_item ) 00069 { 00070 if ( is_in_list( old_item ) ) 00071 { 00072 for ( int i = 0; i < mList.size(); i++ ) 00073 { 00074 if ( mList[i] == old_item ) 00075 { 00076 mList[i] = new_item; 00077 mSet.erase( old_item ); 00078 mSet.insert( new_item ); 00079 return true; 00080 } 00081 } 00082 } 00083 return false; 00084 }; 00085 00086 CubitBoolean insert( X new_item ) 00087 { 00088 if ( !is_in_list( new_item ) ) 00089 { 00090 force_insert( new_item ); 00091 return CUBIT_TRUE; 00092 } 00093 return CUBIT_FALSE; 00094 } 00095 00096 // Append to the end of this list. 00097 CubitBoolean append( X new_item ) 00098 { 00099 return insert( new_item ); 00100 } 00101 00102 // insert at the beginning of the list. 00103 CubitBoolean prepend( X new_item ) 00104 { 00105 if ( !is_in_list( new_item ) ) 00106 { 00107 mList.insert_first( new_item ); 00108 mSet.insert( new_item ); 00109 return CUBIT_TRUE; 00110 } 00111 return CUBIT_FALSE; 00112 } 00113 00114 void force_insert( X new_item ) 00115 { 00116 mList.append( new_item ); 00117 mSet.insert( new_item ); 00118 }; 00119 00120 void reverse( void ) 00121 { 00122 mList.reverse(); 00123 }; 00124 00125 void reset( void ) 00126 { 00127 mList.reset(); 00128 }; 00129 00130 void reserve( int min_size ) 00131 { 00132 mList.reserve(min_size); 00133 }; 00134 00135 X get_and_step( int n = 1 ) 00136 { 00137 X temp = get(); 00138 step( n ); 00139 return temp; 00140 }; 00141 00142 void step( int n = 1 ) 00143 { 00144 mList.step( n ); 00145 }; 00146 00147 X get() const 00148 { 00149 return mList.get(); 00150 }; 00151 00152 const X & operator[](int index) const; 00153 OrderedSet<X>& operator=(const OrderedSet<X>& from); 00154 OrderedSet<X>& operator=(const DLIList<X>& from); 00155 OrderedSet<X>& operator=(const std::vector<X>& from); 00156 OrderedSet<X>& operator+=(const DLIList<X>& from); 00157 OrderedSet<X>& operator+=(const OrderedSet<X>& from); 00158 OrderedSet<X>& operator+=(const std::vector<X>& from); 00159 OrderedSet<X>& operator+=( X from ); 00160 OrderedSet<X>& operator-=(const DLIList<X>& from); 00161 OrderedSet<X>& operator-=(const OrderedSet<X> &from); 00162 OrderedSet<X>& operator-=(const OrderedSet<X> *from); 00163 OrderedSet<X>& operator-=( X from ); 00164 OrderedSet<X> operator&( const OrderedSet<X> other_set ) const; 00165 OrderedSet<X> operator|( const OrderedSet<X> other_set ) const; 00166 00167 CubitBoolean empty( void ) const { return ( mList.size() == 0 ? CUBIT_TRUE : CUBIT_FALSE ); }; 00168 int size( void ) const { return mList.size(); }; 00169 X pop( void ) 00170 { 00171 X item = mList.pop(); 00172 mSet.erase( item ); 00173 return item; 00174 }; 00175 void remove( X item ) 00176 { 00177 if ( !is_in_list( item ) ) 00178 return; 00179 mList.remove_all_with_value( item ); 00180 mSet.erase( item ); 00181 }; 00182 00183 void clean_out( void ) 00184 { 00185 mList.clean_out(); 00186 mSet.clear(); 00187 }; 00188 00189 void list( DLIList<X> &list ) const 00190 { 00191 list = mList; 00192 } 00193 const DLIList<X> * list( void ) const 00194 { 00195 return &mList; 00196 } 00197 00198 const X & last( void ) const; 00199 const X & first( void ) const; 00200 const std::vector<X>& as_vector( void ) const { return mList.as_vector(); }; 00201 }; 00202 00203 template <class X> inline 00204 const X & OrderedSet<X>::last(void) const 00205 { 00206 assert( mList.size() > 0 ); 00207 int index = mList.size()-1; 00208 return mList[index]; 00209 } 00210 00211 template <class X> inline 00212 const X & OrderedSet<X>::first(void) const 00213 { 00214 assert( mList.size() > 0 ); 00215 return mList[0]; 00216 } 00217 00218 template <class X> inline 00219 const X & OrderedSet<X>::operator[](int index) const 00220 { 00221 return mList[index]; 00222 } 00223 00224 template <class X> inline 00225 OrderedSet<X>& OrderedSet<X>::operator=(const OrderedSet<X>& from) 00226 { 00227 mList.clean_out(); 00228 mSet.clear(); 00229 int i; 00230 int from_size = from.mList.size(); 00231 for ( i = 0; i < from_size; i++ ) 00232 { 00233 force_insert( from.mList[i] ); 00234 } 00235 return *this; 00236 } 00237 00238 template <class X> inline 00239 OrderedSet<X>& OrderedSet<X>::operator=(const DLIList<X>& list) 00240 { 00241 mList.clean_out(); 00242 mSet.clear(); 00243 int i; 00244 int list_size = list.size(); 00245 for ( i = 0; i < list_size; i++ ) 00246 { 00247 this->insert( list[i] ); 00248 } 00249 return *this; 00250 } 00251 00252 template <class X> inline 00253 OrderedSet<X>& OrderedSet<X>::operator=(const std::vector<X>& list) 00254 { 00255 mList.clean_out(); 00256 mSet.clear(); 00257 int i; 00258 int list_size = list.size(); 00259 for ( i = 0; i < list_size; i++ ) 00260 { 00261 this->insert( list[i] ); 00262 } 00263 return *this; 00264 } 00265 00266 template <class X> inline 00267 OrderedSet<X>& OrderedSet<X>::operator+=( X from ) 00268 { 00269 this->insert( from ); 00270 return *this; 00271 } 00272 00273 template <class X> inline 00274 OrderedSet<X>& OrderedSet<X>::operator-=( X from ) 00275 { 00276 this->remove( from ); 00277 return *this; 00278 } 00279 00280 template <class X> inline 00281 OrderedSet<X>& OrderedSet<X>::operator+=(const DLIList<X>& list) 00282 { 00283 int i; 00284 for ( i = 0; i < list.size(); i++ ) 00285 { 00286 this->insert( list[i] ); 00287 } 00288 return *this; 00289 } 00290 00291 template <class X> inline 00292 OrderedSet<X>& OrderedSet<X>::operator+=(const std::vector<X>& list) 00293 { 00294 int i; 00295 for ( i = 0; i < list.size(); i++ ) 00296 { 00297 this->insert( list[i] ); 00298 } 00299 return *this; 00300 } 00301 00302 template <class X> inline 00303 OrderedSet<X>& OrderedSet<X>::operator+=(const OrderedSet<X>& list) 00304 { 00305 int i; 00306 for ( i = 0; i < list.size(); i++ ) 00307 { 00308 this->insert( list[i] ); 00309 } 00310 return *this; 00311 } 00312 00313 template <class X> inline 00314 OrderedSet<X>& OrderedSet<X>::operator-=(const DLIList<X>& list) 00315 { 00316 int i; 00317 for ( i = 0; i < list.size(); i++ ) 00318 { 00319 this->remove( list[i] ); 00320 } 00321 return *this; 00322 } 00323 00324 template <class X> inline 00325 OrderedSet<X>& OrderedSet<X>::operator-=(const OrderedSet<X>& list) 00326 { 00327 int i; 00328 for ( i = 0; i < list.size(); i++ ) 00329 { 00330 this->remove( list[i] ); 00331 } 00332 return *this; 00333 } 00334 00335 template <class X> inline 00336 OrderedSet<X>& OrderedSet<X>::operator-=(const OrderedSet<X> *list) 00337 { 00338 int i; 00339 for ( i = 0; i < list->size(); i++ ) 00340 { 00341 this->remove( (*list)[i] ); 00342 } 00343 return *this; 00344 } 00345 00346 template <class X> inline 00347 OrderedSet<X> OrderedSet<X>::operator&( const OrderedSet<X> other_set ) const 00348 { 00349 OrderedSet<X> tmp; 00350 for ( int i = 0; i < other_set.size(); i++ ) 00351 { 00352 if ( this->is_in_list( other_set[i] ) ) 00353 { 00354 tmp.append( other_set[i] ); 00355 } 00356 } 00357 return tmp; 00358 } 00359 00360 template <class X> inline 00361 OrderedSet<X> OrderedSet<X>::operator|( const OrderedSet<X> other_set ) const 00362 { 00363 OrderedSet<X> tmp = *this; 00364 tmp += other_set; 00365 return tmp; 00366 } 00367 00368 #endif // OrderedSet_HPP