ObjFW
OFMapTable.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2008-2023 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 #import "OFObject.h"
17 #import "OFEnumerator.h"
18 
19 OF_ASSUME_NONNULL_BEGIN
20 
28 typedef struct {
30  void *_Nullable (*_Nullable retain)(void *_Nullable object);
32  void (*_Nullable release)(void *_Nullable object);
34  unsigned long (*_Nullable hash)(void *_Nullable object);
36  bool (*_Nullable equal)(void *_Nullable object1,
37  void *_Nullable object2);
39 
40 #ifdef OF_HAVE_BLOCKS
41 
49 typedef void (^OFMapTableEnumerationBlock)(void *_Nullable key,
50  void *_Nullable object, bool *stop);
51 
59 typedef void *_Nullable (^OFMapTableReplaceBlock)(void *_Nullable key,
60  void *_Nullable object);
61 #endif
62 
64 
71 OF_SUBCLASSING_RESTRICTED
73 {
74  OFMapTableFunctions _keyFunctions, _objectFunctions;
75  struct OFMapTableBucket *_Nonnull *_Nullable _buckets;
76  uint32_t _count, _capacity;
77  unsigned char _rotation;
78  unsigned long _mutations;
79 }
80 
84 @property (readonly, nonatomic) OFMapTableFunctions keyFunctions;
85 
89 @property (readonly, nonatomic) OFMapTableFunctions objectFunctions;
90 
94 @property (readonly, nonatomic) size_t count;
95 
103 + (instancetype)mapTableWithKeyFunctions: (OFMapTableFunctions)keyFunctions
104  objectFunctions: (OFMapTableFunctions)objectFunctions;
105 
116 + (instancetype)mapTableWithKeyFunctions: (OFMapTableFunctions)keyFunctions
117  objectFunctions: (OFMapTableFunctions)objectFunctions
118  capacity: (size_t)capacity;
119 
120 - (instancetype)init OF_UNAVAILABLE;
121 
130 - (instancetype)initWithKeyFunctions: (OFMapTableFunctions)keyFunctions
131  objectFunctions: (OFMapTableFunctions)objectFunctions;
132 
143 - (instancetype)initWithKeyFunctions: (OFMapTableFunctions)keyFunctions
144  objectFunctions: (OFMapTableFunctions)objectFunctions
145  capacity: (size_t)capacity
146  OF_DESIGNATED_INITIALIZER;
147 
154 - (nullable void *)objectForKey: (void *)key;
155 
162 - (void)setObject: (nullable void *)object forKey: (nullable void *)key;
163 
169 - (void)removeObjectForKey: (nullable void *)key;
170 
174 - (void)removeAllObjects;
175 
183 - (bool)containsObject: (nullable void *)object;
184 
193 - (bool)containsObjectIdenticalTo: (nullable void *)object;
194 
201 - (OFMapTableEnumerator *)keyEnumerator;
202 
209 - (OFMapTableEnumerator *)objectEnumerator;
210 
211 #ifdef OF_HAVE_BLOCKS
212 
217 - (void)enumerateKeysAndObjectsUsingBlock: (OFMapTableEnumerationBlock)block;
218 
224 - (void)replaceObjectsUsingBlock: (OFMapTableReplaceBlock)block;
225 #endif
226 @end
227 
234 #ifndef OF_MAP_TABLE_M
235 OF_SUBCLASSING_RESTRICTED
236 #endif
237 @interface OFMapTableEnumerator: OFObject
238 {
239  OFMapTable *_mapTable;
240  struct OFMapTableBucket *_Nonnull *_Nullable _buckets;
241  uint32_t _capacity;
242  unsigned long _mutations, *_Nullable _mutationsPtr, _position;
243 }
244 
245 - (instancetype)init OF_UNAVAILABLE;
246 
253 - (void *_Nullable *_Nullable)nextObject;
254 @end
255 
256 OF_ASSUME_NONNULL_END
A struct describing the functions to be used by the map table.
Definition: OFMapTable.h:28
A protocol for fast enumeration.
Definition: OFEnumerator.h:75
The root class for all other classes inside ObjFW.
Definition: OFObject.h:686
void *(^ OFMapTableReplaceBlock)(void *key, void *object)
A block for replacing objects in an OFMapTable.
Definition: OFMapTable.h:59
A class which provides methods to enumerate through an OFMapTable&#39;s keys or objects.
Definition: OFMapTable.h:238
A class similar to OFDictionary, but providing more options how keys and objects should be retained...
Definition: OFMapTable.h:72
void(^ OFMapTableEnumerationBlock)(void *key, void *object, bool *stop)
A block for enumerating an OFMapTable.
Definition: OFMapTable.h:49
A protocol for the creation of copies.
Definition: OFObject.h:1346