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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
|
/*
* Copyright 2001 Adrian Thurston <thurston@complang.org>
*/
/* This file is part of Aapl.
*
* Aapl is free software; you can redistribute it and/or modify it under the
* terms of the GNU Lesser General Public License as published by the Free
* Software Foundation; either version 2.1 of the License, or (at your option)
* any later version.
*
* Aapl is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
* more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Aapl; if not, write to the Free Software Foundation, Inc., 59
* Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#ifndef _AAPL_COMPARE_H
#define _AAPL_COMPARE_H
#include <string.h>
#include "table.h"
#ifdef AAPL_NAMESPACE
namespace Aapl {
#endif
/**
* \defgroup compare Compare
* \brief Basic compare clases.
*
* Compare classes are used by data structures that need to know the relative
* ordering of elemets. To become a compare class, a class must imlement a
* routine long compare(const T &key1, const T &key2) that behaves just like
* strcmp.
*
* Compare classes are passed to the template data structure as a template
* parameter and are inherited. In most cases the compare routine will base
* the key comparision only on the two keys and the compare routine can
* therefore be static. Though sometimes it is useful to include data in the
* compare class and use this data in the comparison. For example the compare
* class may contain a pointer to some other data structure to which the
* comparison is delegated.
*
* @{
*/
/**
* \brief Compare a type for which < and > are implemented.
*
* CmpOrd is suitable for simple types such as integers and pointers that by
* default have the less-than and greater-than operators defined.
*/
template <class T> struct CmpOrd
{
/**
* \brief Compare two ordinal types.
*
* This compare routine copies its arguements in by value.
*/
static inline long compare(const T k1, const T k2)
{
if (k1 < k2)
return -1;
else if (k1 > k2)
return 1;
else
return 0;
}
};
/**
* \brief Compare two tables of type T
*
* Table comparison is useful for keying a data structure on a vector or
* binary search table. T is the element type stored in the table.
* CompareT is the comparison structure used to compare the individual values
* in the table.
*/
template < class T, class CompareT = CmpOrd<T> > struct CmpTable
: public CompareT
{
/**
* \brief Compare two tables storing type T.
*/
static inline long compare(const Table<T> &t1, const Table<T> &t2)
{
if ( t1.tabLen < t2.tabLen )
return -1;
else if ( t1.tabLen > t2.tabLen )
return 1;
else
{
T *i1 = t1.data, *i2 = t2.data;
long len = t1.tabLen, cmpResult;
for ( long pos = 0; pos < len;
pos += 1, i1 += 1, i2 += 1 )
{
cmpResult = CompareT::compare(*i1, *i2);
if ( cmpResult != 0 )
return cmpResult;
}
return 0;
}
}
};
/**
* \brief Compare two tables of type T -- non-static version.
*
* CmpTableNs is identical to CmpTable, however the compare routine is
* non-static. If the CompareT class contains a non-static compare, then this
* version must be used because a static member cannot invoke a non-static
* member.
*
* Table comparison is useful for keying a data structure on a vector or binary
* search table. T is the element type stored in the table. CompareT
* is the comparison structure used to compare the individual values in the
* table.
*/
template < class T, class CompareT = CmpOrd<T> > struct CmpTableNs
: public CompareT
{
/**
* \brief Compare two tables storing type T.
*/
inline long compare(const Table<T> &t1, const Table<T> &t2)
{
if ( t1.tabLen < t2.tabLen )
return -1;
else if ( t1.tabLen > t2.tabLen )
return 1;
else
{
T *i1 = t1.data, *i2 = t2.data;
long len = t1.tabLen, cmpResult;
for ( long pos = 0; pos < len;
pos += 1, i1 += 1, i2 += 1 )
{
cmpResult = CompareT::compare(*i1, *i2);
if ( cmpResult != 0 )
return cmpResult;
}
return 0;
}
}
};
/**
* \brief Compare two implicitly shared tables of type T
*
* This table comparison is for data structures based on implicitly
* shared tables.
*
* Table comparison is useful for keying a data structure on a vector or
* binary search table. T is the element type stored in the table.
* CompareT is the comparison structure used to compare the individual values
* in the table.
*/
template < class T, class CompareT = CmpOrd<T> > struct CmpSTable : public CompareT
{
/**
* \brief Compare two tables storing type T.
*/
static inline long compare(const STable<T> &t1, const STable<T> &t2)
{
long t1Length = t1.length();
long t2Length = t2.length();
/* Compare lengths. */
if ( t1Length < t2Length )
return -1;
else if ( t1Length > t2Length )
return 1;
else {
/* Compare the table data. */
T *i1 = t1.data, *i2 = t2.data;
for ( long pos = 0; pos < t1Length;
pos += 1, i1 += 1, i2 += 1 )
{
long cmpResult = CompareT::compare(*i1, *i2);
if ( cmpResult != 0 )
return cmpResult;
}
return 0;
}
}
};
/**
* \brief Compare two implicitly shared tables of type T -- non-static
* version.
*
* This is a non-static table comparison for data structures based on
* implicitly shared tables. If the CompareT class contains a non-static
* compare, then this version must be used because a static member cannot
* invoke a non-static member.
*
* Table comparison is useful for keying a data structure on a vector or
* binary search table. T is the element type stored in the table.
* CompareT is the comparison structure used to compare the individual values
* in the table.
*/
template < class T, class CompareT = CmpOrd<T> > struct CmpSTableNs
: public CompareT
{
/**
* \brief Compare two tables storing type T.
*/
inline long compare(const STable<T> &t1, const STable<T> &t2)
{
long t1Length = t1.length();
long t2Length = t2.length();
/* Compare lengths. */
if ( t1Length < t2Length )
return -1;
else if ( t1Length > t2Length )
return 1;
else {
/* Compare the table data. */
T *i1 = t1.data, *i2 = t2.data;
for ( long pos = 0; pos < t1Length;
pos += 1, i1 += 1, i2 += 1 )
{
long cmpResult = CompareT::compare(*i1, *i2);
if ( cmpResult != 0 )
return cmpResult;
}
return 0;
}
}
};
/*@}*/
#ifdef AAPL_NAMESPACE
}
#endif
#endif /* _AAPL_COMPARE_H */
|