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 *****
29 /** \file container/CTR_Map.h
37 template <class Key, class Value>
41 Entry (Entry *next, Key key, Value value) :
52 CTR_Map(int num_buckets = 100) : m_num_buckets(num_buckets) {
53 m_buckets = new Entry *[num_buckets];
54 for (int i = 0; i < num_buckets; ++i) {
59 CTR_Map(const CTR_Map& map)
61 m_num_buckets = map.m_num_buckets;
62 m_buckets = new Entry *[m_num_buckets];
64 for (int i = 0; i < m_num_buckets; ++i) {
67 for(Entry *entry = map.m_buckets[i]; entry; entry=entry->m_next)
68 insert(entry->m_key, entry->m_value);
74 for (int i=0;i<m_num_buckets;i++)
76 Entry* bucket = m_buckets[i];
79 bucket = bucket->m_next;
86 Value* at(int index) {
88 for (int i=0;i<m_num_buckets;i++)
90 Entry* bucket = m_buckets[i];
95 return &bucket->m_value;
97 bucket = bucket->m_next;
104 Key* getKey(int index) {
106 for (int i=0;i<m_num_buckets;i++)
108 Entry* bucket = m_buckets[i];
113 return &bucket->m_key;
115 bucket = bucket->m_next;
123 for (int i = 0; i < m_num_buckets; ++i) {
124 Entry *entry_ptr = m_buckets[i];
126 while (entry_ptr != 0) {
127 Entry *tmp_ptr = entry_ptr->m_next;
140 void insert(const Key& key, const Value& value) {
141 Entry *entry_ptr = m_buckets[key.hash() % m_num_buckets];
142 while ((entry_ptr != 0) && !(key == entry_ptr->m_key)) {
143 entry_ptr = entry_ptr->m_next;
146 if (entry_ptr != 0) {
147 entry_ptr->m_value = value;
150 Entry **bucket = &m_buckets[key.hash() % m_num_buckets];
151 *bucket = new Entry(*bucket, key, value);
155 void remove(const Key& key) {
156 Entry **entry_ptr = &m_buckets[key.hash() % m_num_buckets];
157 while ((*entry_ptr != 0) && !(key == (*entry_ptr)->m_key)) {
158 entry_ptr = &(*entry_ptr)->m_next;
161 if (*entry_ptr != 0) {
162 Entry *tmp_ptr = (*entry_ptr)->m_next;
164 *entry_ptr = tmp_ptr;
168 Value *operator[](Key key) {
169 Entry *bucket = m_buckets[key.hash() % m_num_buckets];
170 while ((bucket != 0) && !(key == bucket->m_key)) {
171 bucket = bucket->m_next;
173 return bucket != 0 ? &bucket->m_value : 0;