/usr/share/gnu-smalltalk/kernel/Interval.st is in gnu-smalltalk-common 3.2.5-1build2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 | "======================================================================
|
| Interval Method Definitions
|
|
======================================================================"
"======================================================================
|
| Copyright 1988,92,94,95,99,2000,2001,2002,2005,2008,2009
| Free Software Foundation, Inc.
| Written by Steve Byrne.
|
| This file is part of the GNU Smalltalk class library.
|
| The GNU Smalltalk class library is free software; you can redistribute it
| and/or modify it under the terms of the GNU Lesser General Public License
| as published by the Free Software Foundation; either version 2.1, or (at
| your option) any later version.
|
| The GNU Smalltalk class library is distributed in the hope that it will be
| useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
| General Public License for more details.
|
| You should have received a copy of the GNU Lesser General Public License
| along with the GNU Smalltalk class library; see the file COPYING.LIB.
| If not, write to the Free Software Foundation, 59 Temple Place - Suite
| 330, Boston, MA 02110-1301, USA.
|
======================================================================"
ArrayedCollection subclass: Interval [
| start stop step |
<category: 'Collections-Sequenceable'>
<comment: 'My instances represent ranges of objects, typically Number type
objects. I provide iteration/enumeration messages for producing all the
members that my instance represents.'>
Interval class >> from: startInteger to: stopInteger by: stepInteger [
"Answer an Interval going from startInteger to the
stopInteger, with a step of stepInteger"
<category: 'instance creation'>
^self basicNew
initializeFrom: startInteger
to: stopInteger
by: stepInteger
]
Interval class >> from: startInteger to: stopInteger [
"Answer an Interval going from startInteger to the
stopInteger, with a step of 1"
<category: 'instance creation'>
^self
from: startInteger
to: stopInteger
by: 1
]
Interval class >> withAll: aCollection [
"Answer an Interval containing the same elements as aCollection.
Fail if it is not possible to create one."
<category: 'instance creation'>
| newInterval last delta |
aCollection keysAndValuesDo:
[:index :each |
index > 2
ifTrue:
[last - each = delta
ifFalse:
[SystemExceptions.InvalidArgument signalOn: aCollection
reason: 'argument not an arithmetic progression']]
ifFalse: [last isNil ifFalse: [delta := last - each]].
last := each].
^self
from: aCollection first
to: aCollection last
by: (aCollection last - aCollection first) // (aCollection size - 1)
]
do: aBlock [
"Evaluate the receiver for each element in aBlock"
<category: 'basic'>
| i |
i := start.
step > 0
ifTrue: [[i <= stop] whileTrue:
[aBlock value: i.
i := i + step]]
ifFalse: [[i >= stop] whileTrue:
[aBlock value: i.
i := i + step]]
]
collect: aBlock [
"Evaluate the receiver for each element in aBlock,
collect in an array the result of the evaluations."
<category: 'basic'>
| i result j |
result := self copyEmpty: self size.
i := 1.
j := start.
step > 0
ifTrue:
[[j <= stop] whileTrue:
[result at: i put: (aBlock value: j).
j := j + step.
i := i + 1]]
ifFalse:
[[j >= stop] whileTrue:
[result at: i put: (aBlock value: j).
j := j + step.
i := i + 1]].
^result
]
isExact [
"Answer whether elements of the receiver are computed using exact
arithmetic. This is true as long as the start and step value
are exact (i.e. not floating-point)."
<category: 'testing'>
^start isExact and: [step isExact]
]
isEmpty [
"Answer whether the receiver is empty."
<category: 'basic'>
^(step > 0) == (stop < start)
]
size [
"Answer the number of elements in the receiver."
<category: 'basic'>
step > 0
ifTrue: [stop >= start ifTrue: [^(stop - start) // step + 1] ifFalse: [^0]]
ifFalse: [start >= stop ifTrue: [^(stop - start) // step + 1] ifFalse: [^0]]
]
reverse [
"Answer a copy of the receiver with all of its items reversed"
<category: 'basic'>
^Interval
from: (self at: self size)
to: (self at: 1)
by: step negated
]
species [
<category: 'basic'>
^Array
]
at: index [
"Answer the index-th element of the receiver."
<category: 'basic'>
(index >= 1 and: [index <= self size])
ifTrue: [^start + (step * (index - 1))]
ifFalse: [SystemExceptions.IndexOutOfRange signalOn: self withIndex: index]
]
at: index put: anObject [
<category: 'basic'>
self shouldNotImplement
]
= anInterval [
"Answer whether anInterval is the same interval as the receiver"
<category: 'testing'>
self class == anInterval class ifFalse: [^false].
self isEmpty ifTrue: [ ^anInterval isEmpty ].
anInterval isEmpty ifTrue: [ ^false ].
^self first = anInterval first and: [self last = anInterval last
and: [self increment = anInterval increment]]
]
hash [
"Answer an hash value for the receiver"
<category: 'testing'>
^(start + stop + stop) * step bitAnd: 1073741823
]
copyFrom: startIndex to: stopIndex [
<category: 'basic'>
| last |
stopIndex < startIndex
ifTrue:
[stopIndex = (startIndex - 1)
ifTrue: [^Interval from: start to: start - step by: step].
^SystemExceptions.ArgumentOutOfRange
signalOn: stopIndex
mustBeBetween: startIndex - 1
and: self size].
last := self at: stopIndex.
self isExact ifFalse: [ last := last + (step / 2) ].
^Interval
from: (self at: startIndex)
to: last
by: step
]
printOn: aStream [
"Print a representation for the receiver on aStream"
<category: 'printing'>
| size |
aStream
nextPutAll: self class storeString;
nextPut: $(.
size := self size.
size > 0 ifTrue: [aStream print: start].
size > 1
ifTrue:
[aStream
space;
print: start + step].
size > 2
ifTrue:
[(self at: 3) = stop ifFalse: [aStream nextPutAll: ' ...'].
aStream
space;
print: stop].
aStream nextPut: $)
]
first [
<category: 'printing'>
^self isEmpty
ifTrue: [SystemExceptions.IndexOutOfRange signalOn: self withIndex: 1]
ifFalse: [start]
]
last [
"Answer the last value."
<category: 'printing'>
^self isEmpty
ifTrue: [SystemExceptions.IndexOutOfRange signalOn: self withIndex: 0]
ifFalse: [stop - ((stop - start) \\ step)]
]
increment [
<category: 'printing'>
^step
]
storeOn: aStream [
"Store Smalltalk code compiling to the receiver on aStream"
<category: 'storing'>
aStream nextPut: $(.
aStream nextPutAll: self class storeString.
aStream nextPutAll: ' from: '.
start storeOn: aStream.
aStream nextPutAll: ' to: '.
stop storeOn: aStream.
aStream nextPutAll: ' by: '.
step storeOn: aStream.
aStream nextPut: $)
]
copyEmpty [
"Answer an empty copy of the receiver, with the class answered by the
collect: method."
<category: 'private methods'>
^self species new: self size
]
initializeFrom: startInteger to: stopInteger by: stepInteger [
<category: 'private methods'>
start := startInteger.
stop := stopInteger.
step := stepInteger
]
]
|