|
|
Class: Interval
Object
|
+--Collection
|
+--SequenceableCollection
|
+--ReadOnlySequenceableCollection
|
+--Interval
- Package:
- stx:libbasic
- Category:
- Collections-Sequenceable
- Version:
- rev:
1.51
date: 2009/09/08 15:36:02
- user: cg
- file: Interval.st directory: libbasic
- module: stx stc-classLibrary: libbasic
- Author:
- Claus Gittinger
Intervals represent a collection (or range) of numeric values specified by
a startValue, an endValue and a step.
The interesting thing is that the elements are computed, not stored.
However, protocol-wise, intervals behave like any other (read-only) sequenceable collection.
For example, the interval (1 to:5) containes the elements (1 2 3 4 5) and
(1 to:6 by:2) contains (1 3 5).
examples:
(1 to:10) do:[:i | Transcript showCR:i]
notice, that this is semantically equivalent to:
1 to:10 do:[:i | Transcript showCR:i]
however, the second is preferred, since loops using to:do: are
much faster and do not create temporary garbage objects.
Therefore, Intervals are generally NOT used for this kind of loops.
(1 to:10) asArray
(1 to:10 by:2) asOrderedCollection
converting
-
decodeFromLiteralArray: anArray
-
create & return a new instance from information encoded in anArray.
Re-redefined, since the implementation in SequencableCollection creates instances with an initial
size, which is not allowed for intervals.
instance creation
-
from: start to: stop
-
return a new interval with elements from start to stop by 1
-
from: start to: stop by: step
-
return a new interval with elements from start to stop by step
accessing
-
at: index
-
return (i.e. compute) the index'th element
-
first
-
return the first element of the collection
-
increment
-
alias for #step; for ST-80 compatibility
-
last
-
return the last element of the collection
-
start
-
return the first number of the range
-
start: aNumber
-
set the first number of the range
-
step
-
return the step increment of the range.
OBSOLETE:
Please use #increment for ST-80 compatibility.
-
step: aNumber
-
set the step increment of the range
-
stop
-
return the end number of the range
-
stop: aNumber
-
set the end number of the range
bulk operations
-
sum
-
sum up all elements.
comparing
-
= anInterval
-
-
hash
-
converting
-
fromLiteralArrayEncoding: encoding
-
read my values from an encoding.
The encoding is supposed to be either of the form:
(#Interval start stop step)
This is the reverse operation to #literalArrayEncoding.
-
literalArrayEncoding
-
encode myself as an array literal, from which a copy of the receiver
can be reconstructed with #decodeAsLiteralArray.
converting-reindexed
-
from: startIndex
-
return a new collection representing the receivers elements starting at startIndex.
-
to: endIndex
-
return a new collection representing the receivers elements upTo and including endIndex.
enumerating
-
collect: aBlock
-
evaluate the argument, aBlock for every element in the collection
and return a collection of the results.
Redefined since SeqColl accesses the receiver via at:, which is slow for intervals
-
do: aBlock
-
evaluate the argument, aBlock for every element in the receiver-interval.
Redefined since SeqColl accesses the receiver with at:, which is slow for intervals.
-
reverseDo: aBlock
-
evaluate the argument, aBlock for every element in the receiver-interval in
reverse order.
Redefined since SeqColl accesses the receiver with at:, which is slow for intervals.
-
select: aBlock
-
evaluate the argument, aBlock for every element in the collection
and return a collection of all elements for which the block return true.
Redefined since SeqColl accesses the receiver with at:, which is slow for intervals.
printing & storing
-
displayString
-
-
printOn: aStream
-
append a printed representation to aStream
-
storeOn: aStream
-
store a representation which can reconstruct the receiver to aStream
private
-
setFrom: startInteger to: stopInteger by: stepInteger
-
set start, stop and step components
-
species
-
return the type of collection to be returned by collect, select etc.
queries
-
max
-
return the maximum value in the receiver collection,
redefined, since this can be easily computed.
Raises an error, if the receiver is empty.
-
min
-
return the minimum value in the receiver collection,
redefined, since this can be easily computed.
Raises an error, if the receiver is empty.
-
minMax
-
return the minimum and maximum values in the receiver collection
as a two element array.
Raises an error, if the receiver is empty.
-
size
-
return the number of elements in the collection
set operations
-
intersect: aCollection
-
return a new interval containing all elements of the receiver,
which are also contained in the argument collection
sorting & reordering
-
reversed
-
return a copy with elements in reverse order
testing
-
includes: anElement
-
return true if anElement is in the interval (Numeric compare using =)
visiting
-
acceptVisitor: aVisitor with: aParameter
-
this is special. Some encoders want to encode this as a sequenceable collection,
some want to encode a less expensive representation
|