Polaris: Database.h Source File

Database.h

Go to the documentation of this file.
00001 #ifndef _DATABASE_H
00002 #define _DATABASE_H
00003 ///
00004 ///
00005 ///
00006 /// \class Database 
00007 /// \brief 
00008 ///
00009 /// \endcode
00010 /// \section DESCRIPTION DESCRIPTION
00011 /// Database<key,data>
00012 /// owns(data), value(key)
00013 ///
00014 /// A template for a database for keys of type S
00015 /// to owned objects of type T where:
00016 ///
00017 /// \code
00018 /// 1.  'S' is a class with:
00019 ///
00020 ///     a) <, == and << operators taking S& arguments.
00021 ///     b) a copy constructor S(S &) to clone the
00022 ///        input object or simple enough structure
00023 ///        for the default copy constructor to work.
00024 ///
00025 /// 2.  'T' is a class derived from Listable.
00026 /// \endcode
00027 ///
00028 /// The database is implemented as a tree of pointers to
00029 /// whatever type is specified.  Thus, all methods to
00030 /// insert elements into a database take a reference to the
00031 /// key and a pointer to the data and do not allocate space. 
00032 /// (i.e., the database aliases whatever is inserted).  However,
00033 /// the database takes over ownership of the data on insert
00034 /// and will delete only a map element and not its associated key.
00035 ///
00036 #include "ProtoDatabase.h"
00037 ///
00038 template <class S, class T> class Database : public ProtoDatabase<S, T> {
00039  protected:
00040     virtual INLINE void    _kill_key(BMKey &key);
00041     ///< kill key (in this class, kill the owned key copy)
00042 
00043  public:
00044     INLINE Database();
00045     INLINE Database(const Database<S, T> &m);
00046 
00047     virtual INLINE ~Database();
00048 
00049     INLINE T        &ins(const S &key, T *data);
00050     ///< Insert element with (key, data) into the database, and
00051     ///< return a reference to '*data' (for purposes of
00052     ///< programming convenience).
00053     ///< If a node with the equivalent key already exists,
00054     ///< delete the old one and replace it with the new one.
00055 
00056     INLINE void     del(S &del_key);
00057     ///< delete element with key del_key
00058 
00059     INLINE T       *grab(const S &del_key);
00060     ///< grab an element with key del_key
00061 
00062     INLINE void     print(ostream & out, char *sep1, char *sep2) const;
00063     ///< print map to out with sep1 between key and data and sep2 between each
00064     ///< element of the map
00065 
00066     INLINE          Database<S, T> & operator = (const Database<S, T> &l);
00067     ///< Copy operator completely copies the Database
00068 
00069     INLINE Listable *listable_clone() const;
00070     ///< Needed for Listable class.
00071 
00072     INLINE void      print(ostream &o) const;
00073     ///< Needed for Listable class.
00074 };
00075 
00076 #include "KeyIterator.h"
00077 
00078 
00079 ///< implementation
00080 
00081 template <class S, class T>
00082 INLINE void
00083 Database<S,T>::_kill_key(BMKey & key)
00084 {
00085   ///< Kill the owned copy of the original key
00086 
00087   S   *key_ptr = (S *) key._keyptr;
00088   delete key_ptr;
00089 }
00090 
00091 template <class S, class T>
00092 INLINE
00093 Database<S, T>::Database()
00094 {
00095     ///< nothing to do
00096 }
00097 
00098 template <class S, class T>
00099 INLINE
00100 Database<S, T>::Database(const Database<S, T> &m)
00101 {
00102     ///< Invoke copy constructor
00103     (*this) = m;
00104 }
00105 
00106 template <class S, class T>
00107 INLINE
00108 Database<S, T>::~Database()
00109 {
00110     ///< Delete the information before the class disappears.
00111     clear();
00112 }
00113 
00114 template <class S, class T>
00115 INLINE void 
00116 Database<S,T>::print(ostream & out, char *sep1, char *sep2) const
00117 {
00118     out << "{";
00119 
00120     for (KeyIterator<S,T> map_iter = *this; map_iter.valid(); ++map_iter) {
00121         BMKey printkey;
00122         printkey._keyptr = (void *) &map_iter.current_key();
00123         _print(out, printkey);
00124         out << sep1 << map_iter.current_data() << sep2;
00125     }
00126 
00127     out << "}";
00128 }
00129 
00130 template <class S, class T>
00131 INLINE T &
00132 Database<S, T>::ins(const S &key, T *data)
00133 {
00134     BMKey           inskey;
00135 
00136     inskey._keyptr = (void *) new S(key);
00137 
00138     T *exists = (T *) BaseMapRoot::remove(inskey);
00139 
00140     if (exists) 
00141       delete exists;
00142     
00143     BaseMap::ins(inskey, data);
00144 
00145     return CASTAWAY(T &) *data;
00146 }
00147 
00148 template <class S, class T>
00149 INLINE void 
00150 Database<S, T>::del(S &key)
00151 {
00152     BMKey           delkey;
00153     T              *el;
00154 
00155     delkey._keyptr = (void *) &key;
00156 
00157     el = (T *) BaseMapRoot::remove(delkey);
00158 
00159     p_assert(el, "Database<S,T>::del(): No node found with specified key");
00160 
00161     delete el;
00162 }
00163 
00164 template <class S, class T>
00165 INLINE T *   
00166 Database<S, T>::grab(const S &key)
00167 {
00168     BMKey           delkey;
00169 
00170     delkey._keyptr = (void *) &key;
00171 
00172     return (T *) BaseMapRoot::remove(delkey);
00173 }
00174 
00175 template <class S, class T>
00176 INLINE Database<S, T> &
00177 Database<S, T>::operator = (const Database<S, T> &m) 
00178 {
00179     ///< Clear out current map
00180     clear();
00181 
00182     ///< Copy all the items one by one
00183     for (KeyIterator<S, T> map_iter = m; map_iter.valid(); ++map_iter) 
00184         ins( map_iter.current_key(), 
00185             (T *) map_iter.current_data().listable_clone());
00186 
00187     return *this;
00188 }
00189 
00190 template <class S, class T>
00191 INLINE Listable *
00192 Database<S, T>::listable_clone() const
00193 {
00194     return (Listable *) new Database<S,T>( *this );
00195 }
00196 
00197 template <class S, class T>
00198 INLINE void
00199 Database<S, T>::print(ostream &o) const
00200 {
00201     print( o, ": ", ", " );
00202 }
00203 
00204 ///< This interface is for testing purposes only.  It invokes a non-virtual
00205 ///< member function to print a representation of the data structure at this
00206 ///< level of the Collection hierarchy, and, since it is not a class member
00207 ///< function, it must be explicitly instantiated before use.
00208 
00209 template <class S, class T>
00210 ostream &
00211 operator << (ostream & o, const Database<S, T> &m)
00212 {
00213   m.print(o, ": ", ", ");
00214   return o;
00215 }
00216 
00217 #endif
 © 1995-2005 University of Illinois, Urbana-Champaign. All rights reserved.  Fri Mar 25 23:05:43 2005