ObjFW  Check-in [9e0ca2f627]

Overview
Comment:Add OFArray_subarray and OFArray_adjacenSubarray.

These will prevent a copy of an immutable array when using
-[objectsInRange:].

Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 9e0ca2f627544fe7f3922d4a4bf9f9dd4aff3f322c51a3b090f92e7c816a5154
User & Date: js on 2011-09-11 12:15:55
Other Links: manifest | tags
Context
2011-09-11
12:17
Add new files to Xcode project. check-in: 20a15c98a8 user: js tags: trunk
12:15
Add OFArray_subarray and OFArray_adjacenSubarray. check-in: 9e0ca2f627 user: js tags: trunk
02:55
Style improvements in TableGenerator.m. check-in: 5131770885 user: js tags: trunk
Changes

Modified src/Makefile from [2728704c39] to [ea86bff181].

69
70
71
72
73
74
75


76
77
78
79
80
81
82
	    asprintf.h		\
	    ${ATOMIC_H}		\
	    macros.h		\
	    objfw-defs.h	\
	    ${THREADING_H}

SRCS += OFArray_adjacent.m		\


	OFCountedSet_hashtable.m	\
	OFDictionary_hashtable.m	\
	OFMutableArray_adjacent.m	\
	OFMutableDictionary_hashtable.m	\
	OFMutableSet_hashtable.m	\
	OFSet_hashtable.m		\
	${OFSTREAMPOLLOBSERVER_M}	\







>
>







69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
	    asprintf.h		\
	    ${ATOMIC_H}		\
	    macros.h		\
	    objfw-defs.h	\
	    ${THREADING_H}

SRCS += OFArray_adjacent.m		\
	OFArray_adjacentSubarray.m	\
	OFArray_subarray.m		\
	OFCountedSet_hashtable.m	\
	OFDictionary_hashtable.m	\
	OFMutableArray_adjacent.m	\
	OFMutableDictionary_hashtable.m	\
	OFMutableSet_hashtable.m	\
	OFSet_hashtable.m		\
	${OFSTREAMPOLLOBSERVER_M}	\

Modified src/OFArray.m from [7b80c238f7] to [de78bb7da7].

15
16
17
18
19
20
21

22
23
24
25
26
27
28
 */

#include "config.h"

#include <stdarg.h>

#import "OFArray.h"

#import "OFArray_adjacent.h"
#import "OFString.h"
#import "OFXMLElement.h"
#import "OFAutoreleasePool.h"

#import "OFEnumerationMutationException.h"
#import "OFInvalidArgumentException.h"







>







15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
 */

#include "config.h"

#include <stdarg.h>

#import "OFArray.h"
#import "OFArray_subarray.h"
#import "OFArray_adjacent.h"
#import "OFString.h"
#import "OFXMLElement.h"
#import "OFAutoreleasePool.h"

#import "OFEnumerationMutationException.h"
#import "OFInvalidArgumentException.h"
334
335
336
337
338
339
340






341
342
343
344
345
346
347
348

	return nil;
}

- (OFArray*)objectsInRange: (of_range_t)range
{
	OFArray *ret;






	id *buffer = [self allocMemoryForNItems: range.length
				       withSize: sizeof(*buffer)];

	@try {
		[self getObjects: buffer
			 inRange: range];

		ret = [OFArray arrayWithCArray: buffer







>
>
>
>
>
>
|







335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355

	return nil;
}

- (OFArray*)objectsInRange: (of_range_t)range
{
	OFArray *ret;
	id *buffer;

	if (![self isKindOfClass: [OFMutableArray class]])
		return [OFArray_subarray arrayWithArray: self
						  range: range];

	buffer = [self allocMemoryForNItems: range.length
				       withSize: sizeof(*buffer)];

	@try {
		[self getObjects: buffer
			 inRange: range];

		ret = [OFArray arrayWithCArray: buffer

Modified src/OFArray_adjacent.m from [f4dc3297f2] to [831950ce9b].

16
17
18
19
20
21
22

23
24
25
26
27
28
29

#include "config.h"

#include <stdarg.h>

#import "OFArray_adjacent.h"
#import "OFMutableArray_adjacent.h"

#import "OFDataArray.h"
#import "OFString.h"
#import "OFXMLElement.h"
#import "OFAutoreleasePool.h"

#import "OFEnumerationMutationException.h"
#import "OFInvalidArgumentException.h"







>







16
17
18
19
20
21
22
23
24
25
26
27
28
29
30

#include "config.h"

#include <stdarg.h>

#import "OFArray_adjacent.h"
#import "OFMutableArray_adjacent.h"
#import "OFArray_adjacentSubarray.h"
#import "OFDataArray.h"
#import "OFString.h"
#import "OFXMLElement.h"
#import "OFAutoreleasePool.h"

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

	return OF_INVALID_INDEX;
}


- (OFArray*)objectsInRange: (of_range_t)range
{






	size_t count = [array count];

	if (range.start + range.length > count)
		@throw [OFOutOfRangeException newWithClass: isa];

	return [OFArray arrayWithCArray: (id*)[array cArray] + range.start
				 length: range.length];
}

- (BOOL)isEqual: (id)object
{
	OFArray *otherArray;
	id *cArray, *otherCArray;
	size_t i, count;

	if ([object class] != [OFArray_adjacent class] &&
	    [object class] != [OFMutableArray_adjacent class])

		return [super isEqual: object];

	otherArray = object;

	count = [array count];

	if (count != [otherArray count])







>
>
>
>
>
>
|















|
>







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
298
299
300
301
302
303
304
305

	return OF_INVALID_INDEX;
}


- (OFArray*)objectsInRange: (of_range_t)range
{
	size_t count;

	if (![self isKindOfClass: [OFMutableArray class]])
		return [OFArray_adjacentSubarray arrayWithArray: self
							  range: range];

	count = [array count];

	if (range.start + range.length > count)
		@throw [OFOutOfRangeException newWithClass: isa];

	return [OFArray arrayWithCArray: (id*)[array cArray] + range.start
				 length: range.length];
}

- (BOOL)isEqual: (id)object
{
	OFArray *otherArray;
	id *cArray, *otherCArray;
	size_t i, count;

	if ([object class] != [OFArray_adjacent class] &&
	    [object class] != [OFMutableArray_adjacent class] &&
	    [object class] != [OFArray_adjacentSubarray class])
		return [super isEqual: object];

	otherArray = object;

	count = [array count];

	if (count != [otherArray count])

Added src/OFArray_adjacentSubarray.h version [7db578123a].









































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
/*
 * Copyright (c) 2008, 2009, 2010, 2011
 *   Jonathan Schleifer <js@webkeks.org>
 *
 * All rights reserved.
 *
 * This file is part of ObjFW. It may be distributed under the terms of the
 * Q Public License 1.0, which can be found in the file LICENSE.QPL included in
 * the packaging of this file.
 *
 * Alternatively, it may be distributed under the terms of the GNU General
 * Public License, either version 2 or 3, which can be found in the file
 * LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this
 * file.
 */

#import "OFArray_subarray.h"

@interface OFArray_adjacentSubarray: OFArray_subarray
@end

Added src/OFArray_adjacentSubarray.m version [995549be46].





































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
/*
 * Copyright (c) 2008, 2009, 2010, 2011
 *   Jonathan Schleifer <js@webkeks.org>
 *
 * All rights reserved.
 *
 * This file is part of ObjFW. It may be distributed under the terms of the
 * Q Public License 1.0, which can be found in the file LICENSE.QPL included in
 * the packaging of this file.
 *
 * Alternatively, it may be distributed under the terms of the GNU General
 * Public License, either version 2 or 3, which can be found in the file
 * LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this
 * file.
 */

#include "config.h"

#import "OFArray_adjacentSubarray.h"
#import "OFArray_adjacent.h"
#import "OFMutableArray_adjacent.h"

@implementation OFArray_adjacentSubarray
- (id*)cArray
{
	return [array cArray] + range.start;
}

- (BOOL)isEqual: (id)object
{
	OFArray *otherArray;
	id *cArray, *otherCArray;
	size_t i;

	if ([object class] != [OFArray_adjacent class] &&
	    [object class] != [OFMutableArray_adjacent class] &&
	    [object class] != [OFArray_adjacentSubarray class])
		return [super isEqual: object];

	otherArray = object;

	if (range.length != [otherArray count])
		return NO;

	cArray = [self cArray];
	otherCArray = [otherArray cArray];

	for (i = 0; i < range.length; i++)
		if (![cArray[i] isEqual: otherCArray[i]])
			return NO;

	return YES;
}

#ifdef OF_HAVE_BLOCKS
- (void)enumerateObjectsUsingBlock: (of_array_enumeration_block_t)block
{
	id *cArray = [self cArray];
	size_t i;
	BOOL stop = NO;

	for (i = 0; i < range.length && !stop; i++)
		block(cArray[i], i, &stop);
}
#endif
@end

Added src/OFArray_subarray.h version [92dc57f885].



























































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
/*
 * Copyright (c) 2008, 2009, 2010, 2011
 *   Jonathan Schleifer <js@webkeks.org>
 *
 * All rights reserved.
 *
 * This file is part of ObjFW. It may be distributed under the terms of the
 * Q Public License 1.0, which can be found in the file LICENSE.QPL included in
 * the packaging of this file.
 *
 * Alternatively, it may be distributed under the terms of the GNU General
 * Public License, either version 2 or 3, which can be found in the file
 * LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this
 * file.
 */

#import "OFArray.h"

@interface OFArray_subarray: OFArray
{
	OFArray *array;
	of_range_t range;
}

+ arrayWithArray: (OFArray*)array
	   range: (of_range_t)range;
- initWithArray: (OFArray*)array
	  range: (of_range_t)range;
@end

Added src/OFArray_subarray.m version [bf200eb01f].



























































































































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
/*
 * Copyright (c) 2008, 2009, 2010, 2011
 *   Jonathan Schleifer <js@webkeks.org>
 *
 * All rights reserved.
 *
 * This file is part of ObjFW. It may be distributed under the terms of the
 * Q Public License 1.0, which can be found in the file LICENSE.QPL included in
 * the packaging of this file.
 *
 * Alternatively, it may be distributed under the terms of the GNU General
 * Public License, either version 2 or 3, which can be found in the file
 * LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this
 * file.
 */

#include "config.h"

#import "OFArray_subarray.h"

#import "OFOutOfRangeException.h"

@implementation OFArray_subarray
+ arrayWithArray: (OFArray*)array
	   range: (of_range_t)range
{
	return [[[self alloc] initWithArray: array
				      range: range] autorelease];
}

- initWithArray: (OFArray*)array_
	  range: (of_range_t)range_
{
	self = [super init];

	@try {
		/* Should usually be retain, as it's useless with a copy */
		array = [array_ copy];
		range = range_;
	} @catch (id e) {
		[self release];
		@throw e;
	}

	return self;
}

- (void)dealloc
{
	[array release];

	[super dealloc];
}

- (size_t)count
{
	return range.length;
}

- (id)objectAtIndex: (size_t)index
{
	if (index >= range.length)
		@throw [OFOutOfRangeException newWithClass: isa];

	return [array objectAtIndex: index + range.start];
}

- (void)getObjects: (id*)buffer
	   inRange: (of_range_t)range_
{
	if (range_.start + range_.length > range.length)
		@throw [OFOutOfRangeException newWithClass: isa];

	range_.start += range.start;

	return [array getObjects: buffer
			 inRange: range_];
}

- (size_t)indexOfObject: (id)object
{
	size_t index = [array indexOfObject: object] + range.start;

	if (index > range.length)
		index = OF_INVALID_INDEX;

	return index;
}

- (size_t)indexOfObjectIdenticalTo: (id)object
{
	size_t index = [array indexOfObjectIdenticalTo: object] + range.start;

	if (index > range.length)
		index = OF_INVALID_INDEX;

	return index;
}

- (OFArray*)objectsInRange: (of_range_t)range_
{
	if (range_.start + range_.length > range.length)
		@throw [OFOutOfRangeException newWithClass: isa];

	range_.start += range.start;

	return [array objectsInRange: range_];
}
@end