/[eiffelstudio]/FreeELKS/trunk/library/kernel/integer_64_ref.e
ViewVC logotype

Contents of /FreeELKS/trunk/library/kernel/integer_64_ref.e

Parent Directory Parent Directory | Revision Log Revision Log


Revision 91424 - (show annotations)
Tue Oct 26 18:39:32 2004 UTC (15 years, 2 months ago) by manus_eiffel
File size: 11609 byte(s)
Initial revision

1 indexing
2 description: "References to objects containing an integer value coded on 64 bits"
3 status: "See notice at end of class"
4 date: "$Date$"
5 revision: "$Revision$"
6
7 class
8 INTEGER_64_REF
9
10 inherit
11 NUMERIC
12 rename
13 infix "/" as infix "//"
14 redefine
15 out, is_equal
16 end
17
18 COMPARABLE
19 redefine
20 out, is_equal
21 end
22
23 HASHABLE
24 redefine
25 is_hashable, out, is_equal
26 end
27
28 feature -- Access
29
30 item: INTEGER_64
31 -- Integer value
32
33 hash_code: INTEGER is
34 -- Hash code value
35 do
36 -- Get the positive value of `item' and then do
37 -- a modulo on the maximum INTEGER_32 value.
38 Result := (item & 0x000000007FFFFFFF).to_integer_32
39 end
40
41 sign: INTEGER is
42 -- Sign value (0, -1 or 1)
43 do
44 if item > 0 then
45 Result := 1
46 elseif item < 0 then
47 Result := -1
48 end
49 ensure
50 three_way: Result = three_way_comparison (zero)
51 end
52
53 one: like Current is
54 -- Neutral element for "*" and "/"
55 do
56 create Result
57 Result.set_item (1)
58 end
59
60 zero: like Current is
61 -- Neutral element for "+" and "-"
62 do
63 create Result
64 Result.set_item (0)
65 end
66
67 ascii_char: CHARACTER is
68 -- Returns corresponding ASCII character to `item' value.
69 obsolete
70 "Use to_character instead"
71 require
72 valid_character_code: is_valid_character_code
73 do
74 Result := item.to_character
75 end
76
77 Min_value: INTEGER_64 is -9223372036854775808
78 Max_value: INTEGER_64 is 9223372036854775807
79 -- Minimum and Maximum value hold in `item'.
80
81 feature -- Comparison
82
83 infix "<" (other: like Current): BOOLEAN is
84 -- Is current integer less than `other'?
85 do
86 Result := item < other.item
87 end
88
89 is_equal (other: like Current): BOOLEAN is
90 -- Is `other' attached to an object of the same type
91 -- as current object and identical to it?
92 do
93 Result := other.item = item
94 end
95
96 feature -- Element change
97
98 set_item (i: INTEGER_64) is
99 -- Make `i' the `item' value.
100 do
101 item := i
102 ensure
103 item_set: item = i
104 end
105
106 feature -- Status report
107
108 divisible (other: INTEGER_64_REF): BOOLEAN is
109 -- May current object be divided by `other'?
110 do
111 Result := other.item /= 0
112 ensure then
113 value: Result = (other.item /= 0)
114 end
115
116 exponentiable (other: NUMERIC): BOOLEAN is
117 -- May current object be elevated to the power `other'?
118 local
119 integer_value: INTEGER_REF
120 double_value: DOUBLE_REF
121 real_value: REAL_REF
122 do
123 integer_value ?= other
124 real_value ?= other
125 double_value ?= other
126 if integer_value /= Void then
127 Result := integer_value.item >= 0 or item /= 0
128 elseif real_value /= Void then
129 Result := real_value.item >= 0.0 or item /= 0
130 elseif double_value /= Void then
131 Result := double_value.item >= 0.0 or item /= 0
132 end
133 ensure then
134 safe_values: ((other.conforms_to (0) and item /= 0) or
135 (other.conforms_to (0.0) and item > 0)) implies Result
136 end
137
138 is_hashable: BOOLEAN is
139 -- May current object be hashed?
140 -- (True if it is not its type's default.)
141 do
142 Result := item /= 0
143 end
144
145 is_valid_character_code: BOOLEAN is
146 -- Does current object represent a character?
147 do
148 Result := item >= feature {CHARACTER}.Min_value and item <= feature {CHARACTER}.Max_value
149 end
150
151 feature -- Basic operations
152
153 abs: INTEGER_64 is
154 -- Absolute value
155 do
156 Result := abs_ref.item
157 ensure
158 non_negative: Result >= 0
159 same_absolute_value: (Result = item) or (Result = -item)
160 end
161
162 infix "+" (other: like Current): like Current is
163 -- Sum with `other'
164 do
165 create Result
166 Result.set_item (item + other.item)
167 end
168
169 infix "-" (other: like Current): like Current is
170 -- Result of subtracting `other'
171 do
172 create Result
173 Result.set_item (item - other.item)
174 end
175
176 infix "*" (other: like Current): like Current is
177 -- Product by `other'
178 do
179 create Result
180 Result.set_item (item * other.item)
181 end
182
183 infix "/" (other: like Current): DOUBLE is
184 -- Division by `other'
185 require
186 other_exists: other /= Void
187 good_divisor: divisible (other)
188 do
189 Result := item / other.item
190 end
191
192 prefix "+": like Current is
193 -- Unary plus
194 do
195 create Result
196 Result.set_item (+ item)
197 end
198
199 prefix "-": like Current is
200 -- Unary minus
201 do
202 create Result
203 Result.set_item (- item)
204 end
205
206 infix "//" (other: like Current): like Current is
207 -- Integer division of Current by `other'
208 do
209 create Result
210 Result.set_item (item // other.item)
211 end
212
213 infix "\\" (other: like Current): like Current is
214 -- Remainder of the integer division of Current by `other'
215 require
216 other_exists: other /= Void
217 good_divisor: divisible (other)
218 do
219 create Result
220 Result.set_item (item \\ other.item)
221 ensure
222 result_exists: Result /= Void
223 end
224
225 infix "^" (other: DOUBLE): DOUBLE is
226 -- Integer power of Current by `other'
227 do
228 Result := item ^ other
229 end
230
231 feature {NONE} -- Initialization
232
233 make_from_reference (v: INTEGER_64_REF) is
234 -- Initialize `Current' with `v.item'.
235 require
236 v_not_void: v /= Void
237 do
238 item := v.item
239 ensure
240 item_set: item = v.item
241 end
242
243 feature -- Conversion
244
245 to_reference: INTEGER_64_REF is
246 -- Associated reference of Current
247 do
248 create Result
249 Result.set_item (item)
250 ensure
251 to_reference_not_void: Result /= Void
252 end
253
254 frozen to_boolean: BOOLEAN is
255 -- True if not `zero'.
256 do
257 Result := item /= 0
258 end
259
260 frozen to_integer_8: INTEGER_8 is
261 -- Convert `item' into an INTEGER_8 value.
262 require
263 not_too_small: item >= feature {INTEGER_8}.Min_value
264 not_too_big: item <= feature {INTEGER_8}.Max_value
265 do
266 Result := item.to_integer_8
267 end
268
269 frozen to_integer_16: INTEGER_16 is
270 -- Convert `item' into an INTEGER_16 value.
271 require
272 not_too_small: item >= feature {INTEGER_16}.Min_value
273 not_too_big: item <= feature {INTEGER_16}.Max_value
274 do
275 Result := item.to_integer_16
276 end
277
278 frozen to_integer, frozen to_integer_32: INTEGER is
279 -- Convert `item' into an INTEGER_32 value.
280 require
281 not_too_small: item >= feature {INTEGER}.Min_value
282 not_too_big: item <= feature {INTEGER}.Max_value
283 do
284 Result := item.to_integer
285 end
286
287 frozen to_integer_64: INTEGER_64 is
288 -- Return `item'.
289 do
290 Result := item
291 end
292
293 frozen to_real: REAL is
294 -- Convert `item' into a REAL
295 do
296 Result := item.to_real
297 end
298
299 frozen to_double: DOUBLE is
300 -- Convert `item' into a DOUBLE
301 do
302 Result := item.to_double
303 end
304
305 to_hex_string: STRING is
306 -- Convert `item' into an hexadecimal string.
307 local
308 i: INTEGER
309 val: INTEGER_64
310 a_digit: INTEGER
311 do
312 from
313 i := (create {PLATFORM}).Integer_64_bits // 4
314 create Result.make (i)
315 Result.fill_blank
316 val := item
317 until
318 i = 0
319 loop
320 a_digit := (val & 0x0F).to_integer
321 Result.put (a_digit.to_hex_character, i)
322 val := val |>> 4
323 i := i - 1
324 end
325 ensure
326 Result_not_void: Result /= Void
327 Result_valid_count: Result.count = (create {PLATFORM}).Integer_64_bits // 4
328 end
329
330 to_hex_character: CHARACTER is
331 -- Convert `item' into an hexadecimal character.
332 require
333 in_bounds: 0 <= item and item <= 15
334 local
335 tmp: INTEGER
336 do
337 tmp := item.to_integer
338 if tmp <= 9 then
339 Result := (tmp + ('0').code).to_character
340 else
341 Result := (('A').code + (tmp - 10)).to_character
342 end
343 ensure
344 valid_character: ("0123456789ABCDEF").has (Result)
345 end
346
347 frozen to_character: CHARACTER is
348 -- Returns corresponding ASCII character to `item' value.
349 require
350 valid_character: is_valid_character_code
351 do
352 Result := item.to_character
353 end
354
355 feature -- Bit operations
356
357 frozen infix "&", frozen bit_and (i: like Current): like Current is
358 -- Bitwise and between Current' and `i'.
359 require
360 i_not_void: i /= Void
361 do
362 create Result
363 Result.set_item (item & i.item)
364 ensure
365 bitwise_and_not_void: Result /= Void
366 end
367
368 frozen infix "|", frozen bit_or (i: like Current): like Current is
369 -- Bitwise or between Current' and `i'.
370 require
371 i_not_void: i /= Void
372 do
373 create Result
374 Result.set_item (item | i.item)
375 ensure
376 bitwise_or_not_void: Result /= Void
377 end
378
379 frozen bit_xor (i: like Current): like Current is
380 -- Bitwise xor between Current' and `i'.
381 require
382 i_not_void: i /= Void
383 do
384 create Result
385 Result.set_item (item.bit_xor (i.item))
386 ensure
387 bitwise_xor_not_void: Result /= Void
388 end
389
390 frozen bit_not: like Current is
391 -- One's complement of Current.
392 do
393 create Result
394 Result.set_item (item.bit_not)
395 ensure
396 bit_not_not_void: Result /= Void
397 end
398
399 frozen bit_shift (n: INTEGER): like Current is
400 -- Shift Current from `n' position to right if `n' positive,
401 -- to left otherwise.
402 require
403 n_less_or_equal_to_64: n <= 64
404 n_greater_or_equal_to_minus_64: n >= -64
405 do
406 if n > 0 then
407 Result := bit_shift_right (n)
408 else
409 Result := bit_shift_left (- n)
410 end
411 ensure
412 bit_shift_not_void: Result /= Void
413 end
414
415 frozen infix "|<<", frozen bit_shift_left (n: INTEGER): like Current is
416 -- Shift Current from `n' position to left.
417 require
418 n_nonnegative: n >= 0
419 n_less_or_equal_to_64: n <= 64
420 do
421 create Result
422 Result.set_item (item |<< n)
423 ensure
424 bit_shift_left_not_void: Result /= Void
425 end
426
427 frozen infix "|>>", frozen bit_shift_right (n: INTEGER): like Current is
428 -- Shift Current from `n' position to right.
429 require
430 n_nonnegative: n >= 0
431 n_less_or_equal_to_64: n <= 64
432 do
433 create Result
434 Result.set_item (item |>> n)
435 ensure
436 bit_shift_right_not_void: Result /= Void
437 end
438
439 frozen bit_test (n: INTEGER): BOOLEAN is
440 -- Test `n'-th position of Current.
441 require
442 n_nonnegative: n >= 0
443 n_less_than_64: n < 64
444 do
445 Result := item & ((1).to_integer_64 |<< n) /= 0
446 end
447
448 frozen set_bit (b: BOOLEAN; n: INTEGER): INTEGER_64 is
449 -- Copy of current with `n'-th position
450 -- set to 1 if `b', 0 otherwise.
451 require
452 n_nonnegative: n >= 0
453 n_less_than_64: n < 64
454 do
455 if b then
456 Result := item | ((1).to_integer_64 |<< n)
457 else
458 Result := item & ((1).to_integer_64 |<< n).bit_not
459 end
460 end
461
462 frozen set_bit_with_mask (b: BOOLEAN; m: INTEGER_64): INTEGER_64 is
463 -- Copy of current with all 1 bits of m set to 1
464 -- if `b', 0 otherwise.
465 do
466 if b then
467 Result := item | m
468 else
469 Result := item & m.bit_not
470 end
471 end
472
473 feature -- Output
474
475 out: STRING is
476 -- Printable representation of integer value
477 do
478 Result := item.out
479 end
480
481 feature {NONE} -- Implementation
482
483 abs_ref: INTEGER_64_REF is
484 -- Absolute value
485 do
486 if item >= 0 then
487 Result := Current
488 else
489 Result := -Current
490 end
491 ensure
492 result_exists: Result /= Void
493 same_absolute_value: equal (Result, Current) or equal (Result, - Current)
494 end
495
496 invariant
497
498 sign_times_abs: sign * abs = item
499
500 indexing
501
502 library: "[
503 EiffelBase: Library of reusable components for Eiffel.
504 ]"
505
506 status: "[
507 Copyright 1986-2001 Interactive Software Engineering (ISE).
508 For ISE customers the original versions are an ISE product
509 covered by the ISE Eiffel license and support agreements.
510 ]"
511
512 license: "[
513 EiffelBase may now be used by anyone as FREE SOFTWARE to
514 develop any product, public-domain or commercial, without
515 payment to ISE, under the terms of the ISE Free Eiffel Library
516 License (IFELL) at http://eiffel.com/products/base/license.html.
517 ]"
518
519 source: "[
520 Interactive Software Engineering Inc.
521 ISE Building
522 360 Storke Road, Goleta, CA 93117 USA
523 Telephone 805-685-1006, Fax 805-685-6869
524 Electronic mail <info@eiffel.com>
525 Customer support http://support.eiffel.com
526 ]"
527
528 info: "[
529 For latest info see award-winning pages: http://eiffel.com
530 ]"
531
532 end -- class INTEGER_64_REF
533
534
535

Properties

Name Value
svn:eol-style native
svn:keywords Author Date Id Revision

  ViewVC Help
Powered by ViewVC 1.1.23