ObjFW
OFArray.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2008-2026 Jonathan Schleifer <js@nil.im>
3  *
4  * All rights reserved.
5  *
6  * This program is free software: you can redistribute it and/or modify it
7  * under the terms of the GNU Lesser General Public License version 3.0 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
13  * version 3.0 for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * version 3.0 along with this program. If not, see
17  * <https://www.gnu.org/licenses/>.
18  */
19 
20 #ifndef __STDC_LIMIT_MACROS
21 # define __STDC_LIMIT_MACROS
22 #endif
23 #ifndef __STDC_CONSTANT_MACROS
24 # define __STDC_CONSTANT_MACROS
25 #endif
26 
27 #include <stdarg.h>
28 
29 #import "OFObject.h"
30 #import "OFCollection.h"
31 #import "OFEnumerator.h"
32 #import "OFJSONRepresentation.h"
33 #import "OFMessagePackRepresentation.h"
34 
35 OF_ASSUME_NONNULL_BEGIN
36 
39 @class OFIndexSet;
40 @class OFString;
41 
47 typedef enum {
51 
57 typedef enum {
61 
62 #ifdef OF_HAVE_BLOCKS
63 
71 typedef void (^OFArrayEnumerationBlock)(id object, size_t index, bool *stop);
72 
80 typedef bool (^OFArrayFilterBlock)(id object, size_t index);
81 
89 typedef id _Nonnull (^OFArrayMapBlock)(id object, size_t index);
90 
98 typedef id _Nullable (^OFArrayFoldBlock)(id _Nullable left, id right);
99 #endif
100 
108 @interface OFArray OF_GENERIC(ObjectType): OFObject <OFCopying,
111 #if !defined(OF_HAVE_GENERICS) && !defined(DOXYGEN)
112 # define ObjectType id
113 #endif
114 
121 @property (readonly, nonatomic)
122  ObjectType const __unsafe_unretained _Nonnull *_Nonnull objects;
123 
130 @property OF_NULLABLE_PROPERTY (readonly, nonatomic) ObjectType firstObject;
131 
138 @property OF_NULLABLE_PROPERTY (readonly, nonatomic) ObjectType lastObject;
139 
143 @property (readonly, nonatomic) OFArray OF_GENERIC(ObjectType) *sortedArray;
144 
148 @property (readonly, nonatomic) OFArray OF_GENERIC(ObjectType) *reversedArray;
149 
155 + (instancetype)array;
156 
163 + (instancetype)arrayWithObject: (ObjectType)object;
164 
171 + (instancetype)arrayWithObjects: (ObjectType)firstObject, ... OF_SENTINEL;
172 
179 + (instancetype)arrayWithArray: (OFArray OF_GENERIC(ObjectType) *)array;
180 
189 + (instancetype)arrayWithObjects: (ObjectType const _Nonnull *_Nonnull)objects
190  count: (size_t)count;
191 
197 - (instancetype)init OF_DESIGNATED_INITIALIZER;
198 
205 - (instancetype)initWithObject: (ObjectType)object;
206 
213 - (instancetype)initWithObjects: (ObjectType)firstObject, ... OF_SENTINEL;
214 
222 - (instancetype)initWithObject: (ObjectType)firstObject
223  arguments: (va_list)arguments;
224 
231 - (instancetype)initWithArray: (OFArray OF_GENERIC(ObjectType) *)array;
232 
241 - (instancetype)initWithObjects: (ObjectType const _Nonnull *_Nonnull)objects
242  count: (size_t)count OF_DESIGNATED_INITIALIZER;
243 
249 - (OFEnumerator OF_GENERIC(ObjectType) *)objectEnumerator;
250 
260 - (ObjectType)objectAtIndex: (size_t)index;
261 - (ObjectType)objectAtIndexedSubscript: (size_t)index;
262 
276 - (nullable id)valueForKey: (OFString *)key;
277 
288 - (void)setValue: (nullable id)value forKey: (OFString *)key;
289 
296 - (void)getObjects: (ObjectType __unsafe_unretained _Nonnull *_Nonnull)buffer
297  inRange: (OFRange)range;
298 
307 - (size_t)indexOfObject: (ObjectType)object;
308 
317 - (size_t)indexOfObjectIdenticalTo: (ObjectType)object;
318 
326 - (bool)containsObject: (ObjectType)object;
327 
336 - (bool)containsObjectIdenticalTo: (ObjectType)object;
337 
344 - (OFArray OF_GENERIC(ObjectType) *)objectsInRange: (OFRange)range;
345 
352 - (OFArray OF_GENERIC(ObjectType) *)objectsAtIndexes: (OFIndexSet *)indexes;
353 
360 - (OFString *)componentsJoinedByString: (OFString *)separator;
361 
369 - (OFString *)componentsJoinedByString: (OFString *)separator
370  options: (OFArrayJoinOptions)options;
371 
380 - (OFString *)componentsJoinedByString: (OFString *)separator
381  usingSelector: (SEL)selector;
382 
392 - (OFString *)componentsJoinedByString: (OFString *)separator
393  usingSelector: (SEL)selector
394  options: (OFArrayJoinOptions)options;
395 
403 - (void)makeObjectsPerformSelector: (SEL)selector
404  OF_DEPRECATED(ObjFW, 1, 4, "Use fast enumeration instead");
405 
416 - (void)makeObjectsPerformSelector: (SEL)selector
417  withObject: (nullable id)object
418  OF_DEPRECATED(ObjFW, 1, 4, "Use fast enumeration instead");
419 
429 - (OFArray OF_GENERIC(ObjectType) *)
430  sortedArrayUsingSelector: (SEL)selector
431  options: (OFArraySortOptions)options;
432 
442 - (OFArray OF_GENERIC(ObjectType) *)
443  sortedArrayUsingFunction: (OFCompareFunction)compare
444  context: (nullable void *)context
445  options: (OFArraySortOptions)options;
446 
447 #ifdef OF_HAVE_BLOCKS
448 
456 - (OFArray OF_GENERIC(ObjectType) *)
457  sortedArrayUsingComparator: (OFComparator)comparator
458  options: (OFArraySortOptions)options;
459 #endif
460 
467 - (OFArray OF_GENERIC(ObjectType) *)arrayByAddingObject: (ObjectType)object;
468 
475 - (OFArray OF_GENERIC(ObjectType) *)arrayByAddingObjectsFromArray:
476  (OFArray OF_GENERIC(ObjectType) *)array;
477 
478 #ifdef OF_HAVE_BLOCKS
479 
484 - (void)enumerateObjectsUsingBlock: (OFArrayEnumerationBlock)block;
485 
492 - (OFArray *)mappedArrayUsingBlock: (OFArrayMapBlock)block;
493 
502 - (OFArray OF_GENERIC(ObjectType) *)filteredArrayUsingBlock:
503  (OFArrayFilterBlock)block;
504 
521 - (nullable id)foldUsingBlock: (OFArrayFoldBlock)block;
522 #endif
523 #if !defined(OF_HAVE_GENERICS) && !defined(DOXYGEN)
524 # undef ObjectType
525 #endif
526 @end
527 
528 OF_ASSUME_NONNULL_END
529 
530 #import "OFMutableArray.h"
531 
532 #if !defined(NSINTEGER_DEFINED) && !__has_feature(objc_modules)
533 /* Required for array literals to work */
534 @compatibility_alias NSArray OFArray;
535 #endif
OFArray * reversedArray
The array with the order reversed.
Definition: OFArray.h:148
instancetype array()
Creates a new OFArray.
Definition: OFArray.m:137
An abstract class for storing objects in an array.
Definition: OFArray.h:108
OFArrayJoinOptions
Options for joining the objects of an array.
Definition: OFArray.h:47
OFArraySortOptions
Options for sorting an array.
Definition: OFArray.h:57
ObjectType firstObject
The first object of the array or nil.
Definition: OFArray.h:130
The root class for all other classes inside ObjFW.
Definition: OFObject.h:954
void(^ OFArrayEnumerationBlock)(id object, size_t index, bool *stop)
A block for enumerating an OFArray.
Definition: OFArray.h:71
A class storing a set of indexes as sorted ranges.
Definition: OFIndexSet.h:29
A protocol implemented by classes that support encoding to a JSON representation. ...
Definition: OFJSONRepresentation.h:49
OFEnumerator * objectEnumerator()
Returns an OFEnumerator to enumerate through all objects of the array.
Definition: OFArray.m:832
A class for handling strings.
Definition: OFString.h:142
OFArray * sortedArray
The array sorted in ascending order.
Definition: OFArray.h:143
A protocol with methods common for all collections.
id(^ OFArrayMapBlock)(id object, size_t index)
A block for mapping objects to objects in an OFArray.
Definition: OFArray.h:89
ObjectType const __unsafe_unretained * objects
The objects of the array as a C array.
Definition: OFArray.h:122
A protocol implemented by classes that support encoding to a MessagePack representation.
Definition: OFMessagePackRepresentation.h:33
bool(^ OFArrayFilterBlock)(id object, size_t index)
A block for filtering an OFArray.
Definition: OFArray.h:80
ObjectType lastObject
The last object of the array or nil.
Definition: OFArray.h:138
OFComparisonResult(^ OFComparator)(id left, id right)
A comparator to compare two objects.
Definition: OFObject.h:92
A protocol for the creation of mutable copies.
Definition: OFObject.h:1639
A protocol for the creation of copies.
Definition: OFObject.h:1618
instancetype init()
Initializes an OFArray with no objects.
Definition: OFArray.m:174
Definition: OFArray.h:49
A class which provides methods to enumerate through collections.
Definition: OFEnumerator.h:105
A range.
Definition: OFObject.h:116
id(^ OFArrayFoldBlock)(id left, id right)
A block for folding an OFArray.
Definition: OFArray.h:98
OFComparisonResult(* OFCompareFunction)(id left, id right, void *context)
A function to compare two objects.
Definition: OFObject.h:81
Definition: OFArray.h:59