ObjFW  Check-in [ed438b979d]

Overview
Comment:OFSet_hashtable: Store in OFMapTable.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: ed438b979d6ffe49e375685678c6c1402818306108fc862e8867c6c5b5c3d3db
User & Date: js on 2012-12-01 17:50:44
Other Links: manifest | tags
Context
2012-12-01
18:08
OFDictionary_hashtable: Store in OFMapTable. check-in: 1abbb97747 user: js tags: trunk
17:50
OFSet_hashtable: Store in OFMapTable. check-in: ed438b979d user: js tags: trunk
17:26
Add OFMapTable. check-in: 6f081c14f9 user: js tags: trunk
Changes

Modified src/OFCountedSet_hashtable.h from [764c37fc25] to [9613e59d54].

12
13
14
15
16
17
18
19
20
21
22
23
24
25
 * 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.
 */

#import "OFCountedSet.h"

@class OFMutableDictionary_hashtable;

@interface OFCountedSet_hashtable: OFCountedSet
{
	OFMutableDictionary_hashtable *dictionary;
}
@end







|



|


12
13
14
15
16
17
18
19
20
21
22
23
24
25
 * 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.
 */

#import "OFCountedSet.h"

@class OFMapTable;

@interface OFCountedSet_hashtable: OFCountedSet
{
	OFMapTable *mapTable;
}
@end

Modified src/OFCountedSet_hashtable.m from [2d16b912f5] to [f0855688b6].

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

#import "OFMutableSet_hashtable.h"
#import "OFCountedSet_hashtable.h"
#import "OFMutableDictionary_hashtable.h"
#import "OFString.h"
#import "OFNumber.h"
#import "OFArray.h"
#import "OFXMLElement.h"
#import "OFXMLAttribute.h"

#import "OFInvalidArgumentException.h"
#import "OFInvalidFormatException.h"



#import "autorelease.h"

@implementation OFCountedSet_hashtable
+ (void)initialize
{
	if (self == [OFCountedSet_hashtable class])







<
<


|

<






>
>







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



#import "OFMutableSet_hashtable.h"
#import "OFCountedSet_hashtable.h"
#import "OFMapTable.h"
#import "OFString.h"

#import "OFArray.h"
#import "OFXMLElement.h"
#import "OFXMLAttribute.h"

#import "OFInvalidArgumentException.h"
#import "OFInvalidFormatException.h"
#import "OFEnumerationMutationException.h"
#import "OFOutOfRangeException.h"

#import "autorelease.h"

@implementation OFCountedSet_hashtable
+ (void)initialize
{
	if (self == [OFCountedSet_hashtable class])
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
		objects = [element elementsForName: @"object"
					 namespace: OF_SERIALIZATION_NS];

		enumerator = [objects objectEnumerator];
		while ((objectElement = [enumerator nextObject]) != nil) {
			void *pool2 = objc_autoreleasePoolPush();
			OFXMLElement *object;
			OFXMLAttribute *count;
			OFNumber *number;

			object = [[objectElement elementsForNamespace:
			    OF_SERIALIZATION_NS] firstObject];
			count = [objectElement attributeForName: @"count"];

			if (object == nil || count == nil)
				@throw [OFInvalidFormatException
				    exceptionWithClass: [self class]];

			number = [OFNumber numberWithSize:
			    (size_t)[[count stringValue] decimalValue]];

			[dictionary OF_setObject: number

					  forKey: [object objectByDeserializing]
					 copyKey: NO];

			objc_autoreleasePoolPop(pool2);
		}

		objc_autoreleasePoolPop(pool);
	} @catch (id e) {
		[self release];
		@throw e;
	}

	return 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,
	    BOOL *stop) {
		block(key, [object sizeValue], stop);
	}];





}
#endif

- (void)addObject: (id)object
{
	void *pool = objc_autoreleasePoolPush();
	OFNumber *count;

	count = [[dictionary objectForKey: object] numberByIncreasing];

	if (count == nil)
		count = [OFNumber numberWithSize: 1];


	[dictionary OF_setObject: count
			  forKey: object
			 copyKey: NO];

	objc_autoreleasePoolPop(pool);
}

- (void)removeObject: (id)object
{
	OFNumber *count = [dictionary objectForKey: object];
	void *pool;

	if (count == nil)
		return;

	pool = objc_autoreleasePoolPush();
	count = [count numberByDecreasing];

	if ([count sizeValue] > 0)
		[dictionary OF_setObject: count
				  forKey: object
				 copyKey: NO];
	else
		[dictionary removeObjectForKey: object];

	objc_autoreleasePoolPop(pool);
}

- (void)makeImmutable
{
}
@end







|
|



|

|



<
|

<
>
|
<















|






>
|
|
|
|
>
>
>
>
>





<
<
|
<

|
<
>

|
|
<
<
<




|
<

|


<
|

|
|
|
<

|
<
<






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
		objects = [element elementsForName: @"object"
					 namespace: OF_SERIALIZATION_NS];

		enumerator = [objects objectEnumerator];
		while ((objectElement = [enumerator nextObject]) != nil) {
			void *pool2 = objc_autoreleasePoolPush();
			OFXMLElement *object;
			OFXMLAttribute *count_;
			size_t count;

			object = [[objectElement elementsForNamespace:
			    OF_SERIALIZATION_NS] firstObject];
			count_ = [objectElement attributeForName: @"count"];

			if (object == nil || count_ == nil)
				@throw [OFInvalidFormatException
				    exceptionWithClass: [self class]];


			count = (size_t)[[count_ stringValue] decimalValue];


			[mapTable setValue: (void*)(uintptr_t)count
				    forKey: [object objectByDeserializing]];


			objc_autoreleasePoolPop(pool2);
		}

		objc_autoreleasePoolPop(pool);
	} @catch (id e) {
		[self release];
		@throw e;
	}

	return self;
}

- (size_t)countForObject: (id)object
{
	return (size_t)(uintptr_t)[mapTable valueForKey: object];
}

#ifdef OF_HAVE_BLOCKS
- (void)enumerateObjectsAndCountUsingBlock:
    (of_counted_set_enumeration_block_t)block
{
	@try {
		[mapTable enumerateKeysAndValuesUsingBlock:
		    ^ (void *key, void *value, BOOL *stop) {
			block(key, (size_t)(uintptr_t)value, stop);
		}];
	} @catch (OFEnumerationMutationException *e) {
		@throw [OFEnumerationMutationException
		    exceptionWithClass: [self class]
				object: self];
	}
}
#endif

- (void)addObject: (id)object
{


	size_t count = (size_t)(uintptr_t)[mapTable valueForKey: object];


	if (SIZE_MAX - count < 1)

		@throw [OFOutOfRangeException exceptionWithClass: [self class]];

	[mapTable setValue: (void*)(uintptr_t)(count + 1)
		    forKey: object];



}

- (void)removeObject: (id)object
{
	size_t count = (size_t)(uintptr_t)[mapTable valueForKey: object];


	if (count == 0)
		return;


	count--;

	if (count > 0)
		[mapTable setValue: (void*)(uintptr_t)count
			    forKey: object];

	else
		[mapTable removeValueForKey: object];


}

- (void)makeImmutable
{
}
@end

Modified src/OFMapTable.h from [b9cf6cd576] to [dcccbffbc5].

233
234
235
236
237
238
239











/*!
 * @brief Resets the enumerator, so the next call to nextKey returns the first
 *	  key again.
 */
- (void)reset;
@end

















>
>
>
>
>
>
>
>
>
>
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249

/*!
 * @brief Resets the enumerator, so the next call to nextKey returns the first
 *	  key again.
 */
- (void)reset;
@end

@interface OFMapTableEnumeratorWrapper: OFEnumerator
{
	OFMapTableEnumerator *enumerator;
	id object;
}

- initWithEnumerator: (OFMapTableEnumerator*)enumerator
	      object: (id)object;
@end

Modified src/OFMapTable.m from [e82f78e00b] to [c18a26ad1f].

701
702
703
704
705
706
707
708
709















































	for (; position < capacity && (buckets[position] == NULL ||
	    buckets[position] == &deleted); position++);

	if (position < capacity)
		return buckets[position++]->value;
	else
		return NULL;
}
@end
























































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
	for (; position < capacity && (buckets[position] == NULL ||
	    buckets[position] == &deleted); position++);

	if (position < capacity)
		return buckets[position++]->value;
	else
		return NULL;
}
@end

@implementation OFMapTableEnumeratorWrapper
- initWithEnumerator: (OFMapTableEnumerator*)enumerator_
	      object: (id)object_
{
	self = [super init];

	enumerator = [enumerator_ retain];
	object = [object_ retain];

	return self;
}

- (void)dealloc
{
	[enumerator release];
	[object release];

	[super dealloc];
}

- (id)nextObject
{
	id ret;

	@try {
		ret = [enumerator nextValue];
	} @catch (OFEnumerationMutationException *e) {
		@throw [OFEnumerationMutationException
		    exceptionWithClass: [object class]
				object: object];
	}

	return ret;
}

- (void)reset
{
	@try {
		[enumerator reset];
	} @catch (OFEnumerationMutationException *e) {
		@throw [OFEnumerationMutationException
		    exceptionWithClass: [object class]
				object: object];
	}
}
@end

Modified src/OFMutableSet_hashtable.h from [ee1ba34d52] to [bcdeaaadac].

12
13
14
15
16
17
18
19
20
21
22
23
24
25
 * 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.
 */

#import "OFMutableSet.h"

@class OFMutableDictionary_hashtable;

@interface OFMutableSet_hashtable: OFMutableSet
{
	OFMutableDictionary_hashtable *dictionary;
}
@end







|



|


12
13
14
15
16
17
18
19
20
21
22
23
24
25
 * 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.
 */

#import "OFMutableSet.h"

@class OFMapTable;

@interface OFMutableSet_hashtable: OFMutableSet
{
	OFMapTable *mapTable;
}
@end

Modified src/OFMutableSet_hashtable.m from [f1c4d45a2c] to [2312031144].

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

#import "OFSet_hashtable.h"
#import "OFMutableSet_hashtable.h"
#import "OFMutableDictionary_hashtable.h"
#import "OFNumber.h"

@implementation OFMutableSet_hashtable
+ (void)initialize
{
	if (self == [OFMutableSet_hashtable class])
		[self inheritMethodsFromClass: [OFSet_hashtable class]];
}

- (void)addObject: (id)object
{
	[dictionary OF_setObject: [OFNumber numberWithSize: 1]
			  forKey: object
			 copyKey: NO];
}

- (void)removeObject: (id)object
{
	[dictionary removeObjectForKey: object];
}

- (void)makeImmutable
{
	object_setClass(self, [OFSet_hashtable class]);
}
@end







<
<


<
|










|
|
<




|







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



#import "OFSet_hashtable.h"
#import "OFMutableSet_hashtable.h"

#import "OFMapTable.h"

@implementation OFMutableSet_hashtable
+ (void)initialize
{
	if (self == [OFMutableSet_hashtable class])
		[self inheritMethodsFromClass: [OFSet_hashtable class]];
}

- (void)addObject: (id)object
{
	[mapTable setValue: (void*)1
		    forKey: object];

}

- (void)removeObject: (id)object
{
	[mapTable removeValueForKey: object];
}

- (void)makeImmutable
{
	object_setClass(self, [OFSet_hashtable class]);
}
@end

Modified src/OFSet_hashtable.h from [673218c8e7] to [b2ed28094e].

12
13
14
15
16
17
18
19
20
21
22
23
24
25
 * 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.
 */

#import "OFSet.h"

@class OFMutableDictionary_hashtable;

@interface OFSet_hashtable: OFSet
{
	OFMutableDictionary_hashtable *dictionary;
}
@end







|



|


12
13
14
15
16
17
18
19
20
21
22
23
24
25
 * 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.
 */

#import "OFSet.h"

@class OFMapTable;

@interface OFSet_hashtable: OFSet
{
	OFMapTable *mapTable;
}
@end

Modified src/OFSet_hashtable.m from [b5f65d1f93] to [e6fb2fdb69].

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

#import "OFSet_hashtable.h"
#import "OFMutableSet_hashtable.h"
#import "OFCountedSet_hashtable.h"
#import "OFMutableDictionary_hashtable.h"
#import "OFArray.h"
#import "OFString.h"
#import "OFNumber.h"
#import "OFXMLElement.h"

#import "OFInvalidArgumentException.h"


#import "autorelease.h"

































@implementation OFSet_hashtable
- init
{





	self = [super init];

	@try {
		dictionary = [[OFMutableDictionary_hashtable alloc] init];



	} @catch (id e) {
		[self release];
		@throw e;
	}

	return self;
}

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

	if (set == nil)
		return self;










	@try {
		void *pool = objc_autoreleasePoolPush();
		OFNumber *one;
		OFEnumerator *enumerator;
		id object;

		one = [OFNumber numberWithSize: 1];

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

		objc_autoreleasePoolPop(pool);
	} @catch (id e) {
		[self release];
		@throw e;
	}

	return self;
}

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

	if (array == nil)
		return self;

	@try {









		void *pool = objc_autoreleasePoolPush();
		OFNumber *one = [OFNumber numberWithSize: 1];
		OFEnumerator *enumerator;
		id object;

		enumerator = [array objectEnumerator];
		while ((object = [enumerator nextObject]) != nil)
			[dictionary OF_setObject: one
					  forKey: object
					 copyKey: NO];

		objc_autoreleasePoolPop(pool);
	} @catch (id e) {
		[self release];
		@throw e;
	}

	return self;
}

- initWithObjects: (id const*)objects
	    count: (size_t)count
{
	self = [self init];

	@try {
		void *pool = objc_autoreleasePoolPush();
		OFNumber *one = [OFNumber numberWithSize: 1];
		size_t i;

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

		objc_autoreleasePoolPop(pool);
	} @catch (id e) {
		[self release];
		@throw e;
	}

	return self;
}

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

	@try {
		void *pool = objc_autoreleasePoolPush();
		OFNumber *one = [OFNumber numberWithSize: 1];
		id object;




		[dictionary OF_setObject: one








				  forKey: firstObject
				 copyKey: NO];

		while ((object = va_arg(arguments, id)) != nil)
			[dictionary OF_setObject: one
					  forKey: object
					 copyKey: NO];

		objc_autoreleasePoolPop(pool);
	} @catch (id e) {
		[self release];
		@throw e;
	}

	return self;
}

- initWithSerialization: (OFXMLElement*)element
{
	self = [self init];

	@try {
		void *pool = objc_autoreleasePoolPush();
		OFNumber *one;
		OFEnumerator *enumerator;
		OFXMLElement *child;

		if ((![[element name] isEqual: @"OFSet"] &&
		    ![[element name] isEqual: @"OFMutableSet"]) ||
		    ![[element namespace] isEqual: OF_SERIALIZATION_NS])
			@throw [OFInvalidArgumentException
			    exceptionWithClass: [self class]
				      selector: _cmd];

		one = [OFNumber numberWithSize: 1];

		enumerator = [[element elementsForNamespace:
		    OF_SERIALIZATION_NS] objectEnumerator];
		while ((child = [enumerator nextObject]) != nil) {
			void *pool2  = objc_autoreleasePoolPush();

			[dictionary OF_setObject: one
					  forKey: [child objectByDeserializing]
					 copyKey: NO];

			objc_autoreleasePoolPop(pool2);
		}

		objc_autoreleasePoolPop(pool);
	} @catch (id e) {
		[self release];
		@throw e;
	}

	return self;
}

- (void)dealloc
{
	[dictionary release];

	[super dealloc];
}

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

- (BOOL)containsObject: (id)object
{
	if (object == nil)
		return NO;

	return ([dictionary objectForKey: object] != nil);
}

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

	if (![object isKindOfClass: [OFSet_hashtable class]] &&
	    ![object isKindOfClass: [OFMutableSet_hashtable class]] &&
	    ![object isKindOfClass: [OFCountedSet_hashtable class]])
		return [super isEqual: object];

	otherSet = object;

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

- (OFEnumerator*)objectEnumerator
{

	return [dictionary keyEnumerator];

}

- (int)countByEnumeratingWithState: (of_fast_enumeration_state_t*)state
			   objects: (id*)objects
			     count: (int)count
{
	return [dictionary countByEnumeratingWithState: state
					       objects: objects
						 count: count];
}

#ifdef OF_HAVE_BLOCKS
- (void)enumerateObjectsUsingBlock: (of_set_enumeration_block_t)block
{

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





}
#endif
@end







<
<



|


<



>



>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>



>
>
>
>
>



|
>
>
>










|


|
>
>
>
>
>
>
>
>
>



<



<
<
<
<
<
<


|
|
<












|





>
>
>
>
>
>
>
>
>

<





|
|
<













|


<
<



|
|
<
<
<











|


<
<

>
>

>
|
>
>
>
>
>
>
>
>
|
<


|
|
<
<
<














<










<
<





|
|
<















|






|







|













|




>
|
>






|
|
|





>
|
|
|
|
>
>
>
>
>



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



#import "OFSet_hashtable.h"
#import "OFMutableSet_hashtable.h"
#import "OFCountedSet_hashtable.h"
#import "OFMapTable.h"
#import "OFArray.h"
#import "OFString.h"

#import "OFXMLElement.h"

#import "OFInvalidArgumentException.h"
#import "OFEnumerationMutationException.h"

#import "autorelease.h"

static void*
retain(void *value)
{
	return [(id)value retain];
}

static void
release(void *value)
{
	[(id)value release];
}

static uint32_t
hash(void *value)
{
	return [(id)value hash];
}

static BOOL
equal(void *value1, void *value2)
{
	return [(id)value1 isEqual: (id)value2];
}

static of_map_table_functions_t keyFunctions = {
	.retain = retain,
	.release = release,
	.hash = hash,
	.equal = equal
};
static of_map_table_functions_t valueFunctions = {};

@implementation OFSet_hashtable
- init
{
	return [self initWithCapacity: 0];
}

- initWithCapacity: (size_t)capacity
{
	self = [super init];

	@try {
		mapTable = [[OFMapTable alloc]
		    initWithKeyFunctions: keyFunctions
			  valueFunctions: valueFunctions
				capacity: capacity];
	} @catch (id e) {
		[self release];
		@throw e;
	}

	return self;
}

- initWithSet: (OFSet*)set
{
	size_t count;

	if (set == nil)
		return [self init];

	@try {
		count = [set count];
	} @catch (id e) {
		[self release];
		@throw e;
	}

	self = [self initWithCapacity: count];

	@try {
		void *pool = objc_autoreleasePoolPush();

		OFEnumerator *enumerator;
		id object;







		enumerator = [set objectEnumerator];
		while ((object = [enumerator nextObject]) != nil)
			[mapTable setValue: (void*)1
				    forKey: object];


		objc_autoreleasePoolPop(pool);
	} @catch (id e) {
		[self release];
		@throw e;
	}

	return self;
}

- initWithArray: (OFArray*)array
{
	size_t count;

	if (array == nil)
		return self;

	@try {
		count = [array count];
	} @catch (id e) {
		[self release];
		@throw e;
	}

	self = [self initWithCapacity: count];

	@try {
		void *pool = objc_autoreleasePoolPush();

		OFEnumerator *enumerator;
		id object;

		enumerator = [array objectEnumerator];
		while ((object = [enumerator nextObject]) != nil)
			[mapTable setValue: (void*)1
				    forKey: object];


		objc_autoreleasePoolPop(pool);
	} @catch (id e) {
		[self release];
		@throw e;
	}

	return self;
}

- initWithObjects: (id const*)objects
	    count: (size_t)count
{
	self = [self initWithCapacity: count];

	@try {


		size_t i;

		for (i = 0; i < count; i++)
			[mapTable setValue: (void*)1
				    forKey: objects[i]];



	} @catch (id e) {
		[self release];
		@throw e;
	}

	return self;
}

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

	@try {


		id object;
		va_list argumentsCopy;
		size_t count;

		va_copy(argumentsCopy, arguments);

		for (count = 1; va_arg(argumentsCopy, id) != nil; count++);

		mapTable = [[OFMapTable alloc]
		    initWithKeyFunctions: keyFunctions
			  valueFunctions: valueFunctions
				capacity: count];

		[mapTable setValue: (void*)1
			    forKey: firstObject];


		while ((object = va_arg(arguments, id)) != nil)
			[mapTable setValue: (void*)1
				    forKey: object];



	} @catch (id e) {
		[self release];
		@throw e;
	}

	return self;
}

- initWithSerialization: (OFXMLElement*)element
{
	self = [self init];

	@try {
		void *pool = objc_autoreleasePoolPush();

		OFEnumerator *enumerator;
		OFXMLElement *child;

		if ((![[element name] isEqual: @"OFSet"] &&
		    ![[element name] isEqual: @"OFMutableSet"]) ||
		    ![[element namespace] isEqual: OF_SERIALIZATION_NS])
			@throw [OFInvalidArgumentException
			    exceptionWithClass: [self class]
				      selector: _cmd];



		enumerator = [[element elementsForNamespace:
		    OF_SERIALIZATION_NS] objectEnumerator];
		while ((child = [enumerator nextObject]) != nil) {
			void *pool2  = objc_autoreleasePoolPush();

			[mapTable setValue: (void*)1
				    forKey: [child objectByDeserializing]];


			objc_autoreleasePoolPop(pool2);
		}

		objc_autoreleasePoolPop(pool);
	} @catch (id e) {
		[self release];
		@throw e;
	}

	return self;
}

- (void)dealloc
{
	[mapTable release];

	[super dealloc];
}

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

- (BOOL)containsObject: (id)object
{
	if (object == nil)
		return NO;

	return ([mapTable valueForKey: object] != nil);
}

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

	if (![object isKindOfClass: [OFSet_hashtable class]] &&
	    ![object isKindOfClass: [OFMutableSet_hashtable class]] &&
	    ![object isKindOfClass: [OFCountedSet_hashtable class]])
		return [super isEqual: object];

	otherSet = object;

	return [otherSet->mapTable isEqual: mapTable];
}

- (OFEnumerator*)objectEnumerator
{
	return [[[OFMapTableEnumeratorWrapper alloc]
	    initWithEnumerator: [mapTable keyEnumerator]
			object: self] autorelease];
}

- (int)countByEnumeratingWithState: (of_fast_enumeration_state_t*)state
			   objects: (id*)objects
			     count: (int)count
{
	return [mapTable countByEnumeratingWithState: state
					     objects: objects
					       count: count];
}

#ifdef OF_HAVE_BLOCKS
- (void)enumerateObjectsUsingBlock: (of_set_enumeration_block_t)block
{
	@try {
		[mapTable enumerateKeysAndValuesUsingBlock:
		    ^ (void *key, void *value, BOOL *stop) {
			block(key, stop);
		}];
	} @catch (OFEnumerationMutationException *e) {
		@throw [OFEnumerationMutationException
		    exceptionWithClass: [self class]
				object: self];
	}
}
#endif
@end

Modified tests/OFSet.m from [aefbf99a85] to [d636bb81c7].

47
48
49
50
51
52
53
54
55
56
57
58
59
60
61

	TEST(@"-[isEqual:]", [set1 isEqual: set2])

	TEST(@"-[hash]", [set1 hash] == [set2 hash])

	TEST(@"-[description]",
	    [[set1 description]
	    isEqual: @"{(\n\tfoo,\n\tbaz,\n\tx,\n\tbar\n)}"] &&
	    [[set1 description] isEqual: [set2 description]])

	TEST(@"-[copy]", [set1 isEqual: [[set1 copy] autorelease]])

	TEST(@"-[mutableCopy]",
	    (mutableSet = [[set1 mutableCopy] autorelease]));








|







47
48
49
50
51
52
53
54
55
56
57
58
59
60
61

	TEST(@"-[isEqual:]", [set1 isEqual: set2])

	TEST(@"-[hash]", [set1 hash] == [set2 hash])

	TEST(@"-[description]",
	    [[set1 description]
	    isEqual: @"{(\n\tx,\n\tbar,\n\tfoo,\n\tbaz\n)}"] &&
	    [[set1 description] isEqual: [set2 description]])

	TEST(@"-[copy]", [set1 isEqual: [[set1 copy] autorelease]])

	TEST(@"-[mutableCopy]",
	    (mutableSet = [[set1 mutableCopy] autorelease]));

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
#ifdef OF_HAVE_FAST_ENUMERATION
	ok = YES;
	i = 0;

	for (OFString *s in set1) {
		switch (i) {
		case 0:
			if (![s isEqual: @"foo"])
				ok = NO;
			break;
		case 1:
			if (![s isEqual: @"baz"])
				ok = NO;
			break;
		case 2:
			if (![s isEqual: @"x"])
				ok = NO;
			break;
		case 3:
			if (![s isEqual: @"bar"])
				ok = NO;
			break;
		}

		i++;
	}








|



|



|



|







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
#ifdef OF_HAVE_FAST_ENUMERATION
	ok = YES;
	i = 0;

	for (OFString *s in set1) {
		switch (i) {
		case 0:
			if (![s isEqual: @"x"])
				ok = NO;
			break;
		case 1:
			if (![s isEqual: @"bar"])
				ok = NO;
			break;
		case 2:
			if (![s isEqual: @"foo"])
				ok = NO;
			break;
		case 3:
			if (![s isEqual: @"baz"])
				ok = NO;
			break;
		}

		i++;
	}

Modified tests/serialization.xml from [4afe0bff59] to [0489605714].

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
            <OFMutableArray>
              <OFXMLElement name='y'/>
              <OFXMLCDATA><![CDATA[<]]></OFXMLCDATA>
            </OFMutableArray>
          </children>
        </OFXMLElement>
        <OFSet>
          <OFString>foo</OFString>
          <OFString>bar</OFString>

        </OFSet>
        <OFCountedSet>



          <object count='2'>
            <OFString>foo</OFString>
          </object>
          <object count='1'>
            <OFString>bar</OFString>
          </object>
        </OFCountedSet>
      </OFList>
    </key>
    <object>
      <OFString>list</OFString>
    </object>
  </OFMutableDictionary>
</serialization>







<

>


>
>
>



<
<
<








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
            <OFMutableArray>
              <OFXMLElement name='y'/>
              <OFXMLCDATA><![CDATA[<]]></OFXMLCDATA>
            </OFMutableArray>
          </children>
        </OFXMLElement>
        <OFSet>

          <OFString>bar</OFString>
          <OFString>foo</OFString>
        </OFSet>
        <OFCountedSet>
          <object count='1'>
            <OFString>bar</OFString>
          </object>
          <object count='2'>
            <OFString>foo</OFString>
          </object>



        </OFCountedSet>
      </OFList>
    </key>
    <object>
      <OFString>list</OFString>
    </object>
  </OFMutableDictionary>
</serialization>