ObjFW  Check-in [f6e79c23b4]

Overview
Comment:Improve -[initWithObjects:forKeys:].
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: f6e79c23b454d83f01ccd351b4160383813d521507db3393fae340fd067b0b49
User & Date: js on 2009-12-05 17:15:02
Other Links: manifest | tags
Context
2009-12-05
17:19
Fix optimization in OFDictionary / OFMutableDictionary. check-in: 011e248c30 user: js tags: trunk
17:15
Improve -[initWithObjects:forKeys:]. check-in: f6e79c23b4 user: js tags: trunk
16:54
Improve readability of -[initWithDictionary:]. check-in: e1b790cdfe user: js tags: trunk
Changes

Modified src/OFDictionary.m from [6ffe55f0db] to [0298515cc1].

131
132
133
134
135
136
137
138
139

140
141
142
143
144
145
146
			[self dealloc];
			@throw e;
		}

		@try {
			[dict->data[i].object retain];
		} @catch (OFException *e) {
			[key release];
			[self dealloc];

			@throw e;
		}

		data[i].key = key;
		data[i].object = dict->data[i].object;
		data[i].hash = dict->data[i].hash;
	}







<

>







131
132
133
134
135
136
137

138
139
140
141
142
143
144
145
146
			[self dealloc];
			@throw e;
		}

		@try {
			[dict->data[i].object retain];
		} @catch (OFException *e) {

			[self dealloc];
			[key release];
			@throw e;
		}

		data[i].key = key;
		data[i].object = dict->data[i].object;
		data[i].hash = dict->data[i].hash;
	}
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
197
198

199
200
201
202


203





204
205












































































206
207
208
209
210
211
212
		[self dealloc];
		@throw e;
	}

	@try {
		[obj retain];
	} @catch (OFException *e) {
		[key release];
		[self dealloc];

		@throw e;
	}

	data[0].key = key;
	data[0].object = obj;
	data[0].hash = [key hash];

	return self;
}

/* FIXME: Do it without resizing! */
- initWithObjects: (OFArray*)objs
	  forKeys: (OFArray*)keys
{
	id *objs_carray, *keys_carray;
	size_t objs_count, i;
	const SEL sel = @selector(setObject:forKey:);
	IMP set = [OFMutableDictionary instanceMethodForSelector: sel];

	self = [self init];

	objs_carray = [objs cArray];
	keys_carray = [keys cArray];

	objs_count = [objs count];

	if (objs == nil || keys == nil || objs_count != [keys count]) {
		Class c = isa;
		[self dealloc];
		@throw [OFInvalidArgumentException newWithClass: c
						       selector: _cmd];
	}


	@try {
		for (i = 0; i < objs_count; i++)
			set(self, sel, objs_carray[i], keys_carray[i]);


	} @catch (OFException *e) {





		[self dealloc];
		@throw e;












































































	}

	return self;
}

- initWithKeysAndObjects: (OFObject <OFCopying>*)first, ...
{







<

>










<




|
<
<

|

|
|
>
|

|
<
<
|
<
|
>

<
<
<
>
>

>
>
>
>
>


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







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
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
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
276
277
278
279
280
281
282
283
284
285
286
287
288
		[self dealloc];
		@throw e;
	}

	@try {
		[obj retain];
	} @catch (OFException *e) {

		[self dealloc];
		[key release];
		@throw e;
	}

	data[0].key = key;
	data[0].object = obj;
	data[0].hash = [key hash];

	return self;
}


- initWithObjects: (OFArray*)objs
	  forKeys: (OFArray*)keys
{
	id *objs_carray, *keys_carray;
	size_t i;



	self = [super init];

	@try {
		keys_carray = [keys cArray];
		objs_carray = [objs cArray];
		count = [keys count];

		if (count > SIZE_MAX / 8)


			@throw [OFOutOfRangeException newWithClass: isa];


		for (size = 1; size < count; size <<= 1);




		data = [self allocMemoryForNItems: size
					 withSize: BUCKET_SIZE];
	} @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;
	}
	memset(data, 0, size * BUCKET_SIZE);

	for (i = 0; i < count; i++) {
		uint32_t j, hash;

		hash = [keys_carray[i] hash];

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

		/* In case the last bucket is already used */
		if (j >= size)
			for (j = 0; j < size && data[j].key != nil &&
			    ![data[j].key isEqual: keys_carray[i]]; j++);

		/* Key not in dictionary */
		if (j >= size || ![data[j].key isEqual: keys_carray[i]]) {
			OFObject <OFCopying> *key;

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

			/* In case the last bucket is already used */
			if (j >= size)
				for (j = 0; j < size && data[j].key != nil;
				    j++);
			if (j >= size) {
				Class c = isa;
				[self dealloc];
				@throw [OFOutOfRangeException newWithClass: c];
			}

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

			@try {
				[objs_carray[i] retain];
			} @catch (OFException *e) {
				[self dealloc];
				[key release];
				@throw e;
			}

			data[j].key = key;
			data[j].object = objs_carray[i];
			data[j].hash = hash;

			continue;
		}

		/*
		 * They key is already in the dictionary. However, we just
		 * replace it so that the programmer gets the same behavior
		 * as if he'd call setObject:forKey: for each key/object pair.
		 */
		@try {
			[objs_carray[i] retain];
		} @catch (OFException *e) {
			[self dealloc];
			@throw e;
		}

		@try {
			[data[j].object release];
		} @catch (OFException *e) {
			[self dealloc];
			[objs_carray[i] release];
			@throw e;
		}

		data[j].object = objs_carray[i];
	}

	return self;
}

- initWithKeysAndObjects: (OFObject <OFCopying>*)first, ...
{