ObjFW
OFArray.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017
3  * Jonathan Schleifer <js@heap.zone>
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 _Nonnull (^of_array_map_block_t)(id object, size_t index);
73 
81 typedef id _Nullable (^of_array_fold_block_t)(id _Nullable left, id right);
82 #endif
83 
89 @interface OFArray OF_GENERIC(ObjectType): OFObject <OFCopying,
92 #if !defined(OF_HAVE_GENERICS) && !defined(DOXYGEN)
93 # define ObjectType id
94 #endif
95 
100 + (instancetype)array;
101 
108 + (instancetype)arrayWithObject: (ObjectType)object;
109 
116 + (instancetype)arrayWithObjects: (ObjectType)firstObject, ... OF_SENTINEL;
117 
124 + (instancetype)arrayWithArray: (OFArray OF_GENERIC(ObjectType) *)array;
125 
134 + (instancetype)
135  arrayWithObjects: (ObjectType const _Nonnull *_Nonnull)objects
136  count: (size_t)count;
137 
144 - initWithObject: (ObjectType)object;
145 
152 - initWithObjects: (ObjectType)firstObject, ... OF_SENTINEL;
153 
161 - initWithObject: (ObjectType)firstObject
162  arguments: (va_list)arguments;
163 
170 - initWithArray: (OFArray OF_GENERIC(ObjectType) *)array;
171 
180 - initWithObjects: (ObjectType const _Nonnull *_Nonnull)objects
181  count: (size_t)count;
182 
192 - (ObjectType)objectAtIndex: (size_t)index;
193 - (ObjectType)objectAtIndexedSubscript: (size_t)index;
194 
208 - (nullable id)valueForKey: (OFString *)key;
209 
223 - (void)setValue: (id)value
224  forKey: (OFString *)key;
225 
232 - (void)getObjects: (ObjectType __unsafe_unretained _Nonnull *_Nonnull)buffer
233  inRange: (of_range_t)range;
234 
240 - (ObjectType const __unsafe_unretained _Nonnull *_Nonnull)objects;
241 
250 - (size_t)indexOfObject: (ObjectType)object;
251 
260 - (size_t)indexOfObjectIdenticalTo: (ObjectType)object;
261 
269 - (bool)containsObject: (ObjectType)object;
270 
279 - (bool)containsObjectIdenticalTo: (ObjectType)object;
280 
289 - (nullable ObjectType)firstObject;
290 
299 - (nullable ObjectType)lastObject;
300 
307 - (OFArray OF_GENERIC(ObjectType) *)objectsInRange: (of_range_t)range;
308 
315 - (OFString *)componentsJoinedByString: (OFString *)separator;
316 
328 - (OFString *)componentsJoinedByString: (OFString *)separator
329  options: (int)options;
330 
339 - (OFString *)componentsJoinedByString: (OFString *)separator
340  usingSelector: (SEL)selector;
341 
355 - (OFString *)componentsJoinedByString: (OFString *)separator
356  usingSelector: (SEL)selector
357  options: (int)options;
358 
364 - (void)makeObjectsPerformSelector: (SEL)selector;
365 
374 - (void)makeObjectsPerformSelector: (SEL)selector
375  withObject: (nullable id)object;
376 
382 - (OFArray OF_GENERIC(ObjectType) *)sortedArray;
383 
394 - (OFArray OF_GENERIC(ObjectType) *)sortedArrayWithOptions: (int)options;
395 
401 - (OFArray OF_GENERIC(ObjectType) *)reversedArray;
402 
409 - (OFArray OF_GENERIC(ObjectType) *)arrayByAddingObject: (ObjectType)object;
410 
417 - (OFArray OF_GENERIC(ObjectType) *)arrayByAddingObjectsFromArray:
418  (OFArray OF_GENERIC(ObjectType) *)array;
419 
426 - (OFArray OF_GENERIC(ObjectType) *)arrayByRemovingObject: (ObjectType)object;
427 
434 - (OFEnumerator OF_GENERIC(ObjectType) *)objectEnumerator;
435 
436 #ifdef OF_HAVE_BLOCKS
437 
442 - (void)enumerateObjectsUsingBlock: (of_array_enumeration_block_t)block;
443 
450 - (OFArray *)mappedArrayUsingBlock: (of_array_map_block_t)block;
451 
460 - (OFArray OF_GENERIC(ObjectType) *)filteredArrayUsingBlock:
462 
479 - (nullable id)foldUsingBlock: (of_array_fold_block_t)block;
480 #endif
481 #if !defined(OF_HAVE_GENERICS) && !defined(DOXYGEN)
482 # undef ObjectType
483 #endif
484 @end
485 
486 @interface OFArrayEnumerator: OFEnumerator
487 {
488  OFArray *_array;
489  size_t _count;
490  unsigned long _mutations;
491  unsigned long *_Nullable _mutationsPtr;
492  size_t _position;
493 }
494 
495 - initWithArray: (OFArray *)data
496  mutationsPtr: (nullable unsigned long *)mutationsPtr;
497 @end
498 
499 OF_ASSUME_NONNULL_END
500 
501 #import "OFMutableArray.h"
502 
503 #if !defined(NSINTEGER_DEFINED) && !__has_feature(modules)
504 /* Required for array literals to work */
505 @compatibility_alias NSArray OFArray;
506 #endif
instancetype array()
Creates a new OFArray.
Definition: OFArray.m:131
A protocol for the creation of mutable copies.
Definition: OFObject.h:935
void(^ of_array_enumeration_block_t)(id object, size_t index, bool *stop)
A block for enumerating an OFArray.
Definition: OFArray.h:53
ObjectType const __unsafe_unretained _Nonnull *_Nonnull objects()
Returns the objects of the array as a C array.
Definition: OFArray.m:239
A class for handling strings.
Definition: OFString.h:114
A protocol implemented by classes that support encoding to a JSON representation. ...
Definition: OFJSONRepresentation.h:38
OFEnumerator * objectEnumerator()
Returns an OFEnumerator to enumerate through all objects of the array.
Definition: OFArray.m:797
A class which provides methods to enumerate through collections.
Definition: OFEnumerator.h:44
A protocol with methods common for all collections.
bool(^ of_array_filter_block_t)(id object, size_t index)
A block for filtering an OFArray.
Definition: OFArray.h:63
id _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
nullable ObjectType lastObject()
Returns the last object of the array or nil.
Definition: OFArray.m:378
A protocol implemented by classes that support encoding to a MessagePack representation.
Definition: OFMessagePackRepresentation.h:30
id _Nullable(^ of_array_fold_block_t)(id _Nullable left, id right)
A block for folding an OFArray.
Definition: OFArray.h:81
OFArray * reversedArray()
Returns a copy of the array with the order reversed.
Definition: OFArray.m:761
A protocol for the creation of copies.
Definition: OFObject.h:914
nullable ObjectType firstObject()
Returns the first object of the array or nil.
Definition: OFArray.m:370
OFArray * sortedArray()
Returns a sorted copy of the array.
Definition: OFArray.m:739
The root class for all other classes inside ObjFW.
Definition: OFObject.h:379
A range.
Definition: OFObject.h:66
An abstract class for storing objects in an array.
Definition: OFArray.h:89
A protocol for serializing objects.
Definition: OFSerialization.h:30