ObjFW  Check-in [9f260d5f50]

Overview
Comment:Rewrite OFDictionary code to make it more readable.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 9f260d5f5080374e720cded2c2862c5a292013c3b7ecef65c9708ea0aaa25075
User & Date: js on 2010-04-17 11:12:52
Other Links: manifest | tags
Context
2010-04-17
13:29
Reduce memory usage of OFDictionary and fix hashing. check-in: d46212a8cd user: js tags: trunk
11:12
Rewrite OFDictionary code to make it more readable. check-in: 9f260d5f50 user: js tags: trunk
10:48
Due to a 32 bit hash, a dictionary can never be bigger than UINT32_MAX. check-in: b9015dbc75 user: js tags: trunk
Changes

Modified src/OFDictionary.m from [f856f20ee2] to [cdc962433d].

506
507
508
509
510
511
512
513



514

515

516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532

533
534
535
536
537
538
539
540
	if (key == nil)
		@throw [OFInvalidArgumentException newWithClass: isa
						       selector: _cmd];

	hash = [key hash];
	last = size;

	for (i = hash & (size - 1); i < last && data[i].key != nil &&



	    (data[i].key == DELETED || ![data[i].key isEqual: key]); i++);



	if (i < last && (data[i].key == nil || data[i].key == DELETED))
		return nil;

	/* In case the last bucket is already used */
	if (i >= last) {
		last = hash & (size - 1);

		for (i = 0; i < last && data[i].key != nil &&
		    (data[i].key == DELETED || ![data[i].key isEqual: key]);
		    i++);
	}

	/* Key not in dictionary */
	if (i >= last || data[i].key == nil || data[i].key == DELETED ||
	    ![data[i].key isEqual: key])
		return nil;


	return [[data[i].object retain] autorelease];
}

- (size_t)count
{
	return count;
}








|
>
>
>
|
>
|
>
|



<
|

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







506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524

525
526
527
528
529
530



531
532
533
534
535
536
537
538
539
540
541
542
	if (key == nil)
		@throw [OFInvalidArgumentException newWithClass: isa
						       selector: _cmd];

	hash = [key hash];
	last = size;

	for (i = hash & (size - 1); i < last && data[i].key != nil; i++) {
		if (data[i].key == DELETED)
			continue;

		if ([data[i].key isEqual: key])
			return [[data[i].object retain] autorelease];
	}

	if (i < last)
		return nil;

	/* In case the last bucket is already used */

	last = hash & (size - 1);

	for (i = 0; i < last && data[i].key != nil; i++) {
		if (data[i].key == DELETED)
			continue;




		if ([data[i].key isEqual: key])
			return [[data[i].object retain] autorelease];
	}

	return nil;
}

- (size_t)count
{
	return count;
}

Modified src/OFMutableDictionary.m from [74e169c6cb] to [c6cdd10ae1].

87
88
89
90
91
92
93
94



95


96
97
98
99
100
101
102

103



104
105
106
107
108
109
110
	if (key == nil || obj == nil)
		@throw [OFInvalidArgumentException newWithClass: isa
						       selector: _cmd];

	hash = [key hash];
	last = size;

	for (i = hash & (size - 1); i < last && data[i].key != nil &&



	    (data[i].key == DELETED || ![data[i].key isEqual: key]); i++);



	/* In case the last bucket is already used */
	if (i >= last) {
		last = hash & (size - 1);

		for (i = 0; i < last && data[i].key != nil &&
		    (data[i].key == DELETED || ![data[i].key isEqual: key]);

		    i++);



	}

	/* Key not in dictionary */
	if (i >= last || data[i].key == nil || data[i].key == DELETED ||
	    ![data[i].key isEqual: key]) {
		[self _resizeForCount: count + 1];








|
>
>
>
|
>
>





|
|
>
|
>
>
>







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
	if (key == nil || obj == nil)
		@throw [OFInvalidArgumentException newWithClass: isa
						       selector: _cmd];

	hash = [key hash];
	last = size;

	for (i = hash & (size - 1); i < last && data[i].key != nil; i++) {
		if (data[i].key == DELETED)
			continue;

		if ([data[i].key isEqual: key])
			break;
	}

	/* In case the last bucket is already used */
	if (i >= last) {
		last = hash & (size - 1);

		for (i = 0; i < last && data[i].key != nil; i++) {
			if (data[i].key == DELETED)
				continue;

			if ([data[i].key isEqual: key])
				break;
		}
	}

	/* Key not in dictionary */
	if (i >= last || data[i].key == nil || data[i].key == DELETED ||
	    ![data[i].key isEqual: key]) {
		[self _resizeForCount: count + 1];

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
	if (key == nil)
		@throw [OFInvalidArgumentException newWithClass: isa
						       selector: _cmd];

	hash = [key hash];
	last = size;

	for (i = hash & (size - 1); i < last && data[i].key != nil &&
	    (data[i].key == DELETED || ![data[i].key isEqual: key]); i++);


	if (i < last && (data[i].key == nil || data[i].key == DELETED ||
	    ![data[i].key isEqual: key]))








		return self;






	/* In case the last bucket is already used */
	if (i >= last) {
		last = hash & (size - 1);

		for (i = 0; i < last && data[i].key != nil &&
		    (data[i].key == DELETED || ![data[i].key isEqual: key]);
		    i++);
	}

	/* Key not in dictionary */
	if (i >= last || data[i].key == nil || data[i].key == DELETED ||
	    ![data[i].key isEqual: key])
		return self;

	[data[i].key release];
	[data[i].object release];
	data[i].key = DELETED;

	count--;
	mutations++;
	[self _resizeForCount: count];





	return self;
}

- (id)copy
{
	return [[OFDictionary alloc] initWithDictionary: self];







|
|
>

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

<
|

|
|
|
|
<
<
<
|
<
<
|
|
|

|
|
|
>
>
>
>







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
	if (key == nil)
		@throw [OFInvalidArgumentException newWithClass: isa
						       selector: _cmd];

	hash = [key hash];
	last = size;

	for (i = hash & (size - 1); i < last && data[i].key != nil; i++) {
		if (data[i].key == DELETED)
			continue;


		if ([data[i].key isEqual: key]) {
			[data[i].key release];
			[data[i].object release];
			data[i].key = DELETED;

			count--;
			mutations++;
			[self _resizeForCount: count];

			return self;
		}
	}

	if (i < last)
		return self;

	/* In case the last bucket is already used */

	last = hash & (size - 1);

	for (i = 0; i < last && data[i].key != nil; i++) {
		if (data[i].key == DELETED)
			continue;




		if ([data[i].key isEqual: key]) {


			[data[i].key release];
			[data[i].object release];
			data[i].key = DELETED;

			count--;
			mutations++;
			[self _resizeForCount: count];

			return self;
		}
	}

	return self;
}

- (id)copy
{
	return [[OFDictionary alloc] initWithDictionary: self];