3 * ***** BEGIN GPL LICENSE BLOCK *****
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
20 * All rights reserved.
22 * The Original Code is: all of this file.
24 * Contributor(s): none yet.
26 * ***** END GPL LICENSE BLOCK *****
32 template <class Key, class Value>
36 Entry (Entry *next, Key key, Value value) :
47 CTR_Map(int num_buckets = 100) : m_num_buckets(num_buckets) {
48 m_buckets = new Entry *[num_buckets];
49 for (int i = 0; i < num_buckets; ++i) {
56 for (int i=0;i<m_num_buckets;i++)
58 Entry* bucket = m_buckets[i];
61 bucket = bucket->m_next;
68 Value* at(int index) {
70 for (int i=0;i<m_num_buckets;i++)
72 Entry* bucket = m_buckets[i];
77 return &bucket->m_value;
79 bucket = bucket->m_next;
87 for (int i = 0; i < m_num_buckets; ++i) {
88 Entry *entry_ptr = m_buckets[i];
90 while (entry_ptr != 0) {
91 Entry *tmp_ptr = entry_ptr->m_next;
104 void insert(const Key& key, const Value& value) {
105 Entry *entry_ptr = m_buckets[key.hash() % m_num_buckets];
106 while ((entry_ptr != 0) && !(key == entry_ptr->m_key)) {
107 entry_ptr = entry_ptr->m_next;
110 if (entry_ptr != 0) {
111 entry_ptr->m_value = value;
114 Entry **bucket = &m_buckets[key.hash() % m_num_buckets];
115 *bucket = new Entry(*bucket, key, value);
119 void remove(const Key& key) {
120 Entry **entry_ptr = &m_buckets[key.hash() % m_num_buckets];
121 while ((*entry_ptr != 0) && !(key == (*entry_ptr)->m_key)) {
122 entry_ptr = &(*entry_ptr)->m_next;
125 if (*entry_ptr != 0) {
126 Entry *tmp_ptr = (*entry_ptr)->m_next;
128 *entry_ptr = tmp_ptr;
132 Value *operator[](Key key) {
133 Entry *bucket = m_buckets[key.hash() % m_num_buckets];
134 while ((bucket != 0) && !(key == bucket->m_key)) {
135 bucket = bucket->m_next;
137 return bucket != 0 ? &bucket->m_value : 0;