ObjFW
 
Loading...
Searching...
No Matches
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
35OF_ASSUME_NONNULL_BEGIN
36
38
39@class OFIndexSet;
40@class OFString;
41
51
61
62#ifdef OF_HAVE_BLOCKS
71typedef void (^OFArrayEnumerationBlock)(id object, size_t index, bool *stop);
72
80typedef bool (^OFArrayFilterBlock)(id object, size_t index);
81
89typedef id _Nonnull (^OFArrayMapBlock)(id object, size_t index);
90
98typedef 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
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
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
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
528OF_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
bool(^ OFArrayFilterBlock)(id object, size_t index)
A block for filtering an OFArray.
Definition OFArray.h:80
id(^ OFArrayFoldBlock)(id left, id right)
A block for folding an OFArray.
Definition OFArray.h:98
OFArraySortOptions
Options for sorting an array.
Definition OFArray.h:57
@ OFArraySortDescending
Definition OFArray.h:59
void(^ OFArrayEnumerationBlock)(id object, size_t index, bool *stop)
A block for enumerating an OFArray.
Definition OFArray.h:71
id(^ OFArrayMapBlock)(id object, size_t index)
A block for mapping objects to objects in an OFArray.
Definition OFArray.h:89
OFArrayJoinOptions
Options for joining the objects of an array.
Definition OFArray.h:47
@ OFArraySkipEmptyComponents
Definition OFArray.h:49
OFComparisonResult(^ OFComparator)(id left, id right)
A comparator to compare two objects.
Definition OFObject.h:92
OFComparisonResult(* OFCompareFunction)(id left, id right, void *context)
A function to compare two objects.
Definition OFObject.h:81
const struct objc_selector * SEL
A selector.
Definition ObjFWRT.h:102
An abstract class for storing objects in an array.
Definition OFArray.h:110
ObjectType const __unsafe_unretained * objects
The objects of the array as a C array.
Definition OFArray.m:295
instancetype init()
Initializes an OFArray with no objects.
Definition OFArray.m:174
OFArray * sortedArray
The array sorted in ascending order.
Definition OFArray.m:764
ObjectType lastObject
The last object of the array or nil.
Definition OFArray.m:417
ObjectType firstObject
The first object of the array or nil.
Definition OFArray.m:409
instancetype array()
Creates a new OFArray.
Definition OFArray.m:137
OFEnumerator * objectEnumerator()
Returns an OFEnumerator to enumerate through all objects of the array.
Definition OFArray.m:832
OFArray * reversedArray
The array with the order reversed.
Definition OFArray.m:802
A class which provides methods to enumerate through collections.
Definition OFEnumerator.h:105
A class storing a set of indexes as sorted ranges.
Definition OFIndexSet.h:31
The root class for all other classes inside ObjFW.
Definition OFObject.h:956
A class for handling strings.
Definition OFString.h:143
A protocol with methods common for all collections.
Definition OFCollection.h:29
size_t count
The number of objects in the collection.
Definition OFCollection.h:33
A protocol for the creation of copies.
Definition OFObject.h:1618
A protocol implemented by classes that support encoding to a JSON representation.
Definition OFJSONRepresentation.h:49
A protocol implemented by classes that support encoding to a MessagePack representation.
Definition OFMessagePackRepresentation.h:33
A protocol for the creation of mutable copies.
Definition OFObject.h:1639
A range.
Definition OFObject.h:116