1 |
tim |
70 |
/********************************************************************** |
2 |
|
|
* Copyright (C) 2002-2003 by Gezelter's Group
|
3 |
|
|
*This program is free software; you can redistribute it and/or modify
|
4 |
|
|
*it under the terms of the GNU General Public License as published by
|
5 |
|
|
*the Free Software Foundation version 2 of the License.
|
6 |
|
|
*
|
7 |
|
|
*This program is distributed in the hope that it will be useful,
|
8 |
|
|
*but WITHOUT ANY WARRANTY; without even the implied warranty of
|
9 |
|
|
*MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
10 |
|
|
*GNU General Public License for more details.
|
11 |
|
|
*
|
12 |
|
|
************************************************************************ |
13 |
|
|
*Author: Teng Lin Email: tlin@nd.edu |
14 |
|
|
*Date: 08/13/2002 Version: 1.0 |
15 |
|
|
* |
16 |
|
|
************************************************************************ |
17 |
|
|
*Description: |
18 |
tim |
77 |
* |
19 |
tim |
70 |
***********************************************************************/ |
20 |
|
|
|
21 |
|
|
#include "keylist.h" |
22 |
|
|
|
23 |
|
|
//member function for TKeyList |
24 |
|
|
template<class T1,class T2, class THashFcn, class TEqualKey> |
25 |
tim |
77 |
void TKeyList<T1, T2, THashFcn, TEqualKey>::Clear() |
26 |
tim |
70 |
{ |
27 |
|
|
_num = 0; |
28 |
|
|
_keys.clear(); |
29 |
|
|
_data.clear(); |
30 |
|
|
_hashTable.clear(); |
31 |
|
|
} |
32 |
|
|
|
33 |
tim |
77 |
template<class T1,class T2, class THashFcn, class TEqualKey> |
34 |
|
|
TKeyList<T1, T2, THashFcn, TEqualKey>::TKeyList() |
35 |
|
|
{ |
36 |
|
|
Clear(); |
37 |
|
|
} |
38 |
|
|
|
39 |
tim |
70 |
//memory |
40 |
|
|
template<class T1,class T2, class THashFcn, class TEqualKey> |
41 |
|
|
TKeyList<T1, T2, THashFcn, TEqualKey>::~TKeyList() |
42 |
|
|
{ |
43 |
tim |
77 |
Clear(); |
44 |
tim |
70 |
} |
45 |
|
|
|
46 |
|
|
template<class T1,class T2, class THashFcn, class TEqualKey> |
47 |
tim |
77 |
int TKeyList<T1, T2, THashFcn, TEqualKey>::AddKey(T1 key, T2 &data) |
48 |
tim |
70 |
{ |
49 |
tim |
77 |
hash_map<T1, int, THashFcn, TEqualKey>::iterator i; |
50 |
|
|
i=_hashTable.find(key); |
51 |
tim |
70 |
|
52 |
tim |
77 |
if (i != _hashTable.end()) |
53 |
|
|
{ |
54 |
|
|
return (*i).second; |
55 |
|
|
} |
56 |
|
|
else |
57 |
|
|
{ |
58 |
|
|
_keys.push_back(key); |
59 |
|
|
_data.push_back(data); |
60 |
|
|
_hashTable[key] = _num; |
61 |
|
|
_num++; |
62 |
|
|
return _num-1; |
63 |
|
|
} |
64 |
|
|
|
65 |
tim |
70 |
} |
66 |
|
|
|
67 |
|
|
template<class T1,class T2, class THashFcn, class TEqualKey> |
68 |
|
|
int TKeyList<T1, T2, THashFcn, TEqualKey>::GetIndex(T1 key) |
69 |
|
|
{ |
70 |
tim |
77 |
hash_map<T1, int, THashFcn, TEqualKey>::iterator i; |
71 |
|
|
i=_hashTable.find(key); |
72 |
tim |
70 |
|
73 |
tim |
77 |
if (i != _hashTable.end()) |
74 |
|
|
{ |
75 |
|
|
return (*i).second; |
76 |
|
|
} |
77 |
|
|
else |
78 |
|
|
{ |
79 |
|
|
return -1; |
80 |
|
|
} |
81 |
|
|
|
82 |
tim |
70 |
} |
83 |
|
|
|
84 |
|
|
|
85 |
|
|
template<class T1,class T2, class THashFcn, class TEqualKey> |
86 |
|
|
const T2& TKeyList<T1, T2, THashFcn, TEqualKey>::GetDataByKey(string &str) const |
87 |
|
|
{ |
88 |
tim |
77 |
int i; |
89 |
|
|
i = GetIndex(str) |
90 |
|
|
return GetDataByIndex(i); |
91 |
tim |
70 |
|
92 |
|
|
} |
93 |
|
|
|
94 |
tim |
77 |
template<class T1,class T2, class THashFcn, class TEqualKey> |
95 |
|
|
const T2& TKeyList<T1, T2, THashFcn, TEqualKey>::GetDataByIndex(int index) const |
96 |
|
|
{ |
97 |
|
|
if (index>=0 && index<_num) |
98 |
|
|
{ |
99 |
|
|
return _data[index]; |
100 |
|
|
} |
101 |
|
|
else |
102 |
|
|
{ |
103 |
tim |
70 |
|
104 |
tim |
77 |
} |
105 |
tim |
70 |
|
106 |
tim |
77 |
} |
107 |
tim |
70 |
|
108 |
tim |
77 |
template<class T1,class T2, class THashFcn, class TEqualKey> |
109 |
|
|
const T1& TKeyList<T1, T2, THashFcn, TEqualKey>::GetKeyByIndex(int index) const |
110 |
|
|
{ |
111 |
|
|
if (index>=0 && index<_num) |
112 |
|
|
{ |
113 |
|
|
return _keys[index]; |
114 |
|
|
} |
115 |
|
|
else |
116 |
|
|
{ |
117 |
tim |
70 |
|
118 |
tim |
77 |
} |
119 |
tim |
70 |
|
120 |
tim |
77 |
} |
121 |
tim |
70 |
|
122 |
tim |
77 |
template<class T1,class T2, class THashFcn, class TEqualKey> |
123 |
|
|
void TKeyList<T1, T2, THashFcn, TEqualKey>::SetDataByIndex(int index, T2 &data) |
124 |
|
|
{ |
125 |
|
|
if (index>=0 && index<_num) |
126 |
|
|
{ |
127 |
|
|
_data[index] = data; |
128 |
|
|
} |
129 |
|
|
else |
130 |
|
|
{ |
131 |
tim |
70 |
|
132 |
tim |
77 |
} |
133 |
tim |
70 |
|
134 |
|
|
|
135 |
tim |
77 |
} |
136 |
tim |
70 |
|
137 |
tim |
77 |
template<class T1,class T2, class THashFcn, class TEqualKey> |
138 |
|
|
void TKeyList<T1, T2, THashFcn, TEqualKey>::SetDataByKey(T1 key, T2 &data) |
139 |
|
|
{ |
140 |
|
|
int i; |
141 |
|
|
i=GetIndex(key); |
142 |
|
|
SetDataByIndex(i, data); |
143 |
|
|
} |
144 |
tim |
70 |
|
145 |
tim |
77 |
template<class T1,class T2, class THashFcn, class TEqualKey> |
146 |
|
|
void TKeyList<T1, T2, THashFcn, TEqualKey>::ChangeKey(int index, T1 newKey) |
147 |
|
|
{ |
148 |
|
|
string oldKey; |
149 |
|
|
T2 &data; |
150 |
|
|
hash_map<T1, int, THashFcn, TEqualKey>::iterator i; |
151 |
tim |
70 |
|
152 |
tim |
77 |
if (index>=0 && index<_num) |
153 |
|
|
{ |
154 |
|
|
oldKey = _keys[index]; |
155 |
|
|
i = _hashTable.find(newKey); |
156 |
|
|
if (i == _hashTable.end()) |
157 |
|
|
{ |
158 |
|
|
i = _hashTable.find(oldKey); |
159 |
|
|
data = (*i).second; |
160 |
|
|
_hashTable.erase(i); |
161 |
|
|
_hashTable[newKey] = data; |
162 |
|
|
_keys[index] = newKey; |
163 |
|
|
} |
164 |
|
|
else |
165 |
|
|
{ |
166 |
|
|
//raise exception here |
167 |
|
|
} |
168 |
tim |
70 |
|
169 |
tim |
77 |
} |
170 |
|
|
else |
171 |
|
|
{ |
172 |
tim |
70 |
|
173 |
tim |
77 |
} |
174 |
|
|
} |
175 |
tim |
70 |
|
176 |
tim |
77 |
template<class T1,class T2, class THashFcn, class TEqualKey> |
177 |
|
|
void TKeyList<T1, T2, THashFcn, TEqualKey>::ChangeKey(T1 oldKey, T1 newKey) |
178 |
|
|
{ |
179 |
|
|
int index; |
180 |
|
|
index = GetIndex(oldKey); |
181 |
|
|
Changkey(index, newKey); |
182 |
|
|
} |
183 |
tim |
70 |
|
184 |
|
|
|
185 |
|
|
|
186 |
|
|
|
187 |
|
|
|
188 |
|
|
|
189 |
|
|
|
190 |
|
|
|
191 |
|
|
|
192 |
|
|
|
193 |
|
|
|
194 |
|
|
|
195 |
|
|
|
196 |
|
|
|
197 |
tim |
77 |
|
198 |
|
|
|
199 |
|
|
|
200 |
|
|
|
201 |
|
|
|
202 |
|
|
|
203 |
|
|
|
204 |
|
|
|