Differences From Artifact [6adf8ae2bb]:
- File
src/OFMapTable.m
— part of check-in
[627511b032]
at
2020-11-14 12:23:55
on branch trunk
— Rename of_malloc and of_calloc
The new names should be more accurate. (user: js, size: 16398) [annotate] [blame] [check-ins using] [more...]
To Artifact [8788cb21e4]:
- File src/OFMapTable.m — part of check-in [eb328735f2] at 2021-03-08 00:04:45 on branch trunk — Make GCC happy again (user: js, size: 16447) [annotate] [blame] [check-ins using] [more...]
1 | /* | < < | | 1 2 3 4 5 6 7 8 9 | /* * Copyright (c) 2008-2021 Jonathan Schleifer <js@nil.im> * * All rights reserved. * * This file is part of ObjFW. It may be distributed under the terms of the * Q Public License 1.0, which can be found in the file LICENSE.QPL included in * the packaging of this file. * |
︙ | ︙ | |||
57 58 59 60 61 62 63 | static bool defaultEqual(void *object1, void *object2) { return (object1 == object2); } | < < < < < < < | 55 56 57 58 59 60 61 62 63 64 65 66 67 68 | static bool defaultEqual(void *object1, void *object2) { return (object1 == object2); } OF_DIRECT_MEMBERS @interface OFMapTableEnumerator () - (instancetype)of_initWithMapTable: (OFMapTable *)mapTable buckets: (struct of_map_table_bucket **)buckets capacity: (unsigned long)capacity mutationsPointer: (unsigned long *)mutationsPtr OF_METHOD_FAMILY(init); |
︙ | ︙ | |||
186 187 188 189 190 191 192 193 194 195 196 197 198 199 | } } free(_buckets); [super dealloc]; } - (bool)isEqual: (id)object { OFMapTable *mapTable; if (object == self) return true; | > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > | 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 | } } free(_buckets); [super dealloc]; } static void resizeForCount(OFMapTable *self, unsigned long count) { unsigned long fullness, capacity; struct of_map_table_bucket **buckets; if (count > ULONG_MAX / sizeof(*self->_buckets) || count > ULONG_MAX / 8) @throw [OFOutOfRangeException exception]; fullness = count * 8 / self->_capacity; if (fullness >= 6) { if (self->_capacity > ULONG_MAX / 2) return; capacity = self->_capacity * 2; } else if (fullness <= 1) capacity = self->_capacity / 2; else return; /* * Don't downsize if we have an initial capacity or if we would fall * below the minimum capacity. */ if ((capacity < self->_capacity && count > self->_count) || capacity < MIN_CAPACITY) return; buckets = of_alloc_zeroed(capacity, sizeof(*buckets)); for (unsigned long i = 0; i < self->_capacity; i++) { if (self->_buckets[i] != NULL && self->_buckets[i] != &deleted) { unsigned long j, last; last = capacity; for (j = self->_buckets[i]->hash & (capacity - 1); j < last && buckets[j] != NULL; j++); /* In case the last bucket is already used */ if (j >= last) { last = self->_buckets[i]->hash & (capacity - 1); for (j = 0; j < last && buckets[j] != NULL; j++); } if (j >= last) @throw [OFOutOfRangeException exception]; buckets[j] = self->_buckets[i]; } } free(self->_buckets); self->_buckets = buckets; self->_capacity = capacity; } static void setObject(OFMapTable *restrict self, void *key, void *object, unsigned long hash) { unsigned long i, last; void *old; if (key == NULL || object == NULL) @throw [OFInvalidArgumentException exception]; hash = OF_ROL(hash, self->_rotate); last = self->_capacity; for (i = hash & (self->_capacity - 1); i < last && self->_buckets[i] != NULL; i++) { if (self->_buckets[i] == &deleted) continue; if (self->_keyFunctions.equal(self->_buckets[i]->key, key)) break; } /* In case the last bucket is already used */ if (i >= last) { last = hash & (self->_capacity - 1); for (i = 0; i < last && self->_buckets[i] != NULL; i++) { if (self->_buckets[i] == &deleted) continue; if (self->_keyFunctions.equal( self->_buckets[i]->key, key)) break; } } /* Key not in map table */ if (i >= last || self->_buckets[i] == NULL || self->_buckets[i] == &deleted || !self->_keyFunctions.equal(self->_buckets[i]->key, key)) { struct of_map_table_bucket *bucket; resizeForCount(self, self->_count + 1); self->_mutations++; last = self->_capacity; for (i = hash & (self->_capacity - 1); i < last && self->_buckets[i] != NULL && self->_buckets[i] != &deleted; i++); /* In case the last bucket is already used */ if (i >= last) { last = hash & (self->_capacity - 1); for (i = 0; i < last && self->_buckets[i] != NULL && self->_buckets[i] != &deleted; i++); } if (i >= last) @throw [OFOutOfRangeException exception]; bucket = of_alloc(1, sizeof(*bucket)); @try { bucket->key = self->_keyFunctions.retain(key); } @catch (id e) { free(bucket); @throw e; } @try { bucket->object = self->_objectFunctions.retain(object); } @catch (id e) { self->_keyFunctions.release(bucket->key); free(bucket); @throw e; } bucket->hash = hash; self->_buckets[i] = bucket; self->_count++; return; } old = self->_buckets[i]->object; self->_buckets[i]->object = self->_objectFunctions.retain(object); self->_objectFunctions.release(old); } - (bool)isEqual: (id)object { OFMapTable *mapTable; if (object == self) return true; |
︙ | ︙ | |||
242 243 244 245 246 247 248 | initWithKeyFunctions: _keyFunctions objectFunctions: _objectFunctions capacity: _capacity]; @try { for (unsigned long i = 0; i < _capacity; i++) if (_buckets[i] != NULL && _buckets[i] != &deleted) | | | | < | 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 | initWithKeyFunctions: _keyFunctions objectFunctions: _objectFunctions capacity: _capacity]; @try { for (unsigned long i = 0; i < _capacity; i++) if (_buckets[i] != NULL && _buckets[i] != &deleted) setObject(copy, _buckets[i]->key, _buckets[i]->object, OF_ROR(_buckets[i]->hash, _rotate)); } @catch (id e) { [copy release]; @throw e; } return copy; } |
︙ | ︙ | |||
294 295 296 297 298 299 300 | if (_keyFunctions.equal(_buckets[i]->key, key)) return _buckets[i]->object; } return NULL; } | < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < | < < < | | 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 | if (_keyFunctions.equal(_buckets[i]->key, key)) return _buckets[i]->object; } return NULL; } - (void)setObject: (void *)object forKey: (void *)key { setObject(self, key, object,_keyFunctions.hash(key)); } - (void)removeObjectForKey: (void *)key { unsigned long i, hash, last; if (key == NULL) |
︙ | ︙ | |||
472 473 474 475 476 477 478 | _keyFunctions.release(_buckets[i]->key); _objectFunctions.release(_buckets[i]->object); free(_buckets[i]); _buckets[i] = &deleted; _count--; | | | 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 | _keyFunctions.release(_buckets[i]->key); _objectFunctions.release(_buckets[i]->object); free(_buckets[i]); _buckets[i] = &deleted; _count--; resizeForCount(self, _count); return; } } if (i < last) return; |
︙ | ︙ | |||
497 498 499 500 501 502 503 | _objectFunctions.release(_buckets[i]->object); free(_buckets[i]); _buckets[i] = &deleted; _count--; _mutations++; | | | 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 | _objectFunctions.release(_buckets[i]->object); free(_buckets[i]); _buckets[i] = &deleted; _count--; _mutations++; resizeForCount(self, _count); return; } } } - (void)removeAllObjects |
︙ | ︙ |