Polaris: ProtoDatabase.h Source File

ProtoDatabase.h

Go to the documentation of this file.
00001 ///
00002 ///
00003 #ifndef _PROTO_DATABASE_H
00004 #define _PROTO_DATABASE_H
00005 ///
00006 /// \class ProtoDatabase 
00007 /// \brief 
00008 ///
00009 /// \endcode
00010 /// \section DESCRIPTION DESCRIPTION
00011 /// A template for a map for owned keys of type S
00012 /// to objects of type T where:
00013 ///
00014 /// \code
00015 /// 1.  'S' is a class with:
00016 /// a) <, == and << operators taking S& arguments.
00017 /// b) a copy constructor S(S &) to clone the
00018 /// input object or simple enough structure
00019 /// for the default copy constructor to work.
00020 ///
00021 /// 2.  'T' is a class derived from Listable.
00022 /// \endcode
00023 ///
00024 /// The map is implemented as a tree of pointers to
00025 /// whatever type is specified.  Thus, all methods to
00026 /// insert elements into a map take a pointer to the
00027 /// key and data and do not allocate space. (i.e., the
00028 /// map aliases whatever is in the list).  However,
00029 /// the map takes over ownership of the keys on insert
00030 /// and will delete both a map element and its associated key.
00031 ///
00032 #include "TypedBaseMap.h"
00033 ///
00034 template <class S, class T> class ProtoDatabase : public TypedBaseMap<S,T> {
00035  protected:
00036     virtual INLINE Boolean _less(const BMKey &key1, const BMKey &key2) const;
00037     ///< less than compare
00038 
00039     virtual INLINE Boolean _equal(const BMKey &key1, const BMKey &key2) const;
00040     ///< equal compare
00041 
00042     virtual INLINE void    _print(ostream &o, const BMKey &key) const;
00043     ///< print map entry
00044 
00045  public:
00046     INLINE ProtoDatabase();
00047     INLINE ProtoDatabase(const ProtoDatabase<S, T> &m);
00048 
00049     virtual INLINE ~ProtoDatabase();
00050 
00051     INLINE const T *find_ref(const S &key) const;
00052     INLINE T       *find_ref(const S &key);
00053     ///< find data of element with key, or 0 if no such element
00054 
00055     INLINE Boolean  member(const S &key) const;
00056     ///< Return a flag indicating if there is an element with key.
00057 
00058     INLINE const T &operator[] (const S &key) const;
00059     INLINE T       &operator[] (const S &key);
00060     ///< Return a reference to the data of the element with key.
00061 };
00062 
00063 
00064 ///< implementation
00065 
00066 
00067 template <class S, class T>
00068 INLINE Boolean
00069 ProtoDatabase<S, T>::_less(const BMKey &key1, const BMKey &key2) const
00070 {
00071     const S * key1_ptr = (S *) key1._keyptr;
00072     const S * key2_ptr = (S *) key2._keyptr;
00073 
00074     return (*key1_ptr) < (*key2_ptr);
00075 }
00076 
00077 template <class S, class T>
00078 INLINE Boolean         
00079 ProtoDatabase<S, T>::_equal(const BMKey &key1, const BMKey &key2) const
00080 {
00081     const S * key1_ptr = (S *) key1._keyptr;
00082     const S * key2_ptr = (S *) key2._keyptr;
00083 
00084     return (*key1_ptr) == (*key2_ptr);
00085 }
00086 
00087 template <class S, class T>
00088 INLINE void
00089 ProtoDatabase<S, T>::_print(ostream & o, const BMKey &key) const
00090 {
00091     const S        *key_ptr = (S *) key._keyptr;
00092 
00093     o << (*key_ptr);
00094 }
00095 
00096 template <class S, class T>
00097 INLINE
00098 ProtoDatabase<S, T>::ProtoDatabase()
00099 {
00100     ///< nothing to do
00101 }
00102 
00103 template <class S, class T>
00104 INLINE
00105 ProtoDatabase<S, T>::ProtoDatabase(const ProtoDatabase<S, T> &m)
00106 {
00107     ///< Invoke copy constructor
00108     (*this) = m;
00109 }
00110 
00111 template <class S, class T>
00112 INLINE
00113 ProtoDatabase<S, T>::~ProtoDatabase()
00114 {
00115     ///< nothing to do
00116 }
00117 
00118 template <class S, class T>
00119 INLINE const T *
00120 ProtoDatabase<S, T>::find_ref(const S &key) const
00121 {
00122     BMKey           findkey;
00123 
00124     findkey._keyptr = (void *) &key;
00125 
00126     return (const T *) TypedBaseMap<S,T>::find_ref(findkey);
00127 }
00128 
00129 template <class S, class T>
00130 INLINE T *
00131 ProtoDatabase<S, T>::find_ref(const S &key)
00132 {
00133     BMKey           findkey;
00134 
00135     findkey._keyptr = (void *) &key;
00136 
00137     return (T *) TypedBaseMap<S,T>::find_ref(findkey);
00138 }
00139 
00140 template <class S, class T>
00141 INLINE Boolean
00142 ProtoDatabase<S, T>::member(const S &key) const
00143 {
00144     BMKey           memkey;
00145 
00146     memkey._keyptr = (void *) &key;
00147 
00148     return TypedBaseMap<S,T>::member(memkey);
00149 }
00150 
00151 template <class S, class T>
00152 INLINE const T &
00153 ProtoDatabase<S, T>::operator[] (const S &key) const
00154 {
00155     return *find_ref(key);
00156 }
00157 
00158 template <class S, class T>
00159 INLINE T &
00160 ProtoDatabase<S, T>::operator[] (const S &key)
00161 {
00162     return *find_ref(key);
00163 }
00164 
00165 #endif
 © 1995-2005 University of Illinois, Urbana-Champaign. All rights reserved.  Fri Mar 25 23:06:03 2005