From ab6609282d201ce8823ee6965f5d7130c8dabc57 Mon Sep 17 00:00:00 2001 From: Francisco Ryan Tolmasky I Date: Mon, 27 Dec 2010 03:02:00 -0800 Subject: [PATCH] Some reorganization. Reviewed by me. --- Foundation/CPArray/CPArray.j | 2 - Foundation/CPSet/CPMutableSet.j | 134 ++++++++++++ Foundation/{ => CPSet}/CPSet.j | 253 +---------------------- Foundation/CPSet/_CPConcreteMutableSet.j | 124 +++++++++++ Foundation/Foundation.j | 2 + 5 files changed, 261 insertions(+), 254 deletions(-) create mode 100644 Foundation/CPSet/CPMutableSet.j rename Foundation/{ => CPSet}/CPSet.j (69%) create mode 100644 Foundation/CPSet/_CPConcreteMutableSet.j diff --git a/Foundation/CPArray/CPArray.j b/Foundation/CPArray/CPArray.j index dc467c481..8daaf4e2a 100755 --- a/Foundation/CPArray/CPArray.j +++ b/Foundation/CPArray/CPArray.j @@ -851,6 +851,4 @@ var _CPSharedPlaceholderArray = nil; @end -@import "CPArray.j" -@import "CPMutableArray.j" @import "_CPJavaScriptArray.j" diff --git a/Foundation/CPSet/CPMutableSet.j b/Foundation/CPSet/CPMutableSet.j new file mode 100644 index 000000000..fe219cad2 --- /dev/null +++ b/Foundation/CPSet/CPMutableSet.j @@ -0,0 +1,134 @@ +/*! + @class CPMutableSet + @ingroup compatability + + This class is just an empty subclass of CPSet. + CPSet already implements mutable methods and + this class only exists for source compatability. +*/ + +@import "CPSet.j" + + +@implementation CPMutableSet : CPSet + +/* + Returns an initialized set with a given initial capacity. + @param aCapacity, only present for compatability +*/ +- (id)initWithCapacity:(unsigned)aCapacity +{ + return [self init]; +} + +/* + Creates and returns a set with a given initial capacity. + @param aCapacity, only present for compatability +*/ ++ (id)setWithCapacity:(CPUInteger)aCapacity +{ + return [[self alloc] initWithCapacity:aCapacity]; +} + +- (void)filterUsingPredicate:(CPPredicate)aPredicate +{ + var object, + objectEnumerator = [self objectEnumerator]; + + while ((object = [objectEnumerator nextObject]) !== nil) + if (![aPredicate evaluateWithObject:object]) + [self removeObject:object]; +} + +- (void)removeObject:(id)anObject +{ + _CPRaiseInvalidAbstractInvocation(self, _cmd); +} + +- (void)removeObjectsInArray:(CPArray)anArray +{ + var index = 0, + count = [anArray count]; + + for (; index < count; ++index) + [self removeObject:[anArray objectAtIndex:index]]; +} + +- (void)removeAllObjects +{ + var object, + objectEnumerator = [self objectEnumerator]; + + while ((object = [objectEnumerator nextObject]) !== nil) + [self removeObject:object]; +} + +/* + Adds to the receiver each object contained in a given array that is not already a member. + @param array An array of objects to add to the receiver. +*/ +- (void)addObjectsFromArray:(CPArray)objects +{ + var count = [objects count]; + + while (count--) + [self addObject:objects[count]]; +} + +/* + Adds to the receiver each object contained in another given set + @param set The set of objects to add to the receiver. +*/ +- (void)unionSet:(CPSet)aSet +{ + var object, + objectEnumerator = [aSet objectEnumerator]; + + while ((object = [objectEnumerator nextObject]) !== nil) + [self addObject:object]; +} + +/* + Removes from the receiver each object contained in another given set that is present in the receiver. + @param set The set of objects to remove from the receiver. +*/ +- (void)minusSet:(CPSet)aSet +{ + var object, + objectEnumerator = [aSet objectEnumerator]; + + while ((object = [objectEnumerator nextObject]) !== nil) + [self removeObject:object]; +} + +/* + Removes from the receiver each object that isnŐt a member of another given set. + @param set The set with which to perform the intersection. +*/ +- (void)intersectSet:(CPSet)aSet +{ + var object, + objectEnumerator = [self objectEnumerator], + objectsToRemove = []; + + while ((object = [objectEnumerator nextObject]) !== nil) + if (![aSet containsObject:object]) + objectsToRemove.push(object); + + var count = [objectsToRemove count]; + + while (count--) + [self removeObject:objectsToRemove[count]]; +} + +/* + Empties the receiver, then adds to the receiver each object contained in another given set. + @param set The set whose members replace the receiver's content. +*/ +- (void)setSet:(CPSet)aSet +{ + [self removeAllObjects]; + [self unionSet:aSet]; +} + +@end diff --git a/Foundation/CPSet.j b/Foundation/CPSet/CPSet.j similarity index 69% rename from Foundation/CPSet.j rename to Foundation/CPSet/CPSet.j index 5af7ac9de..1d185c614 100644 --- a/Foundation/CPSet.j +++ b/Foundation/CPSet/CPSet.j @@ -459,255 +459,4 @@ var _CPSharedPlaceholderSet = nil; @end -/*! - @class CPMutableSet - @ingroup compatability - - This class is just an empty subclass of CPSet. - CPSet already implements mutable methods and - this class only exists for source compatability. -*/ - -@implementation CPMutableSet : CPSet - -/* - Returns an initialized set with a given initial capacity. - @param aCapacity, only present for compatability -*/ -- (id)initWithCapacity:(unsigned)aCapacity -{ - return [self init]; -} - -/* - Creates and returns a set with a given initial capacity. - @param aCapacity, only present for compatability -*/ -+ (id)setWithCapacity:(CPUInteger)aCapacity -{ - return [[self alloc] initWithCapacity:aCapacity]; -} - -- (void)filterUsingPredicate:(CPPredicate)aPredicate -{ - var object, - objectEnumerator = [self objectEnumerator]; - - while ((object = [objectEnumerator nextObject]) !== nil) - if (![aPredicate evaluateWithObject:object]) - [self removeObject:object]; -} - -- (void)removeObject:(id)anObject -{ - _CPRaiseInvalidAbstractInvocation(self, _cmd); -} - -- (void)removeObjectsInArray:(CPArray)anArray -{ - var index = 0, - count = [anArray count]; - - for (; index < count; ++index) - [self removeObject:[anArray objectAtIndex:index]]; -} - -- (void)removeAllObjects -{ - var object, - objectEnumerator = [self objectEnumerator]; - - while ((object = [objectEnumerator nextObject]) !== nil) - [self removeObject:object]; -} - -/* - Adds to the receiver each object contained in a given array that is not already a member. - @param array An array of objects to add to the receiver. -*/ -- (void)addObjectsFromArray:(CPArray)objects -{ - var count = [objects count]; - - while (count--) - [self addObject:objects[count]]; -} - -/* - Adds to the receiver each object contained in another given set - @param set The set of objects to add to the receiver. -*/ -- (void)unionSet:(CPSet)aSet -{ - var object, - objectEnumerator = [aSet objectEnumerator]; - - while ((object = [objectEnumerator nextObject]) !== nil) - [self addObject:object]; -} - -/* - Removes from the receiver each object contained in another given set that is present in the receiver. - @param set The set of objects to remove from the receiver. -*/ -- (void)minusSet:(CPSet)aSet -{ - var object, - objectEnumerator = [aSet objectEnumerator]; - - while ((object = [objectEnumerator nextObject]) !== nil) - [self removeObject:object]; -} - -/* - Removes from the receiver each object that isn’t a member of another given set. - @param set The set with which to perform the intersection. -*/ -- (void)intersectSet:(CPSet)aSet -{ - var object, - objectEnumerator = [self objectEnumerator], - objectsToRemove = []; - - while ((object = [objectEnumerator nextObject]) !== nil) - if (![aSet containsObject:object]) - objectsToRemove.push(object); - - var count = [objectsToRemove count]; - - while (count--) - [self removeObject:objectsToRemove[count]]; -} - -/* - Empties the receiver, then adds to the receiver each object contained in another given set. - @param set The set whose members replace the receiver's content. -*/ -- (void)setSet:(CPSet)aSet -{ - [self removeAllObjects]; - [self unionSet:aSet]; -} - -@end - -var hasOwnProperty = Object.prototype.hasOwnProperty; - -/*! - @class CPSet - @ingroup foundation - @brief An unordered collection of objects. -*/ -@implementation _CPConcreteMutableSet : CPMutableSet -{ - Object _contents; - unsigned _count; -} - -/* - Initializes a newly allocated set with members taken from the specified list of objects. - @param objects A array of objects to add to the new set. If the same object appears more than once objects, it is added only once to the returned set. - @param count The number of objects from objects to add to the new set. -*/ -- (id)initWithObjects:(CPArray)objects count:(CPUInteger)aCount -{ - self = [super initWithObjects:objects count:aCount]; - - if (self) - { - _count = 0; - _contents = { }; - - var index = 0, - count = MIN([objects count], aCount); - - for (; index < count; ++index) - [self addObject:objects[index]]; - } - - return self; -} - -- (CPUInteger)count -{ - return _count; -} - -- (id)member:(id)anObject -{ - var UID = [anObject UID]; - - if (!hasOwnProperty.call(_contents, UID)) - return nil; - - var object = _contents[UID]; - - if (object === anObject || [object isEqual:anObject]) - return object; - - return nil; -} - -- (CPArray)allObjects -{ - var array = [], - property; - - for (property in _contents) - { - if (hasOwnProperty.call(_contents, property)) - array.push(_contents[property]); - } - - return array; -} - -- (CPEnumerator)objectEnumerator -{ - return [[self allObjects] objectEnumerator]; -} - -/* - Adds a given object to the receiver. - @param anObject The object to add to the receiver. -*/ -- (void)addObject:(id)anObject -{ - if (anObject === nil || anObject === undefined) - return; - - if ([self containsObject:anObject]) - return; - - _contents[[anObject UID]] = anObject; - _count++; -} - -/* - Removes a given object from the receiver. - @param anObject The object to remove from the receiver. -*/ -- (void)removeObject:(id)anObject -{ - if (![self containsObject:anObject]) - return; - - delete _contents[[anObject UID]]; - _count--; -} - -/* - Performance improvement. -*/ -- (void)removeAllObjects -{ - _contents = {}; - _count = 0; -} - -- (Class)classForCoder -{ - return [CPSet class]; -} - -@end +@import "_CPConcreteMutableSet.j" diff --git a/Foundation/CPSet/_CPConcreteMutableSet.j b/Foundation/CPSet/_CPConcreteMutableSet.j new file mode 100644 index 000000000..c3fba816c --- /dev/null +++ b/Foundation/CPSet/_CPConcreteMutableSet.j @@ -0,0 +1,124 @@ + +@import "CPMutableSet.j" + + +var hasOwnProperty = Object.prototype.hasOwnProperty; + +/*! + @class CPSet + @ingroup foundation + @brief An unordered collection of objects. +*/ +@implementation _CPConcreteMutableSet : CPMutableSet +{ + Object _contents; + unsigned _count; +} + +/* + Initializes a newly allocated set with members taken from the specified list of objects. + @param objects A array of objects to add to the new set. If the same object appears more than once objects, it is added only once to the returned set. + @param count The number of objects from objects to add to the new set. +*/ +- (id)initWithObjects:(CPArray)objects count:(CPUInteger)aCount +{ + self = [super initWithObjects:objects count:aCount]; + + if (self) + { + _count = 0; + _contents = { }; + + var index = 0, + count = MIN([objects count], aCount); + + for (; index < count; ++index) + [self addObject:objects[index]]; + } + + return self; +} + +- (CPUInteger)count +{ + return _count; +} + +- (id)member:(id)anObject +{ + var UID = [anObject UID]; + + if (!hasOwnProperty.call(_contents, UID)) + return nil; + + var object = _contents[UID]; + + if (object === anObject || [object isEqual:anObject]) + return object; + + return nil; +} + +- (CPArray)allObjects +{ + var array = [], + property; + + for (property in _contents) + { + if (hasOwnProperty.call(_contents, property)) + array.push(_contents[property]); + } + + return array; +} + +- (CPEnumerator)objectEnumerator +{ + return [[self allObjects] objectEnumerator]; +} + +/* + Adds a given object to the receiver. + @param anObject The object to add to the receiver. +*/ +- (void)addObject:(id)anObject +{ + if (anObject === nil || anObject === undefined) + return; + + if ([self containsObject:anObject]) + return; + + _contents[[anObject UID]] = anObject; + _count++; +} + +/* + Removes a given object from the receiver. + @param anObject The object to remove from the receiver. +*/ +- (void)removeObject:(id)anObject +{ + if (![self containsObject:anObject]) + return; + + delete _contents[[anObject UID]]; + _count--; +} + +/* + Performance improvement. +*/ +- (void)removeAllObjects +{ + _contents = {}; + _count = 0; +} + +- (Class)classForCoder +{ + return [CPSet class]; +} + +@end diff --git a/Foundation/Foundation.j b/Foundation/Foundation.j index 2dd7339d1..69294e0d3 100755 --- a/Foundation/Foundation.j +++ b/Foundation/Foundation.j @@ -42,6 +42,8 @@ @import "CPKeyedUnarchiver.j" @import "CPKeyValueCoding.j" @import "CPKeyValueObserving.j" +@import "CPMutableArray.j" +@import "CPMutableSet.j" @import "CPNotification.j" @import "CPNotificationCenter.j" @import "CPNull.j"