Polaris: Set.h Source File

Collection/Set.h

Go to the documentation of this file.
00001 ///
00002 ///
00003 #ifndef _SET_H
00004 #define _SET_H
00005 ///
00006 /// file Set.cc
00007 ///
00008 /// \class Set 
00009 /// \brief A very simple set class
00010 /// \defgroup Polaris
00011 /// \ingroup Polaris
00012 ///  C++ VDL
00013 /// \see Set.h
00014 ///
00015 /// \endcode
00016 /// \section Overview Overview
00017 /// A template for a set for type T
00018 /// where 'T' must be derived from Listable. The
00019 /// Set is built from Collection so the Iterator
00020 /// class operates on it. The Set takes ownership
00021 /// of objects which are inserted into it.
00022 ///
00023 /// \endcode
00024 /// \section Bugs Bugs
00025 /// The set is implemented...poorly.  It is a
00026 /// a List.  Due to the simple implementation a
00027 /// number of problems exists including efficiency
00028 /// and confusing error messages.  It is, however,
00029 /// operational (we think).
00030 /// The interface is based on the RefSet.  Thus,
00031 /// it is not particularly good. It should be
00032 /// reconsidered.
00033 ///
00034 /// \endcode
00035 /// \section Hierarchy Hierarchy Structure
00036 /// Set does not directly inherit from Collection. Instead
00037 /// it inherits from TypedCollection (which forms the interface
00038 /// to all Iterator routines) and contains a BaseList
00039 /// which inherits from Collection.
00040 /// The hierarchy can be represented by:
00041 ///
00042 /// \code
00043 ///                     --- :    'inherited from' relationship
00044 ///                     -c- :    'contained in' relationship
00045 ///
00046 ///
00047 ///                         Collection
00048 ///                        /          \                                          //
00049 ///                       /            \                                         //
00050 ///               BaseList   Listable   BaseRefList
00051 ///               |  \          |             /  |
00052 ///               |   \         |            /   |
00053 ///               |    c  TypedCollection   c    |
00054 ///               |     \     /     \      /     |
00055 ///               |      \   /       \    /      |
00056 ///               |      LIST        RefList     |
00057 ///               |      Set         RefSet      |
00058 ///               c                              c
00059 ///               |          Listable            |
00060 ///               |             |                |
00061 ///               |             |                |
00062 ///               |        BaseMapRoot           |
00063 ///               |       /           \          |
00064 ///               |      /             \         |
00065 ///               BaseMap               BaseRefMap
00066 ///                  |                       |
00067 ///                  |                       |                            
00068 ///             TypedBaseMap          TypedBaseRefMap
00069 ///               /     \                /         \                             //
00070 ///              /       \              /           \                            //
00071 ///    ProtoDatabase      ProtoMap   ProtoRefMap    ProtoRefDatabase
00072 ///         /  \           |    |      |     |         |         \               //
00073 ///        /    \          |    |      |     |         |          \              //
00074 /// Database KeyDatabase  Map KeyMap  RefMap KeyMap  RefDatabase RefKeyDatabase
00075 /// \endcode
00076 ///
00077 ///
00078 /// \endcode
00079 /// \section See See Also
00080 /// Collection, Listable, Wrapper, Iterator, TypedCollection, Zombie
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     ///< Used to obtain a specific element of the set.  Use the _member()
00105     ///< function to get the index that is used.
00106 
00107     INLINE void     _del(int loc);
00108     ///< Random Access Delete.
00109     ///< Delete the Node in position loc where 1 <= loc <= entries().
00110 
00111     INLINE void     _modify(int loc, T *el);
00112     ///< Random Access Modify.
00113     ///< Change the node at position loc to be el. If el is already
00114     ///< in the set, the locth position is deleted.
00115 
00116     INLINE int      entries() const;
00117     ///< Return the number of entries in the Set.
00118 
00119     INLINE Boolean  empty() const;
00120     ///< Return a flag indicating if the Set is empty.
00121 
00122     INLINE void     print(ostream & out, char *sep) const;
00123     ///< print set to out with 'sep' sepperating each element
00124 
00125     INLINE T        &ins(const T *new_element);
00126     ///< Insert new_element into the set, and return a reference
00127     ///<   to '*new_element' (for purposes of programming convenience).
00128     ///< If an element with the same key already exists, this old
00129     ///<   object is deleted and replaced with 'new_element'.
00130     
00131     INLINE void        modify(T &el, T *new_element);
00132     ///< Modify 'el' to be the new_element.
00133 
00134     INLINE void     del(T &el);
00135     ///< Delete node 'el'.
00136 
00137     INLINE T       *grab();
00138     ///< Delete and return an arbitrary element of the set.
00139 
00140     INLINE Boolean  member(const T &el) const;
00141     ///< Check to see if node 'el' is a member of the Set.
00142 
00143     INLINE int      _member(const T &el) const;
00144     ///< Check to see if node 'el' is a member of the Set.  Return
00145     ///< -1 if the element was not found otherwise return its index.
00146 
00147     INLINE void     clear();
00148     ///< Delete the entire set.
00149 
00150     INLINE          Set<T> & operator = (const Set<T> &l);
00151     ///< Copy operator completely copies a Set.
00152 
00153     INLINE int      structures_OK() const;
00154     ///< Check the structure of the data for errors or inconsistency
00155     ///< Return 0 and print error message if problems found, otherwise
00156     ///< return 1 without message.
00157 
00158     INLINE Listable *listable_clone() const;
00159     ///< Needed for Listable class.
00160 
00161     INLINE void      print(ostream &o) const;
00162     ///< Needed for Listable class.
00163 };
00164 
00165 
00166 ///< implementation
00167 
00168 template <class T>
00169 INLINE          
00170 Set<T>::Set()
00171 {
00172     ///< nothing to do
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     ///< nothing to do
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       ///< Replace the old with the new
00222       _list.modify(loc, CASTAWAY(T *)new_element);
00223     }
00224     else {
00225       ///< Simply insert at end (is not already in set)
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 ///< This interface is for testing purposes only.  It invokes a non-virtual
00356 ///< member function to print a representation of the data structure at this
00357 ///< level of the Collection hierarchy, and, since it is not a class member
00358 ///< function, it must be explicitly instantiated before use.
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
 © 1995-2005 University of Illinois, Urbana-Champaign. All rights reserved.  Fri Mar 25 23:06:06 2005