Main Page   Class Hierarchy   Compound List   File List   Compound Members   Related Pages  

avlmap.h

00001 //
00002 // avlmap.h --- definition for avl map class
00003 //
00004 // Copyright (C) 1998 Limit Point Systems, Inc.
00005 //
00006 // Author: Curtis Janssen <cljanss@limitpt.com>
00007 // Maintainer: LPS
00008 //
00009 // This file is part of the SC Toolkit.
00010 //
00011 // The SC Toolkit is free software; you can redistribute it and/or modify
00012 // it under the terms of the GNU Library General Public License as published by
00013 // the Free Software Foundation; either version 2, or (at your option)
00014 // any later version.
00015 //
00016 // The SC Toolkit is distributed in the hope that it will be useful,
00017 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00018 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00019 // GNU Library General Public License for more details.
00020 //
00021 // You should have received a copy of the GNU Library General Public License
00022 // along with the SC Toolkit; see the file COPYING.LIB.  If not, write to
00023 // the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
00024 //
00025 // The U.S. Government is granted a limited license as per AL 91-7.
00026 //
00027 
00028 #ifndef _util_container_avlmap_h
00029 #define _util_container_avlmap_h
00030 
00031 #include <util/container/eavlmmap.h>
00032 
00033 template <class K, class T>
00034 class AVLMapNode {
00035   public:
00036     T data;
00037     EAVLMMapNode<K,AVLMapNode<K, T> > node;
00038   public:
00039     AVLMapNode(const K& k, const T& d): data(d), node(k) {};
00040 };
00041 
00042 template <class K, class T>
00043 class AVLMap {
00044   public:
00045     EAVLMMap<K, AVLMapNode<K,T> > map_;
00046   public:
00047     class iterator {
00048       private:
00049         const EAVLMMap<K, AVLMapNode<K,T> > *map_;
00050         AVLMapNode<K, T> *node;
00051       public:
00052         iterator(): map_(0), node(0) {}
00053         iterator(const EAVLMMap<K,AVLMapNode<K,T> > *m,
00054                  AVLMapNode<K,T> *n)
00055           :map_(m), node(n) {}
00056         iterator(const eavl_typename AVLMap<K,T>::iterator &i) { map_=i.map_; node=i.node; }
00057         void operator++() { map_->next(node); }
00058         void operator++(int) { operator++(); }
00059         int operator == (const eavl_typename AVLMap<K,T>::iterator &i) const
00060             { return map_ == i.map_ && node == i.node; }
00061         int operator != (const eavl_typename AVLMap<K,T>::iterator &i) const
00062             { return !operator == (i); }
00063         void operator = (const eavl_typename AVLMap<K,T>::iterator &i)
00064             { map_ = i.map_; node = i.node; }
00065         const K &key() const { return node->node.key; }
00066         T &data() { return node->data; }
00067     };
00068   public:
00069     AVLMap(): map_(&AVLMapNode<K,T>::node) {};
00070     void clear() { map_.clear(); }
00071     void insert(const K& key, const T& data);
00072     void remove(const K& key);
00073     int contains(const K& k) const { return map_.find(k) != 0; }
00074     iterator find(const K&) const;
00075     T &operator[](const K &k);
00076 
00077     int height() { return map_.height(); }
00078     void check() { map_.check(); }
00079 
00080     int length() const { return map_.length(); }
00081 
00082     iterator begin() const { return iterator(&map_,map_.start()); }
00083     iterator end() const { return iterator(&map_,0); }
00084 
00085     void print() { map_.print(); }
00086 };
00087 
00088 template <class K, class T>
00089 inline void
00090 AVLMap<K,T>::insert(const K& key, const T& data)
00091 {
00092   AVLMapNode<K,T> *node = map_.find(key);
00093   if (node) node->data = data;
00094   else map_.insert(new AVLMapNode<K, T>(key,data));
00095 }
00096 
00097 template <class K, class T>
00098 inline void
00099 AVLMap<K,T>::remove(const K& key)
00100 {
00101   AVLMapNode<K, T> *node = map_.find(key);
00102   if (node) {
00103       map_.remove(node);
00104       delete node;
00105     }
00106 }
00107 
00108 template <class K, class T>
00109 inline typename AVLMap<K,T>::iterator
00110 AVLMap<K,T>::find(const K& k) const
00111 {
00112   return iterator(&map_,map_.find(k));
00113 }
00114 
00115 template <class K, class T>
00116 inline T&
00117 AVLMap<K,T>::operator [](const K& k)
00118 {
00119   AVLMapNode<K, T> *node = map_.find(k);
00120   if (node) return node->data;
00121   insert(k,T());
00122   node = map_.find(k);
00123   return node->data;
00124 }
00125 
00126 #endif
00127 
00128 // /////////////////////////////////////////////////////////////////////////
00129 
00130 // Local Variables:
00131 // mode: c++
00132 // c-file-style: "CLJ"
00133 // End:

Generated at Thu Oct 4 18:08:42 2001 for MPQC 2.0.0 using the documentation package Doxygen 1.2.5.