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