![]()
|
sortcltn Specification SheetPortable Object Compiler (c) 1997,98. All Rights Reserved.
SortCltnInherits from: Cltn
Class DescriptionSortCltn (alias SortedCollection) instances are groups of objects that are kept in sorted order in a tree (by default, the first object is the smallest with respect to compare:). Inserting and searching objects in such a sorted collection can be faster than using, say an OrdCltn object collection.
Creating An InstanceThe method new creates an instance that sorts its elements with respect to compare:. The method newDictCompare sends dictCompare: messages to compare pairs of elements. Finally, the method sortBy:: creates a SortCltn that will sort its contents with respect to an arbitrary Block.
Adding ObjectsNormally, you insert an object with the add: method. This method allows you to add an object to the collection, even when it is equal to an element in the collection (when the comparison method returns zero; because you can use a different method than compare:, this doesn't necessarily mean that isEqual: returns YES).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).
Sorting collectionsOne use of SortCltn instances, is to sort collections of objects. For example,
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];
Method typesCreation
InterrogationComparingAddingRemovingTesting ContentsAdding and Removing ContentsCombiningConvertingUsing BlocksMaking elements performDo BlocksLocatingPrintingArchivingMethodsnew+newReturns a new instance that sorts its contents with respect to compare:.
new:+new:(unsigned)nFor this class, this method does not differ from new.
newDictCompare+newDictCompareReturns a new instance that sorts its contents with respect to dictCompare:.
sortBy:+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:+sortBlock:sortBlockSame as sortBy:. Note: There is a SortedCollection method with a similar name in Squeak.
newCmpSel:+newCmpSel:(SEL)aSelFor backwards compatibility only. sortBy: provides a more powerful mechanism to sort given an arbitrary sort block.
copy-copyReturns a new copy of the object (without copying the elements).
deepCopy-deepCopyReturns a new copy of the object. The elements in the new copy are deep copies of the elements in the original object.
emptyYourself-emptyYourselfEmpties all the members of the object (without freeing them). Returns the receiver.
freeContents-freeContentsRemoves and frees the contents of the object, but doesn't free the object itself. Returns the receiver.
free-freeFrees the object, but not its contents. Returns nil. Do :
if you want to free the object and its contents.aSort = [[aSort freeContents] free];
size- (unsigned)sizeReturns the number of elements in the object.
isEmpty- (BOOL)isEmptyWhether the number of elements is equal to zero.
eachElement-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]; hash- (unsigned)hashReturns a hash value based on the receiver's address and the results of sending the hash message to the contents.
isEqual:- (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:-add:anObjectAdds anObject to the receiver, keeping the contents of the object sorted. Duplicate entries are allowed. Returns the receiver.
addNTest:-addNTest:anObjectAdds anObject if it was not previously in the set. Returns anObject if the addition takes place, otherwise returns nil.
filter:-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:-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:-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
find:-find:anObjectReturns any element in the receiver which isEqual: to anObject. Otherwise, returns nil.
contains:- (BOOL)contains:anObjectReturns YES if the receiver contains anObject. Otherwise, returns NO. Implementation is in terms of the receiver's find: method.
printOn:-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:-fileOutOn:aFilerWrites the tree and all its elements to aFiler. Returns the receiver.
fileInFrom:-fileInFrom:aFilerReads the tree and all its elements from aFiler. Returns the receiver.
|