30 @implementation _CPDictionaryValueEnumerator :
CPEnumerator
43 _dictionary = aDictionary;
51 var key = [_keyEnumerator nextObject];
56 return [_dictionary objectForKey:key];
88 var result =
new CFMutableDictionary();
89 result.isa = [
self class];
98 return [[
self alloc] init];
108 return [[
self alloc] initWithDictionary:aDictionary];
117 + (id)dictionaryWithObject:(
id)anObject forKey:(
id)aKey
129 + (id)dictionaryWithObjects:(CPArray)objects forKeys:(CPArray)keys
139 + (id)dictionaryWithJSObject:(JSObject)object
149 + (id)dictionaryWithJSObject:(JSObject)object recursively:(BOOL)recursively
152 dictionary = [[
self alloc] init];
156 if (!
object.hasOwnProperty(key))
159 var value =
object[key];
169 if (value.constructor === Object)
171 else if ([value isKindOfClass:CPArray])
175 count = value.length;
177 for (; i < count; i++)
179 var thisValue = value[i];
181 if (thisValue === null)
183 newValue.push([
CPNull null]);
187 if (thisValue.constructor === Object)
188 newValue.push([
CPDictionary dictionaryWithJSObject:thisValue recursively:YES]);
190 newValue.push(thisValue);
221 + (id)dictionaryWithObjectsAndKeys:(
id)firstObject, ...
223 arguments[0] = [
self alloc];
224 arguments[1] =
@selector(initWithObjectsAndKeys:);
226 return objj_msgSend.apply(
this, arguments);
239 for (key in aDictionary._buckets)
252 - (id)initWithObjects:(CPArray)objects forKeys:(CPArray)keyArray
256 if ([objects count] != [keyArray count])
257 [
CPException raise:CPInvalidArgumentException
reason:[
CPString stringWithFormat:@"Counts are different.(%d != %d)", [objects count], [keyArray count]]];
261 var i = [keyArray count];
265 var value = objects[i],
271 CPLog.warn([
CPString stringWithFormat:
@"[%s %s] DEPRECATED: Attempt to insert nil object from objects[%d]", [
self className], _cmd, i]);
273 if (typeof(objj_backtrace_print) ===
"function")
274 objj_backtrace_print(CPLog.warn);
283 CPLog.warn([
CPString stringWithFormat:
@"[%s %s] DEPRECATED: Attempt to insert nil key from keys[%d]", [
self className], _cmd, i]);
285 if (typeof(objj_backtrace_print) ===
"function")
286 objj_backtrace_print(CPLog.warn);
313 - (id)initWithObjectsAndKeys:(
id)firstObject, ...
315 var argCount = arguments.length;
317 if (argCount % 2 !== 0)
318 [
CPException raise:CPInvalidArgumentException
reason:"Key-value count is mismatched. (" + argCount + " arguments passed)"];
327 for (; index < argCount; index += 2)
329 var value = arguments[index],
330 key = arguments[index + 1];
335 CPLog.warn([
CPString stringWithFormat:
@"[%s %s] DEPRECATED: Attempt to insert nil object from objects[%d]", [
self className], _cmd, (index / 2) - 1]);
337 if (typeof(objj_backtrace_print) ===
"function")
338 objj_backtrace_print(CPLog.warn);
347 CPLog.warn([
CPString stringWithFormat:
@"[%s %s] DEPRECATED: Attempt to insert nil key from keys[%d]", [
self className], _cmd, (index / 2) - 1]);
349 if (typeof(objj_backtrace_print) ===
"function")
350 objj_backtrace_print(CPLog.warn);
384 return [
self._keys
copy];
392 var keys =
self._keys,
397 values.push(
self.valueForKey(keys[index]));
410 - (CPArray)allKeysForObject:(
id)anObject
412 var keys =
self._keys,
419 for (; index < count; ++index)
422 value =
self._buckets[key];
424 if (value.isa && anObject && anObject.isa && [value respondsToSelector:
@selector(
isEqual:)] && [value
isEqual:anObject])
425 matchingKeys.push(key);
426 else if (value === anObject)
427 matchingKeys.push(key);
433 - (CPArray)keysOfEntriesPassingTest:(Function )predicate
438 - (CPArray)keysOfEntriesWithOptions:(CPEnumerationOptions)options passingTest:(Function )predicate
440 var keys =
self._keys;
442 if (options & CPEnumerationReverse)
444 var index = [keys count] - 1,
455 var matchingKeys = [],
459 stopRef = @ref(shouldStop);
461 for (; index !== stop; index += increment)
464 value =
self._buckets[key];
466 if (predicate(key, value, stopRef))
467 matchingKeys.push(key);
476 - (CPArray)keysSortedByValueUsingComparator:(Function )comparator
478 return [[
self allKeys] sortedArrayUsingFunction:function(a, b)
483 return comparator(a, b);
488 - (CPArray)keysSortedByValueUsingSelector:(
SEL)theSelector
490 return [[
self allKeys] sortedArrayUsingFunction:function(a, b)
495 return [a performSelector:theSelector withObject:b];
513 return [[_CPDictionaryValueEnumerator alloc] initWithDictionary:self];
521 if (
self === aDictionary)
524 var count = [
self count];
526 if (count !== [aDictionary count])
534 var currentKey = keys[index],
535 lhsObject =
self._buckets[currentKey],
536 rhsObject = aDictionary._buckets[currentKey];
538 if (lhsObject === rhsObject)
541 if (lhsObject && lhsObject.isa && rhsObject && rhsObject.isa && [lhsObject respondsToSelector:
@selector(
isEqual:)] && [lhsObject
isEqual:rhsObject])
552 if (
self === anObject)
589 - (id)objectForKey:(
id)aKey
591 var
object =
self._buckets[aKey];
593 return (
object === undefined) ? nil : object;
620 - (void)removeAllObjects
622 self.removeAllValues();
629 - (void)removeObjectForKey:(
id)aKey
631 self.removeValueForKey(aKey);
638 - (void)removeObjectsForKeys:(CPArray)keysForRemoval
640 var index = keysForRemoval.length;
663 - (void)setObject:(
id)anObject forKey:(
id)aKey
670 CPLog.warn([
CPString stringWithFormat:
@"[%s %s] DEPRECATED: key cannot be nil", [
self className], _cmd]);
672 if (typeof(objj_backtrace_print) ===
"function")
673 objj_backtrace_print(CPLog.warn);
679 if (anObject === nil)
681 CPLog.warn([
CPString stringWithFormat:
@"[%s %s] DEPRECATED: object cannot be nil (key: %s)", [
self className], _cmd, aKey]);
683 if (typeof(objj_backtrace_print) ===
"function")
684 objj_backtrace_print(CPLog.warn);
694 self.setValueForKey(aKey, anObject);
705 var keys = [aDictionary
allKeys],
706 index = [keys count];
710 var key = keys[index];
728 for (; index < count; ++index)
733 var key = keys[index],
734 value =
self.valueForKey(key);
742 - (BOOL)containsKey:(
id)aKey
745 return ((value !== nil) && (value !== undefined));
748 - (void)enumerateKeysAndObjectsUsingBlock:(Function )aFunction
751 shouldStopRef = @ref(shouldStop),
755 for (var index = 0; index < count; index++)
757 var key = keys[index],
758 value =
self.valueForKey(key);
760 aFunction(key, value, shouldStopRef);
767 - (void)enumerateKeysAndObjectsWithOptions:(CPEnumerationOptions)opts usingBlock:(Function )aFunction
786 return [aCoder _decodeDictionaryOfObjectsForKey:@"CP.objects"];
795 [aCoder _encodeDictionaryOfObjects:self forKey:@"CP.objects"];