ObjFW  Check-in [b48145bb74]

Overview
Comment:Add -[sortedArray] to OFArray.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: b48145bb74890f9cd83fb2ed77d1db2e6eceb38fa6bfde51b2ef1c649aaee9d1
User & Date: js on 2012-01-31 10:59:20
Other Links: manifest | tags
Context
2012-01-31
11:12
Clean up Xcode project. check-in: dd6b29657f user: js tags: trunk
10:59
Add -[sortedArray] to OFArray. check-in: b48145bb74 user: js tags: trunk
2012-01-30
22:56
The new Apple runtime requires +[load] in OFConstantString. check-in: 08389242d9 user: js tags: trunk
Changes

Modified src/OFArray.h from [67421fc5d9] to [076a252b7d].

258
259
260
261
262
263
264







265
266
267
268
269
270
271
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278







+
+
+
+
+
+
+







 * \param selector The selector to perform on all objects in the array
 * \param object The object to perform the selector with on all objects in the
 *	      array
 */
- (void)makeObjectsPerformSelector: (SEL)selector
			withObject: (id)object;

/**
 * \brief Returns a sorted copy of the array.
 *
 * \return A sorted copy of the array
 */
- (OFArray*)sortedArray;

/**
 * \brief Returns a copy of the array with the order reversed.
 *
 * \return A copy of the array with the order reversed
 */
- (OFArray*)reversedArray;

Modified src/OFArray.m from [276f05e269] to [6919605399].

549
550
551
552
553
554
555











556
557
558
559
560
561
562
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573







+
+
+
+
+
+
+
+
+
+
+







	id *cArray = [self cArray];
	size_t i, count = [self count];

	for (i = 0; i < count; i++)
		((void(*)(id, SEL, id))[cArray[i]
		    methodForSelector: selector])(cArray[i], selector, object);
}

- (OFArray*)sortedArray
{
	OFMutableArray *new = [[self mutableCopy] autorelease];

	[new sort];

	[new makeImmutable];

	return new;
}

- (OFArray*)reversedArray
{
	OFMutableArray *new = [[self mutableCopy] autorelease];

	[new reverse];

Modified src/OFMutableArray.h from [5316dda09e] to [66c505dbab].

125
126
127
128
129
130
131





132
133
134
135
136
137
138
139
140
141
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146







+
+
+
+
+










 *
 * \param index1 The index of the first object to swap
 * \param index2 The index of the second object to swap
 */
- (void)swapObjectAtIndex: (size_t)index1
	withObjectAtIndex: (size_t)index2;

/**
 * \brief Sorts the array.
 */
- (void)sort;

/**
 * \brief Reverts the order of the objects in the array.
 */
- (void)reverse;

/**
 * \brief Converts the mutable array to an immutable array.
 */
- (void)makeImmutable;
@end

Modified src/OFMutableArray.m from [20b125814d] to [fdde418d40].

31
32
33
34
35
36
37




































38
39
40
41
42
43
44
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







+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+








static struct {
	Class isa;
} placeholder;

@interface OFMutableArray_placeholder: OFMutableArray
@end

static void
quicksort(OFMutableArray *array, size_t left, size_t right)
{
	size_t i, j;
	id pivot;

	if (left >= right)
		return;

	i = left;
	j = right - 1;
	pivot = [array objectAtIndex: right];

	do {
		while ([[array objectAtIndex: i] compare: pivot] !=
		    OF_ORDERED_DESCENDING && i < right)
			i++;

		while ([[array objectAtIndex: j] compare: pivot] !=
		    OF_ORDERED_ASCENDING && j > left)
			j--;

		if (i < j)
			[array swapObjectAtIndex: i
			       withObjectAtIndex: j];
	} while (i < j);

	if ([[array objectAtIndex: i] compare: pivot] == OF_ORDERED_DESCENDING)
		[array swapObjectAtIndex: i
		       withObjectAtIndex: right];

	if (i > 0)
		quicksort(array, left, i - 1);
	quicksort(array, i + 1, right);
}

@implementation OFMutableArray_placeholder
- init
{
	return (id)[[OFMutableArray_adjacent alloc] init];
}

268
269
270
271
272
273
274










275
276
277
278
279
280
281
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327







+
+
+
+
+
+
+
+
+
+







				withObject: object2];
		[self replaceObjectAtIndex: index2
				withObject: object1];
	} @finally {
		[object1 release];
	}
}

- (void)sort
{
	size_t count = [self count];

	if (count == 0 || count == 1)
		return;

	quicksort(self, 0, count - 1);
}

- (void)reverse
{
	size_t i, j, count = [self count];

	if (count == 0 || count == 1)
		return;

Modified tests/OFArrayTests.m from [2fd6511491] to [59176fc44c].

150
151
152
153
154
155
156








157
158
159
160
161
162
163
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171







+
+
+
+
+
+
+
+







	m[1] = [[a[0] mutableCopy] autorelease];
	[m[1] addObject: @"qux"];
	[m[1] addObject: @"last"];
	TEST(@"-[reversedArray]",
	    [[m[1] reversedArray] isEqual: ([OFArray arrayWithObjects:
	    @"last", @"qux", @"Baz", @"Bar", @"Foo", nil])])

	m[1] = [[a[0] mutableCopy] autorelease];
	[m[1] addObject: @"0"];
	[m[1] addObject: @"z"];
	TEST(@"-[sortedArray]",
	    [[m[1] sortedArray] isEqual: ([OFArray arrayWithObjects:
	    @"0", @"Bar", @"Baz", @"Foo", @"z", nil])])
	puts([[[m[1] sortedArray] description] UTF8String]);

	EXPECT_EXCEPTION(@"Detect out of range in -[objectAtIndex:]",
	    OFOutOfRangeException, [a[0] objectAtIndex: [a[0] count]])

	EXPECT_EXCEPTION(@"Detect out of range in -[removeNObjects:]",
	    OFOutOfRangeException, [m[0] removeNObjects: [m[0] count] + 1])

	TEST(@"-[componentsJoinedByString:]",