ObjFW  Diff

Differences From Artifact [69cf3066e2]:

To Artifact [2a40664f4e]:


12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27




































































28














29
30
31
32
33
34
35
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







-
-

-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+

+
+
+
+
+
+
+
+
+
+
+
+
+
+







 * 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_SET_M

#import "OFSet.h"
#import "OFMutableDictionary_hashtable.h"
#import "OFArray.h"
#import "OFString.h"
#import "OFNumber.h"
#import "OFAutoreleasePool.h"

#import "OFSet_hashtable.h"
#import "OFString.h"
#import "OFAutoreleasePool.h"

#import "OFNotImplementedException.h"

static struct {
	Class isa;
} placeholder;

@implementation OFSet_placeholder
- init
{
	return (id)[[OFSet_hashtable alloc] init];
}

- initWithSet: (OFSet*)set
{
	return (id)[[OFSet_hashtable alloc] initWithSet: set];
}

- initWithArray: (OFArray*)array
{
	return (id)[[OFSet_hashtable alloc] initWithArray: array];
}

- initWithObjects: (id)firstObject, ...
{
	id ret;
	va_list arguments;

	va_start(arguments, firstObject);
	ret = [[OFSet_hashtable alloc] initWithObject: firstObject
					    arguments: arguments];
	va_end(arguments);

	return ret;
}

- initWithObject: (id)firstObject
       arguments: (va_list)arguments
{
	return [[OFSet_hashtable alloc] initWithObject: firstObject
					     arguments: arguments];
}

- 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 OFSet
+ (void)initialize
{
	if (self == [OFSet class])
		placeholder.isa = [OFSet_placeholder class];
}

+ alloc
{
	if (self == [OFSet class])
		return (id)&placeholder;

	return [super alloc];
}

+ set
{
	return [[[self alloc] init] autorelease];
}

+ setWithSet: (OFSet*)set
{
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
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







-
-
-
+
+
-
-

-
+
+


-
+




-
-
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
+
-
-
+
-




-
-
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
+
-
-
+
-


















+
-
-
-
+
+
+
+
+
-
-
+
-
-
-
+
+
-
-
-
-
-
+
+
+
+
-
-
-
+
-
-
-
-
-
+
+
+

+
+
+
-
+


+
+
-
+

-
-
+
+
-











+
+
+
-
+




+
+
+
+
+
+
+
+
+
+
-
+







-
+







-
+







	va_end(arguments);

	return ret;
}

- init
{
	self = [super init];

	@try {
	if (isa == [OFSet class]) {
		Class c = isa;
		dictionary = [[OFMutableDictionary_hashtable alloc] init];
	} @catch (id e) {
		[self release];
		@throw e;
		@throw [OFNotImplementedException newWithClass: c
						      selector: _cmd];
	}

	return self;
	return [super init];
}

- initWithSet: (OFSet*)set
{
	self = [self init];

	Class c = isa;
	@try {
		OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init];
		OFNumber *one = [OFNumber numberWithSize: 1];
		OFEnumerator *enumerator = [set objectEnumerator];
		id object;

		/*
		 * We can't just copy the dictionary as the specified set might
		 * be a counted set, but we're just a normal set.
		 */
		while ((object = [enumerator nextObject]) != nil)
			[dictionary _setObject: one
					forKey: object
				       copyKey: NO];

		[pool release];
	} @catch (id e) {
		[self release];
		@throw e;
	[self release];
	@throw [OFNotImplementedException newWithClass: c
	}

					      selector: _cmd];
	return self;
}

- initWithArray: (OFArray*)array
{
	self = [self init];

	Class c = isa;
	@try {
		OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init];
		OFNumber *one = [OFNumber numberWithSize: 1];
		id *cArray = [array cArray];
		size_t i, count = [array count];

		for (i = 0; i < count; i++)
			[dictionary _setObject: one
					forKey: cArray[i]
				       copyKey: NO];

		[pool release];
	} @catch (id e) {
		[self release];
		@throw e;
	[self release];
	@throw [OFNotImplementedException newWithClass: c
	}

					      selector: _cmd];
	return self;
}

- (id)initWithObjects:(id)firstObject, ...
{
	id ret;
	va_list arguments;

	va_start(arguments, firstObject);
	ret = [self initWithObject: firstObject
			 arguments: arguments];
	va_end(arguments);

	return ret;
}

- initWithObject: (id)firstObject
       arguments: (va_list)arguments
{
	Class c = isa;
	self = [self init];

	@try {
	[self release];
	@throw [OFNotImplementedException newWithClass: c
					      selector: _cmd];
}

		OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init];
		OFNumber *one = [OFNumber numberWithSize: 1];
- (size_t)count
		id object;

		[dictionary _setObject: one
{
	@throw [OFNotImplementedException newWithClass: isa
				forKey: firstObject
			       copyKey: NO];

		while ((object = va_arg(arguments, id)) != nil)
			[dictionary _setObject: one
					      selector: _cmd];
}

- (BOOL)containsObject: (id)object
					forKey: object
				       copyKey: NO];

{
		[pool release];
	} @catch (id e) {
		[self release];
		@throw e;
	}
	@throw [OFNotImplementedException newWithClass: isa
					      selector: _cmd];
}

- (OFEnumerator*)objectEnumerator
{
	@throw [OFNotImplementedException newWithClass: isa
	return self;
					      selector: _cmd];
}

- (int)countByEnumeratingWithState: (of_fast_enumeration_state_t*)state
			   objects: (id*)objects
- (void)dealloc
			     count: (int)count
{
	[dictionary release];

	@throw [OFNotImplementedException newWithClass: isa
					      selector: _cmd];
	[super dealloc];
}

- (BOOL)isEqual: (id)object
{
	OFSet *otherSet;

	if (![object isKindOfClass: [OFSet class]])
		return NO;

	otherSet = object;

	if ([otherSet count] != [self count])
		return NO;

	return [otherSet->dictionary isEqual: dictionary];
	return [otherSet isSubsetOfSet: self];
}

- (uint32_t)hash
{
	OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init];
	OFEnumerator *enumerator = [self objectEnumerator];
	id object;
	uint32_t hash = 0;

	while ((object = [enumerator nextObject]) != nil)
		hash += [object hash];

	[pool release];

	return [dictionary hash];
	return hash;
}

- (OFString*)description
{
	OFMutableString *ret;
	OFAutoreleasePool *pool, *pool2;
	OFEnumerator *enumerator;
	size_t i, count = [dictionary count];
	size_t i, count = [self count];
	id object;

	if (count == 0)
		return @"{()}";

	ret = [OFMutableString stringWithString: @"{(\n"];
	pool = [[OFAutoreleasePool alloc] init];
	enumerator = [dictionary keyEnumerator];
	enumerator = [self objectEnumerator];

	i = 0;
	pool2 = [[OFAutoreleasePool alloc] init];

	while ((object = [enumerator nextObject]) != nil) {
		[ret appendString: [object description]];

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
278
279
280

281

282
283
284
285

286
287

288
289
290
291

292
293
294



295
296
297


298
299
300
301

302
303
304
305
306
307
308

309
310
311


312
313
314
315
316
317
318
319
269
270
271
272
273
274
275

276
277
278
279
280
281
282
283










284
285
286

287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304

305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320

321
322



323


324




325



326
327
328



329
330




331
332
333
334
335
336
337

338



339
340
341
342
343
344
345
346
347
348







-
+







-
-
-
-
-
-
-
-
-
-



-
+

















-
+














+
-
+

-
-
-
+
-
-
+
-
-
-
-
+
-
-
-
+
+
+
-
-
-
+
+
-
-
-
-
+






-
+
-
-
-
+
+








	[pool release];

	return ret;
}

- copy
{
	return [[OFSet alloc] initWithSet: self];
	return [self retain];
}

- mutableCopy
{
	return [[OFMutableSet alloc] initWithSet: self];
}

- (size_t)count
{
	return [dictionary count];
}

- (BOOL)containsObject: (id)object
{
	return ([dictionary objectForKey: object] != nil);
}

- (BOOL)isSubsetOfSet: (OFSet*)set
{
	OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init];
	OFEnumerator *enumerator = [dictionary keyEnumerator];
	OFEnumerator *enumerator = [self objectEnumerator];
	id object;

	while ((object = [enumerator nextObject]) != nil) {
		if (![set containsObject: object]) {
			[pool release];
			return NO;
		}
	}

	[pool release];

	return YES;
}

- (BOOL)intersectsSet: (OFSet*)set
{
	OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init];
	OFEnumerator *enumerator = [dictionary keyEnumerator];
	OFEnumerator *enumerator = [self objectEnumerator];
	id object;

	while ((object = [enumerator nextObject]) != nil) {
		if ([set containsObject: object]) {
			[pool release];
			return YES;
		}
	}

	[pool release];

	return NO;
}

#ifdef OF_HAVE_BLOCKS
- (OFEnumerator*)objectEnumerator
- (void)enumerateObjectsUsingBlock: (of_set_enumeration_block_t)block
{
	return [dictionary keyEnumerator];
}

	OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init];
- (int)countByEnumeratingWithState: (of_fast_enumeration_state_t*)state
			   objects: (id*)objects
	OFEnumerator *enumerator = [self objectEnumerator];
			     count: (int)count
{
	return [dictionary countByEnumeratingWithState: state
					       objects: objects
	id object;
						 count: count];
}

	BOOL stop = NO;

	while (!stop && (object = [enumerator nextObject]) != nil)
#ifdef OF_HAVE_BLOCKS
- (void)enumerateObjectsUsingBlock: (of_set_enumeration_block_t)block
{
		block(object, &stop);

	[dictionary enumerateKeysAndObjectsUsingBlock: ^ (id key, id object,
	    BOOL *stop) {
		block(key, stop);
	}];
	[pool release];
}

- (OFSet*)filteredSetUsingBlock: (of_set_filter_block_t)block
{
	OFMutableSet *ret = [OFMutableSet set];

	[dictionary enumerateKeysAndObjectsUsingBlock: ^ (id key, id object,
	[self enumerateObjectsUsingBlock: ^ (id object, BOOL *stop) {
	    BOOL *stop) {
		if (block(key))
			[ret addObject: key];
		if (block(object))
			[ret addObject: object];
	}];

	[ret makeImmutable];

	return ret;
}
#endif
@end