4799
|
1 |
"{ Encoding: utf8 }"
|
|
2 |
|
2789
|
3 |
"
|
|
4 |
COPYRIGHT (c) 2012 by eXept Software AG
|
|
5 |
All Rights Reserved
|
|
6 |
|
|
7 |
This software is furnished under a license and may be used
|
|
8 |
only in accordance with the terms of that license and with the
|
|
9 |
inclusion of the above copyright notice. This software may not
|
|
10 |
be provided or otherwise made available to, or used by, any
|
|
11 |
other person. No title to or ownership of the software is
|
|
12 |
hereby transferred.
|
|
13 |
"
|
|
14 |
"{ Package: 'stx:libbasic2' }"
|
|
15 |
|
4248
|
16 |
"{ NameSpace: Smalltalk }"
|
|
17 |
|
2789
|
18 |
OrderedSet subclass:#SortedSet
|
|
19 |
instanceVariableNames:''
|
|
20 |
classVariableNames:''
|
|
21 |
poolDictionaries:''
|
|
22 |
category:'Collections-Sequenceable'
|
|
23 |
!
|
|
24 |
|
|
25 |
!SortedSet class methodsFor:'documentation'!
|
|
26 |
|
|
27 |
copyright
|
|
28 |
"
|
|
29 |
COPYRIGHT (c) 2012 by eXept Software AG
|
|
30 |
All Rights Reserved
|
|
31 |
|
|
32 |
This software is furnished under a license and may be used
|
|
33 |
only in accordance with the terms of that license and with the
|
|
34 |
inclusion of the above copyright notice. This software may not
|
|
35 |
be provided or otherwise made available to, or used by, any
|
|
36 |
other person. No title to or ownership of the software is
|
|
37 |
hereby transferred.
|
|
38 |
"
|
|
39 |
!
|
|
40 |
|
|
41 |
documentation
|
|
42 |
"
|
|
43 |
I am a subclass of Set whose elements are ordered in a
|
|
44 |
similar fashion to SortedCollection.
|
|
45 |
That is, I have both Set behavior (only keeping a single instance of
|
|
46 |
an element) but I also remember the sort order.
|
|
47 |
|
|
48 |
I have one additional instance variable:
|
|
49 |
|
|
50 |
order <SortedCollection> Sorted collection of values reflecting the order
|
|
51 |
in the set.
|
|
52 |
|
2790
|
53 |
[caveat:]
|
|
54 |
a tree may be a better choice,
|
|
55 |
as although the set shows O(1) behavior when adding,
|
|
56 |
the sortedCollection does not (especially as inserting is expensive).
|
|
57 |
A balanced tree would show O(lg n) behavior.
|
|
58 |
|
2789
|
59 |
[author:]
|
|
60 |
Claus Gittinger
|
|
61 |
|
|
62 |
[see also:]
|
|
63 |
OrderedCollection
|
|
64 |
Dictionary OrderedDictionary
|
|
65 |
Set Bag
|
|
66 |
"
|
|
67 |
!
|
|
68 |
|
|
69 |
examples
|
|
70 |
"
|
|
71 |
[exBegin]
|
|
72 |
|s|
|
|
73 |
|
|
74 |
s := SortedSet new.
|
|
75 |
s add:'one'.
|
|
76 |
s add:'two'.
|
|
77 |
s add:'one'.
|
|
78 |
s add:'two'.
|
4799
|
79 |
s add:'four'.
|
2789
|
80 |
s add:'three'.
|
|
81 |
s size.
|
|
82 |
s do:[:each | Transcript showCR:each].
|
|
83 |
[exEnd]
|
|
84 |
|
|
85 |
|
|
86 |
[exBegin]
|
|
87 |
|s|
|
|
88 |
|
|
89 |
s := SortedSet new.
|
|
90 |
s add:'one'.
|
|
91 |
s add:'two'.
|
|
92 |
s add:'one'.
|
|
93 |
s add:'two'.
|
|
94 |
s add:'three'.
|
|
95 |
s remove:'one'.
|
|
96 |
s size.
|
|
97 |
s do:[:each | Transcript showCR:each].
|
|
98 |
[exEnd]
|
|
99 |
|
|
100 |
[exBegin]
|
|
101 |
|s|
|
|
102 |
|
|
103 |
s := SortedSet new.
|
|
104 |
s add:'one'.
|
|
105 |
s add:'two'.
|
|
106 |
s add:'three'.
|
|
107 |
s add:'one'.
|
|
108 |
s add:'two'.
|
|
109 |
s add:'three'.
|
|
110 |
s size.
|
|
111 |
Transcript showCR:s.
|
|
112 |
s removeFirst.
|
|
113 |
Transcript showCR:s.
|
|
114 |
s removeFirst.
|
|
115 |
Transcript showCR:s.
|
|
116 |
s removeFirst.
|
|
117 |
Transcript showCR:s.
|
|
118 |
[exEnd]
|
|
119 |
"
|
|
120 |
! !
|
|
121 |
|
|
122 |
!SortedSet class methodsFor:'instance creation'!
|
|
123 |
|
|
124 |
sortBlock:aBlock
|
|
125 |
"return a new sortedSet, whe the sort order is defined by aBlock.
|
|
126 |
This must be a two-argument block which returns true if its arg1 has to come before
|
|
127 |
its arg2 in the collection."
|
|
128 |
|
|
129 |
^ self new setSortBlock:aBlock
|
|
130 |
|
|
131 |
"Created: / 06-08-2012 / 12:34:29 / cg"
|
|
132 |
! !
|
|
133 |
|
3438
|
134 |
!SortedSet methodsFor:'accessing'!
|
|
135 |
|
|
136 |
sortBlock
|
|
137 |
^ order sortBlock
|
|
138 |
! !
|
|
139 |
|
2789
|
140 |
!SortedSet methodsFor:'adding & removing'!
|
|
141 |
|
|
142 |
addFirst:anObject
|
|
143 |
"blocked; only the sort order determines the order"
|
|
144 |
|
|
145 |
self shouldNotImplement
|
|
146 |
|
|
147 |
"Modified: / 06-08-2012 / 12:37:23 / cg"
|
|
148 |
!
|
|
149 |
|
|
150 |
addLast:anObject
|
|
151 |
"blocked; only the sort order determines the order"
|
|
152 |
|
|
153 |
self shouldNotImplement
|
|
154 |
|
|
155 |
"Modified: / 06-08-2012 / 12:37:30 / cg"
|
|
156 |
! !
|
|
157 |
|
3438
|
158 |
!SortedSet methodsFor:'enumerating'!
|
|
159 |
|
|
160 |
collect:aBlock
|
|
161 |
"for each element in the receiver, evaluate the argument, aBlock
|
|
162 |
and return a new collection with the results"
|
|
163 |
|
|
164 |
|newCollection|
|
|
165 |
|
|
166 |
newCollection := self speciesForAdding new.
|
|
167 |
newCollection setSortBlock:self sortBlock.
|
|
168 |
self do:[:element | newCollection add:(aBlock value:element)].
|
|
169 |
^ newCollection
|
|
170 |
|
|
171 |
"
|
|
172 |
#(1 2 3 4) collect:[:e | e odd]
|
|
173 |
(1 to:10) collect:[:e | e even]
|
|
174 |
"
|
|
175 |
|
|
176 |
"Modified: / 07-08-2010 / 16:26:40 / cg"
|
|
177 |
!
|
|
178 |
|
|
179 |
select:aBlock
|
|
180 |
"return a new collection with all elements from the receiver, for which
|
|
181 |
the argument aBlock evaluates to true.
|
|
182 |
See also: #removeAllFoundIn: and #removeAllSuchThat:"
|
|
183 |
|
|
184 |
|newCollection|
|
|
185 |
|
|
186 |
newCollection := self speciesForAdding new.
|
|
187 |
newCollection setSortBlock:self sortBlock.
|
|
188 |
self do:[:each |
|
|
189 |
(aBlock value:each) ifTrue:[newCollection add:each].
|
|
190 |
].
|
|
191 |
^ newCollection
|
|
192 |
|
|
193 |
"
|
|
194 |
#(1 2 3 4) select:[:e | e odd]
|
|
195 |
(1 to:10) select:[:e | e even]
|
|
196 |
"
|
|
197 |
|
|
198 |
"Modified: / 07-08-2010 / 16:26:40 / cg"
|
|
199 |
! !
|
|
200 |
|
2789
|
201 |
!SortedSet methodsFor:'initialization'!
|
|
202 |
|
4248
|
203 |
initializeOrder:anInteger
|
|
204 |
order := SortedCollection new:anInteger
|
2789
|
205 |
|
|
206 |
"Created: / 06-08-2012 / 12:33:31 / cg"
|
|
207 |
!
|
|
208 |
|
|
209 |
setSortBlock:aBlock
|
|
210 |
order sortBlock:aBlock
|
|
211 |
|
|
212 |
"Created: / 06-08-2012 / 12:35:07 / cg"
|
|
213 |
! !
|
|
214 |
|
|
215 |
!SortedSet class methodsFor:'documentation'!
|
|
216 |
|
|
217 |
version
|
4248
|
218 |
^ '$Header$'
|
2789
|
219 |
!
|
|
220 |
|
|
221 |
version_CVS
|
4248
|
222 |
^ '$Header$'
|
2789
|
223 |
! !
|
3438
|
224 |
|