ObjFW
OFArray.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2008-2021 Jonathan Schleifer <js@nil.im>
3  *
4  * All rights reserved.
5  *
6  * This file is part of ObjFW. It may be distributed under the terms of the
7  * Q Public License 1.0, which can be found in the file LICENSE.QPL included in
8  * the packaging of this file.
9  *
10  * Alternatively, it may be distributed under the terms of the GNU General
11  * Public License, either version 2 or 3, which can be found in the file
12  * LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this
13  * file.
14  */
15 
16 #ifndef __STDC_LIMIT_MACROS
17 # define __STDC_LIMIT_MACROS
18 #endif
19 #ifndef __STDC_CONSTANT_MACROS
20 # define __STDC_CONSTANT_MACROS
21 #endif
22 
23 #include <stdarg.h>
24 
25 #import "OFObject.h"
26 #import "OFCollection.h"
27 #import "OFEnumerator.h"
28 #import "OFSerialization.h"
29 #import "OFJSONRepresentation.h"
30 #import "OFMessagePackRepresentation.h"
31 
32 OF_ASSUME_NONNULL_BEGIN
33 
36 @class OFString;
37 
38 enum {
39  OF_ARRAY_SKIP_EMPTY = 1,
40  OF_ARRAY_SORT_DESCENDING = 2
41 };
42 
43 #ifdef OF_HAVE_BLOCKS
52 typedef void (^of_array_enumeration_block_t)(id object, size_t index,
53  bool *stop);
54 
62 typedef bool (^of_array_filter_block_t)(id object, size_t index);
63 
71 typedef id _Nonnull (^of_array_map_block_t)(id object, size_t index);
72 
80 typedef id _Nullable (^of_array_fold_block_t)(id _Nullable left, id right);
81 #endif
82 
90 @interface OFArray OF_GENERIC(ObjectType): OFObject <OFCopying,
93 #if !defined(OF_HAVE_GENERICS) && !defined(DOXYGEN)
94 # define ObjectType id
95 #endif
103 @property (readonly, nonatomic)
104  ObjectType const __unsafe_unretained _Nonnull *_Nonnull objects;
105 
112 @property OF_NULLABLE_PROPERTY (readonly, nonatomic) ObjectType firstObject;
113 
120 @property OF_NULLABLE_PROPERTY (readonly, nonatomic) ObjectType lastObject;
121 
125 @property (readonly, nonatomic) OFArray OF_GENERIC(ObjectType) *sortedArray;
126 
130 @property (readonly, nonatomic) OFArray OF_GENERIC(ObjectType) *reversedArray;
131 
137 + (instancetype)array;
138 
145 + (instancetype)arrayWithObject: (ObjectType)object;
146 
153 + (instancetype)arrayWithObjects: (ObjectType)firstObject, ... OF_SENTINEL;
154 
161 + (instancetype)arrayWithArray: (OFArray OF_GENERIC(ObjectType) *)array;
162 
171 + (instancetype)
172  arrayWithObjects: (ObjectType const _Nonnull *_Nonnull)objects
173  count: (size_t)count;
174 
181 - (instancetype)initWithObject: (ObjectType)object;
182 
189 - (instancetype)initWithObjects: (ObjectType)firstObject, ... OF_SENTINEL;
190 
198 - (instancetype)initWithObject: (ObjectType)firstObject
199  arguments: (va_list)arguments;
200 
207 - (instancetype)initWithArray: (OFArray OF_GENERIC(ObjectType) *)array;
208 
217 - (instancetype)initWithObjects: (ObjectType const _Nonnull *_Nonnull)objects
218  count: (size_t)count;
219 
229 - (ObjectType)objectAtIndex: (size_t)index;
230 - (ObjectType)objectAtIndexedSubscript: (size_t)index;
231 
245 - (nullable id)valueForKey: (OFString *)key;
246 
257 - (void)setValue: (nullable id)value forKey: (OFString *)key;
258 
265 - (void)getObjects: (ObjectType __unsafe_unretained _Nonnull *_Nonnull)buffer
266  inRange: (of_range_t)range;
267 
276 - (size_t)indexOfObject: (ObjectType)object;
277 
286 - (size_t)indexOfObjectIdenticalTo: (ObjectType)object;
287 
295 - (bool)containsObject: (ObjectType)object;
296 
305 - (bool)containsObjectIdenticalTo: (ObjectType)object;
306 
313 - (OFArray OF_GENERIC(ObjectType) *)objectsInRange: (of_range_t)range;
314 
321 - (OFString *)componentsJoinedByString: (OFString *)separator;
322 
334 - (OFString *)componentsJoinedByString: (OFString *)separator
335  options: (int)options;
336 
345 - (OFString *)componentsJoinedByString: (OFString *)separator
346  usingSelector: (SEL)selector;
347 
361 - (OFString *)componentsJoinedByString: (OFString *)separator
362  usingSelector: (SEL)selector
363  options: (int)options;
364 
370 - (void)makeObjectsPerformSelector: (SEL)selector;
371 
380 - (void)makeObjectsPerformSelector: (SEL)selector
381  withObject: (nullable id)object;
382 
396 - (OFArray OF_GENERIC(ObjectType) *)sortedArrayUsingSelector: (SEL)selector
397  options: (int)options;
398 
399 #ifdef OF_HAVE_BLOCKS
412 - (OFArray OF_GENERIC(ObjectType) *)
413  sortedArrayUsingComparator: (of_comparator_t)comparator
414  options: (int)options;
415 #endif
416 
423 - (OFArray OF_GENERIC(ObjectType) *)arrayByAddingObject: (ObjectType)object;
424 
431 - (OFArray OF_GENERIC(ObjectType) *)arrayByAddingObjectsFromArray:
432  (OFArray OF_GENERIC(ObjectType) *)array;
433 
440 - (OFArray OF_GENERIC(ObjectType) *)arrayByRemovingObject: (ObjectType)object;
441 
442 #ifdef OF_HAVE_BLOCKS
448 - (void)enumerateObjectsUsingBlock: (of_array_enumeration_block_t)block;
449 
456 - (OFArray *)mappedArrayUsingBlock: (of_array_map_block_t)block;
457 
466 - (OFArray OF_GENERIC(ObjectType) *)filteredArrayUsingBlock:
468 
485 - (nullable id)foldUsingBlock: (of_array_fold_block_t)block;
486 #endif
487 #if !defined(OF_HAVE_GENERICS) && !defined(DOXYGEN)
488 # undef ObjectType
489 #endif
490 @end
491 
492 OF_ASSUME_NONNULL_END
493 
494 #import "OFMutableArray.h"
495 
496 #if !defined(NSINTEGER_DEFINED) && !__has_feature(modules)
497 /* Required for array literals to work */
498 @compatibility_alias NSArray OFArray;
499 #endif
bool(^ of_array_filter_block_t)(id object, size_t index)
A block for filtering an OFArray.
Definition: OFArray.h:62
void(^ of_array_enumeration_block_t)(id object, size_t index, bool *stop)
A block for enumerating an OFArray.
Definition: OFArray.h:52
id _Nullable(^ of_array_fold_block_t)(id _Nullable left, id right)
A block for folding an OFArray.
Definition: OFArray.h:80
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:71
of_comparison_result_t(^ of_comparator_t)(id _Nonnull left, id _Nonnull right)
A comparator to compare two objects.
Definition: OFObject.h:74
struct objc_object * id
A pointer to any object.
Definition: ObjFWRT.h:90
An abstract class for storing objects in an array.
Definition: OFArray.h:92
ObjectType const __unsafe_unretained _Nonnull *_Nonnull objects
The objects of the array as a C array.
Definition: OFArray.h:104
OFArray * sortedArray
The array sorted in ascending order.
Definition: OFArray.h:125
ObjectType lastObject
The last object of the array or nil.
Definition: OFArray.h:120
ObjectType firstObject
The first object of the array or nil.
Definition: OFArray.h:112
instancetype array()
Creates a new OFArray.
Definition: OFArray.m:131
OFArray * reversedArray
The array with the order reversed.
Definition: OFArray.h:130
The root class for all other classes inside ObjFW.
Definition: OFObject.h:520
A class for handling strings.
Definition: OFString.h:132
A protocol with methods common for all collections.
Definition: OFCollection.h:25
A protocol for the creation of copies.
Definition: OFObject.h:1187
A protocol implemented by classes that support encoding to a JSON representation.
Definition: OFJSONRepresentation.h:39
A protocol implemented by classes that support encoding to a MessagePack representation.
Definition: OFMessagePackRepresentation.h:29
A protocol for the creation of mutable copies.
Definition: OFObject.h:1208
A protocol for serializing objects.
Definition: OFSerialization.h:29
A range.
Definition: OFObject.h:93