ObjFW  Check-in [fe56ea4a9c]

Overview
Comment:OFMutableArray: Add mutation counter, required for fast enumeration.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: fe56ea4a9cf32c3793fd74067bacf2ea7114842f707f50f4c012e39c59b48c3b
User & Date: js on 2010-01-03 18:14:28
Other Links: manifest | tags
Context
2010-01-03
18:28
Implement fast enumeration for OFArray. check-in: 45869ac8ac user: js tags: trunk
18:14
OFMutableArray: Add mutation counter, required for fast enumeration. check-in: fe56ea4a9c user: js tags: trunk
17:36
OFMutableArray: Safer removing of objects. check-in: 0c9d2a8ce9 user: js tags: trunk
Changes

Modified src/OFMutableArray.h from [13789c154b] to [70b773a8e9].

11
12
13
14
15
16
17
18




19
20
21
22
23
24
25

#import "OFArray.h"

/**
 * The OFMutableArray class is a class for storing, adding and removing objects
 * in an array.
 */
@interface OFMutableArray: OFArray {}




/**
 * Adds an object to the OFArray.
 *
 * \param obj An object to add
 */
- addObject: (OFObject*)obj;








|
>
>
>
>







11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29

#import "OFArray.h"

/**
 * The OFMutableArray class is a class for storing, adding and removing objects
 * in an array.
 */
@interface OFMutableArray: OFArray
{
	unsigned long mutations;
}

/**
 * Adds an object to the OFArray.
 *
 * \param obj An object to add
 */
- addObject: (OFObject*)obj;

Modified src/OFMutableArray.m from [d5e78125eb] to [ca890914b8].

36
37
38
39
40
41
42


43
44
45
46
47
48
49
50
51


52
53
54
55
56
57
58
}

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



	return self;
}

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



	return self;
}

- replaceObject: (OFObject*)old
     withObject: (OFObject*)new
{







>
>









>
>







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
}

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

	mutations++;

	return self;
}

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

	mutations++;

	return self;
}

- replaceObject: (OFObject*)old
     withObject: (OFObject*)new
{
106
107
108
109
110
111
112

113


114
115
116
117
118
119
120
121
122
123
124
125
126
127
128


129
130
131
132
133
134
135
{
	OFObject **objs = [array cArray];
	size_t i, count = [array count];

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

			[array removeItemAtIndex: i];


			[obj release];
		}
	}

	return self;
}

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

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


			[obj release];
		}
	}

	return self;
}








>

>
>















>
>







110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
{
	OFObject **objs = [array cArray];
	size_t i, count = [array count];

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

			[array removeItemAtIndex: i];
			mutations++;

			[obj release];
		}
	}

	return self;
}

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

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

			[obj release];
		}
	}

	return self;
}

149
150
151
152
153
154
155

156
157
158
159
160
161
162

	copy = [self allocMemoryForNItems: nobjects
				 withSize: sizeof(OFObject*)];
	memcpy(copy, objs + (count - nobjects), nobjects * sizeof(OFObject*));

	@try {
		[array removeNItems: nobjects];


		for (i = 0; i < nobjects; i++)
			[copy[i] release];
	} @finally {
		[self freeMemory: copy];
	}








>







158
159
160
161
162
163
164
165
166
167
168
169
170
171
172

	copy = [self allocMemoryForNItems: nobjects
				 withSize: sizeof(OFObject*)];
	memcpy(copy, objs + (count - nobjects), nobjects * sizeof(OFObject*));

	@try {
		[array removeNItems: nobjects];
		mutations++;

		for (i = 0; i < nobjects; i++)
			[copy[i] release];
	} @finally {
		[self freeMemory: copy];
	}

175
176
177
178
179
180
181

182
183
184
185
186
187
188
189
190
191
	copy = [self allocMemoryForNItems: nobjects
				 withSize: sizeof(OFObject*)];
	memcpy(copy, objs + index, nobjects * sizeof(OFObject*));

	@try {
		[array removeNItems: nobjects
			    atIndex: index];


		for (i = 0; i < nobjects; i++)
			[copy[i] release];
	} @finally {
		[self freeMemory: copy];
	}

	return self;
}
@end







>










185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
	copy = [self allocMemoryForNItems: nobjects
				 withSize: sizeof(OFObject*)];
	memcpy(copy, objs + index, nobjects * sizeof(OFObject*));

	@try {
		[array removeNItems: nobjects
			    atIndex: index];
		mutations++;

		for (i = 0; i < nobjects; i++)
			[copy[i] release];
	} @finally {
		[self freeMemory: copy];
	}

	return self;
}
@end