ObjFW  Check-in [c90c6510f1]

Overview
Comment:Add - averageItemsPerBucket to OFDictionary.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: c90c6510f132e9b48015f005abdc7bb945ca91b9a2b1eb6311a5e55972bf2a4b
User & Date: js on 2009-05-04 18:04:28
Other Links: manifest | tags
Context
2009-05-04
21:22
Fix OF(Mutable)String documentation. check-in: 82334856a8 user: js tags: trunk
18:04
Add - averageItemsPerBucket to OFDictionary. check-in: c90c6510f1 user: js tags: trunk
17:54
Add - items to OFList. check-in: 59ab431af4 user: js tags: trunk
Changes

Modified src/OFDictionary.h from [a9019ce8f0] to [c2545c6818].

69
70
71
72
73
74
75







76
77
78
79
80
81
82
/**
 * 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







>
>
>
>
>
>
>







69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/**
 * Remove the object with the given key from the dictionary.
 *
 * \param key The key whose object should be removed
 */
- remove: (OFObject*)key;

/**
 * \return The average number of items in a used bucket. Buckets that are
 *	   completely empty are not in the calculation. If this value is >= 2.0,
 *	   you should resize the dictionary, in most cases even earlier!
 */
- (float)averageItemsPerBucket;

/**
 * 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 [4972fbd8f3] to [078e9942b2].

171
172
173
174
175
176
177

















178
179
180
181
182
183
184

			return self;
		}
	}

	@throw [OFNotInSetException newWithClass: isa];
}


















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








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







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

			return self;
		}
	}

	@throw [OFNotInSetException newWithClass: isa];
}

- (float)averageItemsPerBucket
{
	size_t items, buckets, i;

	items = 0;
	buckets = 0;

	for (i = 0; i < size; i++) {
		if (data[i] != nil) {
			items += [data[i] items] / 2;
			buckets++;
		}
	}

	return (float)items / buckets;
}

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

Modified tests/OFDictionary/OFDictionary.m from [4a94ec8c60] to [47966a2668].

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;
}







|




|







|



<






|
>
>
>
>
>










|










|











|



|



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
	[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/8 failed!\033[m");
		return 1;
	}

	if (strcmp([[dict get: key2] cString], "value2")) {
		puts("\033[K\033[1;31mTest 2/8 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/8 failed!\033[m");
		return 1;
	}


	[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/8 failed!\033[m");
		return 1;
	}

	if ([dict averageItemsPerBucket] != 1.0) {
		puts("\033[K\033[1;31mTest 5/8 failed!\033[m");
		return 1;
	}

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

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

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

	return 0;
}