ObjFW  Diff

Differences From Artifact [9fa2393e86]:

  • File src/OFDictionary.m — part of check-in [24ecf55297] at 2009-06-29 12:33:59 on branch trunk — Changes to OFDictionary, OFIterator and OFList - see details.

    OFDictionary:
    * More optimized way to internally store the data.
    * Faster resizing of dictionaries (no rehashing anymore).

    OFIterator:
    * Return a key/object pair rather than first the key and then the
    object.

    OFList:
    * Support for list objects with a different size so you can have your
    own list object structs. (user: js, size: 7621) [annotate] [blame] [check-ins using]

To Artifact [59d8f02b6d]:


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

#include "config.h"

#include <string.h>

#import "OFDictionary.h"
#import "OFIterator.h"

#import "OFExceptions.h"

/* References for static linking */
void _references_to_categories_of_OFDictionary()
{
	_OFIterator_reference = 1;
}

@implementation OFDictionary
+ dictionary;
{
	return [[[self alloc] init] autorelease];
}






+ dictionaryWithHashSize: (int)hashsize
{
	return [[[self alloc] initWithHashSize: hashsize] autorelease];
}

+ dictionaryWithKey: (OFObject <OFCopying>*)key







>













>
>
>
>
>







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

#include "config.h"

#include <string.h>

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

/* References for static linking */
void _references_to_categories_of_OFDictionary()
{
	_OFIterator_reference = 1;
}

@implementation OFDictionary
+ dictionary;
{
	return [[[self alloc] init] autorelease];
}

+ dictionaryWithDictionary: (OFDictionary*)dict
{
	return [[[self alloc] initWithDictionary: dict] autorelease];
}

+ dictionaryWithHashSize: (int)hashsize
{
	return [[[self alloc] initWithHashSize: hashsize] autorelease];
}

+ dictionaryWithKey: (OFObject <OFCopying>*)key
77
78
79
80
81
82
83
















































































84
85
86
87
88
89
90
		 */
		size = 0;
		[self dealloc];
		@throw e;
	}
	memset(data, 0, size * sizeof(OFList*));

















































































	return self;
}

- initWithHashSize: (int)hashsize
{
	self = [super init];








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







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
		 */
		size = 0;
		[self dealloc];
		@throw e;
	}
	memset(data, 0, size * sizeof(OFList*));

	return self;
}

- initWithDictionary: (OFDictionary*)dict
{
	OFAutoreleasePool *pool;
	OFIterator *iter;
	of_iterator_pair_t pair;
	of_iterator_pair_t (*next)(id, SEL);

	self = [super init];

	if (dict == nil) {
		Class c = isa;
		size = 0;
		[self dealloc];
		@throw [OFInvalidArgumentException newWithClass: c
						    andSelector: _cmd];
	}

	size = dict->size;

	@try {
		data = [self allocMemoryForNItems: size
					 withSize: sizeof(OFList*)];
		memset(data, 0, size * sizeof(OFList*));

		pool = [[OFAutoreleasePool alloc] init];
		iter = [dict iterator];
		next = (of_iterator_pair_t(*)(id, SEL))
		    [iter methodForSelector: @selector(nextKeyObjectPair)];
	} @catch (OFException *e) {
		/*
		 * We can't use [super dealloc] on OS X here. Compiler bug?
		 * Anyway, set size to 0 so that [self dealloc] works.
		 */
		size = 0;
		[self dealloc];
		@throw e;
	}

	for (;;) {
		uint32_t hash;
		OFObject <OFCopying> *key;

		pair = next(iter, @selector(nextKeyObjectPair));

		if (pair.key == nil || pair.object == nil)
			break;

		hash = pair.hash & (size - 1);

		@try {
			key = [pair.key copy];
		} @catch (OFException *e) {
			[self dealloc];
			@throw e;
		}

		@try {
			of_dictionary_list_object_t *o;

			if (data[hash] == nil)
				data[hash] = [[OFList alloc]
				    initWithListObjectSize:
				    sizeof(of_dictionary_list_object_t)];

			o = (of_dictionary_list_object_t*)
			    [data[hash] append: pair.object];
			o->key = key;
			o->hash = pair.hash;
		} @catch (OFException *e) {
			[key release];
			[self dealloc];
			@throw e;
		}
	}

	[pool release];

	return self;
}

- initWithHashSize: (int)hashsize
{
	self = [super init];

353
354
355
356
357
358
359
360
361
362
363
364
365
366
367

368
369
370






371
372
373
374
375
376
377
	    iter != NULL; iter = iter->next)
		if ([iter->key isEqual: key])
			return iter->object;

	return nil;
}

/* FIXME: Implement this! */
/*
- (BOOL)isEqual
{
}

- (id)copy
{

}

- (id)mutableCopy






{
}
*/

- (void)dealloc
{
	size_t i;







<
<
<
<
<
<


>



>
>
>
>
>
>







439
440
441
442
443
444
445






446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
	    iter != NULL; iter = iter->next)
		if ([iter->key isEqual: key])
			return iter->object;

	return nil;
}







- (id)copy
{
	return [self retain];
}

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

/* FIXME: Implement this!
- (BOOL)isEqual
{
}
*/

- (void)dealloc
{
	size_t i;