RefSet.hGo to the documentation of this file.00001
00002
00003 #ifndef _REF_SET_H
00004 #define _REF_SET_H
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089 #include <stream.h>
00090 #include "../macros.h"
00091 #include "TypedCollection.h"
00092 #include "BaseRefList.h"
00093
00094 template <class T> class RefSet : public TypedCollection<T> {
00095 friend class Iterator <T>;
00096 friend class Mutator <T>;
00097
00098 protected:
00099 BaseRefList _list;
00100 virtual INLINE const Collection & _base_collection() const;
00101 virtual INLINE Collection & _base_collection();
00102
00103 public:
00104 INLINE RefSet();
00105 INLINE RefSet(const RefSet<T >&l);
00106 virtual INLINE ~RefSet();
00107
00108 INLINE int _member(const T &el) const;
00109
00110
00111
00112 INLINE const T &_element(int loc) const;
00113 INLINE T &_element(int loc);
00114
00115
00116
00117
00118 INLINE void _del(int loc);
00119
00120
00121
00122 INLINE void _modify(int loc, T &el);
00123
00124
00125
00126 INLINE int entries() const;
00127
00128
00129 INLINE Boolean empty() const;
00130
00131
00132 INLINE Boolean any_valid() const;
00133
00134
00135 INLINE void print(ostream & out, char *sep) const;
00136
00137
00138 INLINE T &ins(const T &new_element);
00139
00140
00141
00142
00143
00144 INLINE void del(T &el);
00145
00146
00147 INLINE T &grab();
00148
00149
00150
00151 INLINE Boolean member(const T &el) const;
00152
00153
00154 INLINE Boolean valid(const T &el) const;
00155
00156
00157 INLINE Boolean valid(const int loc) const;
00158
00159
00160 INLINE void clear();
00161
00162
00163 INLINE RefSet<T> & operator -= (const RefSet<T>& l);
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173
00174
00175 INLINE RefSet<T> & operator += (const RefSet<T>& l);
00176
00177
00178
00179
00180
00181
00182
00183 INLINE int operator == (const RefSet<T > &l) const;
00184
00185
00186
00187
00188 INLINE RefSet<T >&operator = (const RefSet<T >&l);
00189
00190
00191 INLINE int structures_OK() const;
00192
00193
00194
00195
00196 INLINE Listable *listable_clone() const;
00197
00198
00199 INLINE void print(ostream &o) const;
00200
00201 };
00202
00203
00204
00205
00206 #include "Iterator.h"
00207 #include "Mutator.h"
00208
00209 template <class T>
00210 INLINE
00211 RefSet<T>::RefSet()
00212 {
00213
00214 }
00215
00216 template <class T>
00217 INLINE
00218 RefSet<T>::RefSet(const RefSet<T >&l)
00219 {
00220 _list = l._list;
00221 }
00222
00223 template <class T>
00224 INLINE
00225 RefSet<T>::~RefSet()
00226 {
00227
00228 }
00229
00230 template <class T>
00231 INLINE int
00232 RefSet<T>::entries() const
00233 {
00234 return _list.entries();
00235 }
00236
00237 template <class T>
00238 INLINE Boolean
00239 RefSet<T>::empty() const
00240 {
00241 return (_list.entries() == 0);
00242 }
00243
00244 template <class T>
00245 INLINE void
00246 RefSet<T>::print(ostream & out, char *sep) const
00247 {
00248 out << "{";
00249
00250 _list.print(out, sep);
00251
00252 out << "}";
00253 }
00254
00255 template <class T>
00256 INLINE T &
00257 RefSet<T>::ins(const T &new_element)
00258 {
00259 int loc = _list.index(new_element);
00260 if (loc >= 0) {
00261
00262 _list.modify(loc, CASTAWAY(T &)new_element);
00263 }
00264 else {
00265
00266 _list.ins(new_element, _list.entries());
00267 }
00268
00269 return CASTAWAY(T &)new_element;
00270 }
00271
00272 template <class T>
00273 INLINE void
00274 RefSet<T>::_del(int loc)
00275 {
00276 _list.del(loc);
00277 }
00278
00279 template <class T>
00280 INLINE void
00281 RefSet<T>::_modify(int loc, T &el)
00282 {
00283 int old_loc = this->_member(el);
00284
00285 if (old_loc != loc) {
00286 _list.modify(loc, el);
00287 if (old_loc != -1) {
00288 _list.del(old_loc);
00289 }
00290 }
00291 }
00292
00293 template <class T>
00294 INLINE void
00295 RefSet<T>::del(T &el)
00296 {
00297 int i = _list.index(el);
00298
00299 if (i >= 0)
00300 _list.del(i);
00301 }
00302
00303 template <class T>
00304 INLINE T &
00305 RefSet<T>::grab()
00306 {
00307 int num = _list.entries()-1;
00308
00309
00310 while (!_list.valid(num)) {
00311 _list.del(num);
00312 num = _list.entries()-1;
00313 }
00314
00315
00316
00317 T &el = (T &) _list[num];
00318
00319 _list.del(num);
00320
00321 return el;
00322 }
00323
00324
00325 template <class T>
00326 INLINE int
00327 RefSet<T>::_member(const T &el) const
00328 {
00329 return _list.index(el);
00330 }
00331
00332 template <class T>
00333 INLINE Boolean
00334 RefSet<T>::member(const T &el) const
00335 {
00336 return (_list.member(el));
00337 }
00338
00339 template <class T>
00340 INLINE Boolean
00341 RefSet<T>::valid(const int loc) const
00342 {
00343 return (_list.valid(loc));
00344 }
00345
00346 template <class T>
00347 INLINE Boolean
00348 RefSet<T>::valid(const T &el) const
00349 {
00350 return (_list.valid(el));
00351 }
00352
00353 template <class T>
00354 INLINE Boolean
00355 RefSet<T>::any_valid() const
00356 {
00357 for (int i = 0; i < _list.entries(); ++i)
00358 if (_list.valid(i))
00359 return 1;
00360
00361 return 0;
00362 }
00363
00364 template <class T>
00365 INLINE void
00366 RefSet<T>::clear()
00367 {
00368 _list.clear();
00369 }
00370
00371 template <class T>
00372 INLINE
00373 int
00374 RefSet<T>::operator == (const RefSet<T> &l) const
00375 {
00376 if (entries() != l.entries())
00377 return 0;
00378
00379 for (Iterator<T> iter = l; iter.valid(); ++iter)
00380 if (!(this->member( iter.current() )))
00381 return 0;
00382
00383 return 1;
00384 }
00385
00386 template <class T>
00387 INLINE RefSet<T> &
00388 RefSet<T>::operator -= (const RefSet<T> &l)
00389 {
00390
00391
00392
00393
00394
00395
00396 for (Iterator<T> iter = l; iter.valid(); ++iter)
00397 if (this->member( iter.current() )) {
00398 this->del(iter.current());
00399 }
00400
00401 return *this;
00402 }
00403
00404 template <class T>
00405 INLINE RefSet<T> &
00406 RefSet<T>::operator += (const RefSet<T> &l)
00407 {
00408
00409
00410
00411
00412
00413
00414 for (Iterator<T> iter = l; iter.valid(); ++iter)
00415 this->ins( iter.current() );
00416
00417 return *this;
00418 }
00419
00420 template <class T>
00421 INLINE RefSet<T> &
00422 RefSet<T>::operator = (const RefSet<T> &l)
00423 {
00424 _list = l._list;
00425
00426 return *this;
00427 }
00428
00429 template <class T>
00430 INLINE int
00431 RefSet<T>::structures_OK() const
00432 {
00433 return _list.structures_OK();
00434 }
00435
00436 template <class T>
00437 INLINE const T &
00438 RefSet<T>::_element(int sub) const
00439 {
00440 return (const T &) _list[sub];
00441 }
00442
00443 template <class T>
00444 INLINE T &
00445 RefSet<T>::_element(int sub)
00446 {
00447 return (T &) _list[sub];
00448 }
00449
00450 template <class T>
00451 INLINE const Collection &
00452 RefSet<T>::_base_collection() const
00453 {
00454 return _list;
00455 }
00456
00457 template <class T>
00458 INLINE Collection &
00459 RefSet<T>::_base_collection()
00460 {
00461 return _list;
00462 }
00463
00464 template <class T>
00465 INLINE Listable *
00466 RefSet<T>::listable_clone() const
00467 {
00468 return (Listable *) new RefSet<T>(*this);
00469 }
00470
00471 template <class T>
00472 INLINE void
00473 RefSet<T>::print(ostream &o) const
00474 {
00475 print(o, ", ");
00476 }
00477
00478
00479
00480
00481
00482
00483 template <class S>
00484 ostream &
00485 operator << (ostream & o, const RefSet<S> &s)
00486 {
00487 s.print(o, ", ");
00488 return o;
00489 }
00490
00491 #endif
00492
|