/src/lib/numeric/natural_64.e

http://github.com/tybor/Liberty · Specman e · 320 lines · 241 code · 33 blank · 46 comment · 4 complexity · 771d37d84fcc2c855433abb818aad49d 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_64
  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_64
  14. ensure
  15. Result = (Current <= 255.to_natural_64)
  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_64 = Current
  24. end
  25. fit_natural_16: BOOLEAN
  26. -- Does `Current' fit in NATURAL_16?
  27. do
  28. Result := Current <= 65535.to_natural_64
  29. ensure
  30. Result = (Current <= 65535.to_natural_64)
  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_64 = Current
  39. end
  40. fit_natural_32: BOOLEAN
  41. -- Does `Current' fit in NATURAL_32?
  42. do
  43. Result := Current <= 2147483647.to_natural_64
  44. ensure
  45. Result = (Current <= 2147483647.to_natural_64)
  46. end
  47. to_natural_32: NATURAL_32
  48. -- Explicit conversion to NATURAL_32.
  49. require
  50. fit_natural_32
  51. external "built_in"
  52. ensure
  53. Result.to_natural_64 = Current
  54. end
  55. fit_integer_8: BOOLEAN
  56. -- Does `Current' fit in INTEGER_8?
  57. do
  58. Result := Current <= 127.to_natural_64
  59. ensure
  60. Result = (Current <= 127.to_natural_64)
  61. end
  62. to_integer_8: INTEGER_8
  63. -- Explicit conversion to INTEGER_8.
  64. require
  65. fit_integer_8
  66. external "built_in"
  67. ensure
  68. Result.to_natural_64 = Current
  69. end
  70. fit_integer_16: BOOLEAN
  71. -- Does `Current' fit in INTEGER_16?
  72. do
  73. Result := Current <= 32767.to_natural_64
  74. ensure
  75. Result = (Current <= 32767.to_natural_64)
  76. end
  77. to_integer_16: INTEGER_16
  78. -- Explicit conversion to INTEGER_16.
  79. require
  80. fit_integer_16
  81. external "built_in"
  82. ensure
  83. Result.to_natural_64 = Current
  84. end
  85. fit_integer_32: BOOLEAN
  86. -- Does `Current' fit in INTEGER_32?
  87. do
  88. Result := Current <= 2147483647.to_natural_64
  89. ensure
  90. Result = (Current <= 2147483647.to_natural_64)
  91. end
  92. to_integer_32: INTEGER_32
  93. -- Explicit conversion to INTEGER_32.
  94. require
  95. fit_integer_32
  96. external "built_in"
  97. ensure
  98. Result.to_natural_64 = Current
  99. end
  100. fit_integer_64: BOOLEAN
  101. -- Does `Current' fit in INTEGER_64?
  102. do
  103. Result := Current <= 9223372036854775807.to_natural_64
  104. ensure
  105. Result = (Current <= 9223372036854775807.to_natural_64)
  106. end
  107. to_integer_64: INTEGER_64
  108. -- Explicit conversion to INTEGER_64.
  109. require
  110. fit_integer_64
  111. external "built_in"
  112. ensure
  113. Result.to_natural_64 = Current
  114. end
  115. fit_real_32: BOOLEAN
  116. -- Does `Current' fit in REAL_32?
  117. do
  118. Result := fit_natural_32 and then to_natural_32.fit_real_32
  119. end
  120. to_real_32: REAL_32
  121. -- Explicit conversion to REAL_32.
  122. require
  123. fit_real_32
  124. do
  125. Result := to_integer_64.force_to_real_32
  126. ensure
  127. Result.force_to_natural_64 = Current
  128. end
  129. fit_real_64: BOOLEAN
  130. -- Does `Current' fit in REAL_64?
  131. do
  132. Result := natural_64_fit_real_64(Current)
  133. end
  134. to_real_64: REAL_64
  135. -- Explicit conversion to REAL_64.
  136. require
  137. fit_real_64
  138. do
  139. Result := to_integer_64.to_real_64
  140. ensure
  141. Result.force_to_natural_64 = Current
  142. end
  143. feature {ANY}
  144. infix "//" (other: like Current): like Current
  145. require
  146. other /= 0.to_natural_64
  147. external "built_in"
  148. end
  149. infix "\\" (other: like Current): like Current
  150. require
  151. other /= 0.to_natural_64
  152. external "built_in"
  153. end
  154. is_odd: BOOLEAN
  155. do
  156. Result := (Current #\\ 2.to_natural_64) = 1.to_natural_64
  157. end
  158. is_even: BOOLEAN
  159. do
  160. Result := (Current #\\ 2.to_natural_64) = 0.to_natural_64
  161. end
  162. hash_code: INTEGER
  163. do
  164. if Current.fit_integer_64 then
  165. Result := to_integer_64.hash_code
  166. else
  167. Result := ((Current - 1.to_natural_64) // 2.to_natural_64).hash_code
  168. end
  169. end
  170. append_in (buffer: STRING)
  171. local
  172. val: like Current; i, idx: INTEGER
  173. do
  174. if Current = 0.to_natural_64 then
  175. buffer.extend('0')
  176. else
  177. from
  178. val := Current
  179. -- Save the position of first character in the buffer.
  180. i := buffer.count + 1
  181. until
  182. val = 0.to_natural_64
  183. loop
  184. buffer.extend((val #\\ 10.to_natural_64).decimal_digit)
  185. val := val #// 10.to_natural_64
  186. end
  187. -- Change character order.
  188. from
  189. idx := buffer.count
  190. until
  191. i >= idx
  192. loop
  193. buffer.swap(i, idx)
  194. idx := idx - 1
  195. i := i + 1
  196. end
  197. end
  198. end
  199. append_in_unicode (buffer: UNICODE_STRING)
  200. local
  201. val: like Current; i, idx: INTEGER
  202. do
  203. if Current = 0.to_natural_64 then
  204. buffer.extend('0'.code)
  205. else
  206. from
  207. val := Current
  208. -- Save the position of first character in the buffer.
  209. i := buffer.count + 1
  210. until
  211. val = 0.to_natural_64
  212. loop
  213. buffer.extend((val #\\ 10.to_natural_64).decimal_digit.code)
  214. val := val #// 10.to_natural_64
  215. end
  216. -- Change character order.
  217. from
  218. idx := buffer.count
  219. until
  220. i >= idx
  221. loop
  222. buffer.swap(i, idx)
  223. idx := idx - 1
  224. i := i + 1
  225. end
  226. end
  227. end
  228. decimal_digit: CHARACTER
  229. require
  230. in_range(0.to_natural_64, 9.to_natural_64)
  231. do
  232. Result := to_integer_8.hexadecimal_digit
  233. end
  234. hexadecimal_digit: CHARACTER
  235. require
  236. in_range(0.to_natural_64, 15.to_natural_64)
  237. do
  238. Result := to_integer_8.hexadecimal_digit
  239. end
  240. to_character: CHARACTER
  241. require
  242. to_integer_16 <= Maximum_character_code
  243. do
  244. Result := to_integer_16.to_character
  245. end
  246. to_number: NUMBER
  247. do
  248. -- Well, there is probably a better way, but this should work:
  249. if Current.fit_integer_64 then
  250. Result := to_integer_64.to_number
  251. elseif is_even then
  252. Result := (Current // 2.to_natural_64).to_integer_64.to_number @* 2
  253. else
  254. Result := (Current - 1.to_natural_64).to_number @+ 1
  255. end
  256. end
  257. bit_count: INTEGER_8 64
  258. feature {}
  259. natural_64_fit_real_64 (natural_64: NATURAL_64): BOOLEAN
  260. external "plug_in"
  261. alias "{
  262. location: "${sys}/runtime"
  263. module_name: "natural_fit_real"
  264. feature_name: "natural_64_fit_real_64"
  265. }"
  266. end
  267. end -- NATURAL_64
  268. --
  269. -- Copyright (C) 2009-2017: by all the people cited in the AUTHORS file.
  270. --
  271. -- Permission is hereby granted, free of charge, to any person obtaining a copy
  272. -- of this software and associated documentation files (the "Software"), to deal
  273. -- in the Software without restriction, including without limitation the rights
  274. -- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  275. -- copies of the Software, and to permit persons to whom the Software is
  276. -- furnished to do so, subject to the following conditions:
  277. --
  278. -- The above copyright notice and this permission notice shall be included in
  279. -- all copies or substantial portions of the Software.
  280. --
  281. -- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  282. -- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  283. -- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  284. -- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  285. -- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  286. -- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  287. -- THE SOFTWARE.