ObjFW
OFMutableArray.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2008-2024 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 #import "OFArray.h"
21 
22 OF_ASSUME_NONNULL_BEGIN
23 
26 #ifdef OF_HAVE_BLOCKS
27 
34 typedef id _Nonnull (^OFArrayReplaceBlock)(id object, size_t index);
35 #endif
36 
47 @interface OFMutableArray OF_GENERIC(ObjectType): OFArray OF_GENERIC(ObjectType)
48 #if !defined(OF_HAVE_GENERICS) && !defined(DOXYGEN)
49 # define ObjectType id
50 #endif
51 
58 + (instancetype)arrayWithCapacity: (size_t)capacity;
59 
65 - (instancetype)init OF_DESIGNATED_INITIALIZER;
66 
74 - (instancetype)initWithCapacity: (size_t)capacity OF_DESIGNATED_INITIALIZER;
75 
81 - (void)addObject: (ObjectType)object;
82 
88 - (void)addObjectsFromArray: (OFArray OF_GENERIC(ObjectType) *)array;
89 
96 - (void)insertObject: (ObjectType)object atIndex: (size_t)index;
97 
104 - (void)insertObjectsFromArray: (OFArray OF_GENERIC(ObjectType) *)array
105  atIndex: (size_t)index;
106 
114 - (void)replaceObject: (ObjectType)oldObject withObject: (ObjectType)newObject;
115 
122 - (void)replaceObjectAtIndex: (size_t)index withObject: (ObjectType)object;
123 
134 - (void)setObject: (ObjectType)object atIndexedSubscript: (size_t)index;
135 
143 - (void)replaceObjectIdenticalTo: (ObjectType)oldObject
144  withObject: (ObjectType)newObject;
145 
151 - (void)removeObject: (ObjectType)object;
152 
159 - (void)removeObjectIdenticalTo: (ObjectType)object;
160 
166 - (void)removeObjectAtIndex: (size_t)index;
167 
173 - (void)removeObjectsInRange: (OFRange)range;
174 
178 - (void)removeLastObject;
179 
183 - (void)removeAllObjects;
184 
185 #ifdef OF_HAVE_BLOCKS
186 
191 - (void)replaceObjectsUsingBlock: (OFArrayReplaceBlock)block;
192 #endif
193 
200 - (void)exchangeObjectAtIndex: (size_t)index1 withObjectAtIndex: (size_t)index2;
201 
205 - (void)sort;
206 
214 - (void)sortUsingSelector: (SEL)selector options: (OFArraySortOptions)options;
215 
223 - (void)sortUsingFunction: (OFCompareFunction)compare
224  context: (nullable void *)context
225  options: (OFArraySortOptions)options;
226 
227 #ifdef OF_HAVE_BLOCKS
228 
234 - (void)sortUsingComparator: (OFComparator)comparator
235  options: (OFArraySortOptions)options;
236 #endif
237 
241 - (void)reverse;
242 
246 - (void)makeImmutable;
247 #if !defined(OF_HAVE_GENERICS) && !defined(DOXYGEN)
248 # undef ObjectType
249 #endif
250 @end
251 
252 OF_ASSUME_NONNULL_END
id(^ OFArrayReplaceBlock)(id object, size_t index)
A block for replacing values in an OFMutableArray.
Definition: OFMutableArray.h:34
An abstract class for storing objects in an array.
Definition: OFArray.h:107
OFArraySortOptions
Options for sorting an array.
Definition: OFArray.h:56
void makeImmutable()
Converts the mutable array to an immutable array.
Definition: OFMutableArray.m:420
void removeAllObjects()
Removes all objects.
Definition: OFMutableArray.m:316
void removeLastObject()
Removes the last object.
Definition: OFMutableArray.m:306
An abstract class for storing, adding and removing objects in an array.
Definition: OFMutableArray.h:47
void sort()
Sorts the array in ascending order.
Definition: OFMutableArray.m:348
void reverse()
Reverts the order of the objects in the array.
Definition: OFMutableArray.m:409
instancetype init()
Initializes an OFMutableArray with no objects.
Definition: OFMutableArray.m:164
OFComparisonResult(^ OFComparator)(id left, id right)
A comparator to compare two objects.
Definition: OFObject.h:86
A range.
Definition: OFObject.h:110
OFComparisonResult(* OFCompareFunction)(id left, id right, void *context)
A function to compare two objects.
Definition: OFObject.h:75