ObjFW  Check-in [2555952c42]

Overview
Comment:Implement -[isEqual:] and -[hash] for OFDictionary.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 2555952c422b18269fd4b6314434bb147e3ad7564fd0413588571142b5641d07
User & Date: js on 2009-11-13 23:18:03
Other Links: manifest | tags
Context
2009-11-14
12:17
New solution for TLS key destructors that works on any OS. check-in: 12101c192f user: js tags: trunk
2009-11-13
23:18
Implement -[isEqual:] and -[hash] for OFDictionary. check-in: 2555952c42 user: js tags: trunk
23:17
Fix a bug in OFDictionary's -[initWithDictionary:]. check-in: 679bce8526 user: js tags: trunk
Changes

Modified src/OFDictionary.m from [88ba6224ae] to [2dc6749eb1].

13
14
15
16
17
18
19

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







+








#include <string.h>

#import "OFDictionary.h"
#import "OFIterator.h"
#import "OFAutoreleasePool.h"
#import "OFExceptions.h"
#import "OFMacros.h"

#define BUCKET_SIZE sizeof(struct of_dictionary_bucket)

/* References for static linking */
void _references_to_categories_of_OFDictionary()
{
	_OFIterator_reference = 1;
247
248
249
250
251
252
253
254
255

256

257
258











259
260
261
262
263
264
265
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







-
-
+

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







}

- (id)mutableCopy
{
	return [[OFMutableDictionary alloc] initWithDictionary: self];
}

/* FIXME: Implement this!
- (BOOL)isEqual
- (BOOL)isEqual: (OFDictionary*)dict
{
	size_t i;
}
*/

	if ([dict count] != count)
		return NO;

	for (i = 0; i < size; i++)
		if (data[i].key != nil &&
		    ![[dict objectForKey: data[i].key] isEqual: data[i].object])
			return NO;

	return YES;
}

- (void)dealloc
{
	size_t i;

	for (i = 0; i < size; i++) {
		if (data[i].key != nil) {
280
281
282
283
284
285
286
287
288
289


290
291























292
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







-


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


- removeObjectForKey: (OFObject*)key
{
	@throw [OFNotImplementedException newWithClass: isa
					      selector: _cmd];
}

/* FIXME: Implement!
- (uint32_t)hash
{
	size_t i;
	uint32_t hash;
}
*/

	OF_HASH_INIT(hash);

	for (i = 0; i < size; i++) {
		if (data[i].key != nil) {
			uint32_t kh = [data[i].key hash];

			OF_HASH_ADD(hash, kh >> 24);
			OF_HASH_ADD(hash, (kh >> 16) & 0xFF);
			OF_HASH_ADD(hash, (kh >> 8) & 0xFF);
			OF_HASH_ADD(hash, kh & 0xFF);

			OF_HASH_ADD(hash, data[i].hash >> 24);
			OF_HASH_ADD(hash, (data[i].hash >> 16) & 0xFF);
			OF_HASH_ADD(hash, (data[i].hash >> 8) & 0xFF);
			OF_HASH_ADD(hash, data[i].hash & 0xFF);
		}
	}

	OF_HASH_FINALIZE(hash);

	return hash;
}
@end

Modified tests/OFDictionary.m from [c71c03a3bd] to [1fa12ca59b].

28
29
30
31
32
33
34
35

36
37
38
39
40
41
42
28
29
30
31
32
33
34

35
36
37
38
39
40
41
42







-
+







	@"value2"
};

void
dictionary_tests()
{
	OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init];
	OFDictionary *dict = [OFMutableDictionary dictionary];
	OFDictionary *dict = [OFMutableDictionary dictionary], *dict2;
	OFIterator *iter;
	of_iterator_pair_t pair[3];
	OFArray *akeys, *avalues;

	[dict setObject: values[0]
		 forKey: keys[0]];
	[dict setObject: values[1]
82
83
84
85
86
87
88

89
90

91
92
93
94
95
96
97
98
99
100
101
102
103









104
105
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







+


+













+
+
+
+
+
+
+
+
+


	    [[dict objectForKey: keys[1]] isEqual: values[1]])

	TEST(@"-[copy]",
	    (dict = [[dict copy] autorelease]) &&
	    [[dict objectForKey: keys[0]] isEqual: values[0]] &&
	    [[dict objectForKey: keys[1]] isEqual: values[1]])

	dict2 = dict;
	TEST(@"-[mutableCopy]",
	    (dict = [[dict mutableCopy] autorelease]) &&
	    [dict count] == [dict2 count] &&
	    [[dict objectForKey: keys[0]] isEqual: values[0]] &&
	    [[dict objectForKey: keys[1]] isEqual: values[1]] &&
	    [dict setObject: @"value3"
		     forKey: @"key3"] &&
	    [[dict objectForKey: @"key3"] isEqual: @"value3"] &&
	    [dict setObject: @"foo"
		     forKey: keys[0]] &&
	    [[dict objectForKey: keys[0]] isEqual: @"foo"])

	TEST(@"-[removeObjectForKey:]",
	    [dict removeObjectForKey: keys[0]] &&
	    [dict objectForKey: keys[0]] == nil)

	[dict setObject: @"foo"
		 forKey: keys[0]];
	TEST(@"-[isEqual:]", ![dict isEqual: dict2] &&
	    [dict removeObjectForKey: @"key3"] &&
	    ![dict isEqual: dict2] &&
	    [dict setObject: values[0]
		     forKey: keys[0]] &&
	    [dict isEqual: dict2])

	[pool release];
}