Polaris: RefList.h Source File

RefList.h

Go to the documentation of this file.
00001 ///
00002 ///
00003 #ifndef _REF_LIST_H
00004 #define _REF_LIST_H
00005 ///
00006 /// \class RefList 
00007 /// \brief template for types derived from Listable
00008 /// \defgroup Polaris
00009 /// \ingroup Polaris
00010 ///  C++ VDL
00011 /// \see RefList.h
00012 ///
00013 /// \endcode
00014 /// \section Overview Overview
00015 /// The RefList class creates a templatized linked-list object for
00016 /// references to objects which must be derived from Listable. Since the
00017 /// RefList is built from Collection it works with Iterators. An object
00018 /// must be in a live structure, like List, before it can be used in a
00019 /// reference structure, like RefList.
00020 ///
00021 /// \endcode
00022 /// \section DESCRIPTION DESCRIPTION
00023 /// All objects in a RefList must exist in some other
00024 /// live structure.  If this is not the case, an error
00025 /// is raised upon insertion.  If the live object is
00026 /// removed from its owner-structure, references to it
00027 /// within the RefList will become invalid.
00028 ///
00029 /// RefList is subject to the restrictions of the
00030 /// Iterators--that is, when a wrapper is taken out of
00031 /// the RefList, only the pointers of the neighboring
00032 /// wrappers are changed--the out-going pointers are
00033 /// maintained (ie, _unlink_wrapper is called).
00034 ///
00035 /// It is possible to have more than one reference to a
00036 /// single object within a RefList.  This could cause
00037 /// confusion in terms of how the methods behave.  If an
00038 /// object (it's address) is used as a reference point for
00039 /// a method and more than one exist, the first one
00040 /// encountered will be operated on.
00041 ///
00042 /// \endcode
00043 /// \section Hierarchy Hierarchy Structure
00044 /// RefList does not inherit from Collection, instead it contains
00045 /// a BaseRefList (which does) and inherits from TypedCollection
00046 /// (which provides an interface with the Iterators).
00047 /// The hierarchy can be represented by:
00048 ///
00049 /// \code
00050 ///                     --- :    inherited from relationship
00051 ///                     -c- :    contained in relationship
00052 ///
00053 ///
00054 ///                         Collection
00055 ///                        /          \                                          //
00056 ///                       /            \                                         //
00057 ///               BaseList   Listable   BaseRefList
00058 ///               |  \          |             /  |
00059 ///               |   \         |            /   |
00060 ///               |    c  TypedCollection   c    |
00061 ///               |     \     /     \      /     |
00062 ///               |      \   /       \    /      |
00063 ///               |      LIST        RefList     |
00064 ///               |      Set         RefSet      |
00065 ///               c                              c
00066 ///               |          Listable            |
00067 ///               |             |                |
00068 ///               |             |                |
00069 ///               |        BaseMapRoot           |
00070 ///               |       /           \          |
00071 ///               |      /             \         |
00072 ///               BaseMap               BaseRefMap
00073 ///                  |                       |
00074 ///                  |                       |                            
00075 ///             TypedBaseMap          TypedBaseRefMap
00076 ///               /     \                /         \                             //
00077 ///              /       \              /           \                            //
00078 ///    ProtoDatabase      ProtoMap   ProtoRefMap    ProtoRefDatabase
00079 ///         /  \           |    |      |     |         |         \               //
00080 ///        /    \          |    |      |     |         |          \              //
00081 /// Database KeyDatabase  Map KeyMap  RefMap KeyMap  RefDatabase RefKeyDatabase
00082 /// \endcode
00083 ///
00084 /// \endcode
00085 /// \section Bugs Bugs
00086 /// Currently, RefLists are not equipped to be static.
00087 ///
00088 /// \endcode
00089 /// \section See See Also
00090 /// Collection, Listable, RefWrapper, Iterator, TypedCollection, Zombie,
00091 /// BaseRefList, RefSet
00092 ///
00093 ///
00094 #include <stream.h>
00095 ///
00096 #include "BaseRefList.h"
00097 #include "TypedCollection.h"
00098 ///
00099 template <class T> class RefList : public TypedCollection<T> {
00100     friend class Iterator <T>;
00101     friend class Mutator <T>;
00102 
00103     ///< gcc-2.96 need the stupid "<>" pair
00104     ///< friend ostream & operator << (ostream & o, const RefList<T> &l);
00105     friend ostream & operator << <> (ostream & o, const RefList<T> &l);
00106 
00107  protected:
00108     BaseRefList _list;
00109     virtual INLINE const Collection & _base_collection() const;
00110     virtual INLINE Collection & _base_collection();
00111 
00112  public:
00113     INLINE          RefList();
00114     INLINE          RefList(const RefList<T >&l);
00115     virtual INLINE  ~RefList();
00116 
00117     INLINE int      entries() const;
00118 
00119     INLINE const T &operator[] (int sub) const;
00120     INLINE T       &operator[] (int sub);
00121     ///< Implements the subscript operator (indexed from 0).
00122     ///< If the subth element is not valid, an error is
00123     ///< raised.
00124     ///< Should be used for random access only.
00125 
00126     INLINE void     print(ostream & out, char *sep) const;
00127     ///< print list to out with 'sep' sepperating each element
00128 
00129     INLINE void     modify(int loc, T &el);
00130     ///< change the reference at loc to a reference to el.
00131 
00132     INLINE void     ins(const T &new_element, int loc);
00133     ///< Insert new_element at location loc, where 0 <= loc < entries().
00134 
00135     INLINE T &      grab();
00136     ///< Remove a node and returns it's reference.
00137 
00138     INLINE T &      grab(T &el);
00139     ///< Remove a node and returns it's reference.
00140 
00141     INLINE T &      grab(int loc);
00142     ///< Remove a node and returns it's reference.
00143 
00144     INLINE void     del(T &el);
00145     ///< Delete node 'el'.  el MUST be in the list (this is not
00146     ///< necessarily checked).
00147 
00148     INLINE void     del(T &el, int loc);
00149     ///< Delete first occurence of node 'el' at or beyond locth position.
00150     ///< el MUST be in the list (this is not necessarily checked).
00151 
00152     INLINE void     del(int loc);
00153     ///< Random Access Delete.
00154     ///< Delete the Node in position loc where 0 <= loc < entries()-1.
00155 
00156     INLINE Boolean  member(const T &el) const;
00157     ///< Check to see if node 'el' is a member of the RefList.  Return
00158     ///< 0 if the element was not found, else return 1.
00159 
00160     INLINE int      index(const T &el) const;
00161     ///< Check to see if node 'el' is a member of the RefList.  Return
00162     ///< -1 if the element was not found, else return the index of the element.
00163 
00164     INLINE Boolean  valid(int loc) const;
00165     ///< Check to see if the locth element is valid.
00166 
00167     INLINE Boolean  valid(const T &el) const;
00168     ///< Check to see if the first occurence of T in the RefList is valid.
00169 
00170     INLINE void     ins_first(const T &new_element);
00171     ///< Prepend new_element to the beginning of the list.
00172 
00173     INLINE void     ins_last(const T &new_element);
00174     ///< Append new_element to the end of the list
00175 
00176     INLINE void     ins_before(const T &new_element, const T &ref);
00177     ///< Insert new_element before the reference element 'ref'
00178     ///< (if ref == 0, insert at END of list).
00179     ///< ref MUST be in the list (this is not necessarily
00180     ///< checked).
00181 
00182     INLINE void     ins_after(const T &new_element, const T &ref);
00183     ///< Insert new_element after the reference element 'ref'
00184     ///< (if ref == 0, insert at BEGINNING of list).
00185     ///< ref MUST be in the list (this is not necessarily
00186     ///< checked).
00187 
00188     INLINE void     clear();
00189     ///< Delete the entire list.
00190 
00191     INLINE          RefList<T >&operator = (const RefList<T >&l);
00192     ///< Copy operator copies all the valid elements of a RefList.
00193 
00194     INLINE int      structures_OK() const;
00195     ///< Check the structure of the data for errors or inconsistency.
00196     ///< Return 0 and print error message if problems found, otherwise
00197     ///< return 1 without message.
00198 
00199     INLINE Listable *listable_clone() const;
00200     ///< Needed for Listable class.
00201 
00202     INLINE void      print(ostream &o) const;
00203     ///< Needed for Listable class.
00204 };
00205 
00206 
00207 ///< implementation
00208 
00209 
00210 template <class T>
00211 INLINE 
00212 RefList<T>::RefList()
00213 {
00214     ///< nothing to do
00215 }
00216 
00217 template <class T>
00218 INLINE 
00219 RefList<T>::RefList(const RefList<T >&l)
00220 {
00221     _list = l._list;
00222 }
00223 
00224 template <class T>
00225 INLINE
00226 RefList<T>::~RefList()
00227 {
00228     ///< nothing to do
00229 }
00230 
00231 template <class T>
00232 INLINE int
00233 RefList<T>::entries() const
00234 {
00235     return _list.entries();
00236 }
00237 
00238 template <class T>
00239 INLINE const T &
00240 RefList<T>::operator[] (int sub) const
00241 {
00242     return (const T &) _list[sub];
00243 }
00244 
00245 template <class T>
00246 INLINE T &
00247 RefList<T>::operator[] (int sub)
00248 {
00249     return (T &) _list[sub];
00250 }
00251 
00252 template <class T>
00253 INLINE void
00254 RefList<T>::print(ostream & out, char *sep) const
00255 {
00256     _list.print(out, sep);
00257 }
00258 
00259 template <class T>
00260 INLINE void
00261 RefList<T>::modify(int loc, T &el)
00262 {
00263     _list.modify( loc, el );
00264 }
00265 
00266 template <class T>
00267 INLINE void 
00268 RefList<T>::ins(const T &new_element, int loc)
00269 {
00270     _list.ins( new_element, loc );
00271 }
00272 
00273 template <class T>
00274 INLINE void
00275 RefList<T>::del(int loc)
00276 {
00277     _list.del(loc);
00278 }
00279 
00280 template <class T>
00281 INLINE void
00282 RefList<T>::del(T &el, int loc)
00283 {
00284     _list.del( el, loc );
00285 }
00286 
00287 template <class T>
00288 INLINE void     
00289 RefList<T>::del(T &el)
00290 {
00291     _list.del( el, 0 );
00292 }
00293 
00294 template <class T>
00295 INLINE T & 
00296 RefList<T>::grab()
00297 {
00298     return grab(0);
00299 }
00300 
00301 template <class T>
00302 INLINE T & 
00303 RefList<T>::grab(int loc)
00304 {
00305     T &el = (*this)[loc];
00306     this->del(loc);
00307     return el;
00308 }
00309 
00310 template <class T>
00311 INLINE T &
00312 RefList<T>::grab(T &el)
00313 {
00314     this->del(el,0);
00315     return el;
00316 }
00317 
00318 template <class T>
00319 INLINE int      
00320 RefList<T>::index(const T &el) const
00321 {
00322     return _list.index( el );
00323 }
00324 
00325 template <class T>
00326 INLINE Boolean  
00327 RefList<T>::member(const T &el) const
00328 {
00329     return _list.member( el );
00330 }
00331 
00332 template <class T>
00333 INLINE Boolean  
00334 RefList<T>::valid(int loc) const
00335 {
00336     return _list.valid(loc);
00337 }
00338 
00339 template <class T>
00340 INLINE Boolean  
00341 RefList<T>::valid(const T &el) const
00342 {
00343     return _list.valid( el );
00344 }
00345 
00346 template <class T>
00347 INLINE void     
00348 RefList<T>::ins_first(const T &new_element)
00349 {
00350     _list.ins( new_element, 0 );
00351 }
00352 
00353 template <class T>
00354 INLINE void     
00355 RefList<T>::ins_last(const T &new_element)
00356 {
00357     _list.ins( new_element, _list.entries() );
00358 }
00359 
00360 template <class T>
00361 INLINE void     
00362 RefList<T>::ins_before(const T &new_element, const T &ref)
00363 {
00364     _list.ins_before( new_element,  ref );
00365 }
00366 
00367 template <class T>
00368 INLINE void     
00369 RefList<T>::ins_after(const T &new_element, const T &ref)
00370 {
00371     _list.ins_after( new_element,  ref );
00372 }
00373 
00374 template <class T>
00375 INLINE void     
00376 RefList<T>::clear()
00377 {
00378     _list.clear();
00379 }
00380 
00381 
00382 template <class T>
00383 INLINE RefList<T> &
00384 RefList<T>::operator = (const RefList<T >&l) 
00385 {
00386     _list = l._list;
00387     return *this;
00388 }
00389 
00390 template <class T>
00391 INLINE int      
00392 RefList<T>::structures_OK() const
00393 {
00394     return _list.structures_OK();
00395 }
00396 
00397 template <class T>
00398 INLINE const Collection &
00399 RefList<T>::_base_collection() const
00400 {
00401     return _list;
00402 }
00403 
00404 template <class T>
00405 INLINE Collection &
00406 RefList<T>::_base_collection()
00407 {
00408     return _list;
00409 }
00410 
00411 template <class T>
00412 INLINE Listable *
00413 RefList<T>::listable_clone() const
00414 {
00415     return (Listable *) new RefList<T>(*this);
00416 }
00417 
00418 template <class T>
00419 INLINE void
00420 RefList<T>::print(ostream &o) const
00421 {
00422     print(o, ", ");
00423 }
00424 
00425 ///< This interface is for testing purposes only.  It invokes a non-virtual
00426 ///< member function to print a representation of the data structure at this
00427 ///< level of the Collection hierarchy, and, since it is not a class member
00428 ///< function, it must be explicitly instantiated before use.
00429 
00430 template <class T>
00431 ostream &
00432 operator << (ostream & o, const RefList<T> &l)
00433 {
00434   l.print(o, ", ");
00435   return o;
00436 }
00437 
00438 #endif
 © 1995-2005 University of Illinois, Urbana-Champaign. All rights reserved.  Fri Mar 25 23:06:05 2005