-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathMap2.cpp
More file actions
212 lines (178 loc) · 4.68 KB
/
Map2.cpp
File metadata and controls
212 lines (178 loc) · 4.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
/*
* Map2.cc --
* Two-dimensional map implementation
*
*/
#include "stdafx.h"
#ifndef _Map2_cc_
#define _Map2_cc_
#ifndef lint
static char Map2_Copyright[] = "Copyright (c) 1999-2010 SRI International. All Rights Reserved.";
static char Map2_RcsId[] = "@(#)$Header: /home/srilm/CVS/srilm/dstruct/src/Map2.cc,v 1.12 2010/06/02 04:52:43 stolcke Exp $";
#endif
#ifdef PRE_ISO_CXX
# include <new.h>
# include <iostream.h>
#else
# include <new>
# include <iostream>
using namespace std;
#endif
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include "Map2.h"
#undef INSTANTIATE_MAP2
#ifdef USE_SARRAY_MAP2
# include "SArray.cc"
# define INSTANTIATE_MAP1(Key1T,Key2T,DataT) \
typedef SArray<Key2T,DataT> Map2Type; \
INSTANTIATE_SARRAY(Key1T, Map2Type ); \
template class Map2< Key1T,Key2T,DataT>; \
template class Map2Iter<Key1T,Key2T,DataT>; \
template class Map2Iter2<Key1T,Key2T,DataT>
# define INSTANTIATE_MAP2(Key1T,Key2T,DataT) \
INSTANTIATE_SARRAY(Key2T, DataT ); \
INSTANTIATE_MAP1(Key1T,Key2T,DataT)
#else /* ! USE_SARRAY_MAP2 */
# include "LHash.cpp"
# define INSTANTIATE_MAP1(Key1T,Key2T,DataT) \
typedef LHash<Key2T,DataT> Map2Type; \
INSTANTIATE_LHASH(Key1T, Map2Type ); \
template class Map2< Key1T,Key2T,DataT>; \
template class Map2Iter<Key1T,Key2T,DataT>; \
template class Map2Iter2<Key1T,Key2T,DataT>
# define INSTANTIATE_MAP2(Key1T,Key2T,DataT) \
INSTANTIATE_LHASH(Key2T, DataT ); \
INSTANTIATE_MAP1(Key1T,Key2T,DataT)
#endif /* USE_SARRAY_MAP2 */
template <class Key1T, class Key2T, class DataT>
Map2<Key1T,Key2T,DataT>::Map2()
: sub(0)
{
}
template <class Key1T, class Key2T, class DataT>
Map2<Key1T,Key2T,DataT>::~Map2()
{
MAP2_ITER_T<Key1T, MAP2_INDEX_T<Key2T,DataT> > iter(sub);
Key1T key;
MAP2_INDEX_T<Key2T,DataT> *node;
/*
* destroy all second-level indices
*/
while ((node = iter.next(key))) {
#if __GNUC__ == 2 && __GNUC_MINOR__ == 8 || defined(sgi) && !defined(__GNUC__)
/* workaround for buggy gcc 2.8.1 */
node->clear(0);
#else
node->~MAP2_INDEX_T<Key2T,DataT>();
#endif
}
}
/*
* Dump contents of Map2 to cerr
*/
template <class Key1T, class Key2T, class DataT>
void
Map2<Key1T,Key2T,DataT>::dump() const
{
MAP2_ITER_T<Key1T, MAP2_INDEX_T<Key2T,DataT> > iter(sub);
Key1T key;
MAP2_INDEX_T<Key2T,DataT> *row;
while ((row = iter.next(key))) {
cerr << "Row Key = " << key << endl;
row->dump();
}
}
/*
* Compute memory stats for Trie, including all children and grand-children
*/
template <class Key1T, class Key2T, class DataT>
void
Map2<Key1T,Key2T,DataT>::memStats(MemStats &stats) const
{
stats.total += sizeof(*this) - sizeof(sub);
sub.memStats(stats);
MAP2_ITER_T<Key1T, MAP2_INDEX_T<Key2T,DataT> > iter(sub);
Key1T key;
MAP2_INDEX_T<Key2T,DataT> *row;
while ((row = iter.next(key))) {
stats.total -= sizeof(*row);
row->memStats(stats);
}
}
template <class Key1T, class Key2T, class DataT>
DataT *
Map2<Key1T,Key2T,DataT>::find(Key1T key1, Key2T key2, Boolean &foundP) const
{
MAP2_INDEX_T<Key2T,DataT> *row = sub.find(key1);
if (row == 0) {
foundP = false;
return 0;
} else {
return row->find(key2, foundP);
}
}
template <class Key1T, class Key2T, class DataT>
DataT *
Map2<Key1T,Key2T,DataT>::insert(Key1T key1, Key2T key2, Boolean &foundP)
{
MAP2_INDEX_T<Key2T,DataT> *row = sub.insert(key1);
return row->insert(key2, foundP);
}
template <class Key1T, class Key2T, class DataT>
DataT *
Map2<Key1T,Key2T,DataT>::remove(Key1T key1, Key2T key2, Boolean &foundP)
{
MAP2_INDEX_T<Key2T,DataT> *row = sub.find(key1);
if (row == 0) {
foundP = false;
return 0;
} else{
return row->remove(key2, foundP);
}
}
template <class Key1T, class Key2T, class DataT>
Boolean
Map2<Key1T,Key2T,DataT>::remove(Key1T key1, Boolean &foundP)
{
MAP2_INDEX_T<Key2T,DataT> *row = sub.find(key1, foundP);
if (row != 0) {
/*
* Destroy the row vector
*/
#if __GNUC__ == 2 && __GNUC_MINOR__ == 8 || defined(sgi) && !defined(__GNUC__)
/* workaround for buggy gcc 2.8.1 */
row->clear(0);
#else
row->~MAP2_INDEX_T<Key2T,DataT>();
#endif
sub.remove(key1);
}
return foundP;
}
template <class Key1T, class Key2T, class DataT>
void
Map2<Key1T,Key2T,DataT>::clear()
{
MAP2_ITER_T<Key1T, MAP2_INDEX_T<Key2T,DataT> > iter1(sub);
Key1T key1;
MAP2_INDEX_T<Key2T,DataT> *row;
/*
* Remove all row vectors
*/
while ((row = iter1.next(key1))) {
#if __GNUC__ == 2 && __GNUC_MINOR__ == 8 || defined(sgi) && !defined(__GNUC__)
/* workaround for buggy gcc 2.8.1 */
row->clear(0);
#else
row->~MAP2_INDEX_T<Key2T,DataT>();
#endif
sub.remove(key1);
}
/*
* Remove the top-level vector
*/
sub.clear(0);
}
#endif /* _Map2_cc_ */