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
|
/**
EEOMutableKnownKeyDictionary.m <title>EEOMutableKnownKeyDictionary</title>
Copyright (C) 2000-2002,2003,2004,2005 Free Software Foundation, Inc.
Author: Manuel Guesdon <mguesdon@orange-concept.com>
Date: October 2000
$Revision: 24307 $
$Date: 2007-01-03 06:43:12 +0100 (Mit, 03 Jän 2007) $
<abstract></abstract>
This file is part of the GNUstep Database Library.
<license>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library 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
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with this library; see the file COPYING.LIB.
If not, write to the Free Software Foundation,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
</license>
**/
#include "config.h"
RCS_ID("$Id: EOMutableKnownKeyDictionary.m 24307 2007-01-03 05:43:12Z ratmice $")
#ifdef GNUSTEP
#include <Foundation/NSArray.h>
#include <Foundation/NSString.h>
#include <Foundation/NSDictionary.h>
#include <Foundation/NSEnumerator.h>
#include <Foundation/NSException.h>
#include <Foundation/NSZone.h>
#include <Foundation/NSMapTable.h>
#else
#include <Foundation/Foundation.h>
#endif
#ifndef GNUSTEP
#include <GNUstepBase/GNUstep.h>
#include <GNUstepBase/GSObjCRuntime.h>
#include <GNUstepBase/GSCategories.h>
#endif
#include <EOControl/EOMutableKnownKeyDictionary.h>
#include <EOControl/EODebug.h>
#include <EOControl/EONull.h>
#include "EOPrivate.h"
@implementation EOMKKDInitializer
+ (void)initialize
{
static BOOL initialized=NO;
if (!initialized)
{
initialized=YES;
GDL2_PrivateInit();
}
}
+ (EOMKKDInitializer*)initializerFromKeyArray: (NSArray*)keys
{
EOMKKDInitializer *initializer = [[self newWithKeyArray: keys] autorelease];
return initializer;
}
+ (id)newWithKeyArray: (NSArray*)keys
{
return [[self alloc]
initWithKeys: keys];
}
+ (id)newWithKeyArray: (NSArray*)keys
zone: (NSZone*)zone
{
return [[self allocWithZone: zone]
initWithKeys: keys];
}
- (id)initWithKeys: (id*)keys
count: (int)count
{
if ((self = [self init]))
{
int i;
NSAssert(keys, @"No array of keys");
NSAssert(count > 0, @"No keys in array");
_keyToIndex = NSCreateMapTableWithZone(NSObjectMapKeyCallBacks,
NSIntMapValueCallBacks,
count,
[self zone]);
_keys = NSZoneMalloc([self zone], count * sizeof(NSString*));
EOFLOGObjectLevelArgs(@"EOMKKD", @"keys=%p _keys=%p", keys, _keys);
for (i = 0; i < count; i++)
{
id key = keys[i];
void *oldValue;
_count = i + 1;
EOFLOGObjectLevelArgs(@"EOMKKD", @"key=%p", key);
EOFLOGObjectLevelArgs(@"EOMKKD", @"key=%@ RETAINCOUNT=%d",
key, [key retainCount]);
oldValue = NSMapInsertIfAbsent(_keyToIndex,key, (const void*)(i + 1)); //+1 because 0 = no object
_keys[i] = key; //Don't retain: already retained by Map
EOFLOGObjectLevelArgs(@"EOMKKD", @"key=%@ RETAINCOUNT=%d",
key, [key retainCount]);
NSAssert1(!oldValue, @"%@ already present", key);
}
EOFLOGObjectLevelArgs(@"EOMKKD", @"self=%p", self);
}
return self;
}
- (id)initWithKeys: (NSArray*)keys
{
int count = [keys count];
NSAssert(keys, @"No array of keys");
NSAssert([keys count] > 0, @"No keys in array");
{
id keysArray[count];
memset(keysArray, 0, count * sizeof(id));
[keys getObjects: keysArray];
if ((self = [self initWithKeys: keysArray
count: count]))
{
}
}
EOFLOGObjectLevelArgs(@"EOMKKD", @"self=%p", self);
return self;
}
- (void)dealloc
{
EOFLOGObjectLevelArgs(@"EOMKKD", @"Deallocate EOMKKDInitliazer %p", self);
if (_keyToIndex)
NSFreeMapTable(_keyToIndex);
if (_keys)
NSZoneFree([self zone],_keys);
[super dealloc];
//EOFLogC("GSWElementIDString end of dealloc");
}
- (NSString*)description
{
NSString *dscr;
int i;
dscr = [NSString stringWithFormat: @"<%s %p - keys=",
object_get_class_name(self),
(void*)self];
for (i = 0; i < _count; i++)
{
dscr = [dscr stringByAppendingFormat: @"%@ [%d] ",
_keys[i],
i];
}
dscr = [dscr stringByAppendingString: @">"];
return dscr;
}
- (void) setObject: (id)object
forIndex: (unsigned int)index
dictionary: (NSMutableDictionary*)dictionary
{
//OK?
id key;
NSAssert2(index < _count, @"bad index %d (count=%u)", index, _count);
key = _keys[index];
[dictionary setObject: object
forKey: key];
}
- (id) objectForIndex: (unsigned int)index
dictionary: (NSDictionary*)dictionary
{
id key;
NSAssert2(index < _count, @"bad index %d (count=%u)", index, _count);
key = _keys[index];
return [dictionary objectForKey: key];
}
- (unsigned int) indexForKey: (id)key
{
void *index = NSMapGet(_keyToIndex, (const void *)key);
if (!index)
return NSNotFound;
else
return (unsigned int)(index - 1);
}
- (BOOL)hasKey: (id)key
{
return (EOMKKDInitializer_indexForKeyWithImpPtr(self,NULL,key) != NSNotFound);
}
- (EOMKKDArrayMapping*) arrayMappingForKeys: (NSArray*)keys
{
int selfKeyCount = [keys count];
int keyCount = [keys count];
EOMKKDArrayMapping *arrayMapping = nil;
NSAssert(keyCount <= selfKeyCount, @"key count greater than our key count");
arrayMapping = [[EOMKKDArrayMapping newInstanceWithKeyCount: selfKeyCount
destinationDescription: self
zone: [self zone]] autorelease];
if (keyCount>0)
{
int i=0;
GDL2IMP_UINT indexForKeyIMP=NULL;
IMP objectAtIndexIMP=[keys methodForSelector:@selector(objectAtIndex:)];
for (i = 0; i < keyCount; i++)
{
NSString *key = GDL2_ObjectAtIndexWithImp(keys,objectAtIndexIMP,i);
int destinationIndex = EOMKKDInitializer_indexForKeyWithImpPtr(self,&indexForKeyIMP,key);
arrayMapping->_destinationOffsetForArrayIndex[i] = destinationIndex + 1;
}
};
return arrayMapping;
}
- (EOMKKDSubsetMapping*) subsetMappingForSourceDictionaryInitializer: (EOMKKDInitializer*)sourceInitializer
sourceKeys: (NSArray*)sourceKeys
destinationKeys: (NSArray*)destinationKeys
{
unsigned int selfKeyCount = [self count];
unsigned int keyCount = [destinationKeys count];
EOMKKDSubsetMapping *subsetMapping = nil;
NSAssert([sourceKeys count] == keyCount, @"Source and destination keys count are different");
NSAssert(keyCount <= selfKeyCount, @"key count greater than our key count");
subsetMapping = [[EOMKKDSubsetMapping newInstanceWithKeyCount: selfKeyCount
sourceDescription: sourceInitializer
destinationDescription: self
zone: [self zone]] autorelease];
EOFLOGObjectLevelArgs(@"EOMKKD", @"sourceDescription=%@", sourceInitializer);
EOFLOGObjectLevelArgs(@"EOMKKD", @"destinationDescription=%@", self);
EOFLOGObjectLevelArgs(@"EOMKKD", @"sourceKeys=%@", sourceKeys);
EOFLOGObjectLevelArgs(@"EOMKKD", @"destinationKeys=%@", destinationKeys);
if (keyCount>0)
{
int i;
GDL2IMP_UINT selfIndexForKeyIMP=NULL;
GDL2IMP_UINT sourceInitializerIndexForKeyIMP=NULL;
IMP sourceObjectAtIndexIMP=[sourceKeys methodForSelector:@selector(objectAtIndex:)];
IMP destinationObjectAtIndexIMP=[destinationKeys methodForSelector:@selector(objectAtIndex:)];
for (i = 0; i < keyCount; i++)
{
NSString *sourceKey = nil;
NSString *destinationKey = nil;
int destinationIndex = 0;
int sourceIndex = 0;
sourceKey =
GDL2_ObjectAtIndexWithImp(sourceKeys,sourceObjectAtIndexIMP,i);
EOFLOGObjectLevelArgs(@"EOMKKD", @"sourceKey=%@", sourceKey);
destinationKey =
GDL2_ObjectAtIndexWithImp(destinationKeys,destinationObjectAtIndexIMP,i);
EOFLOGObjectLevelArgs(@"EOMKKD", @"destinationKey=%@", destinationKey);
destinationIndex =
EOMKKDInitializer_indexForKeyWithImpPtr(self,
&selfIndexForKeyIMP,
destinationKey);
EOFLOGObjectLevelArgs(@"EOMKKD", @"destinationIndex=%d",
destinationIndex);
sourceIndex =
EOMKKDInitializer_indexForKeyWithImpPtr(sourceInitializer,
&sourceInitializerIndexForKeyIMP,
sourceKey);
EOFLOGObjectLevelArgs(@"EOMKKD", @"sourceIndex=%d", sourceIndex);
NSAssert2(destinationIndex != NSNotFound,
@"Destination Key %@ not found in %@",
destinationKey,
self);
NSAssert2(sourceIndex != NSNotFound,
@"Source Key %@ not found in %@",
sourceKey,
sourceInitializer);
subsetMapping->_sourceOffsetForDestinationOffset[destinationIndex]
= sourceIndex + 1;
}
};
return subsetMapping;
}
- (EOMKKDSubsetMapping*)subsetMappingForSourceDictionaryInitializer: (EOMKKDInitializer*)sourceInitializer
{
unsigned keyCount = [self count];
EOMKKDSubsetMapping *subsetMapping = [[EOMKKDSubsetMapping
newInstanceWithKeyCount: keyCount
sourceDescription: sourceInitializer
destinationDescription: self
zone: [self zone]] autorelease];
if (keyCount>0)
{
int i=0;
GDL2IMP_UINT indexForKeyIMP=NULL;
for (i = 0; i < keyCount; i++)
{
NSString *key;
int index;
key = _keys[i];
EOFLOGObjectLevelArgs(@"EOMKKD", @"key=%@", key);
index = EOMKKDInitializer_indexForKeyWithImpPtr(sourceInitializer,
&indexForKeyIMP,key);
EOFLOGObjectLevelArgs(@"EOMKKD", @"index=%d", index);
subsetMapping->_sourceOffsetForDestinationOffset[i]
= (index == NSNotFound ? 0 : index + 1);
}
};
return subsetMapping;
}
- (id*) keys
{
return _keys;
}
- (unsigned int) count
{
return _count;
}
@end
@implementation EOMKKDKeyEnumerator : NSEnumerator
- (id) initWithTarget: (EOMutableKnownKeyDictionary*)target
{
if ((self = [super init]))
{
EOMKKDInitializer *initializer;
NSAssert(target,@"No target");
ASSIGN(_target,target);
ASSIGN(_extraEnumerator, [[_target extraData] keyEnumerator]);
initializer = [_target eoMKKDInitializer];
_end = [initializer count];
_keys = [initializer keys];
}
return self;
}
- (void) dealloc
{
// EOFLOGObjectLevelArgs(@"EOMKKD",@"Deallocate EOMKKDEnumerator %p (target=%p)",self,_target);
DESTROY(_target);
DESTROY(_extraEnumerator);
[super dealloc];
}
- (NSString*)description
{
NSString *dscr;
dscr = [NSString stringWithFormat: @"<%s %p - target=%p>",
object_get_class_name(self),
(void*)self,
_target];
return dscr;
}
- (id) nextObject
{
id object = nil;
if (_position < _end)
{
object = _keys[_position];
_position++;
}
else if (_extraEnumerator)
{
object = [_extraEnumerator nextObject];
if (object)
_position++;
}
return object;
}
@end
@implementation EOMKKDSubsetMapping
+ (id)newInstanceWithKeyCount: (unsigned int)keyCount
sourceDescription: (EOMKKDInitializer*)source
destinationDescription: (EOMKKDInitializer*)destination
zone: (NSZone*)zone
{
unsigned extraBytes = (keyCount > 0 ? (keyCount - 1) : 0) * sizeof(int);
EOMKKDSubsetMapping *subsetMapping;
subsetMapping = (EOMKKDSubsetMapping*)NSAllocateObject([EOMKKDSubsetMapping class],
extraBytes,
zone);
[subsetMapping init];
ASSIGN(subsetMapping-> _sourceDescription,source);
ASSIGN(subsetMapping-> _destinationDescription,destination);
memset(subsetMapping-> _sourceOffsetForDestinationOffset, 0,
extraBytes + sizeof(int));
return subsetMapping;
}
- (void) dealloc
{
DESTROY(_sourceDescription);
DESTROY(_destinationDescription);
[super dealloc];
}
- (NSString*)description
{
NSString *dscr;
NSMutableString *offsets = [NSMutableString string];
int i;
int count = [_destinationDescription count];
dscr = [NSString stringWithFormat: @"<%s %p - ",
object_get_class_name(self),
(void*)self];
dscr = [dscr stringByAppendingFormat: @"\nsourceDescription=%@",
[_sourceDescription description]];
dscr = [dscr stringByAppendingFormat: @"\ndestinationDescription=%@",
[_destinationDescription description]];
for (i = 0; i < count; i++)
[offsets appendFormat: @" %d", _sourceOffsetForDestinationOffset[i]];
dscr = [dscr stringByAppendingFormat:
@"\nsourceOffsetForDestinationOffset:%@>", offsets];
return dscr;
}
@end
@implementation EOMKKDArrayMapping
+ (id)newInstanceWithKeyCount: (unsigned int)keyCount
destinationDescription: (EOMKKDInitializer*)destination
zone: (NSZone*)zone
{
unsigned extraBytes = (keyCount > 0 ? (keyCount - 1) : 0) * sizeof(int);
EOMKKDArrayMapping *arrayMapping;
arrayMapping = (EOMKKDArrayMapping*)NSAllocateObject([EOMKKDArrayMapping class],
extraBytes,
zone);
[arrayMapping init];
ASSIGN(arrayMapping->_destinationDescription, destination);
memset(arrayMapping->_destinationOffsetForArrayIndex, 0,
extraBytes + sizeof(int));
return arrayMapping;
}
- (void) dealloc
{
DESTROY(_destinationDescription);
[super dealloc];
}
- (NSString*)description
{
NSString *dscr;
dscr = [NSString stringWithFormat: @"<%s %p >",
object_get_class_name(self),
(void*)self];
return dscr;
}
@end
@implementation EOMutableKnownKeyDictionary
+ (void)initialize
{
static BOOL initialized=NO;
if (!initialized)
{
initialized=YES;
GDL2_PrivateInit();
}
}
+ (id)dictionaryFromDictionary: (NSDictionary *)dict
subsetMapping: (EOMKKDSubsetMapping *)subsetMapping
{
return [[self newDictionaryFromDictionary: dict
subsetMapping: subsetMapping
zone: NULL] autorelease];
}
+ (id)newDictionaryFromDictionary: (NSDictionary*)dict
subsetMapping: (EOMKKDSubsetMapping*)subsetMapping
zone: (NSZone*)zone
{
EOMutableKnownKeyDictionary *newDict = nil;
int objectsCount;
NSAssert(dict, @"No dictionary");
NSAssert(subsetMapping, @"No subsetMapping");
EOFLOGObjectLevelArgs(@"EOMKKD", @"dict=%@", dict);
EOFLOGObjectLevelArgs(@"EOMKKD", @"subsetMapping->_sourceDescription=%@",
subsetMapping->_sourceDescription);
EOFLOGObjectLevelArgs(@"EOMKKD", @"subsetMapping->_destinationDescription=%@",
subsetMapping->_destinationDescription);
objectsCount = [subsetMapping->_destinationDescription count];
EOFLOGObjectLevelArgs(@"EOMKKD", @"objectsCount=%d", objectsCount);
if (objectsCount > 0)
{
id objects[objectsCount];
int i;
for (i = 0; i < objectsCount; i++)
{
objects[i] = nil;
if (subsetMapping->_sourceOffsetForDestinationOffset[i] > 0)
{
int index = subsetMapping->_sourceOffsetForDestinationOffset[i] - 1;
EOFLOGObjectLevelArgs(@"EOMKKD", @"index=%d", index);
objects[i] = [subsetMapping->_sourceDescription
objectForIndex: index
dictionary: dict];
EOFLOGObjectLevelArgs(@"EOMKKD", @"objects[i]=%@", objects[i]);
NSAssert2(objects[i], @"No object for index %d from row %@",
index,
dict);
}
}
newDict = [self newWithInitializer: subsetMapping->_destinationDescription
objects: objects
zone: zone];
}
else
newDict = [self newWithInitializer: subsetMapping->_destinationDescription
zone: zone];
EOFLOGObjectLevel(@"EOMKKD", @"END");
return newDict;
}
+ (id)newDictionaryWithObjects: (id*)objects
arrayMapping: (id)mapping
zone: (NSZone*)zone
{
return [self notImplemented: _cmd];
}
+ (id)newWithInitializer: (EOMKKDInitializer*)initializer
objects: (id*)objects
zone: (NSZone*)zone
{
return [[self allocWithZone: zone] initWithInitializer: initializer
objects: objects];
}
+ (id)dictionaryWithObjects: (NSArray*)objects
forKeys: (NSArray*)keys
{
EOMutableKnownKeyDictionary *dict = nil;
int objectsCount = [objects count];
int keysCount = [keys count];
NSAssert2(objectsCount == keysCount,
@"Objects Count (%d) is not equal to keys Count (%d)",
objectsCount,
keysCount);
if (objectsCount > 0)
{
id objectIds[objectsCount];
id keyIds[keysCount];
[objects getObjects: objectIds];
[keys getObjects: keyIds];
dict = [[[self alloc] initWithObjects: objectIds
forKeys: keyIds
count: objectsCount] autorelease];
}
return dict;
}
+ (EOMKKDInitializer*)initializerFromKeyArray: (NSArray*)keys
{
return [EOMKKDInitializer initializerFromKeyArray: keys];
}
+ (id)newWithInitializer: (EOMKKDInitializer*)initializer
{
return [self newWithInitializer: initializer
zone: NULL];
}
+ (id)newWithInitializer: (EOMKKDInitializer*)initializer
zone: (NSZone*)zone
{
return [[self allocWithZone: zone] initWithInitializer: initializer];
}
+ (id) dictionaryWithInitializer: (EOMKKDInitializer*)initializer
{
return [[self newWithInitializer: initializer]autorelease];
}
- (id) initWithInitializer: (EOMKKDInitializer*)initializer
{
EOFLOGObjectFnStart();
if ((self = [self init]))
{
int count;
NSAssert(initializer, @"No Initializer");
EOFLOGObjectLevel(@"EOMKKD", @"suite");
ASSIGN(_MKKDInitializer, initializer);
count = [_MKKDInitializer count];
EOFLOGObjectLevelArgs(@"EOMKKD", @"count=%d", count);
_values = NSZoneMalloc([self zone], count * sizeof(id));
memset(_values, 0, count * sizeof(id));
}
EOFLOGObjectFnStop();
return self;
}
- (id) initWithInitializer: (EOMKKDInitializer*)initializer
objects: (id*)objects
{
EOFLOGObjectFnStart();
if ((self = [self initWithInitializer: initializer]))
{
EOFLOGObjectLevelArgs(@"EOMKKD", @"suite objects=%p initializer=%p",
objects, _MKKDInitializer);
if (objects)
{
int i;
int count = [_MKKDInitializer count];
EOFLOGObjectLevelArgs(@"EOMKKD", @"count=%d", count);
for (i = 0; i < count; i++)
{
EOFLOGObjectLevelArgs(@"EOMKKD", @"%d=%p (old=%p)",
i, objects[i], _values[i]);
ASSIGN(_values[i], objects[i]);
}
}
}
EOFLOGObjectLevel(@"EOMKKD", @"END");
return self;
}
// This is the designated initializer
- (id) initWithObjects: (id*)objects
forKeys: (id*)keys
count: (unsigned int)count
{
//OK
EOMKKDInitializer *initializer = nil;
EOFLOGObjectFnStart();
if (count > 0)
{
NSAssert(keys, @"No keys array");
NSAssert(count > 0, @"No keys");
initializer = [[[EOMKKDInitializer alloc] initWithKeys: keys
count: count] autorelease];
NSAssert(initializer, @"No Initializer");
EOFLOGObjectLevel(@"EOMKKD", @"suite");
ASSIGN(_MKKDInitializer, initializer);
_values = NSZoneMalloc([self zone], count * sizeof(id));
if (objects)
{
int i;
for (i = 0; i < count; i++)
{
ASSIGN(_values[i], objects[i]);
}
}
else
{
memset(_values, 0, count * sizeof(id));
}
}
return self;
}
- (void) dealloc
{
EOFLOGObjectLevelArgs(@"EOMKKD", @"Deallocate EOMKKDDictionary %p", self);
if (_values)
{
int i;
unsigned int count = [_MKKDInitializer count];
for (i = 0; i < count; i++)
{
DESTROY(_values[i]);
}
NSZoneFree([self zone], _values);
}
DESTROY(_MKKDInitializer);
DESTROY(_extraData);
[super dealloc];
}
- (unsigned int) count
{
NSAssert(_MKKDInitializer, @"No _MKKDInitializer");
return [_MKKDInitializer count];
}
- (id) objectForKey: (id)key
{
id object = nil;
unsigned int index;
// EOFLOGObjectFnStart();
NSAssert(_MKKDInitializer, @"No _MKKDInitializer");
index = EOMKKDInitializer_indexForKeyWithImpPtr(_MKKDInitializer,NULL,key);
// EOFLOGObjectLevelArgs(@"EOMKKD", @"index=%d", index);
if (index == NSNotFound)
{
if (_extraData)
object = [_extraData objectForKey: key];
}
else
{
NSAssert2(index < [_MKKDInitializer count], @"bad index %d (count=%u)",
index, [_MKKDInitializer count]);
object = _values[index];
}
// EOFLOGObjectLevelArgs(@"EOMKKD",@"object=%p",object);
// EOFLOGObjectFnStop();
return object;
}
- (void) setObject: (id)object
forKey: (id)key
{
unsigned int index;
NSAssert(_MKKDInitializer, @"No _MKKDInitializer");
index = EOMKKDInitializer_indexForKeyWithImpPtr(_MKKDInitializer,NULL,key);
if (index == NSNotFound)
{
if (!_extraData)
_extraData = [NSMutableDictionary new];
[_extraData setObject: object
forKey: key];
}
else
{
NSAssert2(index < [_MKKDInitializer count], @"bad index %d (count=%u)",
index, [_MKKDInitializer count]);
ASSIGN(_values[index], object);
}
}
- (void) removeObjectForKey: (id)key
{
unsigned int index;
NSAssert(_MKKDInitializer, @"No _MKKDInitializer");
index = EOMKKDInitializer_indexForKeyWithImpPtr(_MKKDInitializer,NULL,key);
if (index == NSNotFound)
{
if (_extraData)
[_extraData removeObjectForKey: key];
}
else
{
NSAssert2(index < [_MKKDInitializer count], @"bad index %d (count=%u)",
index, [_MKKDInitializer count]);
DESTROY(_values[index]);
}
}
- (BOOL) containsObjectsNotIdenticalTo: (id)object
{
BOOL result = NO;
int i;
unsigned int count = [_MKKDInitializer count];
for (i = 0; !result && i < count; i++)
{
if (_values[i] != object)
{
if (_isNilOrEONull(_values[i]))
result =! _isNilOrEONull(object);
else if (_isNilOrEONull(object))
result = YES;
else
result = ![_values[i] isEqual: object];
}
}
EOFLOGObjectLevelArgs(@"EOMKKD", @"result = %s", (result ? "YES" : "NO"));
return result;
}
- (void)addEntriesFromDictionary: (NSDictionary*)dictionary
{
NSEnumerator *e = [dictionary keyEnumerator];
id key=nil;
IMP indexForKeyIMP=NULL;
while ((key = [e nextObject]))
{
if (!EOMKKD_objectForKeyWithImpPtr(self,&indexForKeyIMP,key)) //Don't overwrite already present values ?
{
[self setObject: [dictionary objectForKey: key]
forKey: key];
}
}
}
- (NSEnumerator*) keyEnumerator
{
EOMKKDKeyEnumerator *MKKDEnum;
MKKDEnum = [[[EOMKKDKeyEnumerator alloc] initWithTarget: self] autorelease];
return MKKDEnum;
}
- (EOMKKDInitializer*) eoMKKDInitializer
{
return _MKKDInitializer;
}
- (NSMutableDictionary*)extraData
{
return _extraData;
}
- (NSString*)debugDescription
{
NSString *dscr;
int i;
int count;
id *keys;
dscr = [NSString stringWithFormat: @"<%s %p - KV=",
object_get_class_name(self),
(void*)self];
count = [_MKKDInitializer count];
keys = [_MKKDInitializer keys];
for (i = 0; i < count; i++)
dscr = [dscr stringByAppendingFormat: @"%@=%@\n", keys[i], _values[i]];
dscr = [dscr stringByAppendingFormat: @"extraDatas:%@", _extraData];
dscr = [dscr stringByAppendingString: @">"];
return dscr;
}
- (BOOL)hasKey: (id)key
{
if ([_MKKDInitializer hasKey: key])
return YES;
else if ([_extraData objectForKey: key])
return YES;
else
return NO;
}
@end
|