ObjFW  Check-in [bd9f5d90ae]

Overview
Comment:Add -[removeObject:] and -[removeObjectIdenticalTo:] to OFMutableArray.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: bd9f5d90aedcdb6f8490b99f4d387290e83ecf06e7f1bd92442eaa33c4ba695a
User & Date: js on 2009-11-28 20:48:44
Other Links: manifest | tags
Context
2009-11-29
01:59
Add OF_BSWAP{16,32,64}_CONST[_IF_{BE,LE}]. check-in: 68fb0166a7 user: js tags: trunk
2009-11-28
20:48
Add -[removeObject:] and -[removeObjectIdenticalTo:] to OFMutableArray. check-in: bd9f5d90ae user: js tags: trunk
20:44
Don't care in OFDataArray if realloc failed if we only made it smaller. check-in: 044eee2ed9 user: js tags: trunk
Changes

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

123
124
125
126
127
128
129


130
131
132
133
134
135
136

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

- addObject: (OFObject*)obj;


- removeObjectAtIndex: (size_t)index;
- removeNObjects: (size_t)nobjects;
- removeNObjects: (size_t)nobjects
	 atIndex: (size_t)index;
@end

#import "OFMutableArray.h"







>
>







123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138

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

- addObject: (OFObject*)obj;
- removeObject: (id)obj;
- removeObjectIdenticalTo: (id)obj;
- removeObjectAtIndex: (size_t)index;
- removeNObjects: (size_t)nobjects;
- removeNObjects: (size_t)nobjects
	 atIndex: (size_t)index;
@end

#import "OFMutableArray.h"

Modified src/OFArray.m from [e74beec694] to [da98a6238c].

149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
	return [self retain];
}

- (id)mutableCopy
{
	OFArray *new = [[OFMutableArray alloc] init];
	OFObject **objs;
	size_t len, i;

	objs = [array cArray];
	len = [array count];

	[new->array addNItems: len
		   fromCArray: objs];

	for (i = 0; i < len; i++)
		[objs[i] retain];

	return new;
}

- (id)objectAtIndex: (size_t)index
{







|


|

|


|







149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
	return [self retain];
}

- (id)mutableCopy
{
	OFArray *new = [[OFMutableArray alloc] init];
	OFObject **objs;
	size_t count, i;

	objs = [array cArray];
	count = [array count];

	[new->array addNItems: count
		   fromCArray: objs];

	for (i = 0; i < count; i++)
		[objs[i] retain];

	return new;
}

- (id)objectAtIndex: (size_t)index
{
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

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

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

	if (![obj isKindOfClass: [OFArray class]])
		return NO;

	len = [array count];
	len2 = [obj count];

	if (len != len2)
		return NO;

	objs = [array cArray];
	objs2 = [obj cArray];

	for (i = 0; i < len; i++)
		if (![objs[i] isEqual: objs2[i]])
			return NO;

	return YES;
}

- (uint32_t)hash







|




|
|

|





|







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

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

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

	if (![obj isKindOfClass: [OFArray class]])
		return NO;

	count = [array count];
	count2 = [obj count];

	if (count != count2)
		return NO;

	objs = [array cArray];
	objs2 = [obj cArray];

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

	return YES;
}

- (uint32_t)hash
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
289
290
	OF_HASH_FINALIZE(hash);

	return hash;
}

- (void)dealloc
{
	OFObject **objs;
	size_t len, i;

	if (array != nil) {
		objs = [array cArray];
		len = [array count];

		for (i = 0; i < len; i++)
			[objs[i] release];

		[array release];
	}

	[super dealloc];
}

- addObject: (OFObject*)obj












{
	@throw [OFNotImplementedException newWithClass: isa
					      selector: _cmd];
}

- removeObjectAtIndex: (size_t)index
{







|
<
<
<
<
|

|
|

|
<





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







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
289
290
291
292
293
294
295
296
297
	OF_HASH_FINALIZE(hash);

	return hash;
}

- (void)dealloc
{
	OFObject **objs = [array cArray];




	size_t i, count = [array count];

	for (i = 0; i < count; i++)
		[objs[i] release];

	[array release];


	[super dealloc];
}

- addObject: (OFObject*)obj
{
	@throw [OFNotImplementedException newWithClass: isa
					      selector: _cmd];
}

- removeObject: (id)obj
{
	@throw [OFNotImplementedException newWithClass: isa
					      selector: _cmd];
}

- removeObjectIdenticalTo: (id)obj
{
	@throw [OFNotImplementedException newWithClass: isa
					      selector: _cmd];
}

- removeObjectAtIndex: (size_t)index
{

Modified src/OFMutableArray.h from [e4a5edb3f5] to [426beeceb7].

19
20
21
22
23
24
25














26
27
28
29
30
31
32
/**
 * Adds an object to the OFDataArray.
 *
 * \param obj An object to add
 */
- addObject: (OFObject*)obj;















/**
 * Removes the object at the specified index.
 *
 * \param index The index of the object to remove
 */
- removeObjectAtIndex: (size_t)index;








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







19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
/**
 * Adds an object to the OFDataArray.
 *
 * \param obj An object to add
 */
- addObject: (OFObject*)obj;

/**
 * Removes the first object equivalent to the specified object.
 *
 * \param obj The object to remove
 */
- removeObject: (id)obj;

/**
 * Removes the first object that has the same address as the specified object.
 *
 * \param obj The object to remove
 */
- removeObjectIdenticalTo: (id)obj;

/**
 * Removes the object at the specified index.
 *
 * \param index The index of the object to remove
 */
- removeObjectAtIndex: (size_t)index;

Modified src/OFMutableArray.m from [c4806dc153] to [1cec556c42].

15
16
17
18
19
20
21
22
23
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
83
84
85
86
87
88
89
#import "OFExceptions.h"

@implementation OFMutableArray
- (id)copy
{
	OFArray *new = [[OFArray alloc] init];
	OFObject **objs;
	size_t len, i;

	objs = [array cArray];
	len = [array count];

	[new->array addNItems: len
		   fromCArray: objs];

	for (i = 0; i < len; i++)
		[objs[i] retain];

	return new;
}

- addObject: (OFObject*)obj
{
	[array addItem: &obj];
	[obj retain];

	return self;
}































- removeObjectAtIndex: (size_t)index
{
	return [self removeNObjects: 1
			    atIndex: index];
}

- removeNObjects: (size_t)nobjects
{
	OFObject **objs;
	size_t len, i;

	objs = [array cArray];
	len = [array count];

	if (nobjects > len)
		@throw [OFOutOfRangeException newWithClass: isa];

	for (i = len - nobjects; i < len; i++)
		[objs[i] release];

	[array removeNItems: nobjects];

	return self;
}

- removeNObjects: (size_t)nobjects
	 atIndex: (size_t)index
{
	OFObject **objs;
	size_t len, i;

	objs = [array cArray];
	len = [array count];

	if (nobjects > len)
		@throw [OFOutOfRangeException newWithClass: isa];

	for (i = index; i < len && i < index + nobjects; i++)
		[objs[i] release];

	[array removeNItems: nobjects
		    atIndex: index];

	return self;
}
@end







|


|

|


|












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









|
<
<
<
|

|


|










|
<
<
<
|

|


|








15
16
17
18
19
20
21
22
23
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



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
#import "OFExceptions.h"

@implementation OFMutableArray
- (id)copy
{
	OFArray *new = [[OFArray alloc] init];
	OFObject **objs;
	size_t count, i;

	objs = [array cArray];
	count = [array count];

	[new->array addNItems: count
		   fromCArray: objs];

	for (i = 0; i < count; i++)
		[objs[i] retain];

	return new;
}

- addObject: (OFObject*)obj
{
	[array addItem: &obj];
	[obj retain];

	return self;
}

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

	for (i = 0; i < count; i++) {
		if ([objs[i] isEqual: obj]) {
			[objs[i] release];
			[array removeItemAtIndex: i];
		}
	}

	return self;
}

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

	for (i = 0; i < count; i++) {
		if (objs[i] == obj) {
			[obj release];
			[array removeItemAtIndex: i];
		}
	}

	return self;
}

- removeObjectAtIndex: (size_t)index
{
	return [self removeNObjects: 1
			    atIndex: index];
}

- removeNObjects: (size_t)nobjects
{
	OFObject **objs = [array cArray];



	size_t i, count = [array count];

	if (nobjects > count)
		@throw [OFOutOfRangeException newWithClass: isa];

	for (i = count - nobjects; i < count; i++)
		[objs[i] release];

	[array removeNItems: nobjects];

	return self;
}

- removeNObjects: (size_t)nobjects
	 atIndex: (size_t)index
{
	OFObject **objs = [array cArray];



	size_t i, count = [array count];

	if (nobjects > count)
		@throw [OFOutOfRangeException newWithClass: isa];

	for (i = index; i < count && i < index + nobjects; i++)
		[objs[i] release];

	[array removeNItems: nobjects
		    atIndex: index];

	return self;
}
@end

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

59
60
61
62
63
64
65








66
67
68
69
70
71
72
	    [[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]])








>
>
>
>
>
>
>
>







59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
	    [[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(@"-[removeObject:]",
	    [a[0] removeObject: c_ary[1]] && [a[0] count] == 2)

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

	[a[0] addObject: c_ary[0]];
	[a[0] addObject: c_ary[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]])