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

  1. "======================================================================
  2. |
  3. |   Integer 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. | sbb          9 Nov 90      Converted to use fractions.
  36. |
  37. | sbb         21 Sep 90      Fixed printOn: to reflect the fact that String
  38. |              printOn: no longer ""does the right thing"".
  39. |
  40. | sbyrne     25 Apr 90      Fixed (oh...happy birthday, Integer.st!) bitInvert.
  41. |              After fixing the lexer to be pickier about integer
  42. |              literals that were too large to be represented as
  43. |              Smalltalk literals, the previous code (which xored
  44. |              with 7fffffff) broke, so as a hack I computed two's
  45. |              complement and subtracted one.
  46. |
  47. | sbyrne     25 Apr 89      created.
  48. |
  49. "
  50.  
  51. Number variableWordSubclass: #Integer "### Not really a variable word subclass"
  52.        instanceVariableNames: ''
  53.        classVariableNames: ''
  54.        poolDictionaries: ''
  55.        category: nil
  56. !
  57.  
  58. Integer comment:
  59. 'I am the integer class of the GNU Smalltalk system.  My instances can 
  60. represent 31 bit integers and are as efficient as possible.' !
  61.  
  62. !Integer methodsFor: 'Misc math operators'!
  63.  
  64. hash
  65.     ^self
  66. !!
  67.  
  68.  
  69.  
  70. !Integer methodsFor: 'Other iterators'!
  71.  
  72. timesRepeat: aBlock
  73.     | i |
  74.     i _ 1.
  75.     [ i <= self ] whileTrue: [ aBlock value.
  76.                                i _ i + 1 ]
  77. !!
  78.  
  79.  
  80.  
  81. !Integer methodsFor: 'bit operators'!
  82.  
  83. bitAt: index
  84.     ^(self bitShift: index negated) bitAnd: 1
  85. !
  86.  
  87. bitInvert
  88.     "Return the 1's complement of the bits of the receiver"
  89.     ^self negated - 1        "compute 2's complement then remove 1"
  90. !
  91.  
  92.  
  93. allMask: anInteger
  94.     "True if all bits in anInteger are 1 in the receiver"
  95.     ^(self bitAnd: anInteger) = anInteger
  96. !
  97.  
  98. anyMask: anInteger
  99.     "True if any 1 bits in anInteger are 1 in the receiver"
  100.     ^(self bitAnd: anInteger) ~= 0
  101. !
  102.  
  103. noMask: anInteger
  104.     "True if no 1 bits in anInteger are 1 in the receiver"
  105.     ^(self bitAnd: anInteger) = 0
  106. !
  107.  
  108. highBit
  109.     "Return the index of the highest order 1 bit of the receiver"
  110.     self = 0 ifTrue: [ ^-1 ].    "??? I don't know what the right value is"
  111.     30 to: 1 step: -1 do:
  112.         [ :i | (self bitAnd: (1 bitShift: i)) ~= 0 ifTrue: [ ^i ] ]
  113. !!
  114.  
  115.  
  116.  
  117. !Integer methodsFor: 'Math methods'!
  118.  
  119. factorial
  120.     self < 2 ifTrue: [ ^1 ]
  121.              ifFalse: [ ^self * (self - 1) factorial ]
  122. !
  123.  
  124. gcd: anInteger
  125.     | selfInteger temp |
  126.     "Return the greatest common divisor (Euclid's algorithm)"
  127.     selfInteger _ self.
  128.     [ anInteger ~= 0 ]
  129.         whileTrue: [ temp _ selfInteger \\ anInteger.
  130.                  selfInteger _ anInteger.
  131.              anInteger _ temp. ].
  132.     ^selfInteger
  133. !
  134.  
  135. lcm: anInteger
  136.     ^(self * anInteger) abs // (self gcd: anInteger)
  137. !
  138.  
  139. even
  140.     ^(self bitAnd: 1) = 0
  141. !
  142.  
  143. odd
  144.     ^(self bitAnd: 1) ~= 0
  145. !!
  146.  
  147.  
  148.  
  149. !Integer methodsFor: 'Coercion methods (heh heh heh)'!
  150.  
  151. asCharacter
  152.     "Return self as an ascii character"
  153.     (self <= 255 and: [ self >= 0])
  154.         ifTrue: [ ^Character value: self ]
  155.     ifFalse: [ ^self error: 'Integer not convertible to character' ]
  156. !
  157.  
  158. coerce: aNumber
  159.     ^aNumber truncated
  160. !
  161.  
  162. generality
  163.     ^1
  164. !
  165.  
  166. ceiling
  167.     ^self
  168. !
  169.  
  170. floor
  171.     ^self
  172. !
  173.  
  174. truncated
  175.     ^self
  176. !
  177.  
  178. rounded
  179.     ^self
  180. !!
  181.  
  182.  
  183.  
  184.  
  185. !Integer methodsFor: 'copying'!
  186.  
  187. shallowCopy
  188.     ^self
  189. !
  190.  
  191. deepCopy
  192.     ^self
  193. !!
  194.  
  195.  
  196.  
  197.  
  198. !Integer methodsFor: 'printing'!
  199.  
  200. printOn: aStream base: b
  201.     aStream nextPutAll: (self radix: b)
  202. !
  203.  
  204. radix: baseInteger
  205.     ^self signedStringBase: baseInteger showRadix: true
  206. !
  207.  
  208. printOn: aStream
  209.     aStream nextPutAll: (self signedStringBase: 10 showRadix: false)
  210. !!
  211.  
  212.  
  213.  
  214. !Integer methodsFor: 'storing'!
  215.  
  216. storeOn: aStream
  217.     self printOn: aStream        "they print and store the same"
  218. !!
  219.  
  220.  
  221.  
  222. !Integer methodsFor: 'private'!
  223.  
  224. signedStringBase: baseInteger showRadix: showRadix
  225.     | str revString string sign len num i |
  226.     self < 0
  227.         ifTrue: [ sign _ true.
  228.               num _ self negated ]
  229.     ifFalse: [ sign _ false.
  230.                num _ self ].
  231.     revString _ num revDigitsBase: baseInteger.
  232.     str _ WriteStream on: (String new: 1).
  233.     showRadix ifTrue:
  234.         [ baseInteger printOn: str.
  235.       str nextPut: $r ].
  236.     sign ifTrue: [ str nextPut: $- ].
  237.     revString reverseDo:
  238.         [ :char | str nextPut: char ].
  239.     ^str contents
  240. !
  241.  
  242. revDigitsBase: b    
  243.     | str num |
  244.     str _ WriteStream on: (String new: 1).
  245.     self = 0
  246.         ifTrue: [ str nextPut: $0 ]
  247.     ifFalse: [
  248.             num _ self.
  249.         [ num = 0 ] whileFalse:
  250.             [ str nextPut: (Character digitValue: num \\ b).
  251.           num _ num // b ] ].
  252.     ^str contents
  253.  
  254. !!
  255.  
  256.