ObjFW
OFArray.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015
3  * Jonathan Schleifer <js@webkeks.org>
4  *
5  * All rights reserved.
6  *
7  * This file is part of ObjFW. It may be distributed under the terms of the
8  * Q Public License 1.0, which can be found in the file LICENSE.QPL included in
9  * the packaging of this file.
10  *
11  * Alternatively, it may be distributed under the terms of the GNU General
12  * Public License, either version 2 or 3, which can be found in the file
13  * LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this
14  * file.
15  */
16 
17 #ifndef __STDC_LIMIT_MACROS
18 # define __STDC_LIMIT_MACROS
19 #endif
20 #ifndef __STDC_CONSTANT_MACROS
21 # define __STDC_CONSTANT_MACROS
22 #endif
23 
24 #include <stdarg.h>
25 
26 #import "OFObject.h"
27 #import "OFCollection.h"
28 #import "OFEnumerator.h"
29 #import "OFSerialization.h"
30 #import "OFJSONRepresentation.h"
31 #import "OFMessagePackRepresentation.h"
32 
33 OF_ASSUME_NONNULL_BEGIN
34 
37 @class OFString;
38 
39 enum {
40  OF_ARRAY_SKIP_EMPTY = 1,
41  OF_ARRAY_SORT_DESCENDING = 2,
42 };
43 
44 #ifdef OF_HAVE_BLOCKS
45 
53 typedef void (^of_array_enumeration_block_t)(id object, size_t index,
54  bool *stop);
55 
63 typedef bool (^of_array_filter_block_t)(id object, size_t index);
64 
72 typedef id OF_NONNULL (^of_array_map_block_t)(id object, size_t index);
73 
81 typedef id OF_NULLABLE (^of_array_fold_block_t)(id OF_NULLABLE left, id right);
82 #endif
83 
89 #ifdef OF_HAVE_GENERICS
90 @interface OFArray <ObjectType>:
91 #else
92 # ifndef DOXYGEN
93 # define ObjectType id
94 # endif
95 @interface OFArray:
96 #endif
99 #ifdef OF_HAVE_PROPERTIES
100 @property (readonly) size_t count;
101 #endif
102 
108 + (instancetype)array;
109 
116 + (instancetype)arrayWithObject: (ObjectType)object;
117 
124 + (instancetype)arrayWithObjects: (ObjectType)firstObject, ... OF_SENTINEL;
125 
132 + (instancetype)arrayWithArray: (OFArray OF_GENERIC(ObjectType)*)array;
133 
142 + (instancetype)
143  arrayWithObjects: (ObjectType const OF_NONNULL *OF_NONNULL)objects
144  count: (size_t)count;
145 
152 - initWithObject: (ObjectType)object;
153 
160 - initWithObjects: (ObjectType)firstObject, ... OF_SENTINEL;
161 
169 - initWithObject: (ObjectType)firstObject
170  arguments: (va_list)arguments;
171 
178 - initWithArray: (OFArray OF_GENERIC(ObjectType)*)array;
179 
188 - initWithObjects: (ObjectType const OF_NONNULL *OF_NONNULL)objects
189  count: (size_t)count;
190 
200 - (ObjectType)objectAtIndex: (size_t)index;
201 - (ObjectType)objectAtIndexedSubscript: (size_t)index;
202 
209 - (void)getObjects: (ObjectType __unsafe_unretained OF_NONNULL *OF_NONNULL)
210  buffer
211  inRange: (of_range_t)range;
212 
218 - (ObjectType const __unsafe_unretained OF_NONNULL *OF_NONNULL)objects;
219 
228 - (size_t)indexOfObject: (ObjectType)object;
229 
238 - (size_t)indexOfObjectIdenticalTo: (ObjectType)object;
239 
247 - (bool)containsObject: (nullable ObjectType)object;
248 
257 - (bool)containsObjectIdenticalTo: (nullable ObjectType)object;
258 
267 - (nullable ObjectType)firstObject;
268 
277 - (nullable ObjectType)lastObject;
278 
285 - (OFArray OF_GENERIC(ObjectType)*)objectsInRange: (of_range_t)range;
286 
293 - (OFString*)componentsJoinedByString: (OFString*)separator;
294 
306 - (OFString*)componentsJoinedByString: (OFString*)separator
307  options: (int)options;
308 
317 - (OFString*)componentsJoinedByString: (OFString*)separator
318  usingSelector: (SEL)selector;
319 
333 - (OFString*)componentsJoinedByString: (OFString*)separator
334  usingSelector: (SEL)selector
335  options: (int)options;
336 
342 - (void)makeObjectsPerformSelector: (SEL)selector;
343 
352 - (void)makeObjectsPerformSelector: (SEL)selector
353  withObject: (nullable id)object;
354 
360 - (OFArray OF_GENERIC(ObjectType)*)sortedArray;
361 
372 - (OFArray OF_GENERIC(ObjectType)*)sortedArrayWithOptions: (int)options;
373 
379 - (OFArray OF_GENERIC(ObjectType)*)reversedArray;
380 
387 - (OFArray OF_GENERIC(ObjectType)*)arrayByAddingObject: (ObjectType)object;
388 
395 - (OFArray OF_GENERIC(ObjectType)*)arrayByAddingObjectsFromArray:
396  (OFArray OF_GENERIC(ObjectType)*)array;
397 
404 - (OFArray OF_GENERIC(ObjectType)*)arrayByRemovingObject: (ObjectType)object;
405 
412 - (OFEnumerator OF_GENERIC(ObjectType)*)objectEnumerator;
413 
414 #ifdef OF_HAVE_BLOCKS
415 
420 - (void)enumerateObjectsUsingBlock: (of_array_enumeration_block_t)block;
421 
428 - (OFArray*)mappedArrayUsingBlock: (of_array_map_block_t)block;
429 
438 - (OFArray OF_GENERIC(ObjectType)*)filteredArrayUsingBlock:
440 
457 - (nullable id)foldUsingBlock: (of_array_fold_block_t)block;
458 #endif
459 @end
460 #if !defined(OF_HAVE_GENERICS) && !defined(DOXYGEN)
461 # undef ObjectType
462 #endif
463 
464 @interface OFArrayEnumerator: OFEnumerator
465 {
466  OFArray *_array;
467  size_t _count;
468  unsigned long _mutations;
469  unsigned long *_mutationsPtr;
470  size_t _position;
471 }
472 
473 - initWithArray: (OFArray*)data
474  mutationsPtr: (unsigned long *OF_NULLABLE)mutationsPtr;
475 @end
476 
477 OF_ASSUME_NONNULL_END
478 
479 #import "OFMutableArray.h"
480 
481 #ifndef NSINTEGER_DEFINED
482 /* Required for array literals to work */
483 @compatibility_alias NSArray OFArray;
484 #endif
A protocol for serializing objects.
Definition: OFSerialization.h:30
instancetype array()
Creates a new OFArray.
Definition: OFArray.m:133
An abstract class for storing objects in an array.
Definition: OFArray.h:95
nullable ObjectType firstObject()
Returns the first object of the array or nil.
Definition: OFArray.m:322
The root class for all other classes inside ObjFW.
Definition: OFObject.h:364
nullable ObjectType lastObject()
Returns the last object of the array or nil.
Definition: OFArray.m:330
id OF_NULLABLE(^ of_array_fold_block_t)(id OF_NULLABLE left, id right)
A block for folding an OFArray.
Definition: OFArray.h:81
ObjectType const __unsafe_unretained OF_NONNULL *OF_NONNULL objects()
Returns the objects of the array as a C array.
Definition: OFArray.m:243
A protocol implemented by classes that support encoding to a JSON representation. ...
Definition: OFJSONRepresentation.h:38
OFEnumerator OF_GENERIC(ObjectType objectEnumerator()
Returns an OFEnumerator to enumerate through all objects of the array.
Definition: OFArray.m:752
A class for handling strings.
Definition: OFString.h:91
size_t count()
Returns the number of objects in the collection.
void(^ of_array_enumeration_block_t)(id object, size_t index, bool *stop)
A block for enumerating an OFArray.
Definition: OFArray.h:53
OFArray OF_GENERIC(ObjectType sortedArray()
Returns a sorted copy of the array.
Definition: OFArray.m:694
A protocol with methods common for all collections.
Definition: OFCollection.h:26
bool(^ of_array_filter_block_t)(id object, size_t index)
A block for filtering an OFArray.
Definition: OFArray.h:63
A protocol implemented by classes that support encoding to a MessagePack representation.
Definition: OFMessagePackRepresentation.h:30
A range.
Definition: OFObject.h:65
A protocol for the creation of mutable copies.
Definition: OFObject.h:917
A protocol for the creation of copies.
Definition: OFObject.h:896
id OF_NONNULL(^ of_array_map_block_t)(id object, size_t index)
A block for mapping objects to objects in an OFArray.
Definition: OFArray.h:72
A class which provides methods to enumerate through collections.
Definition: OFEnumerator.h:52
OFArray OF_GENERIC(ObjectType reversedArray()
Returns a copy of the array with the order reversed.
Definition: OFArray.m:716