|
Class: ReindexedCollection
Object
|
+--Collection
|
+--SequenceableCollection
|
+--ReindexedCollection
- Package:
- stx:libbasic2
- Category:
- Collections-Sequenceable
- Version:
- rev:
1.14
date: 2018/08/26 19:40:23
- user: cg
- file: ReindexedCollection.st directory: libbasic2
- module: stx stc-classLibrary: libbasic2
- Author:
- Travis Griggs (tgriggs@keyww.com or tkc@bmi.net)
- Ported from Squeak by Claus Gittinger (not much of a port, though)
ReindexedCollection is a wrapper around a sequenceable collection that remaps the indices
with in linear algorithm.
The elements in the ReindexedCollection are the elements of the original collection
at 'some start' to 'some stop' (optionally 'by some step').
ReindexedCollection allows for efficient use of first/rest-like algorithms (i.e. aka Lisp)
applied to Sequenceable collections, as they avoid element-copying.
For example,
coll1 := #(1 2 3 4 5 6 7 8 9 10).
coll2 := coll1 from:8.
gives us a collection in coll2, which 'contains' 3 elements, 8, 9, 10
with indices 1,2,3.
I.e. a slice from the other array.
The reindexed collection is 'read-only'. I.e. it does not allow for elements to be changed.
See class side examples.
[Instance Variables:]
sequence <SequenceableCollection> the sequence that will be reindexed.
interval <Interval> the object that describes indicies of interest in the sequence.
[Origin:]
Part of the Engineering Math Goodies package from Travis.
instance creation
-
on: aSequence from: start
-
Create a reindexedCollection on aSequence from start to the end of aSequence
-
on: aSequence from: start by: step
-
Create a reindexedCollection on aSequence start to the end of aSequence
if step is positive, else
from start to the beginning of the sequence if step is negative.
-
on: aSequence from: start to: stop
-
Create a reindexedCollection on aSequence from start to stop by 1
(or -1 if start is greater than stop)
-
on: aSequence from: start to: stop by: step
-
Create a reindexedCollection on aSequence from start to stop by step
-
on: aSequence to: stop
-
Create a reindexedCollection on aSequence from 1 to stop by 1
-
on: aSequence to: stop by: step
-
Create a reindexedCollection on aSequence from 1 to stop (if step is
positive) or the end to stop (if
step is negative). Note: if step is not 1 or -1, there is a chance that the
index specified by stop may
not be in the interval.
-
on: aSequence with: anInterval
-
Create a reindexedCollection on aSequence
accessing
-
at: index
-
Answer the value of an indexable field in the sequence instance variable.
-
at: index put: value
-
Store the argument value in the indexable field of the sequence
instance variable indicated by index.
Answer the value that was stored.
-
size
-
Answer how many elements the receiver contains.
-
slide
-
slide by 1
-
slide: anIncrement
-
given an increment, adjust the reindex map by sliding it that far
adding & removing
-
add: anObject
-
report an error; reindexedCollections cannot add elements
converting-reindexed
-
from: startIndex
-
return a new collection representing the receiver's elements starting at startIndex.
-
from: startIndex to: stopIndex
-
return a new collection representing the receiver's elements
starting at startIndex upTo and including endIndex.
-
to: stopIndex
-
return a new collection representing the receiver's elements upTo and including endIndex.
initialization
-
initialize: aSequence from: start to: stop by: step
-
-
initialize: aSequence with: anInterval
-
Modified (format): / 22-02-2017 / 10:46:38 / cg
queries
-
characterSize
-
-
isFixedSize
-
return true if the receiver cannot grow
-
species
-
Answer the preferred class for reconstructing the receiver,
that is, the sequence.
-
speciesForAdding
-
Answer the preferred class for reconstructing the receiver incrementally
|coll|
coll := #(1 2 3 4 5 6 7 8 9 10) from:8.
Transcript show:'from 8: '; showCR:coll.
Transcript show:'size: '; showCR:(coll size).
Transcript show:'at 1: '; showCR:(coll at:1).
Transcript show:'first: '; showCR:(coll first).
Transcript show:'last: '; showCR:(coll last).
coll do:[:each | Transcript show:'do: '; showCR:each].
coll reverseDo:[:each | Transcript show:'reverseDo: '; showCR:each].
|coll|
coll := (1 to:10) asOrderedCollection from:3 to:8.
coll.
coll size.
coll at:1.
coll do:[:each | Transcript showCR:each].
|coll|
coll := (1 to:10) asOrderedCollection to:4.
coll.
coll size.
coll at:1.
coll last.
coll do:[:each | Transcript showCR:each].
|