|
Class: SortedSet
Object
|
+--Collection
|
+--Set
|
+--OrderedSet
|
+--SortedSet
- Package:
- stx:libbasic2
- Category:
- Collections-Sequenceable
- Version:
- rev:
1.5
date: 2019/02/10 12:48:50
- user: cg
- file: SortedSet.st directory: libbasic2
- module: stx stc-classLibrary: libbasic2
- Author:
- Claus Gittinger
I am a subclass of Set whose elements are ordered in a
similar fashion to SortedCollection.
That is, I have both Set behavior (only keeping a single instance of
an element) but I also remember the sort order.
I have one additional instance variable:
order <SortedCollection> Sorted collection of values reflecting the order
in the set.
[caveat:]
a tree may be a better choice,
as although the set shows O(1) behavior when adding,
the sortedCollection does not (especially as inserting is expensive).
A balanced tree would show O(lg n) behavior.
OrderedCollection
Dictionary
OrderedDictionary
Set
Bag
instance creation
-
sortBlock: aBlock
-
return a new sortedSet, whe the sort order is defined by aBlock.
This must be a two-argument block which returns true if its arg1 has to come before
its arg2 in the collection.
accessing
-
sortBlock
-
adding & removing
-
addFirst: anObject
-
blocked; only the sort order determines the order
-
addLast: anObject
-
blocked; only the sort order determines the order
enumerating
-
collect: aBlock
-
for each element in the receiver, evaluate the argument, aBlock
and return a new collection with the results
usage example(s):
#(1 2 3 4) collect:[:e | e odd]
(1 to:10) collect:[:e | e even]
|
-
select: aBlock
-
return a new collection with all elements from the receiver, for which
the argument aBlock evaluates to true.
See also: #removeAllFoundIn: and #removeAllSuchThat:
usage example(s):
#(1 2 3 4) select:[:e | e odd]
(1 to:10) select:[:e | e even]
|
initialization
-
initializeOrder: anInteger
-
-
setSortBlock: aBlock
-
|s|
s := SortedSet new.
s add:'one'.
s add:'two'.
s add:'one'.
s add:'two'.
s add:'four'.
s add:'three'.
s size.
s do:[:each | Transcript showCR:each].
|
|s|
s := SortedSet new.
s add:'one'.
s add:'two'.
s add:'one'.
s add:'two'.
s add:'three'.
s remove:'one'.
s size.
s do:[:each | Transcript showCR:each].
|
|s|
s := SortedSet new.
s add:'one'.
s add:'two'.
s add:'three'.
s add:'one'.
s add:'two'.
s add:'three'.
s size.
Transcript showCR:s.
s removeFirst.
Transcript showCR:s.
s removeFirst.
Transcript showCR:s.
s removeFirst.
Transcript showCR:s.
|
|