home *** CD-ROM | disk | FTP | other *** search
/ The CDPD Public Domain Collection for CDTV 3 / CDPDIII.bin / pd / programming / gnusmalltalk / interval.st < prev    next >
Text File  |  1992-02-15  |  5KB  |  216 lines

  1. "======================================================================
  2. |
  3. |   Interval Method Definitions
  4. |
  5.  ======================================================================"
  6.  
  7.  
  8. "======================================================================
  9. |
  10. | Copyright (C) 1990, 1991, 1992 Free Software Foundation, Inc.
  11. | Written by Steve Byrne.
  12. |
  13. | This file is part of GNU Smalltalk.
  14. |
  15. | GNU Smalltalk is free software; you can redistribute it and/or modify it
  16. | under the terms of the GNU General Public License as published by the Free
  17. | Software Foundation; either version 1, or (at your option) any later version.
  18. | GNU Smalltalk is distributed in the hope that it will be useful, but WITHOUT
  19. | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  20. | FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
  21. | details.
  22. | You should have received a copy of the GNU General Public License along with
  23. | GNU Smalltalk; see the file COPYING.  If not, write to the Free Software
  24. | Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  
  25. |
  26.  ======================================================================"
  27.  
  28.  
  29. "
  30. |     Change Log
  31. | ============================================================================
  32. | Author       Date       Change 
  33. | sbb         16 Mar 91      Class creation now separate statement.
  34. |
  35. | sbyrne     25 Apr 89      created.
  36. |
  37. "
  38.  
  39. SequenceableCollection subclass: #Interval
  40.                instanceVariableNames: 'start stop step'
  41.                classVariableNames: ''
  42.                poolDictionaries: ''
  43.                category: nil
  44. !
  45.  
  46. Interval comment: 
  47. 'My instances represent ranges of objects, typically Magnitude type
  48. objects.  I provide iteration/enumeration messages for producing all the
  49. members that my instance represents.' !
  50.  
  51. !Interval class methodsFor: 'instance creation'!
  52.  
  53. from: startInteger to: stopInteger by: stepInteger
  54.     | int |
  55.     ^self new initializeFrom: startInteger to: stopInteger by: stepInteger
  56. !
  57.  
  58. from: startInteger to: stopInteger    
  59.     ^self from: startInteger to: stopInteger by: 1
  60.  
  61. !!
  62.  
  63.  
  64.  
  65. !Interval methodsFor: 'basic'!
  66.  
  67. " Note to the reader: these two methods (do: and collect:) are implemented
  68. in this expanded way, instead of just making the whileTrue: condition be a
  69. block that's conditionally assigned based on the sign of step and then
  70. invoking a single whileTrue: loop.  However, in this form, the optimizer
  71. can optimize these while loops into direct byte codes and not have to bother
  72. with expensive (relatively) block and method context creation and sending
  73. messages to blocks. "
  74.  
  75. do: aBlock
  76.     | i |
  77.     i _ start.
  78.     step > 0
  79.         ifTrue: [
  80.             [ i <= stop ] whileTrue:
  81.                 [ aBlock value: i.
  82.                   i _ i + step ]
  83.         ]
  84.         ifFalse: [
  85.         [ i >= stop ] whileTrue:
  86.                 [ aBlock value: i.
  87.                   i _ i + step ]
  88.         ]
  89. !
  90.  
  91. collect: aBlock
  92.     | i result j |
  93.     result _ self species new: self size.
  94.     i _ 1.
  95.     j _ start.
  96.     step > 0
  97.         ifTrue: [
  98.         [ j <= stop ]
  99.             whileTrue:
  100.             [ result at: i put: (aBlock value: j).
  101.                 j _ j + step.
  102.             i _ i + 1 ]
  103.         ]
  104.     ifFalse: [
  105.         [ j >= stop ]
  106.             whileTrue:
  107.             [ result at: i put: (aBlock value: j).
  108.               j _ j + step.
  109.               i _ i + 1 ]
  110.         ]
  111. !    
  112.  
  113. size
  114.     step > 0
  115.         ifTrue: [
  116.         stop >= start ifTrue: [ ^(stop - start) // step + 1 ]
  117.                       ifFalse: [ ^0 ]
  118.     ]
  119.     ifFalse: [
  120.             start >= stop ifTrue: [ ^(start - stop) // step + 1 ]
  121.                       ifFalse: [ ^0 ]
  122.     ]
  123. !
  124.  
  125. species
  126.     ^Array
  127. !
  128.  
  129.  
  130. at: index
  131.     (index >= 1 and: [index <= self size])
  132.         ifTrue: [ ^start + (step * (index - 1)) ]
  133.     ifFalse: [ self error: 'subscript out of bounds' ]
  134. !
  135.  
  136. at: index put: anObject
  137.     self error: 'you cannot store into an Interval'
  138. !
  139.  
  140. add: newObject
  141.     self error: 'elements cannot be add3ed to an Interval'
  142. !
  143.  
  144. remove: newObject
  145.     self error: 'elements canot be removed from an Interval'
  146. !!
  147.  
  148.  
  149.  
  150. !Interval methodsFor: 'testing'!
  151.  
  152. = anInterval
  153.     ^(start = anInterval start) &
  154.     (stop = anInterval stop) &
  155.     (step = anInterval step)
  156. !
  157.  
  158. hash
  159.     ^(start bitXor: stop) bitXor: step
  160. !!
  161.  
  162.  
  163.  
  164. !Interval methodsFor: 'printing'!
  165.  
  166. printOn: aStream
  167.     aStream nextPutAll: self classNameString.
  168.     aStream nextPut: Character space.
  169.     start printOn: aStream.
  170.     aStream nextPut: $,.
  171.     stop printOn: aStream.
  172.     aStream nextPut: $,.
  173.     step printOn: aStream
  174. !!
  175.  
  176.  
  177.  
  178. !Interval methodsFor: 'storing'!
  179.  
  180. storeOn: aStream
  181.     aStream nextPut: $(.
  182.     aStream nextPutAll: self classNameString.
  183.     aStream nextPutAll: ' from: '.
  184.     start storeOn: aStream.
  185.     aStream nextPutAll: ' to: '.
  186.     stop storeOn: aStream.
  187.     aStream nextPutAll: ' by: '.
  188.     step storeOn: aStream.
  189.     aStream nextPut: $)
  190. !!
  191.  
  192.  
  193.  
  194. !Interval methodsFor: 'private methods'!
  195.  
  196. initializeFrom: startInteger to: stopInteger by: stepInteger
  197.     start _ startInteger.
  198.     stop _ stopInteger.
  199.     step _ stepInteger
  200. !
  201.  
  202. start
  203.     ^start
  204. !
  205.  
  206. stop
  207.     ^stop
  208. !
  209.  
  210. step
  211.     ^step
  212. !!
  213.  
  214.