Polaris: ProtoMap.h Source File

ProtoMap.h

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