You can also choose not to add duplicate entries. The addNTest: method adds if the object was absent and returns a value that can be used to test whether the object was found or not. The filter: method frees a new entry when it's a duplicate. The replace: method always replaces duplicates (returning the object that was previously in the collection).
will sequence of the contents of aCltn and will add the members of the collection to a new SortCltn instance. This is equivalent to sorting the collection. To obtain a sorted OrdCltn instance (as opposed to a SortCltn), simply convert back like this,aSortCltn = [[SortCltn new] addAll:aCltn];
To filter out duplicate entries, it's also possible to insert a Set instance in the conversion process.aCltn = [[OrdCltn new] addAll:aSortCltn];
+newReturns a new instance that sorts its contents with respect to compare:.
+new:(unsigned)nFor this class, this method does not differ from new.
+newDictCompareReturns a new instance that sorts its contents with respect to dictCompare:.
+sortBy:sortBlockReturns a new instance that sorts its contents with respect to sortBlock. This block should take two objects a and b as argument, and return a positive value if a is greater than b, or zero if a and b are equal, and a negative value if a is less than b.
id c; int r; c = [SortCltn sortBy:{ :a:b | [a compare:b] }];
+sortBlock:sortBlockSame as sortBy:.
Note: There is a SortedCollection method with a similar name in Squeak.
+newCmpSel:(SEL)aSelFor backwards compatibility only. sortBy: provides a more powerful mechanism to sort given an arbitrary sort block.
+with:(int)nArgs,...Returns a new object with nArgs elements. For example,
creates a collection and adds anObject and otherObject to it. In a similar way, Set or SortCltn instances can be created like this.id aCltn = [OrdCltn with:2,anObject,otherObject];
+with:firstObjectwith:nextObjectThis method is equivalent to with: 2,firstObject,nextObject.
+add:firstObjectThis method is equivalent to with: 1,firstObject.
This (factory) method has the same name as the instance method add: and can be used as follows, in circumstances when the user does not want to allocate a collection unless it is actually used :
This shows that creation of the collection is delayed until it is actually needed. If the collection already exists, objects are simply added, using the instance method add:.aCltn = [ (aCltn)?aCltn:OrdCltn add:myObject ];
-copyReturns a new copy of the object (without copying the elements).
-deepCopyReturns a new copy of the object. The elements in the new copy are deep copies of the elements in the original object.
-emptyYourselfEmpties all the members of the object (without freeing them). Returns the receiver.
-freeContentsRemoves and frees the contents of the object, but doesn't free the object itself. Returns the receiver.
-freeFrees the object, but not its contents. Returns nil. Do :
if you want to free the object and its contents.aSort = [[aSort freeContents] free];
- (unsigned)sizeReturns the number of elements in the object.
- (BOOL)isEmptyWhether the number of elements is equal to zero.
-eachElementReturns a sequence of sorted elements. The first element in the sequence is the smallest with respect to the ordering.
aSeq = [aSort eachElement]; while ((anElement = [aSeq next])) { /* do something */ } aSeq = [aSeq free];
- (unsigned)hashReturns a hash value based on the receiver's address and the results of sending the hash message to the contents.
- (BOOL)isEqual:aSortReturns YES if aSort is an SortCltn instance, and if each member of its contents responds affirmatively to the message isEqual: when compared to the corresponding member of the receiver's contents.
-add:anObjectAdds anObject to the receiver, keeping the contents of the object sorted. Duplicate entries are allowed. Returns the receiver.
-addNTest:anObjectAdds anObject if it was not previously in the set. Returns anObject if the addition takes place, otherwise returns nil.
-filter:anObjectIf anObject compares equally to some object in the contents of the receiver, then anObject is freed, and the matching object is returned. Otherwise, anObject is added and returned.
-replace:anObjectIf a matching object is found, then anObject replaces that object, and the matching object is returned. If there is no matching object, anObject is added to the receiver, and nil is returned.
-remove:oldObjectRemoves oldObject or the element that matches (when the compare method returns zero). Returns the removed entry, or nil if there is no matching entry.
Note: Not implemented
- (BOOL)includesAllOf:aCltnAnswer whether all the elements of aCltn are in the receiver, by sending includes: for each individual element.
- (BOOL)includesAnyOf:aCltnAnswer whether any element of aCltn is in the receiver, by sending includes: for each individual element.
-addAll:aCltnAdds each member of aCltn to the receiver. If aCltn is nil, no action is taken. The argument aCltn need not be a collection, so long as it responds to eachElement in the same way as collections do. Returns the receiver.
Note: If aCltn is the same object as the receiver, a addYourself message is sent to the object.
-addContentsOf:aCltnThis method is equivalent to addAll: and is provided for Stepstone ICpak101 compatibility.
-addContentsTo:aCltnThis method is equivalent to addAll:, but with argument and receiver interchanged, and is provided for Stepstone ICpak101 compatibility.
-removeAll:aCltnRemoves all of the members of aCltn from the receiver. The argument aCltn need not be a collection, as long as it responds to eachElement as collections do. Returns the receiver.
Note: If aCltn is the same object as the receiver, it empties itself using emptyYourself and returns the receiver.
-removeContentsFrom:aCltnThis method is equivalent to removeAll:, and is provided for compatibility with Stepstone ICpak101.
-removeContentsOf:aCltnThis method is equivalent to removeAll:, and is provided for compatibility with Stepstone ICpak101.
-intersection:bagReturns a new Collection which is the intersection of the receiver and bag. The new Collection contains only those elements that were in both the receiver and bag. The argument bag need not be an actual Set or Bag instance, as long as it implements find: as Sets do.
-union:bagReturns a new Collection which is the union of the receiver and bag. The new Collection returned has all the elements from both the receiver and bag. The argument bag need not be an actual Set or Bag instance, as long as it implements eachElement: as Sets and Bags do.
-difference:bagReturns a new Collection which is the difference of the receiver and bag. The new Collection returned has only those elements in the receiver that are not in bag.
-asSetCreates a Set instance and adds the contents of the object to the set.
-asOrdCltnCreates a OrdCltn instance and adds the contents of the object to the set.
-detect:aBlockThis message returns the first element in the receiver for which aBlock evaluates to something that is non-nil . For example, the following :
Returns nil if there's no element for which aBlock evaluates to something that non-nil.[ aCltn detect: { :each | [each isEqual:anObject] } ];
-detect:aBlockifNone:noneBlockThis message returns the first element in the receiver for which aBlock evaluates to something that is non-nil.
Evaluates noneBlock if there's no element for which aBlock evaluates to something that is non-nil, and returns the return value of that block. For example,
[ aCltn detect: { :e | [e isEqual:anObject]} ifNone: {anObject} ];
-select:testBlockThis message will return a subset of the receiver containing all elements for which testBlock evaluates to an Object that is non-nil. For example,
Returns a new empty instance of the same class as the receiver, if there's no element for which testBlock evaluates to something that is non-nil.[ aCltn select: { :each | [each isEqual:anObject] } ];
-reject:testBlockComplement of select:
This message will return a subset of the receiver containing all elements for which testBlock evaluates to nil. For example,
Returns a new empty instance of the same class as the receiver, if there's no element for which testBlock evaluates to nil.[ aCltn reject: { :each | [each isEqual:anObject] } ];
-collect:transformBlockThis message creates and returns a new collection of the same size and type as the receiver. The elements are the result of performing transformBlock on each element in the receiver (elements for which the Block would return nil are filtered out).
- (unsigned)count:aBlockEvaluate aBlock with each of the receiver's elements as the argument. Return the number that answered a non-nil value.
-elementsPerform:(SEL)aSelectorSend aSelector to all objects in the collection, starting from the object at offset 0. For Stepstone compatibility. Producer uses this.
-elementsPerform:(SEL)aSelectorwith:anObjectSend aSelector to all objects in the collection, starting from the object at offset 0. For Stepstone compatibility. Producer uses this.
-elementsPerform:(SEL)aSelectorwith:anObjectwith:otherObjectSend aSelector to all objects in the collection, starting from the object at offset 0. For Stepstone compatibility. Producer uses this.
-elementsPerform:(SEL)aSelectorwith:anObjectwith:otherObjectwith:thirdObjSend aSelector to all objects in the collection, starting from the object at offset 0. For Stepstone compatibility. ICpak201 uses this.
-do:aBlockEvaluates aBlock for each element in the collection and returns self. aBlock must be a block taking one object (element) as argument; the return value of the block is ignored by this method.
Often, the Block would, as a side-effect, modify a variable, as in:
int count = 0; [contents do: { :what | if (what == anObject) count++; }];
-do:aBlockuntil:(BOOL*)flagEvaluates aBlock for each element in the collection, or until the variable pointed to by flag becomes true, and returns self. aBlock must be a block taking one object (element) as argument; the return value of the block is ignored by this method.
Typically the Block will modify the variable flag when some condition holds:
BOOL found = NO; [contents do:{ :what | if (what == findObject) found=YES;} until:&found]; if (found) { ... }
-find:anObjectReturns any element in the receiver which isEqual: to anObject. Otherwise, returns nil.
- (BOOL)contains:anObjectReturns YES if the receiver contains anObject. Otherwise, returns NO. Implementation is in terms of the receiver's find: method.
-printOn:(IOD)aFilePrints a comma separated list of the objects in the set by sending each individual object a printOn: message. Returns the receiver.
-fileOutOn:aFilerWrites the tree and all its elements to aFiler. Returns the receiver.
-fileInFrom:aFilerReads the tree and all its elements from aFiler. Returns the receiver.