author | Claus Gittinger <cg@exept.de> |
Tue, 06 Feb 1996 20:01:35 +0100 | |
changeset 183 | 01781ca31dad |
parent 182 | 2ba5785a441e |
child 184 | 9200f060f2dc |
permissions | -rw-r--r-- |
182 | 1 |
Object subclass:#SequenceableCollectionSorter |
2 |
instanceVariableNames:'collection atSelector putSelector sizeSelector sortBlock' |
|
3 |
classVariableNames:'DefaultSortBlock' |
|
4 |
poolDictionaries:'' |
|
5 |
category:'Collections-Support' |
|
6 |
! |
|
7 |
||
8 |
!SequenceableCollectionSorter class methodsFor:'documentation'! |
|
9 |
||
10 |
documentation |
|
11 |
" |
|
12 |
a SequenceableCollectionSorter allows for anything which responds to |
|
13 |
keyed at/atPut messages to be sorted just like a SequenceableCollection. |
|
183
01781ca31dad
really use #<= for comparing (used #<)
Claus Gittinger <cg@exept.de>
parents:
182
diff
changeset
|
14 |
Since the access messages can be customized, even non collections |
01781ca31dad
really use #<= for comparing (used #<)
Claus Gittinger <cg@exept.de>
parents:
182
diff
changeset
|
15 |
(or collection simulators, models etc.) can be sorted with these sorters. |
182 | 16 |
(use #atSelector: / #putSelector: and #sizeSelector: for customization). |
17 |
||
18 |
As with collection sorting, the sortBlock can be specified and defaults to |
|
19 |
a block which compares using #< messages. |
|
20 |
" |
|
21 |
! |
|
22 |
||
23 |
examples |
|
24 |
" |
|
25 |
|a sorter| |
|
26 |
||
27 |
a := #(10 5 2 17 5 99 -5). |
|
28 |
sorter := SequenceableCollectionSorter on:a. |
|
29 |
sorter sort. |
|
30 |
Transcript showCr:a printString |
|
31 |
" |
|
32 |
! ! |
|
33 |
||
34 |
!SequenceableCollectionSorter class methodsFor:'initialization'! |
|
35 |
||
36 |
initialize |
|
37 |
DefaultSortBlock := [:a :b | a <= b] |
|
38 |
||
39 |
" |
|
40 |
SequenceableCollectionSorter initialize |
|
41 |
" |
|
42 |
||
43 |
"Modified: 6.2.1996 / 15:47:48 / cg" |
|
44 |
! ! |
|
45 |
||
46 |
!SequenceableCollectionSorter class methodsFor:'instance creation'! |
|
47 |
||
48 |
on:aCollection |
|
49 |
^ self new collection:aCollection; sortBlock:DefaultSortBlock |
|
50 |
||
51 |
"Created: 6.2.1996 / 15:39:09 / cg" |
|
52 |
"Modified: 6.2.1996 / 16:07:36 / cg" |
|
53 |
! |
|
54 |
||
55 |
on:aCollection using:aTwoArgBlock |
|
56 |
^ self new collection:aCollection; sortBlock:aTwoArgBlock |
|
57 |
||
58 |
"Created: 6.2.1996 / 15:39:32 / cg" |
|
59 |
! |
|
60 |
||
61 |
sort:aCollection |
|
62 |
(self new collection:aCollection; sortBlock:DefaultSortBlock) sort |
|
63 |
||
64 |
"Created: 6.2.1996 / 15:40:04 / cg" |
|
65 |
"Modified: 6.2.1996 / 16:07:42 / cg" |
|
66 |
! |
|
67 |
||
68 |
sort:aCollection using:aTwoArgBlock |
|
69 |
(self new collection:aCollection; sortBlock:aTwoArgBlock) sort |
|
70 |
||
71 |
"Created: 6.2.1996 / 15:39:58 / cg" |
|
72 |
! ! |
|
73 |
||
74 |
!SequenceableCollectionSorter methodsFor:'accessing'! |
|
75 |
||
76 |
atSelector:aSymbol |
|
77 |
"set the selector to access elements. |
|
78 |
If nil (the default), elements are accessed via #at:" |
|
79 |
||
80 |
atSelector := aSymbol |
|
81 |
||
82 |
"Created: 6.2.1996 / 15:35:49 / cg" |
|
83 |
! |
|
84 |
||
85 |
collection |
|
86 |
"return the collections being sorted." |
|
87 |
||
88 |
^ collection |
|
89 |
||
90 |
"Created: 6.2.1996 / 15:37:02 / cg" |
|
91 |
! |
|
92 |
||
93 |
collection:aCollection |
|
94 |
"set the collections being sorted." |
|
95 |
||
96 |
collection := aCollection |
|
97 |
||
98 |
"Created: 6.2.1996 / 15:37:20 / cg" |
|
99 |
! |
|
100 |
||
101 |
putSelector:aSymbol |
|
102 |
"set the selector to access elements. |
|
103 |
If nil (the default), elements are accessed via #at:put:" |
|
104 |
||
105 |
putSelector := aSymbol |
|
106 |
||
107 |
"Created: 6.2.1996 / 15:36:14 / cg" |
|
108 |
! |
|
109 |
||
110 |
sizeSelector:aSymbol |
|
111 |
"set the selector to get the collections size. |
|
112 |
If nil (the default), elements are accessed via #size" |
|
113 |
||
114 |
sizeSelector := aSymbol |
|
115 |
||
116 |
"Created: 6.2.1996 / 15:36:33 / cg" |
|
117 |
! |
|
118 |
||
119 |
sortBlock |
|
120 |
"return the sortBlock which is used to compare two elements. |
|
121 |
The default sortBlock compares elements by sending the #< message." |
|
122 |
||
123 |
^ sortBlock |
|
124 |
||
125 |
"Created: 6.2.1996 / 15:38:32 / cg" |
|
126 |
! |
|
127 |
||
128 |
sortBlock:aTwoArgBlock |
|
129 |
"set the sortBlock which is used to compare two elements. |
|
130 |
The default sortBlock compares elements by sending the #< message." |
|
131 |
||
132 |
sortBlock := aTwoArgBlock |
|
133 |
||
134 |
"Created: 6.2.1996 / 15:38:20 / cg" |
|
135 |
! ! |
|
136 |
||
137 |
!SequenceableCollectionSorter methodsFor:'sorting'! |
|
138 |
||
139 |
defaultSort:inBegin to:inEnd |
|
140 |
"actual sort worker for sorting when the sortBlock is nil (or the default) |
|
141 |
and default atSelector/putSelectors are to be used." |
|
142 |
||
143 |
|begin "{ Class: SmallInteger }" |
|
144 |
end "{ Class: SmallInteger }" |
|
145 |
b "{ Class: SmallInteger }" |
|
146 |
e "{ Class: SmallInteger }" |
|
147 |
middleElement temp1 temp2 | |
|
148 |
||
149 |
begin := inBegin. "/ this also does a type-check |
|
150 |
end := inEnd. |
|
151 |
||
152 |
b := begin. |
|
153 |
e := end. |
|
154 |
middleElement := collection at:((b + e) // 2). |
|
155 |
||
156 |
[b < e] whileTrue:[ |
|
183
01781ca31dad
really use #<= for comparing (used #<)
Claus Gittinger <cg@exept.de>
parents:
182
diff
changeset
|
157 |
[b < end and:[(collection at:b) <= middleElement]] whileTrue:[b := b + 1]. |
01781ca31dad
really use #<= for comparing (used #<)
Claus Gittinger <cg@exept.de>
parents:
182
diff
changeset
|
158 |
[e > begin and:[middleElement <= (collection at:e)]] whileTrue:[e := e - 1]. |
182 | 159 |
|
160 |
(b <= e) ifTrue:[ |
|
161 |
(b == e) ifFalse:[ |
|
162 |
temp1 := collection at:b. temp2 := collection at:e. |
|
163 |
collection at:b put:temp2. collection at:e put:temp1 |
|
164 |
]. |
|
165 |
b := b + 1. |
|
166 |
e := e - 1 |
|
167 |
] |
|
168 |
]. |
|
169 |
(begin < e) ifTrue:[self defaultSort:begin to:e]. |
|
170 |
(b < end) ifTrue:[self defaultSort:b to:end] |
|
171 |
||
172 |
"Created: 6.2.1996 / 15:44:37 / cg" |
|
183
01781ca31dad
really use #<= for comparing (used #<)
Claus Gittinger <cg@exept.de>
parents:
182
diff
changeset
|
173 |
"Modified: 6.2.1996 / 18:00:56 / cg" |
182 | 174 |
! |
175 |
||
176 |
nonDefaultSort:inBegin to:inEnd |
|
177 |
"actual sort worker for sorting when a non default sortBlock |
|
178 |
or access selectors are used." |
|
179 |
||
180 |
|begin "{ Class: SmallInteger }" |
|
181 |
end "{ Class: SmallInteger }" |
|
182 |
b "{ Class: SmallInteger }" |
|
183 |
e "{ Class: SmallInteger }" |
|
184 |
middleElement temp1 temp2 | |
|
185 |
||
186 |
begin := inBegin. "/ this also does a type-check |
|
187 |
end := inEnd. |
|
188 |
||
189 |
b := begin. |
|
190 |
e := end. |
|
191 |
middleElement := collection perform:atSelector with:((b + e) // 2). |
|
192 |
||
193 |
[b < e] whileTrue:[ |
|
194 |
[b < end |
|
195 |
and:[sortBlock |
|
196 |
value:(collection perform:atSelector with:b) |
|
197 |
value:middleElement]] |
|
198 |
whileTrue:[b := b + 1]. |
|
199 |
[e > begin |
|
200 |
and:[sortBlock |
|
201 |
value:middleElement |
|
202 |
value:(collection perform:atSelector with:e)]] |
|
203 |
whileTrue:[e := e - 1]. |
|
204 |
||
205 |
(b <= e) ifTrue:[ |
|
206 |
(b == e) ifFalse:[ |
|
207 |
temp1 := (collection perform:atSelector with:b). |
|
208 |
temp2 := (collection perform:atSelector with:e). |
|
209 |
collection perform:putSelector with:b with:temp2. |
|
210 |
collection perform:putSelector with:e with:temp1. |
|
211 |
]. |
|
212 |
b := b + 1. |
|
213 |
e := e - 1 |
|
214 |
] |
|
215 |
]. |
|
216 |
(begin < e) ifTrue:[self nonDefaultSort:begin to:e]. |
|
217 |
(b < end) ifTrue:[self nonDefaultSort:b to:end] |
|
218 |
||
219 |
"Modified: 6.2.1996 / 15:42:51 / cg" |
|
220 |
"Created: 6.2.1996 / 16:06:46 / cg" |
|
221 |
! |
|
222 |
||
223 |
sort |
|
224 |
|sz| |
|
225 |
||
226 |
sizeSelector isNil ifTrue:[ |
|
227 |
sz := collection size |
|
228 |
] ifFalse:[ |
|
229 |
sz := collection perform:sizeSelector |
|
230 |
]. |
|
231 |
self sort:1 to:sz |
|
232 |
||
233 |
"Created: 6.2.1996 / 15:41:21 / cg" |
|
234 |
! |
|
235 |
||
236 |
sort:from to:to |
|
237 |
"actual sort worker for sort-messages" |
|
238 |
||
239 |
(atSelector isNil |
|
240 |
and:[putSelector isNil |
|
241 |
and:[sortBlock isNil or:[sortBlock == DefaultSortBlock]]]) ifTrue:[ |
|
242 |
self defaultSort:from to:to |
|
243 |
] ifFalse:[ |
|
244 |
self nonDefaultSort:from to:to |
|
245 |
]. |
|
246 |
||
247 |
"Modified: 6.2.1996 / 15:51:50 / cg" |
|
248 |
! ! |
|
249 |
||
250 |
!SequenceableCollectionSorter class methodsFor:'documentation'! |
|
251 |
||
252 |
version |
|
183
01781ca31dad
really use #<= for comparing (used #<)
Claus Gittinger <cg@exept.de>
parents:
182
diff
changeset
|
253 |
^ '$Header: /cvs/stx/stx/libbasic2/Attic/SeqCollSorter.st,v 1.2 1996-02-06 19:01:35 cg Exp $' |
182 | 254 |
! ! |
255 |
SequenceableCollectionSorter initialize! |