00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
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
00131
00132
00133