/src/lib/numeric/natural_32.e

http://github.com/tybor/Liberty · Specman e · 229 lines · 160 code · 30 blank · 39 comment · 1 complexity · 8e8302fbe81994c2fe4de15b02ee8ce9 MD5 · raw file

  1. -- This file is part of a Liberty Eiffel library.
  2. -- See the full copyright at the end.
  3. --
  4. expanded class NATURAL_32
  5. insert
  6. NATURAL_GENERAL
  7. redefine infix "//", infix "\\"
  8. end
  9. feature {ANY} -- Explicit conversions:
  10. fit_natural_8: BOOLEAN
  11. -- Does `Current' fit in NATURAL_8?
  12. do
  13. Result := Current <= 255.to_natural_32
  14. ensure
  15. Result = (Current <= 255.to_natural_32)
  16. end
  17. to_natural_8: NATURAL_8
  18. -- Explicit conversion to NATURAL_8.
  19. require
  20. fit_natural_8
  21. external "built_in"
  22. ensure
  23. Result.to_natural_32 = Current
  24. end
  25. fit_natural_16: BOOLEAN
  26. -- Does `Current' fit in NATURAL_16?
  27. do
  28. Result := Current <= 65535.to_natural_32
  29. ensure
  30. Result = (Current <= 65535.to_natural_32)
  31. end
  32. to_natural_16: NATURAL_16
  33. -- Explicit conversion to NATURAL_16.
  34. require
  35. fit_natural_16
  36. external "built_in"
  37. ensure
  38. Result.to_natural_32 = Current
  39. end
  40. to_natural_32: NATURAL_32
  41. -- Dummy, no-op conversion, useful to achieve 32-64 bit portability.
  42. do
  43. Result := Current
  44. end
  45. to_natural_64: NATURAL_64
  46. -- Explicit conversion to NATURAL_64.
  47. external "built_in"
  48. ensure
  49. Result.to_natural_32 = Current
  50. end
  51. fit_integer_8: BOOLEAN
  52. -- Does `Current' fit in INTEGER_8?
  53. do
  54. Result := Current <= 127.to_natural_32
  55. ensure
  56. Result = (Current <= 127.to_natural_32)
  57. end
  58. to_integer_8: INTEGER_8
  59. -- Explicit conversion to INTEGER_8.
  60. require
  61. fit_integer_8
  62. external "built_in"
  63. ensure
  64. Result.to_natural_32 = Current
  65. end
  66. fit_integer_16: BOOLEAN
  67. -- Does `Current' fit in INTEGER_16?
  68. do
  69. Result := Current <= 32767.to_natural_32
  70. ensure
  71. Result = (Current <= 32767.to_natural_32)
  72. end
  73. to_integer_16: INTEGER_16
  74. -- Explicit conversion to INTEGER_16.
  75. require
  76. fit_integer_16
  77. external "built_in"
  78. ensure
  79. Result.to_natural_32 = Current
  80. end
  81. fit_integer_32: BOOLEAN
  82. -- Does `Current' fit in INTEGER_32?
  83. do
  84. Result := Current <= 2147483647.to_natural_32
  85. ensure
  86. Result = (Current <= 2147483647.to_natural_32)
  87. end
  88. to_integer_32: INTEGER_32
  89. -- Explicit conversion to INTEGER_32.
  90. require
  91. fit_integer_32
  92. external "built_in"
  93. ensure
  94. Result.to_natural_32 = Current
  95. end
  96. to_integer_64: INTEGER_64
  97. -- Explicit conversion to INTEGER_64.
  98. external "built_in"
  99. ensure
  100. Result.to_natural_32 = Current
  101. end
  102. fit_real_32: BOOLEAN
  103. -- Does `Current' fit in REAL_32?
  104. do
  105. Result := to_integer_64.fit_real_32
  106. end
  107. to_real_32: REAL_32
  108. -- Explicit conversion to REAL_32.
  109. require
  110. fit_real_32
  111. do
  112. Result := to_integer_64.force_to_real_32
  113. ensure
  114. Result.force_to_natural_32 = Current
  115. end
  116. to_real_64: REAL_64
  117. -- Explicit conversion to REAL_64.
  118. do
  119. Result := to_integer_64.to_real_64
  120. end
  121. feature {ANY}
  122. infix "//" (other: like Current): like Current
  123. require
  124. other /= 0.to_natural_32
  125. external "built_in"
  126. end
  127. infix "\\" (other: like Current): like Current
  128. require
  129. other /= 0.to_natural_32
  130. external "built_in"
  131. end
  132. is_odd: BOOLEAN
  133. do
  134. Result := to_integer_64.is_odd
  135. end
  136. is_even: BOOLEAN
  137. do
  138. Result := to_integer_64.is_even
  139. end
  140. hash_code: INTEGER
  141. do
  142. if fit_integer_32 then
  143. Result := to_integer_32
  144. else
  145. Result := (Current - 2147483648.to_natural_32).to_integer_32
  146. end
  147. end
  148. append_in (buffer: STRING)
  149. do
  150. to_integer_64.append_in(buffer)
  151. end
  152. append_in_unicode (buffer: UNICODE_STRING)
  153. do
  154. to_integer_64.append_in_unicode(buffer)
  155. end
  156. decimal_digit: CHARACTER
  157. require
  158. in_range(0.to_natural_32, 9.to_natural_32)
  159. do
  160. Result := to_integer_8.hexadecimal_digit
  161. end
  162. hexadecimal_digit: CHARACTER
  163. require
  164. in_range(0.to_natural_32, 15.to_natural_32)
  165. do
  166. Result := to_integer_8.hexadecimal_digit
  167. end
  168. to_character: CHARACTER
  169. require
  170. to_integer_16 <= Maximum_character_code
  171. do
  172. Result := to_integer_16.to_character
  173. end
  174. to_number: NUMBER
  175. do
  176. Result := to_integer_64.to_number
  177. end
  178. bit_count: INTEGER_8 32
  179. end -- NATURAL_32
  180. --
  181. -- Copyright (C) 2009-2017: by all the people cited in the AUTHORS file.
  182. --
  183. -- Permission is hereby granted, free of charge, to any person obtaining a copy
  184. -- of this software and associated documentation files (the "Software"), to deal
  185. -- in the Software without restriction, including without limitation the rights
  186. -- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  187. -- copies of the Software, and to permit persons to whom the Software is
  188. -- furnished to do so, subject to the following conditions:
  189. --
  190. -- The above copyright notice and this permission notice shall be included in
  191. -- all copies or substantial portions of the Software.
  192. --
  193. -- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  194. -- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  195. -- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  196. -- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  197. -- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  198. -- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  199. -- THE SOFTWARE.