00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 @import "CPObject.j"
00025 @import "CPEnumerator.j"
00026 @import "CPException.j"
00027
00028
00029 @implementation _CPDictionaryValueEnumerator : CPEnumerator
00030 {
00031 CPEnumerator _keyEnumerator;
00032 CPDictionary _dictionary;
00033 }
00034
00035 - (id)initWithDictionary:(CPDictionary)aDictionary
00036 {
00037 self = [super init];
00038
00039 if (self)
00040 {
00041 _keyEnumerator = [aDictionary keyEnumerator];
00042 _dictionary = aDictionary;
00043 }
00044
00045 return self;
00046 }
00047
00048 - (id)nextObject
00049 {
00050 var key = [_keyEnumerator nextObject];
00051
00052 if (!key)
00053 return nil;
00054
00055 return [_dictionary objectForKey:key];
00056 }
00057
00058 @end
00059
00073 @implementation CPDictionary : CPObject
00074 {
00075 }
00076
00077
00078
00079
00080 + (id)alloc
00081 {
00082 return new objj_dictionary();
00083 }
00084
00088 + (id)dictionary
00089 {
00090 return [[self alloc] init];
00091 }
00092
00098 + (id)dictionaryWithDictionary:(CPDictionary)aDictionary
00099 {
00100 return [[self alloc] initWithDictionary:aDictionary];
00101 }
00102
00109 + (id)dictionaryWithObject:(id)anObject forKey:(id)aKey
00110 {
00111 return [[self alloc] initWithObjects:[anObject] forKeys:[aKey]];
00112 }
00113
00121 + (id)dictionaryWithObjects:(CPArray)objects forKeys:(CPArray)keys
00122 {
00123 return [[self alloc] initWithObjects:objects forKeys:keys];
00124 }
00125
00131 - (id)initWithDictionary:(CPDictionary)aDictionary
00132 {
00133 var key = "",
00134 dictionary = [[CPDictionary alloc] init];
00135
00136 for (key in aDictionary._buckets)
00137 [dictionary setObject:[aDictionary objectForKey:key] forKey:key];
00138
00139 return dictionary;
00140 }
00141
00149 - (id)initWithObjects:(CPArray)objects forKeys:(CPArray)keyArray
00150 {
00151 self = [super init];
00152
00153 if ([objects count] != [keyArray count])
00154 [CPException raise:CPInvalidArgumentException reason:"Counts are different.("+[objects count]+"!="+[keyArray count]+")"];
00155
00156 if (self)
00157 {
00158 var i = [keyArray count];
00159
00160 while (i--)
00161 [self setObject:objects[i] forKey:keyArray[i]];
00162 }
00163
00164 return self;
00165 }
00166
00170 - (CPDictionary)copy
00171 {
00172 return [CPDictionary dictionaryWithDictionary:self];
00173 }
00174
00178 - (int)count
00179 {
00180 return count;
00181 }
00182
00186 - (CPArray)allKeys
00187 {
00188 return _keys;
00189 }
00190
00194 - (CPArray)allValues
00195 {
00196 var index = _keys.length,
00197 values = [];
00198
00199 while (index--)
00200 values.push(dictionary_getValue(self, [_keys[index]]));
00201
00202 return values;
00203 }
00204
00208 - (CPEnumerator)keyEnumerator
00209 {
00210 return [_keys objectEnumerator];
00211 }
00212
00216 - (CPEnumerator)objectEnumerator
00217 {
00218 return [[_CPDictionaryValueEnumerator alloc] initWithDictionary:self];
00219 }
00220
00224 - (BOOL)isEqualToDictionary:(CPDictionary)aDictionary
00225 {
00226 if (count != [aDictionary count])
00227 return NO;
00228
00229 var index = count;
00230 while (index--)
00231 {
00232 var currentKey = _keys[index],
00233 lhsObject = _buckets[currentKey],
00234 rhsObject = aDictionary._buckets[currentKey];
00235
00236 if (lhsObject === rhsObject)
00237 continue;
00238
00239 if ([lhsObject respondsToSelector:@selector(isEqual:)] && [lhsObject isEqual:rhsObject])
00240 continue;
00241
00242 return NO;
00243 }
00244
00245 return YES;
00246 }
00247
00248
00249
00250
00251
00252
00253
00254
00255
00256
00257
00258
00259
00260
00261
00262
00263
00264
00265
00266
00267
00268
00269
00270
00271
00272
00273
00274
00275
00276
00277
00278
00279
00280
00281
00282
00283
00284
00285
00286
00287
00288
00289
00290
00291
00292
00293
00294
00295
00296
00297
00298
00299
00300
00306 - (id)objectForKey:(CPString)aKey
00307 {
00308
00309 return _buckets[aKey];
00310
00311 }
00312
00313
00314
00315
00316
00317
00318
00319
00320
00321
00322
00323
00324
00325
00326
00327
00328
00329
00330
00331
00332
00333
00334
00335
00336
00337
00338
00339
00340
00341
00342
00343
00347 - (void)removeAllObjects
00348 {
00349 _keys = [];
00350 count = 0;
00351 _buckets = {};
00352 }
00353
00358 - (void)removeObjectForKey:(id)aKey
00359 {
00360 dictionary_removeValue(self, aKey);
00361 }
00362
00363
00364
00365
00366
00367
00368
00369
00370
00371
00372
00373
00374
00375
00376
00377
00378
00379
00380
00381
00382
00383
00384
00385
00386
00387
00388
00394 - (void)setObject:(id)anObject forKey:(id)aKey
00395 {
00396 dictionary_setValue(self, aKey, anObject);
00397 }
00398
00399
00400
00401
00402
00403
00404
00405
00406
00407
00408
00409
00410
00414 - (CPString)description
00415 {
00416 var description = @"CPDictionary {\n";
00417
00418 var i = _keys.length;
00419
00420 while (i--)
00421 description += _keys[i] +":"+[_buckets[_keys[i]] description]+"\n";
00422
00423 description += "}";
00424
00425 return description;
00426 }
00427
00428 @end
00429
00430 @implementation CPDictionary (CPCoding)
00431
00432
00433
00434
00435
00436
00437 - (id)initWithCoder:(CPCoder)aCoder
00438 {
00439 return [aCoder _decodeDictionaryOfObjectsForKey:@"CP.objects"];
00440 }
00441
00446 - (void)encodeWithCoder:(CPCoder)aCoder
00447 {
00448 [aCoder _encodeDictionaryOfObjects:self forKey:@"CP.objects"];
00449 }
00450
00451 @end
00452
00453 objj_dictionary.prototype.isa = CPDictionary;