Debian Codegraph @

texk/lcdf-typetools/lcdf-typetools-src/include/lcdf/hashmap.cc

raw
1 #ifndef LCDF_HASHMAP_CC
2 #define LCDF_HASHMAP_CC
3
4 /*
5 * hashmap.{cc,hh} -- simple open-coded hash table class
6 * Eddie Kohler
7 *
8 * Copyright (c) 1999-2000 Massachusetts Institute of Technology
9 * Copyright (c) 2001-2003 International Computer Science Institute
10 *
11 * Permission is hereby granted, free of charge, to any person obtaining a
12 * copy of this software and associated documentation files (the "Software"),
13 * to deal in the Software without restriction, subject to the conditions
14 * listed in the Click LICENSE file. These conditions include: you must
15 * preserve this copyright notice, and you cannot mention the copyright
16 * holders in advertising related to the Software without their permission.
17 * The Software is provided WITHOUT ANY WARRANTY, EXPRESS OR IMPLIED. This
18 * notice is a summary of the Click LICENSE file; the license in that file is
19 * legally binding.
20 */
21
22 /* #include <lcdf/hashmap.hh> */
23
24 template <class K, class V>
25 HashMap<K, V>::HashMap()
26 : _capacity(0), _grow_limit(0), _n(0), _e(0), _default_value()
27 {
28 increase(-1);
29 }
30
31 template <class K, class V>
32 HashMap<K, V>::HashMap(const V &def)
33 : _capacity(0), _grow_limit(0), _n(0), _e(0), _default_value(def)
34 {
35 increase(-1);
36 }
37
38
39 template <class K, class V>
40 HashMap<K, V>::HashMap(const HashMap<K, V> &m)
41 : _capacity(m._capacity), _grow_limit(m._grow_limit), _n(m._n),
42 _e(new Pair[m._capacity]), _default_value(m._default_value)
43 {
44 for (int i = 0; i < _capacity; i++)
45 _e[i] = m._e[i];
46 }
47
48
49 template <class K, class V>
50 HashMap<K, V> &
51 HashMap<K, V>::operator=(const HashMap<K, V> &o)
52 {
53 // This works with self-assignment.
54
55 _capacity = o._capacity;
56 _grow_limit = o._grow_limit;
57 _n = o._n;
58 _default_value = o._default_value;
59
60 Pair *new_e = new Pair[_capacity];
61 for (int i = 0; i < _capacity; i++)
62 new_e[i] = o._e[i];
63
64 delete[] _e;
65 _e = new_e;
66
67 return *this;
68 }
69
70
71 template <class K, class V>
72 void
73 HashMap<K, V>::increase(int min_size)
74 {
75 int ncap = (_capacity < 8 ? 8 : _capacity * 2);
76 while (ncap < min_size && ncap > 0)
77 ncap *= 2;
78 if (ncap <= 0) // want too many elements
79 return;
80
81 Pair *ne = new Pair[ncap];
82 if (!ne) // out of memory
83 return;
84
85 Pair *oe = _e;
86 int ocap = _capacity;
87 _e = ne;
88 _capacity = ncap;
89 _grow_limit = ((3 * _capacity) >> 2) - 1;
90
91 Pair *otrav = oe;
92 for (int i = 0; i < ocap; i++, otrav++)
93 if (otrav->key) {
94 int j = bucket(otrav->key);
95 _e[j] = *otrav;
96 }
97
98 delete[] oe;
99 }
100
101 template <class K, class V>
102 inline void
103 HashMap<K, V>::check_capacity()
104 {
105 if (_n >= _grow_limit)
106 increase(-1);
107 }
108
109 template <class K, class V>
110 bool
111 HashMap<K, V>::insert(const K &key, const V &val)
112 {
113 check_capacity();
114 int i = bucket(key);
115 bool is_new = !(bool)_e[i].key;
116 _e[i].key = key;
117 _e[i].value = val;
118 _n += is_new;
119 return is_new;
120 }
121
122 template <class K, class V>
123 V &
124 HashMap<K, V>::find_force(const K &key, const V &value)
125 {
126 check_capacity();
127 int i = bucket(key);
128 if (!(bool)_e[i].key) {
129 _e[i].key = key;
130 _e[i].value = value;
131 _n++;
132 }
133 return _e[i].value;
134 }
135
136 template <class K, class V>
137 void
138 HashMap<K, V>::clear()
139 {
140 delete[] _e;
141 _e = 0;
142 _capacity = _grow_limit = _n = 0;
143 increase(-1);
144 }
145
146 template <class K, class V>
147 void
148 HashMap<K, V>::swap(HashMap<K, V> &o)
149 {
150 int capacity = _capacity;
151 int grow_limit = _grow_limit;
152 int n = _n;
153 Pair *e = _e;
154 V default_value = _default_value;
155 _capacity = o._capacity;
156 _grow_limit = o._grow_limit;
157 _n = o._n;
158 _e = o._e;
159 _default_value = o._default_value;
160 o._capacity = capacity;
161 o._grow_limit = grow_limit;
162 o._n = n;
163 o._e = e;
164 o._default_value = default_value;
165 }
166
167 template <class K, class V>
168 _HashMap_const_iterator<K, V>::_HashMap_const_iterator(const HashMap<K, V> *hm, int pos)
169 : _hm(hm), _pos(pos)
170 {
171 typename HashMap<K, V>::Pair *e = _hm->_e;
172 int capacity = _hm->_capacity;
173 while (_pos < capacity && !(bool)e[_pos].key)
174 _pos++;
175 }
176
177 template <class K, class V>
178 void
179 _HashMap_const_iterator<K, V>::operator++(int)
180 {
181 typename HashMap<K, V>::Pair *e = _hm->_e;
182 int capacity = _hm->_capacity;
183 for (_pos++; _pos < capacity && !(bool)e[_pos].key; _pos++)
184 ;
185 }
186
187 #endif