52 var result =
new CFMutableDictionary();
53 result.isa = [
self class];
62 return [[
self alloc] init];
72 return [[
self alloc] initWithDictionary:aDictionary];
81 + (id)dictionaryWithObject:(
id)anObject forKey:(
id)aKey
93 + (id)dictionaryWithObjects:(CPArray)objects forKeys:(CPArray)keys
103 + (id)dictionaryWithJSObject:(JSObject)object
113 + (id)dictionaryWithJSObject:(JSObject)object recursively:(BOOL)recursively
116 dictionary = [[
self alloc] init];
120 if (!
object.hasOwnProperty(key))
123 var value =
object[key];
133 if (value.constructor === Object)
135 else if ([value isKindOfClass:CPArray])
139 count = value.length;
141 for (; i < count; i++)
143 var thisValue = value[i];
145 if (thisValue === null)
147 newValue.push([
CPNull null]);
151 if (thisValue.constructor === Object)
152 newValue.push([
CPDictionary dictionaryWithJSObject:thisValue recursively:YES]);
154 newValue.push(thisValue);
185 + (id)dictionaryWithObjectsAndKeys:(
id)firstObject, ...
187 arguments[0] = [
self alloc];
188 arguments[1] =
@selector(initWithObjectsAndKeys:);
190 return objj_msgSend.apply(
this, arguments);
203 for (key in aDictionary._buckets)
216 - (id)initWithObjects:(CPArray)objects forKeys:(CPArray)keyArray
220 var i = [keyArray count];
222 if ([objects count] != i)
229 var value = objects[i],
233 [
CPException raise:CPInvalidArgumentException
reason:@"Attempt to insert nil object from objects[" + i + @"]"];
259 - (id)initWithObjectsAndKeys:(
id)firstObject, ...
261 var argCount = arguments.length;
263 if (argCount % 2 !== 0)
264 [
CPException raise:CPInvalidArgumentException
reason:"Key-value count is mismatched. (" + argCount + " arguments passed)"];
271 while (argCount-- > 2)
273 var key = arguments[argCount--],
274 value = arguments[argCount];
277 [
CPException raise:CPInvalidArgumentException
reason:@"Attempt to insert nil object from objects[" + ((argCount / 2) - 1) + @"]"];
280 [
CPException raise:CPInvalidArgumentException
reason:@"Attempt to insert nil key from keys[" + ((argCount / 2) - 1) + @"]"];
310 return [
self._keys
copy];
318 var keys =
self._keys,
323 values.push(
self.valueForKey(keys[index]));
336 - (CPArray)allKeysForObject:(
id)anObject
338 var keys =
self._keys,
345 for (; index < count; ++index)
348 value =
self._buckets[key];
350 if (value.isa && anObject && anObject.isa && [value respondsToSelector:
@selector(
isEqual:)] && [value
isEqual:anObject])
351 matchingKeys.push(key);
352 else if (value === anObject)
353 matchingKeys.push(key);
359 - (CPArray)keysOfEntriesPassingTest:(Function )predicate
364 - (CPArray)keysOfEntriesWithOptions:(CPEnumerationOptions)options passingTest:(Function )predicate
366 var keys =
self._keys;
368 if (options & CPEnumerationReverse)
370 var index = [keys count] - 1,
381 var matchingKeys = [],
385 stopRef = @ref(shouldStop);
390 value =
self._buckets[key];
392 if (predicate(key, value, stopRef))
393 matchingKeys.push(key);
402 - (CPArray)keysSortedByValueUsingComparator:(Function )comparator
404 return [[
self allKeys] sortedArrayUsingFunction:function(a, b)
409 return comparator(a, b);
414 - (CPArray)keysSortedByValueUsingSelector:(
SEL)theSelector
416 return [[
self allKeys] sortedArrayUsingFunction:function(a, b)
421 return [a performSelector:theSelector withObject:b];
439 return [[_CPDictionaryValueEnumerator alloc] initWithDictionary:self];
447 if (
self === aDictionary)
450 var count = [
self count];
452 if (count !== [aDictionary count])
460 var currentKey = keys[index],
461 lhsObject =
self._buckets[currentKey],
462 rhsObject = aDictionary._buckets[currentKey];
464 if (lhsObject === rhsObject)
467 if (lhsObject && lhsObject.isa && rhsObject && rhsObject.isa && [lhsObject respondsToSelector:
@selector(
isEqual:)] && [lhsObject
isEqual:rhsObject])
478 if (
self === anObject)
515 - (id)objectForKey:(
id)aKey
517 var
object =
self._buckets[aKey];
519 return (
object === undefined) ? nil : object;
546 - (void)removeAllObjects
548 self.removeAllValues();
555 - (void)removeObjectForKey:(
id)aKey
557 self.removeValueForKey(aKey);
564 - (void)removeObjectsForKeys:(CPArray)keysForRemoval
566 var index = keysForRemoval.length;
589 - (void)setObject:(
id)anObject forKey:(
id)aKey
594 if (anObject === nil)
597 self.setValueForKey(aKey, anObject);
608 var keys = [aDictionary
allKeys],
609 index = [keys count];
613 var key = keys[index];
631 for (; index < count; ++index)
636 var key = keys[index],
637 value =
self.valueForKey(key);
645 - (BOOL)containsKey:(
id)aKey
648 return ((value !== nil) && (value !== undefined));
651 - (void)enumerateKeysAndObjectsUsingBlock:(Function )aFunction
654 shouldStopRef = @ref(shouldStop),
658 for (var index = 0; index < count; index++)
660 var key = keys[index],
661 value =
self.valueForKey(key);
663 aFunction(key, value, shouldStopRef);
670 - (void)enumerateKeysAndObjectsWithOptions:(CPEnumerationOptions)opts usingBlock:(Function )aFunction
689 return [aCoder _decodeDictionaryOfObjectsForKey:@"CP.objects"];
698 [aCoder _encodeDictionaryOfObjects:self forKey:@"CP.objects"];
705 @implementation _CPDictionaryValueEnumerator :
CPEnumerator 718 _dictionary = aDictionary;
726 var key = [_keyEnumerator nextObject];
731 return [_dictionary objectForKey:key];
Used to implement exception handling (creating & raising).
BOOL isEqualToDictionary:(CPDictionary aDictionary)
An object representation of nil.
function CPDescriptionOfObject(anObject, maximumRecursionDepth)
CPEnumerator keyEnumerator()
void raise:reason:(CPString aName, [reason] CPString aReason)
void removeObjectForKey:(id aKey)
id dictionaryWithJSObject:recursively:(JSObject object, [recursively] BOOL recursively)
A mutable key-value pair collection.
FrameUpdater prototype stop
An immutable string (collection of characters).
Defines an interface for enumerators.
id objectForKey:(id aKey)
CPArray keysOfEntriesWithOptions:passingTest:(CPEnumerationOptions options, [passingTest] Function/*(id key, id obj, @ref BOOL stop) */predicate)
CPEnumerator objectEnumerator()
Defines methods for use when archiving & restoring (enc/decoding).
void enumerateKeysAndObjectsUsingBlock:(Function/*(id aKey, id anObject, @ref BOOL stop) */aFunction)
var CPDictionaryMaxDescriptionRecursion
id initWithObjects:forKeys:(CPArray objects, [forKeys] CPArray keyArray)
void setObject:forKey:(id anObject, [forKey] id aKey)
id dictionaryWithDictionary:(CPDictionary aDictionary)
CompletionHandlerAgent prototype increment
id stringWithFormat:(CPString format, [,] ...)
FrameUpdater prototype description