home *** CD-ROM | disk | FTP | other *** search
/ Really Useful CD 1 / ReallyUsefulCD1.iso / extras / languages / smalltalk / _smalltalk / prelude / interval < prev    next >
Encoding:
Text File  |  1987-12-30  |  1.6 KB  |  58 lines

  1. Class Interval :SequenceableCollection
  2. | lower upper step current |
  3. [
  4.      from: lowerBound to: upperBound by: stepSize
  5.           current <- lower <- lowerBound.
  6.           upper <- upperBound.
  7.           step  <- stepSize
  8.  
  9. |    size 
  10.           ^ ((step strictlyPositive)
  11.                ifTrue: [upper < lower]
  12.                ifFalse: [lower < upper] )
  13.              ifTrue: [ 0 ]
  14.              ifFalse: [upper - lower // step + 1]
  15.  
  16. |    inRange: value
  17.           ^ (step strictlyPositive)
  18.                ifTrue: [(value >= lower) and: [value <= upper]]
  19.                ifFalse: [(value >= upper) and: [value <= lower]]
  20.  
  21. |       first
  22.                 current <- lower.
  23.           ^ (self inRange: current) ifTrue: [current]
  24.  
  25. |       next
  26.                 current <- current + step.
  27.           ^ (self inRange: current) ifTrue: [current]
  28.  
  29. |    at: index ifAbsent: exceptionBlock | val |
  30.           val <- lower + (step * (index - 1)).
  31.           ^ (self inRange: val)
  32.              ifTrue: [ val ]
  33.              ifFalse: [exceptionBlock value]
  34.  
  35. |    printString
  36.           ^ 'Interval ', lower printString , ' to ',
  37.                      upper printString , ' by ' , step printString 
  38.  
  39. |    coerce: newcollection
  40.           ^ newcollection asArray
  41.  
  42. |    at: index put: val
  43.           ^ self error: 'cannot store into Interval'
  44.  
  45. |    add: val
  46.           ^ self error: 'cannot store into Interval'
  47.  
  48. |    removeKey: key ifAbsent: exceptionBlock
  49.           self error: 'cannot remove from Interval'.
  50.           ^ exceptionBlock value
  51. |
  52.      deepCopy
  53.           ^ lower to: upper by: step
  54. |    
  55.      shallowCopy
  56.           ^ lower to: upper by: step
  57. ]
  58.