eXept Software AG Logo

Smalltalk/X Webserver

Documentation of class 'ReindexedCollection':

Home

Documentation
www.exept.de
Everywhere
for:
[back]

Class: ReindexedCollection


Inheritance:

   Object
   |
   +--Collection
      |
      +--SequenceableCollection
         |
         +--ReindexedCollection

Package:
stx:libbasic2
Category:
Collections-Sequenceable
Version:
rev: 1.21 date: 2022/06/10 11:30:09
user: cg
file: ReindexedCollection.st directory: libbasic2
module: stx stc-classLibrary: libbasic2

Description:


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.


copyright

COPYRIGHT (c) 2006 by eXept Software AG All Rights Reserved This software is furnished under a license and may be used only in accordance with the terms of that license and with the inclusion of the above copyright notice. This software may not be provided or otherwise made available to, or used by, any other person. No title to or ownership of the software is hereby transferred.

Class protocol:

instance creation
o  on: aSequence from: start
Create a reindexedCollection on aSequence from start to the end of aSequence

o  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.

o  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)

o  on: aSequence from: start to: stop by: step
Create a reindexedCollection on aSequence from start to stop by step

o  on: aSequence to: stop
Create a reindexedCollection on aSequence from 1 to stop by 1

o  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.

o  on: aSequence with: anInterval
Create a reindexedCollection on aSequence


Instance protocol:

accessing
o  at: index
Answer the value of an indexable field in the sequence instance variable.

o  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.

o  size
Answer how many elements the receiver contains.

o  slide
slide by 1

o  slide: anIncrement
given an increment, adjust the reindex map by sliding it that far

adding & removing
o  add: anObject
report an error; reindexedCollections cannot add elements

converting-reindexed
o  from: startIndex
return a new collection representing the receiver's elements starting at startIndex.

Usage example(s):

     |coll cdr cddr cdddr cdddr2|

     coll := #(1 2 3 4 5 6 7 8 9 10).
     cdr := coll from:2.            
     cddr := cdr from:2.        
     cdddr := cddr from:2.      
     cdddr2 := coll from:4.  

Usage example(s):

     |coll coll2|

     coll := #(1 2 3 4 5 6 7 8 9 10).
     coll2 := coll from:2 by:2.            
     Transcript showCR:coll2.        

Usage example(s):

     |coll coll2 coll3|

     coll := #(1 2 3 4 5 6 7 8 9 10).   
     coll2 := coll from:2 by:2.            
     coll3 := coll2 from:2.            
     Transcript showCR:coll3.        

o  from: startIndex to: stopIndex
return a new collection representing the receiver's elements
starting at startIndex upTo and including endIndex.

Usage example(s):

     |coll cdrButLast cddrButLast2 cdddrButLast3|

     coll := #(1 2 3 4 5 6 7 8 9 10).
     cdrButLast := coll from:2 to:9.             
     cddrButLast2 := cdrButLast from:2 to:7.          
     cdddrButLast3 := cddrButLast2 from:2 to:5.     

Usage example(s):

     |coll cdrButLast cddrButLast2 cdddrButLast3|

     coll := 1 to:100.
     cdrButLast := coll from:2 to:99.             
     cddrButLast2 := cdrButLast from:2 to:97.          
     cdddrButLast3 := cddrButLast2 from:2 to:95.     

o  to: stopIndex
return a new collection representing the receiver's elements upTo and including endIndex.

Usage example(s):

     |coll butLast butLast2 butLast3|

     coll := #(1 2 3 4 5 6 7 8 9 10).
     butLast := coll to:9.              
     butLast2 := butLast to:8.          
     butLast3 := butLast2 to:7.     

Usage example(s):

     |coll coll2|

     coll := #(1 2 3 4 5 6 7 8 9 10).
     coll2 := coll to:9 by:2.              

Usage example(s):

     |coll coll2 coll3|

     coll := #(1 2 3 4 5 6 7 8 9 10 11 12).
     coll2 := coll to:9 by:2.              
     coll3 := coll2 to:3.              

copying
o  copy
return a new Collection containing the elements of the receiver.

Usage example(s):

redefinition is a consequence of the implementation, which references the underlying array.
     otherwise we get a shallow copy of the sequence, which is not what we want here

Usage example(s):

     (#(1 2 3 4 5 6 7 8 9 10) from:3) copy  

     (#(1 2 3 4 5 6 7 8 9 10) butLast:3) copy

     (#(1 2 3 4 5 6 7 8 9 10) from:2 to:7) copy    

initialization
o  initialize: aSequence from: start to: stop by: step

o  initialize: aSequence with: anInterval
Modified (format): / 22-02-2017 / 10:46:38 / cg

queries
o  characterSize

o  isFixedSize
return true if the receiver cannot grow

o  species
Answer the preferred class for reconstructing the receiver,
that is, the sequence.

o  speciesForAdding
Answer the preferred class for reconstructing the receiver incrementally


Examples:


taking a sub-slice of a bigger collection (elements 8,9 and 10)
  |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].


taking a sub-slice of a bigger collection (elements 3,4,5,6,7 and 8)
  |coll|

  coll := (1 to:10) asOrderedCollection from:3 to:8.
  coll.     
  coll size.  
  coll at:1.   
  coll do:[:each | Transcript showCR:each].


taking the first 4 elements of a bigger collection
  |coll|

  coll := (1 to:10) asOrderedCollection to:4.
  coll.     
  coll size.  
  coll at:1.   
  coll last.   
  coll do:[:each | Transcript showCR:each].


taking every second element from a bigger collection
  |coll|

  coll := (1 to:10) asOrderedCollection from:1 to:8 by:2.
  coll.     
  coll size.  
  coll at:1.   
  coll last.   
  coll do:[:each | Transcript showCR:each].


taking every second element from a bigger collection
  |coll|

  coll := (1 to:10) from:1 to:8 by:2.
  coll.     
  coll size.  
  coll at:1.   
  coll last.   
  coll do:[:each | Transcript showCR:each].


ST/X 7.7.0.0; WebServer 1.702 at 20f6060372b9.unknown:8081; Sat, 23 Nov 2024 07:55:43 GMT