ObjFW  Check-in [f1e3f965fc]

Overview
Comment:Add -[indexOfObject:] and -[indexOfObjectIdenticalTo:] to OFArray.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: f1e3f965fcbb3df6572d9187de303d9cfdfd0720acee13d25a0e7fada6d085fb
User & Date: js on 2009-11-28 20:29:39
Other Links: manifest | tags
Context
2009-11-28
20:44
Don't care in OFDataArray if realloc failed if we only made it smaller. check-in: 044eee2ed9 user: js tags: trunk
20:29
Add -[indexOfObject:] and -[indexOfObjectIdenticalTo:] to OFArray. check-in: f1e3f965fc user: js tags: trunk
2009-11-26
09:50
Add -[firstItem] to OFDataArray and -[firstObject] to OFArray. check-in: 4d523be264 user: js tags: trunk
Changes

Modified src/OFArray.h from [10a046cc18] to [48f509abb1].

93
94
95
96
97
98
99



















100
101
102
103
104
105
106
 * Returns a specific object of the OFArray.
 *
 * \param index The number of the object to return
 * \return The specified object of the OFArray
 */
- (id)objectAtIndex: (size_t)index;




















/**
 * \return The first object of the OFArray or nil
 */
- (id)firstObject;

/**
 * \return The last object of the OFArray or nil







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







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
 * Returns a specific object of the OFArray.
 *
 * \param index The number of the object to return
 * \return The specified object of the OFArray
 */
- (id)objectAtIndex: (size_t)index;

/**
 * Returns the index of the first object that is equivalent to the specified
 * object.
 *
 * \param obj The object whose index is returned
 * \return The index of the first object equivalent to the specified object
 */
- (size_t)indexOfObject: (id)obj;

/**
 * Returns the index of the first object that has the same address as the
 * specified object.
 *
 * \param obj The object whose index is returned
 * \return The index of the first object that has the same aaddress as
 *	   the specified object
 */
- (size_t)indexOfObjectIdenticalTo: (id)obj;

/**
 * \return The first object of the OFArray or nil
 */
- (id)firstObject;

/**
 * \return The last object of the OFArray or nil

Modified src/OFArray.m from [4ee8184863] to [e74beec694].

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

- (id)objectAtIndex: (size_t)index
{
	return *((OFObject**)[array itemAtIndex: index]);
}































- (id)firstObject
{
	void *first = [array firstItem];

	return (first != NULL ? *((id*)first) : nil);
}

- (id)lastObject
{
	void *last = [array lastItem];

	return (last != NULL ? *((id*)last) : nil);
}

- (BOOL)isEqual: (id)obj
{
	OFObject **objs, **objs2;
	size_t i, len, len2;









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


|

|




|

|







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

- (id)objectAtIndex: (size_t)index
{
	return *((OFObject**)[array itemAtIndex: index]);
}

- (size_t)indexOfObject: (id)obj
{
	id *objs = [array cArray];
	size_t i, count = [array count];

	if (objs == NULL)
		return SIZE_MAX;

	for (i = 0; i < count; i++)
		if ([objs[i] isEqual: obj])
			return i;

	return SIZE_MAX;
}

- (size_t)indexOfObjectIdenticalTo: (id)obj
{
	id *objs = [array cArray];
	size_t i, count = [array count];

	if (objs == NULL)
		return SIZE_MAX;

	for (i = 0; i < count; i++)
		if (objs[i] == obj)
			return i;

	return SIZE_MAX;
}

- (id)firstObject
{
	id *first = [array firstItem];

	return (first != NULL ? *first : nil);
}

- (id)lastObject
{
	id *last = [array lastItem];

	return (last != NULL ? *last : nil);
}

- (BOOL)isEqual: (id)obj
{
	OFObject **objs, **objs2;
	size_t i, len, len2;

208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
			return NO;

	return YES;
}

- (uint32_t)hash
{
	OFObject **carray = [array cArray];
	size_t i, count = [array count];
	uint32_t hash;

	OF_HASH_INIT(hash);

	for (i = 0; i < count; i++) {
		uint32_t h = [carray[i] hash];

		OF_HASH_ADD(hash, h >> 24);
		OF_HASH_ADD(hash, (h >> 16) & 0xFF);
		OF_HASH_ADD(hash, (h >> 8) & 0xFF);
		OF_HASH_ADD(hash, h & 0xFF);
	}








|






|







238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
			return NO;

	return YES;
}

- (uint32_t)hash
{
	OFObject **objs = [array cArray];
	size_t i, count = [array count];
	uint32_t hash;

	OF_HASH_INIT(hash);

	for (i = 0; i < count; i++) {
		uint32_t h = [objs[i] hash];

		OF_HASH_ADD(hash, h >> 24);
		OF_HASH_ADD(hash, (h >> 16) & 0xFF);
		OF_HASH_ADD(hash, (h >> 8) & 0xFF);
		OF_HASH_ADD(hash, h & 0xFF);
	}

Modified tests/OFArray.m from [e9a290fd61] to [ae1625611c].

53
54
55
56
57
58
59





60
61
62
63
64
65
66
	    [[a[0] objectAtIndex: 2] isEqual: c_ary[2]] &&
	    [[a[1] objectAtIndex: 0] isEqual: c_ary[0]] &&
	    [[a[1] objectAtIndex: 1] isEqual: c_ary[1]] &&
	    [[a[1] objectAtIndex: 2] isEqual: c_ary[2]] &&
	    [[a[2] objectAtIndex: 0] isEqual: c_ary[0]] &&
	    [[a[2] objectAtIndex: 1] isEqual: c_ary[1]] &&
	    [[a[2] objectAtIndex: 2] isEqual: c_ary[2]])






	TEST(@"-[removeNObjects:]", [a[0] removeNObjects: 2] &&
	    [a[0] count] == 1 && [[a[0] objectAtIndex: 0] isEqual: c_ary[0]])

	a[1] = [[a[1] mutableCopy] autorelease];
	TEST(@"-[removeObjectAtIndex:]", [a[1] removeObjectAtIndex: 1] &&
	    [a[1] count] == 2 && [[a[1] objectAtIndex: 1] isEqual: c_ary[2]])







>
>
>
>
>







53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
	    [[a[0] objectAtIndex: 2] isEqual: c_ary[2]] &&
	    [[a[1] objectAtIndex: 0] isEqual: c_ary[0]] &&
	    [[a[1] objectAtIndex: 1] isEqual: c_ary[1]] &&
	    [[a[1] objectAtIndex: 2] isEqual: c_ary[2]] &&
	    [[a[2] objectAtIndex: 0] isEqual: c_ary[0]] &&
	    [[a[2] objectAtIndex: 1] isEqual: c_ary[1]] &&
	    [[a[2] objectAtIndex: 2] isEqual: c_ary[2]])

	TEST(@"-[indexOfObject:]", [a[0] indexOfObject: c_ary[1]] == 1)

	TEST(@"-[indexOfObjectIdenticalTo:]",
	    [a[0] indexOfObjectIdenticalTo: c_ary[1]] == 1)

	TEST(@"-[removeNObjects:]", [a[0] removeNObjects: 2] &&
	    [a[0] count] == 1 && [[a[0] objectAtIndex: 0] isEqual: c_ary[0]])

	a[1] = [[a[1] mutableCopy] autorelease];
	TEST(@"-[removeObjectAtIndex:]", [a[1] removeObjectAtIndex: 1] &&
	    [a[1] count] == 2 && [[a[1] objectAtIndex: 1] isEqual: c_ary[2]])