ObjFW
OFMapTable.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017
3  * Jonathan Schleifer <js@heap.zone>
4  *
5  * All rights reserved.
6  *
7  * This file is part of ObjFW. It may be distributed under the terms of the
8  * Q Public License 1.0, which can be found in the file LICENSE.QPL included in
9  * the packaging of this file.
10  *
11  * Alternatively, it may be distributed under the terms of the GNU General
12  * Public License, either version 2 or 3, which can be found in the file
13  * LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this
14  * file.
15  */
16 
17 #import "OFObject.h"
18 #import "OFEnumerator.h"
19 
20 OF_ASSUME_NONNULL_BEGIN
21 
29 typedef struct {
31  void *_Nullable (*_Nullable retain)(void *_Nullable object);
33  void (*_Nullable release)(void *_Nullable object);
35  uint32_t (*_Nullable hash)(void *_Nullable object);
37  bool (*_Nullable equal)(void *_Nullable object1,
38  void *_Nullable object2);
40 
41 #ifdef OF_HAVE_BLOCKS
42 
50 typedef void (^of_map_table_enumeration_block_t)(void *key, void *object,
51  bool *stop);
52 
60 typedef void *_Nonnull (^of_map_table_replace_block_t)(void *key, void *object);
61 #endif
62 
64 
72 {
73  of_map_table_functions_t _keyFunctions, _objectFunctions;
74  struct of_map_table_bucket *_Nonnull *_Nullable _buckets;
75  uint32_t _count, _capacity;
76  uint8_t _rotate;
77  unsigned long _mutations;
78 }
79 
83 @property (readonly, nonatomic) of_map_table_functions_t keyFunctions;
84 
88 @property (readonly, nonatomic) of_map_table_functions_t objectFunctions;
89 
97 + (instancetype)mapTableWithKeyFunctions: (of_map_table_functions_t)keyFunctions
98  objectFunctions: (of_map_table_functions_t)
99  objectFunctions;
100 
111 + (instancetype)mapTableWithKeyFunctions: (of_map_table_functions_t)keyFunctions
112  objectFunctions: (of_map_table_functions_t)
113  objectFunctions
114  capacity: (size_t)capacity;
115 
116 - init OF_UNAVAILABLE;
117 
126 - initWithKeyFunctions: (of_map_table_functions_t)keyFunctions
127  objectFunctions: (of_map_table_functions_t)objectFunctions;
128 
139 - initWithKeyFunctions: (of_map_table_functions_t)keyFunctions
140  objectFunctions: (of_map_table_functions_t)objectFunctions
141  capacity: (size_t)capacity OF_DESIGNATED_INITIALIZER;
142 
148 - (size_t)count;
149 
156 - (nullable void *)objectForKey: (void *)key;
157 
164 - (void)setObject: (void *)object
165  forKey: (void *)key;
166 
172 - (void)removeObjectForKey: (void *)key;
173 
177 - (void)removeAllObjects;
178 
186 - (bool)containsObject: (nullable void *)object;
187 
196 - (bool)containsObjectIdenticalTo: (nullable void *)object;
197 
205 
213 
214 #ifdef OF_HAVE_BLOCKS
215 
220 - (void)enumerateKeysAndObjectsUsingBlock:
222 
228 - (void)replaceObjectsUsingBlock: (of_map_table_replace_block_t)block;
229 #endif
230 @end
231 
238 @interface OFMapTableEnumerator: OFObject
239 {
240  OFMapTable *_mapTable;
241  struct of_map_table_bucket *_Nonnull *_Nullable _buckets;
242  uint32_t _capacity;
243  unsigned long _mutations;
244  unsigned long *_Nullable _mutationsPtr;
245  uint32_t _position;
246 }
247 
248 - init OF_UNAVAILABLE;
249 
255 - (nullable void *)nextObject;
256 
261 - (void)reset;
262 @end
263 
264 OF_ASSUME_NONNULL_END
of_map_table_functions_t keyFunctions
Definition: OFMapTable.h:84
OFMapTableEnumerator * keyEnumerator()
Returns an OFMapTableEnumerator to enumerate through the map table&#39;s keys.
Definition: OFMapTable.m:576
A struct describing the functions to be used by the map table.
Definition: OFMapTable.h:29
OFMapTableEnumerator * objectEnumerator()
Returns an OFMapTableEnumerator to enumerate through the map table&#39;s objects.
Definition: OFMapTable.m:585
size_t count()
Returns the number of objects in the map table.
Definition: OFMapTable.m:260
A class which provides methods to enumerate through an OFMapTable&#39;s keys or objects.
Definition: OFMapTable.h:239
id init()
Initializes an already allocated object.
Definition: OFObject.m:488
A protocol for the creation of copies.
Definition: OFObject.h:914
A protocol for fast enumeration.
Definition: OFEnumerator.h:105
void(^ of_map_table_enumeration_block_t)(void *key, void *object, bool *stop)
A block for enumerating an OFMapTable.
Definition: OFMapTable.h:50
void *_Nonnull(^ of_map_table_replace_block_t)(void *key, void *object)
A block for replacing objects in an OFMapTable.
Definition: OFMapTable.h:60
The root class for all other classes inside ObjFW.
Definition: OFObject.h:379
void removeAllObjects()
Removes all objects.
Definition: OFMapTable.m:513
A class similar to OFDictionary, but providing more options how keys and objects should be retained...
Definition: OFMapTable.h:71
of_map_table_functions_t objectFunctions
Definition: OFMapTable.h:89
void *_Nullable(* _Nullable)(void *_Nullable object)
Definition: OFMapTable.h:31