Collection/Set.hGo to the documentation of this file.00001
00002
00003 #ifndef _SET_H
00004 #define _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 #include <stream.h>
00084
00085 #include "BaseList.h"
00086 #include "TypedCollection.h"
00087
00088 template <class T> class Set : public TypedCollection<T> {
00089 friend class Iterator <T>;
00090 friend class Mutator <T>;
00091
00092 protected:
00093 BaseList _list;
00094 virtual INLINE const Collection & _base_collection() const;
00095 virtual INLINE Collection & _base_collection();
00096
00097 public:
00098 INLINE Set();
00099 INLINE Set(const Set<T> &l);
00100 virtual INLINE ~Set();
00101
00102 INLINE const T &_element(int loc) const;
00103 INLINE T &_element(int loc);
00104
00105
00106
00107 INLINE void _del(int loc);
00108
00109
00110
00111 INLINE void _modify(int loc, T *el);
00112
00113
00114
00115
00116 INLINE int entries() const;
00117
00118
00119 INLINE Boolean empty() const;
00120
00121
00122 INLINE void print(ostream & out, char *sep) const;
00123
00124
00125 INLINE T &ins(const T *new_element);
00126
00127
00128
00129
00130
00131 INLINE void modify(T &el, T *new_element);
00132
00133
00134 INLINE void del(T &el);
00135
00136
00137 INLINE T *grab();
00138
00139
00140 INLINE Boolean member(const T &el) const;
00141
00142
00143 INLINE int _member(const T &el) const;
00144
00145
00146
00147 INLINE void clear();
00148
00149
00150 INLINE Set<T> & operator = (const Set<T> &l);
00151
00152
00153 INLINE int structures_OK() const;
00154
00155
00156
00157
00158 INLINE Listable *listable_clone() const;
00159
00160
00161 INLINE void print(ostream &o) const;
00162
00163 };
00164
00165
00166
00167
00168 template <class T>
00169 INLINE
00170 Set<T>::Set()
00171 {
00172
00173 }
00174
00175 template <class T>
00176 INLINE
00177 Set<T>::Set(const Set<T> &l)
00178 {
00179 _list = l._list;
00180 }
00181
00182 template <class T>
00183 INLINE
00184 Set<T>::~Set()
00185 {
00186
00187 }
00188
00189 template <class T>
00190 INLINE int
00191 Set<T>::entries() const
00192 {
00193 return _list.entries();
00194 }
00195
00196 template <class T>
00197 INLINE Boolean
00198 Set<T>::empty() const
00199 {
00200 return (_list.entries() == 0);
00201 }
00202
00203 template <class T>
00204 INLINE void
00205 Set<T>::print(ostream & out, char *sep) const
00206 {
00207 out << "{";
00208
00209 _list.print(out, sep);
00210
00211 out << "}";
00212 }
00213
00214 template <class T>
00215 INLINE T &
00216 Set<T>::ins(const T *new_element)
00217 {
00218 int loc = _list.index(*new_element);
00219
00220 if (loc >= 0) {
00221
00222 _list.modify(loc, CASTAWAY(T *)new_element);
00223 }
00224 else {
00225
00226 _list.ins(new_element, _list.entries());
00227 }
00228
00229 return CASTAWAY(T &)*new_element;
00230 }
00231
00232 template <class T>
00233 INLINE void
00234 Set<T>::_del(int loc)
00235 {
00236 _list.del(loc);
00237 }
00238
00239 template <class T>
00240 INLINE void
00241 Set<T>::_modify(int loc, T *el)
00242 {
00243 if (_list.member(*el))
00244 _list.del(loc);
00245 else
00246 _list.modify(loc, el);
00247 }
00248
00249 template <class T>
00250 INLINE void
00251 Set<T>::modify(T &el, T *new_element)
00252 {
00253 del( el );
00254 _list.ins(new_element, _list.entries());
00255 }
00256
00257
00258 template <class T>
00259 INLINE void
00260 Set<T>::del(T &el)
00261 {
00262 int i = _list.index(el);
00263
00264 if (i >= 0)
00265 _list.del(i);
00266 }
00267
00268 template <class T>
00269 INLINE T *
00270 Set<T>::grab()
00271 {
00272 return (T *) _list.grab(_list.entries() - 1);
00273 }
00274
00275
00276 template <class T>
00277 INLINE int
00278 Set<T>::_member(const T &el) const
00279 {
00280 return _list.index(el);
00281 }
00282
00283 template <class T>
00284 INLINE Boolean
00285 Set<T>::member(const T &el) const
00286 {
00287 return (_list.member(el));
00288 }
00289
00290 template <class T>
00291 INLINE void
00292 Set<T>::clear()
00293 {
00294 _list.clear();
00295 }
00296
00297 template <class T>
00298 INLINE Set<T> &
00299 Set<T>::operator = (const Set<T> &l)
00300 {
00301 _list = l._list;
00302
00303 return *this;
00304 }
00305
00306 template <class T>
00307 INLINE int
00308 Set<T>::structures_OK() const
00309 {
00310 return _list.structures_OK();
00311 }
00312
00313 template <class T>
00314 INLINE const T &
00315 Set<T>::_element(int sub) const
00316 {
00317 return (const T &) _list[sub];
00318 }
00319
00320 template <class T>
00321 INLINE T &
00322 Set<T>::_element(int sub)
00323 {
00324 return (T &) _list[sub];
00325 }
00326
00327 template <class T>
00328 INLINE const Collection &
00329 Set<T>::_base_collection() const
00330 {
00331 return _list;
00332 }
00333
00334 template <class T>
00335 INLINE Collection &
00336 Set<T>::_base_collection()
00337 {
00338 return _list;
00339 }
00340
00341 template <class T>
00342 INLINE Listable *
00343 Set<T>::listable_clone() const
00344 {
00345 return (Listable *) new Set<T>(*this);
00346 }
00347
00348 template <class T>
00349 INLINE void
00350 Set<T>::print(ostream &o) const
00351 {
00352 print(o, ", ");
00353 }
00354
00355
00356
00357
00358
00359
00360 template <class T>
00361 ostream &
00362 operator << (ostream & o, const Set<T> &s)
00363 {
00364 s.print(o, ", ");
00365 return o;
00366 }
00367
00368 #endif
|