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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
|
/*
* tkIntSetPriv.h --
*
* Private implementation for integer set.
*
* Copyright (c) 2015-2017 Gregor Cramer
*
* See the file "license.terms" for information on usage and redistribution of
* this file, and for a DISCLAIMER OF ALL WARRANTIES.
*/
#ifndef _TKINTSET
# error "do not include this private header file"
#endif
#ifndef _TKINTSETPRIV
#define _TKINTSETPRIV
MODULE_SCOPE bool TkIntSetContains__(
const TkIntSetType *set1, const TkIntSetType *end1,
const TkIntSetType *set2, const TkIntSetType *end2);
MODULE_SCOPE bool TkIntSetDisjunctive__(
const TkIntSetType *set1, const TkIntSetType *end1,
const TkIntSetType *set2, const TkIntSetType *end2);
MODULE_SCOPE bool TkIntSetIsEqual__(
const TkIntSetType *set1, const TkIntSetType *end1,
const TkIntSetType *set2, const TkIntSetType *end2);
#endif /* _TKINTSETPRIV */
#ifdef _TK_NEED_IMPLEMENTATION
#include <assert.h>
extern TkIntSetType *
TkIntSetLowerBound(
TkIntSetType *first,
TkIntSetType *last,
TkIntSetType value);
inline
const unsigned char *
TkIntSetData(
const TkIntSet *set)
{
assert(set);
return (const void *) set->buf;
}
inline
unsigned
TkIntSetByteSize(
const TkIntSet *set)
{
assert(set);
return (set->end - set->buf)*sizeof(TkIntSetType);
}
inline
bool
TkIntSetIsEmpty(
const TkIntSet *set)
{
assert(set);
return set->end == set->buf;
}
inline
bool
TkIntSetIsEqual(
const TkIntSet *set1,
const TkIntSet *set2)
{
assert(set1);
assert(set2);
return set1 == set2 || TkIntSetIsEqual__(set1->buf, set1->end, set2->buf, set2->end);
}
inline
bool
TkIntSetContains(
const TkIntSet *set1,
const TkIntSet *set2)
{
assert(set1);
assert(set2);
return set1 == set2 || TkIntSetContains__(set1->buf, set1->end, set2->buf, set2->end);
}
inline
bool
TkIntSetDisjunctive(
const TkIntSet *set1,
const TkIntSet *set2)
{
assert(set1);
assert(set2);
if (set1 == set2) {
return TkIntSetIsEmpty(set1);
}
return TkIntSetDisjunctive__(set1->buf, set1->end, set2->buf, set2->end);
}
inline
unsigned
TkIntSetSize(
const TkIntSet *set)
{
assert(set);
return set->end - set->buf;
}
inline
unsigned
TkIntSetMax(
const TkIntSet *set)
{
assert(!TkIntSetIsEmpty(set));
return set->end[-1];
}
inline
unsigned
TkIntSetRefCount(
const TkIntSet *set)
{
assert(set);
return set->refCount;
}
inline
void
TkIntSetIncrRefCount(TkIntSet *set)
{
assert(set);
set->refCount += 1;
}
inline
unsigned
TkIntSetDecrRefCount(TkIntSet *set)
{
unsigned refCount;
assert(set);
assert(set->refCount > 0);
if ((refCount = --set->refCount) == 0) {
TkIntSetDestroy(&set);
}
return refCount;
}
inline
TkIntSetType
TkIntSetAccess(
const TkIntSet *set,
unsigned index)
{
assert(set);
assert(index < TkIntSetSize(set));
return set->buf[index];
}
inline
void
TkIntSetChange(
TkIntSet *set,
unsigned index,
unsigned n)
{
assert(set);
assert(index < TkIntSetSize(set));
set->buf[index] = n;
}
inline
bool
TkIntSetTest(
const TkIntSet *set,
unsigned n)
{
const TkIntSetType *pos;
assert(set);
pos = TkIntSetLowerBound(((TkIntSet *) set)->buf, ((TkIntSet *) set)->end, n);
return pos < set->end && *pos == n;
}
inline
bool
TkIntSetNone(
const TkIntSet *set)
{
assert(set);
return set->buf == set->end;
}
inline
bool
TkIntSetAny(
const TkIntSet *set)
{
assert(set);
return set->buf < set->end;
}
inline
bool
TkIntSetIntersects(
const TkIntSet *set1,
const TkIntSet *set2)
{
return !TkIntSetDisjunctive(set1, set2);
}
inline
unsigned
TkIntSetFindNext(
const TkIntSet *set)
{
assert(set);
return set->curr == set->end ? TK_SET_NPOS : *(((TkIntSet *) set)->curr++); /* 'curr' is mutable */
}
inline
unsigned
TkIntSetFindFirst(
const TkIntSet *set)
{
assert(set);
((TkIntSet *) set)->curr = ((TkIntSet *) set)->buf; /* 'curr' is mutable */
return TkIntSetFindNext(set);
}
inline
TkIntSet *
TkIntSetAddOrErase(
TkIntSet *set,
unsigned n,
bool add)
{
assert(set);
return add ? TkIntSetAdd(set, n) : TkIntSetErase(set, n);
}
#undef _TK_NEED_IMPLEMENTATION
#endif /* _TK_NEED_IMPLEMENTATION */
/* vi:set ts=8 sw=4: */
|