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
00029
00030 #ifndef _hash_map_h_included
00031 #define _hash_map_h_included
00032
00033 namespace Instr {
00034
00035 template <class T1, class T2> class pair
00036 {
00037 public:
00038 typedef T1 first_type;
00039 typedef T2 second_type;
00040 T1 first;
00041 T2 second;
00042
00043 pair () : first(), second() {}
00044 pair (const T1& f, const T2& s) : first (f), second (s) {}
00045 };
00046
00047 #define mapsize 1009
00048
00049 template <class T1, class T2> struct _Hash_node;
00050 template <class T1, class T2> class hash_map;
00051
00052 template <class _Key, class _Data>
00053 struct _Hash_map_iterator
00054 {
00055 typedef _Hash_map_iterator<_Key, _Data> iterator;
00056 typedef _Hash_node<_Key, _Data> _Bucket_type;
00057 typedef hash_map<_Key, _Data> hash_map_type;
00058
00059 _Bucket_type * curr;
00060 hash_map_type * h;
00061
00062 _Hash_map_iterator () : curr (NULL), h(NULL) {}
00063 _Hash_map_iterator (_Bucket_type * b, hash_map_type * h)
00064 : curr (b), h (h) {}
00065
00066 bool operator== (const iterator& it) const
00067 {
00068 return curr == it.curr;
00069 }
00070 bool operator!= (const iterator& it) const
00071 {
00072 return curr != it.curr;
00073 }
00074 iterator& operator++ ()
00075 {
00076 _Bucket_type * old = curr;
00077 curr = curr->next;
00078 if (!curr)
00079 {
00080 int bucket_num = ((long)old->val.first) % mapsize;
00081 while (!curr && ++bucket_num < mapsize)
00082 curr = h->start[bucket_num];
00083 }
00084 return *this;
00085 }
00086 pair<_Key, _Data>& operator* ()
00087 {
00088 return curr->val;
00089 }
00090 };
00091
00092 template <class T1, class T2> struct _Hash_node
00093 {
00094 typedef pair<T1, T2> val_type;
00095 _Hash_node* next;
00096 val_type val;
00097 typename val_type::second_type second;
00098 void Set_val (val_type v)
00099 {
00100 val = v;
00101 second = v.second;
00102 }
00103 };
00104
00105 template <class _Key, class _Data> class hash_map
00106 {
00107 typedef pair<_Key, _Data> _Node;
00108 typedef _Hash_node<_Key, _Data> _Bucket_type;
00109 public:
00110 friend struct _Hash_map_iterator<_Key, _Data>;
00111 typedef _Hash_map_iterator<_Key, _Data> iterator;
00112 hash_map () { for (int i=0; i<mapsize; i++) start[i] = NULL; }
00113
00114 _Data& operator[] (const _Key& k)
00115 {
00116 long key = (long) k;
00117 int bucket_num = key % mapsize;
00118
00119 _Bucket_type * f = start[bucket_num];
00120 for (; f; f = f->next)
00121 if (f->val.first == k)
00122 return f->val.second;
00123
00124 _Bucket_type * newobj = (_Bucket_type *) malloc (sizeof (_Bucket_type));
00125 newobj->next = start[bucket_num];
00126 _Node tmp (k, _Data());
00127 newobj->Set_val (tmp);
00128 start[bucket_num] = newobj;
00129
00130 return newobj->val.second;
00131 }
00132
00133 iterator begin ()
00134 {
00135 for (int i=0; i<mapsize; ++i)
00136 if (start[i])
00137 return iterator (start[i], this);
00138 return end();
00139 }
00140
00141 iterator end ()
00142 {
00143 return iterator (NULL, this);
00144 }
00145
00146 private:
00147 _Bucket_type * start[mapsize];
00148 };
00149
00150 }
00151
00152 #endif