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
|
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
|
-
-
-
-
-
+
+
+
+
+
+
+
+
-
-
+
+
-
-
+
+
-
-
-
-
-
-
-
-
-
-
-
-
+
+
+
+
-
-
-
+
+
+
-
-
-
+
+
-
-
-
-
+
-
-
+
+
+
+
+
+
-
+
+
+
+
-
-
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
+
+
-
+
+
+
+
+
+
+
-
-
-
+
+
+
-
-
+
-
-
-
-
+
+
+
-
|
* Public License, either version 2 or 3, which can be found in the file
* LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this
* file.
*/
#include "config.h"
#define OF_COUNTED_SET_M
#import "OFCountedSet.h"
#import "OFMutableDictionary_hashtable.h"
#import "OFNumber.h"
#import "OFArray.h"
#import "OFCountedSet_hashtable.h"
#import "OFNumber.h"
#import "OFString.h"
#import "OFAutoreleasePool.h"
#import "OFNotImplementedException.h"
static struct {
Class isa;
} placeholder;
@implementation OFCountedSet
- initWithSet: (OFSet*)set
@implementation OFCountedSet_placeholder
- init
{
self = [super init];
return (id)[[OFCountedSet_hashtable alloc] init];
}
@try {
[dictionary release];
dictionary = nil;
dictionary = [[OFMutableDictionary_hashtable alloc]
_initWithDictionary: set->dictionary
copyKeys: NO];
} @catch (id e) {
[self release];
@throw e;
}
return self;
- initWithSet: (OFSet*)set
{
return (id)[[OFCountedSet_hashtable alloc] initWithSet: set];
}
- initWithArray: (OFArray*)array
{
self = [super init];
@try {
return (id)[[OFCountedSet_hashtable alloc] initWithArray: array];
}
id *cArray = [array cArray];
size_t i, count = [array count];
- initWithObjects: (id)firstObject, ...
{
for (i = 0; i < count; i++)
[self addObject: cArray[i]];
} @catch (id e) {
[self release];
id ret;
@throw e;
}
va_list arguments;
va_start(arguments, firstObject);
ret = [[OFCountedSet_hashtable alloc] initWithObject: firstObject
arguments: arguments];
va_end(arguments);
return self;
return ret;
}
- initWithObject: (id)firstObject
arguments: (va_list)arguments
{
return [[OFCountedSet_hashtable alloc] initWithObject: firstObject
arguments: arguments];
}
self = [super init];
@try {
id object;
[self addObject: firstObject];
while ((object = va_arg(arguments, id)) != nil)
[self addObject: object];
} @catch (id e) {
- retain
{
return self;
}
- autorelease
{
return self;
}
- (void)release
{
}
- (void)dealloc
{
@throw [OFNotImplementedException newWithClass: isa
selector: _cmd];
[super dealloc]; /* Get rid of a stupid warning */
}
@end
@implementation OFCountedSet
+ (void)initialize
{
if (self == [OFCountedSet class])
placeholder.isa = [OFCountedSet_placeholder class];
}
+ alloc
{
if (self == [OFCountedSet class])
return (id)&placeholder;
return [super alloc];
}
- init
{
if (isa == [OFCountedSet class]) {
Class c = isa;
[self release];
@throw e;
@throw [OFNotImplementedException newWithClass: c
selector: _cmd];
}
return self;
return [super init];
}
- (size_t)countForObject: (id)object
{
@throw [OFNotImplementedException newWithClass: isa
selector: _cmd];
}
- (OFString*)description
{
OFMutableString *ret;
OFAutoreleasePool *pool, *pool2;
OFEnumerator *keyEnumerator, *objectEnumerator;
size_t i, count = [dictionary count];
id key, object;
OFEnumerator *enumerator;
size_t i, count = [self count];
id object;
if (count == 0)
return @"{()}";
ret = [OFMutableString stringWithString: @"{(\n"];
pool = [[OFAutoreleasePool alloc] init];
keyEnumerator = [dictionary keyEnumerator];
objectEnumerator = [dictionary objectEnumerator];
enumerator = [self objectEnumerator];
i = 0;
pool2 = [[OFAutoreleasePool alloc] init];
while ((key = [keyEnumerator nextObject]) != nil &&
(object = [objectEnumerator nextObject]) != nil) {
[ret appendString: [key description]];
[ret appendString: @": "];
while ((object = [enumerator nextObject]) != nil) {
[ret appendString: object];
[ret appendFormat: @": %zu", [self countForObject: object]];
[ret appendString: [object description]];
if (++i < count)
[ret appendString: @",\n"];
[pool2 releaseObjects];
}
[ret replaceOccurrencesOfString: @"\n"
|
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
|
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
|
-
-
-
-
-
-
+
-
-
+
-
+
-
-
-
-
+
+
-
-
+
-
-
+
-
-
-
+
-
-
-
+
-
-
+
-
-
-
+
-
-
-
+
-
-
-
+
-
-
-
-
-
+
-
-
-
-
-
-
-
-
-
-
-
-
-
+
+
-
-
-
-
-
-
-
-
-
-
-
+
-
-
-
-
-
+
-
-
-
+
-
-
-
-
-
+
-
-
-
+
+
-
-
+
+
-
-
-
-
+
+
+
+
-
-
-
+
+
+
+
+
+
-
-
+
+
|
}
- mutableCopy
{
return [[OFCountedSet alloc] initWithSet: self];
}
- (size_t)countForObject: (id)object
{
return [[dictionary objectForKey: object] sizeValue];
}
#ifdef OF_HAVE_BLOCKS
- (void)enumerateObjectsAndCountUsingBlock:
(of_counted_set_enumeration_block_t)block
{
[dictionary enumerateKeysAndObjectsUsingBlock: ^ (id key, id object,
[self enumerateObjectsUsingBlock: ^ (id object, BOOL *stop) {
BOOL *stop) {
block(key, [object sizeValue], stop);
block(object, [self countForObject: object], stop);
}];
}
#endif
- (void)addObject: (id)object
- (void)minusSet: (OFSet*)set
{
OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init];
OFNumber *count;
count = [[dictionary objectForKey: object] numberByIncreasing];
if (count == nil)
if ([set isKindOfClass: [OFCountedSet class]]) {
OFCountedSet *countedSet = (OFCountedSet*)set;
count = [OFNumber numberWithSize: 1];
OFEnumerator *enumerator = [countedSet objectEnumerator];
[dictionary _setObject: count
forKey: object
id object;
copyKey: NO];
mutations++;
while ((object = [enumerator nextObject]) != nil) {
[pool release];
}
size_t i, count = [countedSet countForObject: object];
- (void)removeObject: (id)object
{
OFNumber *count = [dictionary objectForKey: object];
OFAutoreleasePool *pool;
for (i = 0; i < count; i++)
if (count == nil)
return;
[self removeObject: object];
pool = [[OFAutoreleasePool alloc] init];
count = [count numberByDecreasing];
}
if ([count sizeValue] > 0)
[dictionary _setObject: count
forKey: object
copyKey: NO];
else
} else {
[dictionary removeObjectForKey: object];
mutations++;
[pool release];
}
- (void)minusSet: (OFSet*)set
{
OFCountedSet *countedSet;
OFAutoreleasePool *pool;
OFEnumerator *enumerator;
id object;
OFEnumerator *enumerator = [set objectEnumerator];
id object;
if (![set isKindOfClass: [OFCountedSet class]]) {
[super minusSet: set];
return;
}
countedSet = (OFCountedSet*)set;
pool = [[OFAutoreleasePool alloc] init];
enumerator = [countedSet objectEnumerator];
while ((object = [enumerator nextObject]) != nil) {
while ((object = [enumerator nextObject]) != nil)
size_t i, count = [countedSet countForObject: object];
for (i = 0; i < count; i++)
[self removeObject: object];
}
[pool release];
}
- (void)unionSet: (OFSet*)set
{
OFCountedSet *countedSet;
OFAutoreleasePool *pool;
OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init];
OFEnumerator *enumerator;
id object;
if (![set isKindOfClass: [OFCountedSet class]]) {
if ([set isKindOfClass: [OFCountedSet class]]) {
[super unionSet: set];
return;
}
countedSet = (OFCountedSet*)set;
OFCountedSet *countedSet = (OFCountedSet*)set;
pool = [[OFAutoreleasePool alloc] init];
enumerator = [countedSet objectEnumerator];
OFEnumerator *enumerator = [countedSet objectEnumerator];
id object;
while ((object = [enumerator nextObject]) != nil) {
size_t i, count = [countedSet countForObject: object];
while ((object = [enumerator nextObject]) != nil) {
size_t i, count = [countedSet countForObject: object];
for (i = 0; i < count; i++)
[self addObject: object];
}
for (i = 0; i < count; i++)
[self addObject: object];
}
} else {
[pool release];
}
OFEnumerator *enumerator = [set objectEnumerator];
id object;
while ((object = [enumerator nextObject]) != nil)
[self addObject: object];
}
- (void)makeImmutable
{
[pool release];
}
@end
|