Polaris: BaseMapRoot.h Source File

BaseMapRoot.h

Go to the documentation of this file.
00001 ///
00002 ///
00003 #ifndef _BASE_MAP_ROOT_H
00004 #define _BASE_MAP_ROOT_H
00005 ///
00006 /// \class BaseMapRoot 
00007 /// \brief The underlying tree structure for Maps
00008 /// \defgroup Polaris
00009 /// \ingroup Polaris
00010 ///  C++ VDL
00011 /// \see BaseMapNode.h
00012 /// \see BaseMapRoot.h
00013 /// \see BaseMapRoot.cc
00014 ///
00015 /// \endcode
00016 /// \section Overview Overview
00017 /// BaseMapRoot is an abstract base class for a map of keys
00018 /// to objects of type Listable.  The internal nodes and
00019 /// descriptions of the key objects are described in BaseMapNode.h.
00020 /// BaseMapRoot is the base of a wide variety of Maps and
00021 /// Databases with full functionality, however, this class can
00022 /// not be used on its own.  Derived classes can be used as
00023 /// follows:
00024 ///
00025 /// \code
00026 /// 1.  BaseMapRoot should be used to define other BaseMap classes
00027 ///     (BaseMap & BaseRefMap) which will define _list_remove and
00028 ///     _list_insert.
00029 ///
00030 /// 2.  The pure virtual functions _kill_key, _less, _equal and _print have
00031 ///     been defined in the class derived from BaseMapRoot.
00032 /// \endcode
00033 ///
00034 /// \endcode
00035 /// \section Not Not Really a Collection
00036 /// BaseMapRoot is not really a part of the Collection hierarchy in the
00037 /// strictest sense.  Instead it is a tree implementation which is fairly
00038 /// independent of the world of Collections.  It only inherits from Listable
00039 /// to allow all Maps to be placed in Collections. The Map classes only
00040 /// really join the Collection structure when BaseMap and BaseRefMap inherit
00041 /// from BaseMapRoot and also contain an internal Collection strucutre which
00042 /// is used to keep track of elements.
00043 ///
00044 /// \endcode
00045 /// \section Hierarchy Hierarchy Structure
00046 /// BaseMapRoot is the base of a long inheritance chain which eventually
00047 /// results in usable classes.  The hierarchy can be modeled as follows:
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 References References
00086 /// The routines used in this class are derived from algorithms given in
00087 /// pseudocode form in Chapters 13 and 14 of "Introduction to Algorithms"
00088 /// by Cormen, Leiserson and Rivest.
00089 ///
00090 /// \endcode
00091 /// \section See See Also
00092 /// BaseMapNode, Listable, BaseMap, BaseRefMap
00093 ///
00094 #ifdef POLARIS_GNU_PRAGMAS
00095 #pragma interface
00096 #endif
00097 ///
00098 #include "../Listable.h"
00099 #include "../Boolean.h"
00100 #include "Wrapper.h"
00101 #include "BaseMapNode.h"
00102 ///
00103 enum BM_NODE_TYPE {     /// type of base map node
00104     NONROOT = 0,        /// node contains a parent pointer
00105     ROOT    = 1         /// node contains a map pointer
00106 };
00107 ///
00108 enum BASE_MAP_POS {     /// insertion position of node from ptr
00109     TOP,                /// node should be inserted as root
00110     LEFT,               /// node should be inserted as lc of ptr
00111     RIGHT,              /// node should be inserted as rc of ptr
00112     MATCH               /// matching node pointed to by ptr
00113 };
00114 ///
00115 struct BMPosition {         /// position of node
00116     BASE_MAP_POS  _type;    /// type of position
00117     BMNode       *_ptr;     /// pointer to match or parent
00118 };
00119 ///
00120 class BaseMapIter;
00121 
00122 class BaseMapRoot : public Listable {
00123  private:
00124     int             _entries;   ///< number of entries in map
00125     BMNode         *_root;      ///< ptr to root (sentinel if empty tree)
00126     BMNode         *_sentinel;  ///< pointer to sentinel
00127 
00128     Boolean         _own_key;   ///< Are the keys owned by the map?
00129 
00130     friend class         BaseMapIter;
00131     friend          ostream & operator << (ostream &o, const BMNode &node);
00132     ///<
00133 
00134     Boolean         _check_node(const BMNode *node, const BMNode *parent_node,
00135     /*&*/                       int black_height) const;
00136     ///< check node and children for testing purposes
00137 
00138     void            _kill_node(BMNode *node);
00139     ///< kill node and children (this also kills data)
00140 
00141     inline Boolean  _leaf(const BMNode *z) const;
00142     ///< find if leaf node
00143 
00144     void            _left_rotate(BMNode *x);
00145     ///< perform left rotation
00146 
00147     BMNode         *_minimum(const BMNode *z) const;
00148     ///< find a minimum node
00149 
00150     BMNode         *_maximum(const BMNode *z) const;
00151     ///< find a minimum node
00152 
00153     void            _print_node(ostream &o, const BMNode &node) const;
00154     ///< print node
00155 
00156     void            _right_rotate(BMNode *x);
00157     ///< perform right rotation
00158 
00159  protected:
00160     BMNode         *_successor(const BMNode *z) const;
00161     ///< find successor node
00162 
00163     void            _position(const BMKey &pos_key, BMPosition &pos) const;
00164     ///< set position to node with key pos_key
00165 
00166     void            _first(BMPosition &first) const;
00167     ///< set position to first node
00168 
00169     void            _last(BMPosition &last) const;
00170     ///< set position to last node
00171 
00172     virtual void    _list_insert(BMNode *z, Listable *new_data) = 0;
00173     ///< Insert new_data into the internal list. Set z's _data field
00174     ///< to point to new_data's wrapper.
00175 
00176     virtual void   *_list_remove(Wrapper *rem_wrap);
00177     ///< Remove node y's data from the internal list.
00178 
00179     virtual void    _list_delete(Wrapper *del_wrap);
00180     ///< Delete node y's data from the internal list.
00181 
00182     virtual void    _kill_key(BMKey &key);
00183     ///< kill key
00184 
00185     virtual void    _kill_data(void *data);
00186     ///< kill data 
00187 
00188     virtual Boolean _less(const BMKey &key1, const BMKey &key2) const = 0;
00189     ///< less than compare
00190 
00191     virtual Boolean _equal(const BMKey &key1, const BMKey &key2) const = 0;
00192     ///< equal compare
00193 
00194     virtual void    _print(ostream &o, const BMKey &key) const = 0;
00195     ///< print map node
00196 
00197     inline Boolean  _owned_keys() const;
00198     ///< Are the keys owned by the map?
00199 
00200     inline void     _set_owned_keys(Boolean truth);
00201     ///< Set the above condition.
00202 
00203     inline const BMKey *_arb_bmkey_ref() const;
00204     ///< return an arbitrary node in the tree, or 0 if the tree is empty
00205 
00206  public:
00207     friend          ostream & operator << (ostream &o, const BaseMapRoot &map);
00208     ///< For testing purposes only
00209 
00210     BaseMapRoot();
00211     virtual         ~BaseMapRoot();
00212 
00213     inline int      entries() const;
00214     ///< Return the number of elements in the BaseMap.
00215 
00216     void            *key_ref( Listable *data ) const;
00217     ///< Return a pointer to data's key or 0 if none.
00218 
00219     int             ins(BMKey &new_key, Listable *new_data);
00220     ///< Insert a node with key new_key and data new_data.
00221     ///< If a node already exists with an equivalent key, do nothing
00222     ///< and return 0, otherwise insert and return 1.
00223 
00224     void           *remove(BMKey &del_key);
00225     ///< delete a node with key del_key, if such a node is present,
00226     ///< otherwise do nothing and return 0
00227 
00228     Listable       *find_ref(const BMKey &find_key) const;
00229     ///< find data of node with key of find_key, or 0 if no such node
00230 
00231     Boolean         member(const BMKey &mem_key) const;
00232     ///< Return a flag indicating if there is a node with key mem_key
00233 
00234     inline void     clear();
00235     ///< clear the entire map
00236 
00237     int             structures_OK() const;
00238     ///< Check the structure of the data for errors or inconsistency
00239     ///< Return 0 and print error message if problems found, otherwise
00240     ///< return 1 without message.
00241 
00242 };
00243 
00244 
00245 ///< implementation
00246 
00247 ///< Find if leaf node
00248 
00249 inline Boolean
00250 BaseMapRoot::_leaf(const BMNode *z) const
00251 {
00252     return (z == _sentinel);
00253 }
00254 
00255 ///< Clear out map
00256 
00257 inline void 
00258 BaseMapRoot::clear()
00259 {
00260     if (_entries>0) {
00261         _kill_node(_root);
00262         _root = _sentinel;
00263     }
00264 }
00265 
00266 ///< Return number of entries
00267 
00268 inline int 
00269 BaseMapRoot::entries() const
00270 {
00271     return _entries;
00272 }
00273 
00274 inline Boolean
00275 BaseMapRoot::_owned_keys() const
00276 {
00277     return _own_key;
00278 }
00279 
00280 inline void
00281 BaseMapRoot::_set_owned_keys(Boolean truth)
00282 {
00283     _own_key = truth;
00284 }
00285 
00286 inline const BMKey *
00287 BaseMapRoot::_arb_bmkey_ref() const
00288 {
00289     if (entries() == 0)
00290         return 0;
00291 
00292     return (_leaf(_root) ? 0 : &_root->_key);
00293 }
00294 
00295 #endif
 © 1995-2005 University of Illinois, Urbana-Champaign. All rights reserved.  Fri Mar 25 23:05:40 2005