ObjFW  Check-in [5e77541e4d]

Overview
Comment:Add - changeHashSize: to OFDictionary.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 5e77541e4d439463a3c0c021d964be9a545df47da09fcca0ea0206196a523846
User & Date: js on 2009-05-04 17:27:17
Other Links: manifest | tags
Context
2009-05-04
17:54
Add - items to OFList. check-in: 59ab431af4 user: js tags: trunk
17:27
Add - changeHashSize: to OFDictionary. check-in: 5e77541e4d user: js tags: trunk
16:57
Fix wrong memset in OFDictionary. check-in: 27a9132794 user: js tags: trunk
Changes

Modified src/OFDictionary.h from [0e40b77f27] to [a9019ce8f0].

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
/**
 * Creates a new OFDictionary, defaulting to a 12 bit hash.
 *
 * \return A new autoreleased OFDictionary
 */
+ dictionary;

/*
 * Creates a new OFDictionary with a hash of N bits.
 *
 * \param bits The size of the hash to use
 * \return A new autoreleased OFDictionary
 */
+ dictionaryWithHashSize: (int)hashsize;

/**
 * Initializes an already allocated OFDictionary, defaulting to a 12 bit hash.
 *
 * \return An initialized OFDictionary
 */
- init;

/*
 * Initializes an already allocated OFDictionary with a hash of N bits.
 *
 * \param bits The size of the hash to use
 * \return An initialized OFDictionary
 */
- initWithHashSize: (int)hashsize;

/*
 * Sets a key to an object. A key can be any object.
 *
 * \param key The key to set
 * \param obj The object to set the key to
 */
- set: (OFObject*)key
   to: (OFObject*)obj;

/*
 * \param key The key whose object should be returned
 * \return The object for the given key
 */
- get: (OFObject*)key;

/*
 * Remove the object with the given key from the dictionary.
 *
 * \param key The key whose object should be removed
 */
- remove: (OFObject*)key;







@end







|














|







|








|





|





>
>
>
>
>
>
>

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
/**
 * Creates a new OFDictionary, defaulting to a 12 bit hash.
 *
 * \return A new autoreleased OFDictionary
 */
+ dictionary;

/**
 * Creates a new OFDictionary with a hash of N bits.
 *
 * \param bits The size of the hash to use
 * \return A new autoreleased OFDictionary
 */
+ dictionaryWithHashSize: (int)hashsize;

/**
 * Initializes an already allocated OFDictionary, defaulting to a 12 bit hash.
 *
 * \return An initialized OFDictionary
 */
- init;

/**
 * Initializes an already allocated OFDictionary with a hash of N bits.
 *
 * \param bits The size of the hash to use
 * \return An initialized OFDictionary
 */
- initWithHashSize: (int)hashsize;

/**
 * Sets a key to an object. A key can be any object.
 *
 * \param key The key to set
 * \param obj The object to set the key to
 */
- set: (OFObject*)key
   to: (OFObject*)obj;

/**
 * \param key The key whose object should be returned
 * \return The object for the given key
 */
- get: (OFObject*)key;

/**
 * Remove the object with the given key from the dictionary.
 *
 * \param key The key whose object should be removed
 */
- remove: (OFObject*)key;

/**
 * Changes the hash size of the dictionary.
 *
 * \param hashsize The new hash size for the dictionary
 */
- changeHashSize: (int)hashsize;
@end

Modified src/OFDictionary.m from [3e22e7bae1] to [4972fbd8f3].

12
13
14
15
16
17
18

19
20
21
22
23
24
25
#import "config.h"

#include <string.h>

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


/* Reference for static linking */
void _reference_to_OFIterator_in_OFDictionary() { [OFIterator class]; }

@implementation OFDictionary
+ dictionary;
{







>







12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#import "config.h"

#include <string.h>

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

/* Reference for static linking */
void _reference_to_OFIterator_in_OFDictionary() { [OFIterator class]; }

@implementation OFDictionary
+ dictionary;
{
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
	return self;
}

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

	if (hashsize < 8 || hashsize > 31) {
		Class c = isa;
		[super free];
		@throw [OFInvalidArgumentException
			newWithClass: c
			 andSelector: _cmd];
	}

	size = (size_t)1 << hashsize;

	@try {
		data = [self allocNItems: size
				withSize: sizeof(OFList*)];







|


|
<
|







55
56
57
58
59
60
61
62
63
64
65

66
67
68
69
70
71
72
73
	return self;
}

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

	if (hashsize < 8 || hashsize >= 28) {
		Class c = isa;
		[super free];
		@throw [OFInvalidArgumentException newWithClass: c

						    andSelector: _cmd];
	}

	size = (size_t)1 << hashsize;

	@try {
		data = [self allocNItems: size
				withSize: sizeof(OFList*)];
171
172
173
174
175
176
177








































178
179
180
181
182
183
184
185

			return self;
		}
	}

	@throw [OFNotInSetException newWithClass: isa];
}









































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







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








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

			return self;
		}
	}

	@throw [OFNotInSetException newWithClass: isa];
}

- changeHashSize: (int)hashsize
{
	OFList **newdata;
	size_t newsize, i;
	of_list_object_t *iter;

	if (hashsize < 8 || hashsize >= 28)
		@throw [OFInvalidArgumentException newWithClass: isa
						    andSelector: _cmd];

	newsize = (size_t)1 << hashsize;
	newdata = [self allocNItems: newsize
			   withSize: sizeof(OFList*)];
	memset(data, 0, newsize * sizeof(OFList*));

	for (i = 0; i < size; i++) {
		if (OF_LIKELY(data[i] == nil))
			continue;

		for (iter = [data[i] first]; iter != NULL;
		    iter = iter->next->next) {
			uint32_t hash = [iter->object hash] & (newsize - 1);

			if (newdata[hash] == nil)
				newdata[hash] = [[OFList alloc] init];

			[newdata[hash] append: iter->object];
			[newdata[hash] append: iter->next->object];
		}

		[data[i] release];
	}

	[self freeMem: data];
	data = newdata;
	size = newsize;

	return self;
}

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

Modified tests/OFDictionary/OFDictionary.m from [51f348148f] to [4a94ec8c60].

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
	[dict set: key1
	       to: value1];
	[dict set: key2
	       to: value2];
	[pool release];

	if (strcmp([[dict get: @"key1"] cString], "value1")) {
		puts("\033[K\033[1;31mTest 1/6 failed!\033[m");
		return 1;
	}

	if (strcmp([[dict get: key2] cString], "value2")) {
		puts("\033[K\033[1;31mTest 2/6 failed!\033[m");
		return 1;
	}

	if (![[iter nextObject] isEqual: @"key2"] ||
	    ![[iter nextObject] isEqual: @"value2"] ||
	    ![[iter nextObject] isEqual: @"key1"] ||
	    ![[iter nextObject] isEqual: @"value1"]) {
		puts("\033[K\033[1;31mTest 3/6 failed!\033[m");











		return 1;
	}

	caught = NO;
	@try {
		[iter nextObject];
	} @catch (OFNotInSetException *e) {
		caught = YES;
	}
	if (!caught) {
		puts("\033[K\033[1;31mTest 4/6 failed!\033[m");
		return 1;
	}

	caught = NO;
	@try {
		[dict get: @"key3"];
	} @catch (OFNotInSetException *e) {
		caught = YES;
	}
	if (!caught) {
		puts("\033[K\033[1;31mTest 5/6 failed!\033[m");
		return 1;
	}

	[dict remove: @"key2"];
	caught = NO;
	@try {
		[dict remove: @"key2"];
	} @catch (OFNotInSetException *e) {
		caught = YES;
	}
	if (!caught) {
		puts("\033[K\033[1;31mTest 6/6 failed!\033[m");
		return 1;
	}

	puts("\033[1;32mTests successful: 6/6\033[0m");

	return 0;
}







|




|







|
>
>
>
>
>
>
>
>
>
>
>










|










|











|



|



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
	[dict set: key1
	       to: value1];
	[dict set: key2
	       to: value2];
	[pool release];

	if (strcmp([[dict get: @"key1"] cString], "value1")) {
		puts("\033[K\033[1;31mTest 1/7 failed!\033[m");
		return 1;
	}

	if (strcmp([[dict get: key2] cString], "value2")) {
		puts("\033[K\033[1;31mTest 2/7 failed!\033[m");
		return 1;
	}

	if (![[iter nextObject] isEqual: @"key2"] ||
	    ![[iter nextObject] isEqual: @"value2"] ||
	    ![[iter nextObject] isEqual: @"key1"] ||
	    ![[iter nextObject] isEqual: @"value1"]) {
		puts("\033[K\033[1;31mTest 3/7 failed!\033[m");
		return 1;
	}

	[iter release];
	[dict changeHashSize: 8];
	iter = [dict iterator];
	if (![[iter nextObject] isEqual: @"key1"] ||
	    ![[iter nextObject] isEqual: @"value1"] ||
	    ![[iter nextObject] isEqual: @"key2"] ||
	    ![[iter nextObject] isEqual: @"value2"]) {
		puts("\033[K\033[1;31mTest 4/7 failed!\033[m");
		return 1;
	}

	caught = NO;
	@try {
		[iter nextObject];
	} @catch (OFNotInSetException *e) {
		caught = YES;
	}
	if (!caught) {
		puts("\033[K\033[1;31mTest 5/7 failed!\033[m");
		return 1;
	}

	caught = NO;
	@try {
		[dict get: @"key3"];
	} @catch (OFNotInSetException *e) {
		caught = YES;
	}
	if (!caught) {
		puts("\033[K\033[1;31mTest 6/7 failed!\033[m");
		return 1;
	}

	[dict remove: @"key2"];
	caught = NO;
	@try {
		[dict remove: @"key2"];
	} @catch (OFNotInSetException *e) {
		caught = YES;
	}
	if (!caught) {
		puts("\033[K\033[1;31mTest 7/7 failed!\033[m");
		return 1;
	}

	puts("\033[1;32mTests successful: 7/7\033[0m");

	return 0;
}