ObjFW  Check-in [de45535c23]

Overview
Comment:Only handle the first matching object in OFMutalbeArray operations.

This only makes sense as we use "Object" in the method names, implying
it's only affecting one object.

Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: de45535c23983c1f258c37cbcadb97bfd861cc00204dcd4f9fb6779d701d5bbb
User & Date: js on 2010-09-19 02:34:10
Other Links: manifest | tags
Context
2010-09-19
02:52
Improve OFStreamObserver. check-in: b554732eae user: js tags: trunk
02:34
Only handle the first matching object in OFMutalbeArray operations. check-in: de45535c23 user: js tags: trunk
02:05
Update buildsys. check-in: c3cc5bb657 user: js tags: trunk
Changes

Modified src/OFMutableArray.h from [a52014b3ad] to [cb7ba1bfa9].

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
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







-
+













-

-
-
+
+


-
-
+
+








-
+






-
+









-

-
+







 * \param obj An object to add
 * \param index The index where the object should be added
 */
- (void)addObject: (id)obj
	  atIndex: (size_t)index;

/**
 * Replaces all objects equivalent to the first specified object with the
 * Replaces the first object equivalent to the first specified object with the
 * second specified object.
 *
 * \param old The object to replace
 * \param new The replacement object
 */
- (void)replaceObject: (id)old
	   withObject: (id)new_;

/**
 * Replaces the object at the specified index with the specified object.
 *
 * \param index The index of the object to replace
 * \param obj The replacement object
 * \return The old object, autoreleased
 */
- (id)replaceObjectAtIndex: (size_t)index
		withObject: (id)obj;
- (void)replaceObjectAtIndex: (size_t)index
		  withObject: (id)obj;

/**
 * Replaces all objects that have the same address as the first specified object
 * with the second specified object.
 * Replaces the first object that has the same address as the first specified
 * object with the second specified object.
 *
 * \param old The object to replace
 * \param new The replacement object
 */
- (void)replaceObjectIdenticalTo: (id)old
		      withObject: (id)new_;

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

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

/**
 * Removes the object at the specified index.
 *
 * \param index The index of the object to remove
 * \return The object that was at the index, autoreleased
 */
- (id)removeObjectAtIndex: (size_t)index;
- (void)removeObjectAtIndex: (size_t)index;

/**
 * Removes the specified amount of objects from the end of the OFArray.
 *
 * \param nobjects The number of objects to remove
 */
- (void)removeNObjects: (size_t)nobjects;

Modified src/OFMutableArray.m from [cd027f3f1d] to [c66c422e10].

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
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
145
146
147
148
149

150
151
152
153
154
155
156
157
158

159
160
161
162
163


164
165

166
167
168
169
170
171
172
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
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
145
146

147
148
149
150
151
152
153
154







+
+




-
-
+
+









-
-
+













+
+


















-
-
-
-
-
-
-
+
-
-
-
-
















-
-
-
-
-
-
-
+
-
-
-
-




-
+


-
-
-
+
+

-
+







	size_t i, count = [array count];

	for (i = 0; i < count; i++) {
		if ([objs[i] isEqual: old]) {
			[new retain];
			[objs[i] release];
			objs[i] = new;

			return;
		}
	}
}

- (id)replaceObjectAtIndex: (size_t)index
		withObject: (id)obj
- (void)replaceObjectAtIndex: (size_t)index
		  withObject: (id)obj
{
	id *objs = [array cArray];
	id old;

	if (index >= [array count])
		@throw [OFOutOfRangeException newWithClass: isa];

	old = objs[index];
	objs[index] = [obj retain];

	return [old autorelease];
	[old release];
}

- (void)replaceObjectIdenticalTo: (id)old
		      withObject: (id)new
{
	id *objs = [array cArray];
	size_t i, count = [array count];

	for (i = 0; i < count; i++) {
		if (objs[i] == old) {
			[new retain];
			[objs[i] release];
			objs[i] = new;

			return;
		}
	}
}

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

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

			[array removeItemAtIndex: i];
			mutations++;

			[obj release];

			/*
			 * We need to get the C array again as it might have
			 * been relocated. We also need to adjust the count
			 * as otherwise we would have an out of bounds access.
			 * As another object will be at the current index now,
			 * we also need to handle the same index again, thus we
			 * decrease it.
			return;
			 */
			objs = [array cArray];
			count--;
			i--;
		}
	}
}

- (void)removeObjectIdenticalTo: (id)obj
{
	id *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];

			/*
			 * We need to get the C array again as it might have
			 * been relocated. We also need to adjust the count
			 * as otherwise we would have an out of bounds access.
			 * As another object will be at the current index now,
			 * we also need to handle the same index again, thus we
			 * decrease it.
			return;
			 */
			objs = [array cArray];
			count--;
			i--;
		}
	}
}

- (id)removeObjectAtIndex: (size_t)index
- (void)removeObjectAtIndex: (size_t)index
{
	id old = [self objectAtIndex: index];

	[self removeNObjects: 1
		     atIndex: index];
	[array removeItemAtIndex: index];
	[old release];

	return old;
	mutations++;
}

- (void)removeNObjects: (size_t)nobjects
{
	id *objs = [array cArray], *copy;
	size_t i, count = [array count];

Modified tests/OFArrayTests.m from [415ab0ae8e] to [895b4e152f].

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
114
115
116
117
118
119
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
114
115
116
117
118
119







-
+



-
-
+
+

-
+



-
+










-
+







	    [[m[0] objectAtIndex: 1] isEqual: c_ary[0]] &&
	    [[m[0] objectAtIndex: 2] isEqual: c_ary[2]])

	TEST(@"-[replaceObject:identicalTo:]",
	    R([m[0] replaceObjectIdenticalTo: c_ary[0]
				  withObject: c_ary[1]]) &&
	    [[m[0] objectAtIndex: 0] isEqual: c_ary[1]] &&
	    [[m[0] objectAtIndex: 1] isEqual: c_ary[1]] &&
	    [[m[0] objectAtIndex: 1] isEqual: c_ary[0]] &&
	    [[m[0] objectAtIndex: 2] isEqual: c_ary[2]])

	TEST(@"-[replaceObjectAtIndex:withObject:]",
	    [m[0] replaceObjectAtIndex: 0
			    withObject: c_ary[0]] &&
	    R([m[0] replaceObjectAtIndex: 0
			      withObject: c_ary[0]]) &&
	    [[m[0] objectAtIndex: 0] isEqual: c_ary[0]] &&
	    [[m[0] objectAtIndex: 1] isEqual: c_ary[1]] &&
	    [[m[0] objectAtIndex: 1] isEqual: c_ary[0]] &&
	    [[m[0] objectAtIndex: 2] isEqual: c_ary[2]])

	TEST(@"-[removeObject:]",
	    R([m[0] removeObject: c_ary[1]]) && [m[0] count] == 2)
	    R([m[0] removeObject: c_ary[0]]) && [m[0] count] == 2)

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

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

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

	m[1] = [[a[0] mutableCopy] autorelease];
	TEST(@"-[removeNObjects:atIndex:]", R([m[1] removeNObjects: 2
							   atIndex: 0]) &&
	    [m[1] count] == 1 && [[m[1] objectAtIndex: 0] isEqual: c_ary[2]])