1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 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 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083
|
/** Implementation of NSHashTable for GNUStep
Copyright (C) 2009 Free Software Foundation, Inc.
Written by: Richard Frith-Macdonald <rfm@gnu.org>
Date: April 2009
Based on original o_hash code by Albin L. Jones <Albin.L.Jones@Dartmouth.EDU>
This file is part of the GNUstep Base Library.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free
Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110 USA.
$Date: 2008-06-08 11:38:33 +0100 (Sun, 08 Jun 2008) $ $Revision: 26606 $
*/
#import "common.h"
#import "Foundation/NSArray.h"
#import "Foundation/NSAutoreleasePool.h"
#import "Foundation/NSDictionary.h"
#import "Foundation/NSEnumerator.h"
#import "Foundation/NSException.h"
#import "Foundation/NSHashTable.h"
#import "NSConcretePointerFunctions.h"
#import "NSCallBacks.h"
#import "GSPrivate.h"
static Class concreteClass = Nil;
static unsigned instanceSize = 0;
/* Here is the interface for the concrete class as used by the functions.
*/
typedef struct _GSIMapBucket GSIMapBucket_t;
typedef struct _GSIMapNode GSIMapNode_t;
typedef GSIMapBucket_t *GSIMapBucket;
typedef GSIMapNode_t *GSIMapNode;
@interface NSConcreteHashTable : NSHashTable
{
@public
NSZone *zone;
size_t nodeCount; /* Number of used nodes in hash. */
size_t bucketCount; /* Number of buckets in hash. */
GSIMapBucket buckets; /* Array of buckets. */
GSIMapNode freeNodes; /* List of unused nodes. */
GSIMapNode *nodeChunks; /* Chunks of allocated memory. */
size_t chunkCount; /* Number of chunks in array. */
size_t increment; /* Amount to grow by. */
unsigned long version; /* For fast enumeration. */
BOOL legacy; /* old style callbacks? */
union {
PFInfo pf;
NSHashTableCallBacks old;
}cb;
}
@end
#define GSI_MAP_HAS_VALUE 0
#define GSI_MAP_KTYPES GSUNION_PTR | GSUNION_OBJ
#define GSI_MAP_TABLE_T NSConcreteHashTable
#define GSI_MAP_TABLE_S instanceSize
#define IS_WEAK(M) \
M->cb.pf.options & (NSPointerFunctionsZeroingWeakMemory | NSPointerFunctionsWeakMemory)
#define GSI_MAP_HASH(M, X)\
(M->legacy ? M->cb.old.hash(M, X.ptr) \
: pointerFunctionsHash(&M->cb.pf, X.ptr))
#define GSI_MAP_EQUAL(M, X, Y)\
(M->legacy ? M->cb.old.isEqual(M, X.ptr, Y.ptr) \
: pointerFunctionsEqual(&M->cb.pf, X.ptr, Y.ptr))
#define GSI_MAP_RELEASE_KEY(M, X)\
(M->legacy ? M->cb.old.release(M, X.ptr) \
: IS_WEAK(M) ? nil : pointerFunctionsRelinquish(&M->cb.pf, &X.ptr))
#define GSI_MAP_RETAIN_KEY(M, X)\
(M->legacy ? M->cb.old.retain(M, X.ptr) \
: IS_WEAK(M) ? nil : pointerFunctionsAcquire(&M->cb.pf, &X.ptr, X.ptr))
#define GSI_MAP_ZEROED(M)\
(M->legacy ? 0 \
: (IS_WEAK(M) ? YES : NO))
#define GSI_MAP_WRITE_KEY(M, addr, x) \
if (M->legacy) \
*(addr) = x;\
else\
pointerFunctionsAssign(&M->cb.pf, (void**)addr, (x).obj);
#define GSI_MAP_READ_KEY(M,addr) \
(M->legacy ? *(addr) :\
(__typeof__(*addr))pointerFunctionsRead(&M->cb.pf, (void**)addr))
#define GSI_MAP_ENUMERATOR NSHashEnumerator
#include "GNUstepBase/GSIMap.h"
/**** Function Implementations ****/
/**
* Returns an array of all the objects in the table.
* NB. The table <em>must</em> contain objects for its keys.
*/
NSArray *
NSAllHashTableObjects(NSHashTable *table)
{
return [table allObjects];
}
/**
* Compares the two hash tables for equality.
* If the tables are different sizes, returns NO.
* Otherwise, compares the values in the two tables
* and returns NO if they differ.<br />
* The GNUstep implementation enumerates the values in table1
* and uses the hash and isEqual functions of table2 for comparison.
*/
BOOL
NSCompareHashTables(NSHashTable *table1, NSHashTable *table2)
{
if (table1 == table2)
{
return YES;
}
if (table1 == nil)
{
NSWarnFLog(@"Nul first argument supplied");
return NO;
}
if (table2 == nil)
{
NSWarnFLog(@"Nul second argument supplied");
return NO;
}
if ([table1 count] != [table2 count])
{
return NO;
}
if (object_getClass(table1) != concreteClass
&& object_getClass(table2) == concreteClass)
{
id t = table1;
table1 = table2;
table2 = t;
}
if (object_getClass(table1) == concreteClass)
{
BOOL result = YES;
NSHashEnumerator enumerator;
GSIMapNode n1;
enumerator = NSEnumerateHashTable(table1);
if (object_getClass(table2) == concreteClass)
{
GSIMapTable t2 = (GSIMapTable)table2;
while ((n1 = GSIMapEnumeratorNextNode(&enumerator)) != 0)
{
if (GSIMapNodeForKey(t2, n1->key) == 0)
{
result = NO;
break;
}
}
}
else
{
while ((n1 = GSIMapEnumeratorNextNode(&enumerator)) != 0)
{
void *v1 = n1->key.ptr;
void *v2;
v2 = NSHashGet(table2, v1);
if (v2 == 0 && v2 != v1)
{
result = NO;
break;
}
}
}
NSEndHashTableEnumeration(&enumerator);
return result;
}
else
{
BOOL result = YES;
NSHashEnumerator enumerator;
void *v1;
enumerator = NSEnumerateHashTable(table1);
while ((v1 = NSNextHashEnumeratorItem(&enumerator)) != 0)
{
void *v2;
v2 = NSHashGet(table2, v1);
if (v2 == 0 && v2 != v1)
{
result = NO;
break;
}
}
NSEndHashTableEnumeration(&enumerator);
return result;
}
}
/**
* Copy the supplied hash table.<br />
* Returns a hash table, space for which is allocated in zone, which
* has (newly retained) copies of table's contents. As always,
* if zone is 0, then NSDefaultMallocZone() is used.
*/
NSHashTable *
NSCopyHashTableWithZone(NSHashTable *table, NSZone *zone)
{
GSIMapTable o = (GSIMapTable)table;
GSIMapTable t;
GSIMapNode n;
NSHashEnumerator enumerator;
if (table == nil)
{
NSWarnFLog(@"Null table argument supplied");
return 0;
}
t = (GSIMapTable)[concreteClass allocWithZone: zone];
t->legacy = o->legacy;
if (t->legacy == YES)
{
t->cb.old = o->cb.old;
}
else
{
t->cb.pf = o->cb.pf;
}
GSIMapInitWithZoneAndCapacity(t, zone, ((GSIMapTable)table)->nodeCount);
enumerator = GSIMapEnumeratorForMap((GSIMapTable)table);
while ((n = GSIMapEnumeratorNextNode(&enumerator)) != 0)
{
GSIMapAddKey(t, n->key);
}
GSIMapEndEnumerator((GSIMapEnumerator)&enumerator);
return (NSHashTable*)t;
}
/**
* Returns the number of items in the table.
*/
NSUInteger
NSCountHashTable(NSHashTable *table)
{
return [table count];
}
/**
* Create a new hash table by calling NSCreateHashTableWithZone() using
* NSDefaultMallocZone().<br />
* Returns a (pointer to) an NSHashTable space for which is allocated
* in the default zone. If capacity is small or 0, then the returned
* table has a reasonable capacity.
*/
NSHashTable *
NSCreateHashTable(
NSHashTableCallBacks callBacks,
NSUInteger capacity)
{
return NSCreateHashTableWithZone(callBacks, capacity, NSDefaultMallocZone());
}
/**
* Create a new hash table using the supplied callbacks structures.
* If any functions in the callback structures are null the default
* values are used ... as for non-owned pointers.<br />
* Of course, if you send 0 for zone, then the hash table will be
* created in NSDefaultMallocZone().<br />
* The table will be created with the specified capacity ... ie ready
* to hold at least that many items.
*/
NSHashTable *
NSCreateHashTableWithZone(
NSHashTableCallBacks k,
NSUInteger capacity,
NSZone *zone)
{
GSIMapTable table;
if (concreteClass == Nil)
{
[NSConcreteHashTable class]; // Force +initialize
NSCAssert(concreteClass != Nil, NSInternalInconsistencyException);
}
table = (GSIMapTable)[concreteClass allocWithZone: zone];
if (k.hash == 0)
k.hash = NSNonOwnedPointerHashCallBacks.hash;
if (k.isEqual == 0)
k.isEqual = NSNonOwnedPointerHashCallBacks.isEqual;
if (k.retain == 0)
k.retain = NSNonOwnedPointerHashCallBacks.retain;
if (k.release == 0)
k.release = NSNonOwnedPointerHashCallBacks.release;
if (k.describe == 0)
k.describe = NSNonOwnedPointerHashCallBacks.describe;
table->legacy = YES;
table->cb.old = k;
GSIMapInitWithZoneAndCapacity(table, zone, capacity);
return (NSHashTable*)table;
}
/**
* Function to be called when finished with the enumerator.
* This permits memory used by the enumerator to be released!
*/
void
NSEndHashTableEnumeration(NSHashEnumerator *enumerator)
{
if (enumerator == 0)
{
NSWarnFLog(@"Null enumerator argument supplied");
return;
}
if (enumerator->map != 0)
{
/* The 'map' field is non-null, so this NSHashEnumerator is actually
* a GSIMapEnumerator.
*/
GSIMapEndEnumerator((GSIMapEnumerator)enumerator);
}
else if (enumerator->node != 0)
{
/* The 'map' field is null but the 'node' field is not, so the
* NSHashEnumerator structure actually contains an NSEnumerator
* in the 'node' field.
*/
[(id)enumerator->node release];
memset(enumerator, '\0', sizeof(NSHashEnumerator));
}
}
/**
* Return an enumerator for stepping through a hash table using the
* NSNextHashEnumeratorPair() function.
*/
NSHashEnumerator
NSEnumerateHashTable(NSHashTable *table)
{
if (table == nil)
{
NSHashEnumerator v = {0, 0, 0};
NSWarnFLog(@"Null table argument supplied");
return v;
}
if (object_getClass(table) == concreteClass)
{
return GSIMapEnumeratorForMap((GSIMapTable)table);
}
else
{
NSHashEnumerator v = {0, 0, 0};
v.node = (void*)[[table objectEnumerator] retain];
return v;
}
}
/**
* Destroy the hash table and release its contents.<br />
* Releases all the keys and values of table (using the key and
* value callbacks specified at the time of table's creation),
* and then proceeds to deallocate the space allocated for table itself.
*/
void
NSFreeHashTable(NSHashTable *table)
{
[table release];
}
/**
* Returns the value for the specified item, or a null pointer if the
* item is not found in the table.
*/
void *
NSHashGet(NSHashTable *table, const void *element)
{
if (table == nil)
{
NSWarnFLog(@"Null table argument supplied");
return 0;
}
if (object_getClass(table) == concreteClass)
{
GSIMapNode n;
n = GSIMapNodeForKey((GSIMapTable)table, (GSIMapKey)element);
if (n == 0)
{
return 0;
}
else
{
return n->key.ptr;
}
}
return [table member: (id)element];
}
/**
* Adds the element to table.<br />
* If an equal element is already in table, replaces it with the new one.<br />
* If element is null raises an NSInvalidArgumentException.
*/
void
NSHashInsert(NSHashTable *table, const void *element)
{
if (table == 0)
{
[NSException raise: NSInvalidArgumentException
format: @"Attempt to place value in null hash table"];
}
if (element == 0)
{
[NSException raise: NSInvalidArgumentException
format: @"Attempt to place null in hash table"];
}
if (object_getClass(table) == concreteClass)
{
GSIMapTable t = (GSIMapTable)table;
GSIMapNode n;
n = GSIMapNodeForKey(t, (GSIMapKey)element);
if (n == 0)
{
GSIMapAddKey(t, (GSIMapKey)element);
((NSConcreteHashTable*)table)->version++;
}
else if (element != n->key.ptr)
{
GSI_MAP_RELEASE_KEY(t, n->key);
n->key = (GSIMapKey)element;
GSI_MAP_RETAIN_KEY(t, n->key);
((NSConcreteHashTable*)table)->version++;
}
}
else
{
[table addObject: (id)element];
}
}
/**
* Adds the element to table and returns nul.<br />
* If an equal element is already in table, returns the old element
* instead of adding the new one.<br />
* If element is nul, raises an NSInvalidArgumentException.
*/
void *
NSHashInsertIfAbsent(NSHashTable *table, const void *element)
{
if (table == 0)
{
[NSException raise: NSInvalidArgumentException
format: @"Attempt to place value in null hash table"];
}
if (element == 0)
{
[NSException raise: NSInvalidArgumentException
format: @"Attempt to place null in hash table"];
}
if (object_getClass(table) == concreteClass)
{
GSIMapTable t = (GSIMapTable)table;
GSIMapNode n;
n = GSIMapNodeForKey(t, (GSIMapKey)element);
if (n == 0)
{
GSIMapAddKey(t, (GSIMapKey)element);
((NSConcreteHashTable*)table)->version++;
return 0;
}
else
{
return n->key.ptr;
}
}
else
{
id old = [table member: (id)element];
if (old == nil)
{
[table addObject: (id)element];
return 0;
}
else
{
return (void*)old;
}
}
}
/**
* Adds the element to table and returns nul.<br />
* If an equal element is already present, raises NSInvalidArgumentException.
* <br />If element is null raises an NSInvalidArgumentException.
*/
void
NSHashInsertKnownAbsent(NSHashTable *table, const void *element)
{
if (table == 0)
{
[NSException raise: NSInvalidArgumentException
format: @"Attempt to place value in null hash table"];
}
if (element == 0)
{
[NSException raise: NSInvalidArgumentException
format: @"Attempt to place null in hash table"];
}
if (object_getClass(table) == concreteClass)
{
GSIMapTable t = (GSIMapTable)table;
GSIMapNode n;
n = GSIMapNodeForKey(t, (GSIMapKey)element);
if (n == 0)
{
GSIMapAddKey(t, (GSIMapKey)element);
((NSConcreteHashTable*)table)->version++;
}
else
{
[NSException raise: NSInvalidArgumentException
format: @"NSHashInsertKnownAbsent ... item not absent"];
}
}
else
{
id old = [table member: (id)element];
if (old == nil)
{
[table addObject: (id)element];
}
else
{
[NSException raise: NSInvalidArgumentException
format: @"NSHashInsertKnownAbsent ... item not absent"];
}
}
}
/**
* Remove the specified element from the table.
*/
void
NSHashRemove(NSHashTable *table, const void *element)
{
if (table == 0)
{
NSWarnFLog(@"Nul table argument supplied");
return;
}
if (object_getClass(table) == concreteClass)
{
GSIMapTable map = (GSIMapTable)table;
GSIMapBucket bucket;
GSIMapNode node;
bucket = GSIMapBucketForKey(map, (GSIMapKey)element);
node = GSIMapNodeForKeyInBucket(map, bucket, (GSIMapKey)element);
if (node != 0)
{
GSIMapRemoveNodeFromMap(map, bucket, node);
GSIMapFreeNode(map, node);
((NSConcreteHashTable*)table)->version++;
}
}
else
{
[table removeObject: (id)element];
}
}
/**
* Step through the hash table ... return the next item or
* return nul if we hit the of the table.
*/
void *
NSNextHashEnumeratorItem(NSHashEnumerator *enumerator)
{
if (enumerator == 0)
{
NSWarnFLog(@"Nul enumerator argument supplied");
return 0;
}
if (enumerator->map != 0) // Got a GSIMapTable enumerator
{
GSIMapNode n;
/* The 'map' field is non-null, so this NSHashEnumerator is actually
* a GSIMapEnumerator.
*/
n = GSIMapEnumeratorNextNode((GSIMapEnumerator)enumerator);
if (n == 0)
{
return 0;
}
else
{
NSConcreteHashTable *map = enumerator->map;
return GSI_MAP_READ_KEY(map, &n->key).ptr;
}
}
else if (enumerator->node != 0) // Got an enumerator object
{
/* The 'map' field is null but the 'node' field is not, so the
* NSHashEnumerator structure actually contains an NSEnumerator
* in the 'node' field, and the map table being enumerated in the
* 'bucket' field.
*/
return (void*)[(id)enumerator->node nextObject];
}
else
{
return 0;
}
}
/**
* Empty the hash table (releasing all elements), but preserve its capacity.
*/
void
NSResetHashTable(NSHashTable *table)
{
if (table == 0)
{
NSWarnFLog(@"Nul table argument supplied");
return;
}
if (object_getClass(table) == concreteClass)
{
NSConcreteHashTable *t = (NSConcreteHashTable*)table;
if (t->nodeCount > 0)
{
GSIMapCleanMap((GSIMapTable)table);
t->version++;
}
}
else
{
[table removeAllObjects];
}
}
/**
* Returns a string describing the table contents.<br />
* For each item, a string of the form "value;\n"
* is appended. The appropriate describe function is used to generate
* the strings for each item.
*/
GS_DECLARE NSString *
NSStringFromHashTable(NSHashTable *table)
{
GSIMapTable t = (GSIMapTable)table;
NSMutableString *string;
NSHashEnumerator enumerator;
const void *element;
if (table == 0)
{
NSWarnFLog(@"Nul table argument supplied");
return nil;
}
/* This will be our string. */
string = [NSMutableString stringWithCapacity: 0];
/* Get an enumerator for TABLE. */
enumerator = NSEnumerateHashTable(table);
/* Iterate over the elements of TABLE, appending the description of
* each to the mutable string STRING. */
if (t->legacy)
{
while ((element = NSNextHashEnumeratorItem(&enumerator)) != nil)
{
[string appendFormat: @"%@;\n",
(t->cb.old.describe)(table, element)];
}
}
else
{
while ((element = NSNextHashEnumeratorItem(&enumerator)) != nil)
{
[string appendFormat: @"%@;\n",
(t->cb.pf.descriptionFunction)(element)];
}
}
NSEndHashTableEnumeration(&enumerator);
return string;
}
/* These are to increase readabilty locally. */
typedef NSUInteger (*NSHT_hash_func_t)(NSHashTable *, const void *);
typedef BOOL (*NSHT_isEqual_func_t)(NSHashTable *, const void *, const void *);
typedef void (*NSHT_retain_func_t)(NSHashTable *, const void *);
typedef void (*NSHT_release_func_t)(NSHashTable *, void *);
typedef NSString *(*NSHT_describe_func_t)(NSHashTable *, const void *);
/** For sets of pointer-sized or smaller quantities. */
const NSHashTableCallBacks NSIntegerHashCallBacks =
{
(NSHT_hash_func_t) _NS_int_hash,
(NSHT_isEqual_func_t) _NS_int_is_equal,
(NSHT_retain_func_t) _NS_int_retain,
(NSHT_release_func_t) _NS_int_release,
(NSHT_describe_func_t) _NS_int_describe
};
/** For backward compatibility. */
const NSHashTableCallBacks NSIntHashCallBacks =
{
(NSHT_hash_func_t) _NS_int_hash,
(NSHT_isEqual_func_t) _NS_int_is_equal,
(NSHT_retain_func_t) _NS_int_retain,
(NSHT_release_func_t) _NS_int_release,
(NSHT_describe_func_t) _NS_int_describe
};
/** For sets of pointers hashed by address. */
const NSHashTableCallBacks NSNonOwnedPointerHashCallBacks =
{
(NSHT_hash_func_t) _NS_non_owned_void_p_hash,
(NSHT_isEqual_func_t) _NS_non_owned_void_p_is_equal,
(NSHT_retain_func_t) _NS_non_owned_void_p_retain,
(NSHT_release_func_t) _NS_non_owned_void_p_release,
(NSHT_describe_func_t) _NS_non_owned_void_p_describe
};
/** For sets of objects without retaining and releasing. */
const NSHashTableCallBacks NSNonRetainedObjectHashCallBacks =
{
(NSHT_hash_func_t) _NS_non_retained_id_hash,
(NSHT_isEqual_func_t) _NS_non_retained_id_is_equal,
(NSHT_retain_func_t) _NS_non_retained_id_retain,
(NSHT_release_func_t) _NS_non_retained_id_release,
(NSHT_describe_func_t) _NS_non_retained_id_describe
};
/** For sets of objects; similar to [NSSet]. */
const NSHashTableCallBacks NSObjectHashCallBacks =
{
(NSHT_hash_func_t) _NS_id_hash,
(NSHT_isEqual_func_t) _NS_id_is_equal,
(NSHT_retain_func_t) _NS_id_retain,
(NSHT_release_func_t) _NS_id_release,
(NSHT_describe_func_t) _NS_id_describe
};
/** For sets of pointers with transfer of ownership upon insertion. */
const NSHashTableCallBacks NSOwnedPointerHashCallBacks =
{
(NSHT_hash_func_t) _NS_owned_void_p_hash,
(NSHT_isEqual_func_t) _NS_owned_void_p_is_equal,
(NSHT_retain_func_t) _NS_owned_void_p_retain,
(NSHT_release_func_t) _NS_owned_void_p_release,
(NSHT_describe_func_t) _NS_owned_void_p_describe
};
/** For sets of pointers to structs when the first field of the
* struct is the size of an int. */
const NSHashTableCallBacks NSPointerToStructHashCallBacks =
{
(NSHT_hash_func_t) _NS_int_p_hash,
(NSHT_isEqual_func_t) _NS_int_p_is_equal,
(NSHT_retain_func_t) _NS_int_p_retain,
(NSHT_release_func_t) _NS_int_p_release,
(NSHT_describe_func_t) _NS_int_p_describe
};
@interface NSConcreteHashTableEnumerator : NSEnumerator
{
NSConcreteHashTable *table;
GSIMapEnumerator_t enumerator;
}
- (id) initWithHashTable: (NSConcreteHashTable*)t;
@end
@implementation NSConcreteHashTable
- (NSUInteger) sizeOfContentExcluding: (NSHashTable*)exclude
{
/* Can't safely calculate for mutable object; just buffer size
*/
return nodeCount * sizeof(GSIMapNode);
}
+ (void) initialize
{
if (concreteClass == Nil)
{
concreteClass = [NSConcreteHashTable class];
instanceSize = class_getInstanceSize(concreteClass);
}
}
- (void) addObject: (id)anObject
{
GSIMapTable t = (GSIMapTable)self;
GSIMapNode n;
if (anObject == nil)
{
[NSException raise: NSInvalidArgumentException
format: @"[%@-%@:] given nil argument",
NSStringFromClass([self class]), NSStringFromSelector(_cmd)];
}
n = GSIMapNodeForKey(t, (GSIMapKey)anObject);
if (n == 0)
{
GSIMapAddKey(t, (GSIMapKey)anObject);
version++;
}
else if (n->key.obj != anObject)
{
GSI_MAP_RELEASE_KEY(t, n->key);
n->key = (GSIMapKey)anObject;
GSI_MAP_RETAIN_KEY(t, n->key);
version++;
}
}
- (NSArray*) allObjects
{
NSHashEnumerator enumerator;
NSUInteger index;
NSArray *a;
GS_BEGINITEMBUF(objects, nodeCount, id);
enumerator = NSEnumerateHashTable(self);
index = 0;
while (index < nodeCount
&& (objects[index] = NSNextHashEnumeratorItem(&enumerator)) != nil)
{
index++;
}
NSEndHashTableEnumeration(&enumerator);
a = [[[NSArray alloc] initWithObjects: objects count: index] autorelease];
GS_ENDITEMBUF();
return a;
}
- (id) anyObject
{
GSIMapNode node = GSIMapFirstNode(self);
if (node == 0)
{
return nil;
}
return node->key.obj;
}
- (BOOL) containsObject: (id)anObject
{
if (anObject != nil)
{
GSIMapNode node = GSIMapNodeForKey(self, (GSIMapKey)anObject);
if (node)
{
return YES;
}
}
return NO;
}
- (id) copyWithZone: (NSZone*)aZone
{
return NSCopyHashTableWithZone(self, aZone);
}
- (NSUInteger) count
{
GSIMapRemoveWeak(self);
return (NSUInteger)nodeCount;
}
- (NSUInteger) countByEnumeratingWithState: (NSFastEnumerationState*)state
objects: (id*)stackbuf
count: (NSUInteger)len
{
state->mutationsPtr = &version;
return GSIMapCountByEnumeratingWithStateObjectsCount
(self, state, stackbuf, len);
}
- (void) dealloc
{
GSIMapEmptyMap(self);
[super dealloc];
}
- (void) encodeWithCoder: (NSCoder*)aCoder
{
[self subclassResponsibility: _cmd];
}
- (void) finalize
{
GSIMapEmptyMap(self);
}
- (NSUInteger) hash
{
return (NSUInteger)nodeCount;
}
- (id) init
{
return [self initWithPointerFunctions: nil capacity: 0];
}
- (id) initWithCoder: (NSCoder*)aCoder
{
[self subclassResponsibility: _cmd];
return nil;
}
- (id) initWithPointerFunctions: (NSPointerFunctions*)functions
capacity: (NSUInteger)initialCapacity
{
legacy = NO;
if (![functions isKindOfClass: [NSConcretePointerFunctions class]])
{
static NSConcretePointerFunctions *defaultFunctions = nil;
if (defaultFunctions == nil)
{
defaultFunctions
= [[NSConcretePointerFunctions alloc] initWithOptions: 0];
}
functions = defaultFunctions;
}
memcpy(&self->cb.pf, &((NSConcretePointerFunctions*)functions)->_x,
sizeof(self->cb.pf));
#if GC_WITH_GC
if (self->cb.pf.usesWeakReadAndWriteBarriers)
{
zone = (NSZone*)nodeW;
}
else
{
zone = (NSZone*)nodeS;
}
#endif
GSIMapInitWithZoneAndCapacity(self, zone, initialCapacity);
return self;
}
- (NSEnumerator*) objectEnumerator
{
NSEnumerator *e;
e = [[NSConcreteHashTableEnumerator alloc] initWithHashTable: self];
return [e autorelease];
}
- (id) member: (id)aKey
{
if (aKey != nil)
{
GSIMapNode node = GSIMapNodeForKey(self, (GSIMapKey)aKey);
if (node)
{
return node->key.obj;
}
}
return nil;
}
- (NSPointerFunctions*) pointerFunctions
{
NSConcretePointerFunctions *p = [NSConcretePointerFunctions new];
p->_x = self->cb.pf;
return [p autorelease];
}
- (void) removeAllObjects
{
if (nodeCount > 0)
{
GSIMapCleanMap(self);
version++;
}
}
- (void) removeObject: (id)anObject
{
if (anObject == nil)
{
NSWarnMLog(@"attempt to remove nil object from hash table %@", self);
return;
}
if (nodeCount > 0)
{
GSIMapTable map = (GSIMapTable)self;
GSIMapBucket bucket;
GSIMapNode node;
bucket = GSIMapBucketForKey(map, (GSIMapKey)anObject);
node = GSIMapNodeForKeyInBucket(map, bucket, (GSIMapKey)anObject);
if (node != 0)
{
GSIMapRemoveNodeFromMap(map, bucket, node);
GSIMapFreeNode(map, node);
version++;
}
}
}
@end
@implementation NSConcreteHashTableEnumerator
- (id) initWithHashTable: (NSConcreteHashTable*)t
{
table = RETAIN(t);
enumerator = GSIMapEnumeratorForMap(table);
return self;
}
- (id) nextObject
{
GSIMapNode node = GSIMapEnumeratorNextNode(&enumerator);
if (node == 0)
{
return nil;
}
return node->key.obj;
}
- (void) dealloc
{
GSIMapEndEnumerator(&enumerator);
RELEASE(table);
[super dealloc];
}
@end
|