ObjFW  Check-in [188586881d]

Overview
Comment:OFArray: Add sorting with options.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 188586881d5b7d36c2032fef4c575b747c9326d353c4e289a10e866281a371b0
User & Date: js on 2013-01-14 00:39:07
Other Links: manifest | tags
Context
2013-01-14
19:02
Documentation improvements. check-in: 30ab311006 user: js tags: trunk
00:39
OFArray: Add sorting with options. check-in: 188586881d user: js tags: trunk
2013-01-13
16:10
PLATFORMS.md: Add Linux/m68k. check-in: 3d8fb22695 user: js tags: trunk
Changes

Modified src/OFArray.h from [8b84a12053] to [0c918cd51d].

26
27
28
29
30
31
32




33
34
35
36
37
38
39
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43







+
+
+
+







#import "OFObject.h"
#import "OFCollection.h"
#import "OFEnumerator.h"
#import "OFSerialization.h"
#import "OFJSONRepresentation.h"

@class OFString;

enum {
	OF_SORT_OPTIONS_DESCENDING = 1
};

#ifdef OF_HAVE_BLOCKS
typedef void (^of_array_enumeration_block_t)(id object, size_t index,
    BOOL *stop);
typedef BOOL (^of_array_filter_block_t)(id odject, size_t index);
typedef id (^of_array_map_block_t)(id object, size_t index);
typedef id (^of_array_fold_block_t)(id left, id right);
261
262
263
264
265
266
267












268
269
270
271
272
273
274
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







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







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

/*!
 * @brief Returns a sorted copy of the array.
 *
 * @param options The options to use when sorting the array.@n
 *		  Possible values are:
 *		  Value                      | Description
 *		  ---------------------------|-------------------------
 *		  OF_SORT_OPTIONS_DESCENDING | Sort in descending order
 * @return A sorted copy of the array
 */
- (OFArray*)sortedArrayWithOptions: (int)options;

/*!
 * @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 [f016136df9] to [915e088fa7].

578
579
580
581
582
583
584











585
586
587
588
589
590
591
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602







+
+
+
+
+
+
+
+
+
+
+








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

	[new sort];

	[new makeImmutable];

	return new;
}

- (OFArray*)sortedArrayWithOptions: (int)options
{
	OFMutableArray *new = [[self mutableCopy] autorelease];

	[new sortWithOptions: options];

	[new makeImmutable];

	return new;
}

- (OFArray*)reversedArray
{

Modified src/OFMutableArray.h from [9f2e35cd47] to [e3a64cd9d6].

146
147
148
149
150
151
152











153
154
155
156
157
158
159
160
161
162
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
173







+
+
+
+
+
+
+
+
+
+
+










	    withObjectAtIndex: (size_t)index2;

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

/*!
 * @brief Sorts the array.
 *
 * @param options The options to use when sorting the array.@n
 *		  Possible values are:
 *		  Value                      | Description
 *		  ---------------------------|-------------------------
 *		  OF_SORT_OPTIONS_DESCENDING | Sort in descending order
 */
- (void)sortWithOptions: (int)options;

/*!
 * @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 [b27a848d5b] to [c800430259].

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







-
+

+
+
+
+
+
+
+
+
+
+










-
+



-
+







-
+
-






-
+

-
+







	Class isa;
} placeholder;

@interface OFMutableArray_placeholder: OFMutableArray
@end

static void
quicksort(OFMutableArray *array, size_t left, size_t right)
quicksort(OFMutableArray *array, size_t left, size_t right, int options)
{
	of_comparison_result_t ascending, descending;

	if (options & OF_SORT_OPTIONS_DESCENDING) {
		ascending = OF_ORDERED_DESCENDING;
		descending = OF_ORDERED_ASCENDING;
	} else {
		ascending = OF_ORDERED_ASCENDING;
		descending = OF_ORDERED_DESCENDING;
	}

	while (left < right) {
		size_t i, j;
		id pivot;

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

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

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

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

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

		if (i > 0) {
			if (i > left && i - 1 - left > right - i - 1) {
				right = i - 1;
				quicksort(array, i + 1, right);
				quicksort(array, i + 1, right, options);
			} else {
				quicksort(array, left, i - 1);
				quicksort(array, left, i - 1, options);
				left = i + 1;
			}
		} else
			left = i + 1;
	}
}

375
376
377
378
379
380
381





382
383
384
385
386
387

388
389
390
391
392
393
394
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400

401
402
403
404
405
406
407
408







+
+
+
+
+





-
+







	} @finally {
		[object1 release];
	}
}

- (void)sort
{
	[self sortWithOptions: 0];
}

- (void)sortWithOptions: (int)options
{
	size_t count = [self count];

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

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

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

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

Modified tests/OFArrayTests.m from [39ba49136f] to [a50d671c0a].

149
150
151
152
153
154
155
156




157
158
159
160
161
162
163
149
150
151
152
153
154
155

156
157
158
159
160
161
162
163
164
165
166







-
+
+
+
+







	    @"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])])
	    @"0", @"Bar", @"Baz", @"Foo", @"z", nil])] &&
	    [[m[1] sortedArrayWithOptions: OF_SORT_OPTIONS_DESCENDING]
	    isEqual: ([OFArray arrayWithObjects:
	    @"z", @"Foo", @"Baz", @"Bar", @"0", nil])])

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

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