ObjFW
OFSet.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 #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 
32 OF_ASSUME_NONNULL_BEGIN
33 
36 @class OFArray OF_GENERIC(ObjectType);
37 
38 #ifdef OF_HAVE_BLOCKS
39 
46 typedef void (^OFSetEnumerationBlock)(id object, bool *stop);
47 
54 typedef bool (^OFSetFilterBlock)(id object);
55 #endif
56 
68 @interface OFSet OF_GENERIC(ObjectType): OFObject <OFCollection, OFCopying,
70 #if !defined(OF_HAVE_GENERICS) && !defined(DOXYGEN)
71 # define ObjectType id
72 #endif
73 
76 @property (readonly, nonatomic) OFArray OF_GENERIC(ObjectType) *allObjects;
77 
81 @property OF_NULLABLE_PROPERTY (readonly, nonatomic) ObjectType anyObject;
82 
88 + (instancetype)set;
89 
96 + (instancetype)setWithSet: (OFSet OF_GENERIC(ObjectType) *)set;
97 
104 + (instancetype)setWithArray: (OFArray OF_GENERIC(ObjectType) *)array;
105 
112 + (instancetype)setWithObjects: (ObjectType)firstObject, ...;
113 
121 + (instancetype)setWithObjects: (ObjectType const _Nonnull *_Nonnull)objects
122  count: (size_t)count;
123 
129 - (instancetype)init OF_DESIGNATED_INITIALIZER;
130 
137 - (instancetype)initWithSet: (OFSet OF_GENERIC(ObjectType) *)set;
138 
145 - (instancetype)initWithArray: (OFArray OF_GENERIC(ObjectType) *)array;
146 
153 - (instancetype)initWithObjects: (ObjectType)firstObject, ... OF_SENTINEL;
154 
163 - (instancetype)initWithObject: (ObjectType)firstObject
164  arguments: (va_list)arguments;
165 
173 - (instancetype)initWithObjects: (ObjectType const _Nonnull *_Nonnull)objects
174  count: (size_t)count OF_DESIGNATED_INITIALIZER;
175 
181 - (OFEnumerator OF_GENERIC(ObjectType) *)objectEnumerator;
182 
188 - (bool)isSubsetOfSet: (OFSet OF_GENERIC(ObjectType) *)set;
189 
197 - (bool)intersectsSet: (OFSet OF_GENERIC(ObjectType) *)set;
198 
205 - (OFSet OF_GENERIC(ObjectType) *)setByAddingObjectsFromSet:
206  (OFSet OF_GENERIC(ObjectType) *)set;
207 
215 - (bool)containsObject: (ObjectType)object;
216 
229 - (nullable id)valueForKey: (OFString *)key;
230 
241 - (void)setValue: (nullable id)value forKey: (OFString *)key;
242 
243 #ifdef OF_HAVE_BLOCKS
244 
249 - (void)enumerateObjectsUsingBlock: (OFSetEnumerationBlock)block;
250 
258 - (OFSet OF_GENERIC(ObjectType) *)
259  filteredSetUsingBlock: (OFSetFilterBlock)block;
260 #endif
261 #if !defined(OF_HAVE_GENERICS) && !defined(DOXYGEN)
262 # undef ObjectType
263 #endif
264 @end
265 
266 OF_ASSUME_NONNULL_END
267 
268 #import "OFMutableSet.h"
OFEnumerator * objectEnumerator()
Returns an OFEnumerator to enumerate through all objects of the set.
Definition: OFSet.m:312
An abstract class for storing objects in an array.
Definition: OFArray.h:107
The root class for all other classes inside ObjFW.
Definition: OFObject.h:690
A class for handling strings.
Definition: OFString.h:142
void(^ OFSetEnumerationBlock)(id object, bool *stop)
A block for enumerating an OFSet.
Definition: OFSet.h:46
OFArray * allObjects
An array of all objects in the set.
Definition: OFSet.h:76
A protocol with methods common for all collections.
An abstract class for an unordered set of unique objects.
Definition: OFSet.h:68
instancetype init()
Initializes an already allocated set to be empty.
Definition: OFSet.m:140
instancetype set()
Creates a new set.
Definition: OFSet.m:106
A protocol for the creation of mutable copies.
Definition: OFObject.h:1371
A protocol for the creation of copies.
Definition: OFObject.h:1350
bool(^ OFSetFilterBlock)(id object)
A block for filtering an OFSet.
Definition: OFSet.h:54
ObjectType anyObject
An arbitrary object in the set.
Definition: OFSet.h:81
A class which provides methods to enumerate through collections.
Definition: OFEnumerator.h:103