1 |
/********************************************************************** |
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 |
* |
19 |
***********************************************************************/ |
20 |
|
21 |
#include "keylist.h" |
22 |
|
23 |
//member function for TKeyList |
24 |
template<class T1,class T2, class THashFcn, class TEqualKey> |
25 |
void TKeyList<T1, T2, THashFcn, TEqualKey>::Clear() |
26 |
{ |
27 |
_num = 0; |
28 |
_keys.clear(); |
29 |
_data.clear(); |
30 |
_hashTable.clear(); |
31 |
} |
32 |
|
33 |
template<class T1,class T2, class THashFcn, class TEqualKey> |
34 |
TKeyList<T1, T2, THashFcn, TEqualKey>::TKeyList() |
35 |
{ |
36 |
Clear(); |
37 |
} |
38 |
|
39 |
//memory |
40 |
template<class T1,class T2, class THashFcn, class TEqualKey> |
41 |
TKeyList<T1, T2, THashFcn, TEqualKey>::~TKeyList() |
42 |
{ |
43 |
Clear(); |
44 |
} |
45 |
|
46 |
template<class T1,class T2, class THashFcn, class TEqualKey> |
47 |
int TKeyList<T1, T2, THashFcn, TEqualKey>::AddKey(T1 key, T2 &data) |
48 |
{ |
49 |
hash_map<T1, int, THashFcn, TEqualKey>::iterator i; |
50 |
i=_hashTable.find(key); |
51 |
|
52 |
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 |
} |
66 |
|
67 |
template<class T1,class T2, class THashFcn, class TEqualKey> |
68 |
int TKeyList<T1, T2, THashFcn, TEqualKey>::GetIndex(T1 key) |
69 |
{ |
70 |
hash_map<T1, int, THashFcn, TEqualKey>::iterator i; |
71 |
i=_hashTable.find(key); |
72 |
|
73 |
if (i != _hashTable.end()) |
74 |
{ |
75 |
return (*i).second; |
76 |
} |
77 |
else |
78 |
{ |
79 |
return -1; |
80 |
} |
81 |
|
82 |
} |
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 |
int i; |
89 |
i = GetIndex(str) |
90 |
return GetDataByIndex(i); |
91 |
|
92 |
} |
93 |
|
94 |
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 |
|
104 |
} |
105 |
|
106 |
} |
107 |
|
108 |
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 |
|
118 |
} |
119 |
|
120 |
} |
121 |
|
122 |
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 |
|
132 |
} |
133 |
|
134 |
|
135 |
} |
136 |
|
137 |
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 |
|
145 |
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 |
|
152 |
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 |
|
169 |
} |
170 |
else |
171 |
{ |
172 |
|
173 |
} |
174 |
} |
175 |
|
176 |
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 |
|
184 |
|
185 |
|
186 |
|
187 |
|
188 |
|
189 |
|
190 |
|
191 |
|
192 |
|
193 |
|
194 |
|
195 |
|
196 |
|
197 |
|
198 |
|
199 |
|
200 |
|
201 |
|
202 |
|
203 |
|
204 |
|