home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus 2004 #2 / Amiga Plus CD - 2004 - No. 02.iso / AmigaPlus / Tools / Development / AmigaTalk / general / Interval.st < prev    next >
Encoding:
Text File  |  2004-01-31  |  1.9 KB  |  101 lines

  1.  
  2. Class Interval :SequenceableCollection
  3. ! lower upper step current !
  4. [
  5.    from: lowerBound to: upperBound by: stepSize
  6.  
  7.      current <- lower <- lowerBound.
  8.      upper   <- upperBound.
  9.      step    <- stepSize
  10. |  
  11.    from: lowerBound to: upperBound
  12.  
  13.      current <- lower <- lowerBound.
  14.      upper   <- upperBound.
  15.      step    <- 1
  16. |
  17.    lowerBound " Added on 16-Oct-2003 for V2.5+"
  18.  
  19.      ^ lower  
  20. |
  21.    upperBound " Added on 16-Oct-2003 for V2.5+"
  22.  
  23.      ^ upper
  24. |
  25.    stepSize   " Added on 16-Oct-2003 for V2.5+"
  26.    
  27.      ^ step
  28. |
  29.    size   
  30.  
  31.      ^ ((step strictlyPositive) 
  32.           ifTrue:  [upper < lower]
  33.           ifFalse: [lower < upper] )
  34.  
  35.         ifTrue:  [ 0 ]
  36.         ifFalse: [upper - lower // step + 1]
  37. |  
  38.    inRange: value
  39.  
  40.      (step > 0)   "(step strictlyPositive)"
  41.         ifTrue:  [^ ((value >= lower) and: [value <= upper]) ]
  42.         ifFalse: [^ ((value >= upper) and: [value <= lower]) ]
  43.    first
  44.  
  45.      current <- lower.
  46.  
  47.      ^ (self inRange: current) ifTrue: [current]
  48. |
  49.    last
  50.  
  51.      current <- upper.
  52.  
  53.      ^ (self inRange: current) ifTrue: [current]
  54. |   
  55.    next
  56.  
  57.      current <- current + step.
  58.  
  59.      ^ (self inRange: current) ifTrue: [current]
  60. |
  61.    at: index ifAbsent: exceptionBlock   ! val !
  62.  
  63.      val <- lower + (step * (index - 1)).
  64.  
  65.      ^ (self inRange: val)
  66.          ifTrue: [ val ]
  67.         ifFalse: [exceptionBlock value]
  68.    printString
  69.  
  70.      ^ 'Interval ', lower printString , ' to ',
  71.                     upper printString , ' by ' , step printString 
  72. |   
  73.    coerce: newcollection
  74.  
  75.      ^ newcollection asArray
  76. |   
  77.    at: index put: val
  78.  
  79.      ^ self error: 'cannot store into Interval'
  80. |   
  81.    add: val
  82.  
  83.      ^ self error: 'cannot store into Interval'
  84. |   
  85.    removeKey: key ifAbsent: exceptionBlock
  86.  
  87.      self error: 'cannot remove from Interval'.
  88.  
  89.      ^ exceptionBlock value
  90. |
  91.    deepCopy
  92.  
  93.      ^ lower to: upper by: step
  94. |   
  95.    shallowCopy
  96.  
  97.      ^ lower to: upper by: step
  98. ]
  99.