/[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 91477 - (show annotations)
Sun Jan 14 09:47:13 2007 UTC (13 years ago) by ericb
File size: 14253 byte(s)
Synchronized with ISE 6.0.65740
1 indexing
2 description: "References to objects containing an integer value coded on 64 bits"
3 library: "Free implementation of ELKS library"
4 copyright: "Copyright (c) 1986-2005, Eiffel Software and others"
5 license: "Eiffel Forum License v2 (see forum.txt)"
6 date: "$Date$"
7 revision: "$Revision$"
8
9 class
10 INTEGER_64_REF
11
12 inherit
13 NUMERIC
14 rename
15 infix "/" as infix "//"
16 redefine
17 out, is_equal
18 end
19
20 COMPARABLE
21 redefine
22 out, is_equal
23 end
24
25 HASHABLE
26 redefine
27 is_hashable, out, is_equal
28 end
29
30 feature -- Access
31
32 item: INTEGER_64 is
33 -- Integer value
34 external
35 "built_in"
36 end
37
38 hash_code: INTEGER is
39 -- Hash code value
40 do
41 -- Get the positive value of `item' and then do
42 -- a modulo on the maximum INTEGER_32 value.
43 Result := (item & 0x000000007FFFFFFF).to_integer_32
44 end
45
46 sign: INTEGER is
47 -- Sign value (0, -1 or 1)
48 do
49 if item > 0 then
50 Result := 1
51 elseif item < 0 then
52 Result := -1
53 end
54 ensure
55 three_way: Result = three_way_comparison (zero)
56 end
57
58 one: like Current is
59 -- Neutral element for "*" and "/"
60 do
61 create Result
62 Result.set_item (1)
63 end
64
65 zero: like Current is
66 -- Neutral element for "+" and "-"
67 do
68 create Result
69 Result.set_item (0)
70 end
71
72 ascii_char: CHARACTER is
73 -- Returns corresponding ASCII character to `item' value.
74 obsolete
75 "Use to_character_8 instead"
76 require
77 valid_character_code: is_valid_character_8_code
78 do
79 Result := item.to_character_8
80 end
81
82 Min_value: INTEGER_64 is -9223372036854775808
83 Max_value: INTEGER_64 is 9223372036854775807
84 -- Minimum and Maximum value hold in `item'.
85
86 feature -- Comparison
87
88 infix "<" (other: like Current): BOOLEAN is
89 -- Is current integer less than `other'?
90 do
91 Result := item < other.item
92 end
93
94 is_equal (other: like Current): BOOLEAN is
95 -- Is `other' attached to an object of the same type
96 -- as current object and identical to it?
97 do
98 Result := other.item = item
99 end
100
101 feature -- Element change
102
103 set_item (i: INTEGER_64) is
104 -- Make `i' the `item' value.
105 external
106 "built_in"
107 ensure
108 item_set: item = i
109 end
110
111 feature -- Status report
112
113 divisible (other: like Current): BOOLEAN is
114 -- May current object be divided by `other'?
115 do
116 Result := other.item /= 0
117 ensure then
118 value: Result = (other.item /= 0)
119 end
120
121 exponentiable (other: NUMERIC): BOOLEAN is
122 -- May current object be elevated to the power `other'?
123 local
124 integer_value: INTEGER_REF
125 double_value: DOUBLE_REF
126 real_value: REAL_REF
127 do
128 integer_value ?= other
129 real_value ?= other
130 double_value ?= other
131 if integer_value /= Void then
132 Result := integer_value.item >= 0 or item /= 0
133 elseif real_value /= Void then
134 Result := real_value.item >= 0.0 or item /= 0
135 elseif double_value /= Void then
136 Result := double_value.item >= 0.0 or item /= 0
137 end
138 ensure then
139 safe_values: ((other.conforms_to (0) and item /= 0) or
140 (other.conforms_to (0.0) and item > 0)) implies Result
141 end
142
143 is_hashable: BOOLEAN is
144 -- May current object be hashed?
145 -- (True if it is not its type's default.)
146 do
147 Result := item /= 0
148 end
149
150 is_valid_character_code: BOOLEAN is
151 -- Does current object represent a CHARACTER_8?
152 obsolete
153 "Use `is_valid_character_8_code' instead."
154 do
155 Result := is_valid_character_8_code
156 end
157
158 is_valid_character_8_code: BOOLEAN is
159 -- Does current object represent a CHARACTER_8?
160 do
161 Result := item >= {CHARACTER_8}.Min_value and
162 item <= {CHARACTER_8}.Max_value
163 end
164
165 is_valid_character_32_code: BOOLEAN is
166 -- Does current object represent a character?
167 do
168 Result := item >= 0 and then
169 item.to_natural_64 >= {CHARACTER_32}.Min_value and
170 item.to_natural_64 <= {CHARACTER_32}.Max_value
171 end
172
173 feature -- Basic operations
174
175 abs: INTEGER_64 is
176 -- Absolute value
177 do
178 Result := abs_ref.item
179 ensure
180 non_negative: Result >= 0
181 same_absolute_value: (Result = item) or (Result = -item)
182 end
183
184 infix "+" (other: like Current): like Current is
185 -- Sum with `other'
186 do
187 create Result
188 Result.set_item (item + other.item)
189 end
190
191 infix "-" (other: like Current): like Current is
192 -- Result of subtracting `other'
193 do
194 create Result
195 Result.set_item (item - other.item)
196 end
197
198 infix "*" (other: like Current): like Current is
199 -- Product by `other'
200 do
201 create Result
202 Result.set_item (item * other.item)
203 end
204
205 infix "/" (other: like Current): DOUBLE is
206 -- Division by `other'
207 require
208 other_exists: other /= Void
209 good_divisor: divisible (other)
210 do
211 Result := item / other.item
212 end
213
214 prefix "+": like Current is
215 -- Unary plus
216 do
217 create Result
218 Result.set_item (+ item)
219 end
220
221 prefix "-": like Current is
222 -- Unary minus
223 do
224 create Result
225 Result.set_item (- item)
226 end
227
228 infix "//" (other: like Current): like Current is
229 -- Integer division of Current by `other'
230 do
231 create Result
232 Result.set_item (item // other.item)
233 end
234
235 infix "\\" (other: like Current): like Current is
236 -- Remainder of the integer division of Current by `other'
237 require
238 other_exists: other /= Void
239 good_divisor: divisible (other)
240 do
241 create Result
242 Result.set_item (item \\ other.item)
243 ensure
244 result_exists: Result /= Void
245 end
246
247 infix "^" (other: DOUBLE): DOUBLE is
248 -- Integer power of Current by `other'
249 do
250 Result := item ^ other
251 end
252
253 feature {NONE} -- Initialization
254
255 make_from_reference (v: INTEGER_64_REF) is
256 -- Initialize `Current' with `v.item'.
257 require
258 v_not_void: v /= Void
259 do
260 set_item (v.item)
261 ensure
262 item_set: item = v.item
263 end
264
265 feature -- Conversion
266
267 to_reference: INTEGER_64_REF is
268 -- Associated reference of Current
269 do
270 create Result
271 Result.set_item (item)
272 ensure
273 to_reference_not_void: Result /= Void
274 end
275
276 frozen to_boolean: BOOLEAN is
277 -- True if not `zero'.
278 do
279 Result := item /= 0
280 end
281
282 as_natural_8: NATURAL_8 is
283 -- Convert `item' into an NATURAL_8 value.
284 do
285 Result := item.as_natural_8
286 end
287
288 as_natural_16: NATURAL_16 is
289 -- Convert `item' into an NATURAL_16 value.
290 do
291 Result := item.as_natural_16
292 end
293
294 as_natural_32: NATURAL_32 is
295 -- Convert `item' into an NATURAL_32 value.
296 do
297 Result := item.as_natural_32
298 end
299
300 as_natural_64: NATURAL_64 is
301 -- Convert `item' into an NATURAL_64 value.
302 do
303 Result := item.as_natural_64
304 end
305
306 as_integer_8: INTEGER_8 is
307 -- Convert `item' into an INTEGER_8 value.
308 do
309 Result := item.as_integer_8
310 end
311
312 as_integer_16: INTEGER_16 is
313 -- Convert `item' into an INTEGER_16 value.
314 do
315 Result := item.as_integer_16
316 end
317
318 as_integer_32: INTEGER is
319 -- Convert `item' into an INTEGER_32 value.
320 do
321 Result := item.as_integer_32
322 end
323
324 as_integer_64: INTEGER_64 is
325 -- Convert `item' into an INTEGER_64 value.
326 do
327 Result := item.as_integer_64
328 end
329
330 frozen to_natural_8: NATURAL_8 is
331 -- Convert `item' into an NATURAL_8 value.
332 require
333 item_non_negative: item >= 0
334 not_too_big: item <= {NATURAL_8}.Max_value
335 do
336 Result := as_natural_8
337 end
338
339 frozen to_natural_16: NATURAL_16 is
340 -- Convert `item' into an NATURAL_16 value.
341 require
342 item_non_negative: item >= 0
343 not_too_big: item <= {NATURAL_16}.Max_value
344 do
345 Result := as_natural_16
346 end
347
348 frozen to_natural_32: NATURAL_32 is
349 -- Convert `item' into an NATURAL_32 value.
350 require
351 item_non_negative: item >= 0
352 not_too_big: item <= {NATURAL_32}.Max_value
353 do
354 Result := as_natural_32
355 end
356
357 frozen to_natural_64: NATURAL_64 is
358 -- Convert `item' into an NATURAL_64 value.
359 require
360 item_non_negative: item >= 0
361 do
362 Result := as_natural_64
363 end
364
365 frozen to_integer_8: INTEGER_8 is
366 -- Convert `item' into an INTEGER_8 value.
367 require
368 not_too_small: item >= {INTEGER_8}.Min_value
369 not_too_big: item <= {INTEGER_8}.Max_value
370 do
371 Result := as_integer_8
372 end
373
374 frozen to_integer_16: INTEGER_16 is
375 -- Convert `item' into an INTEGER_16 value.
376 require
377 not_too_small: item >= {INTEGER_16}.Min_value
378 not_too_big: item <= {INTEGER_16}.Max_value
379 do
380 Result := as_integer_16
381 end
382
383 frozen to_integer, frozen to_integer_32: INTEGER is
384 -- Convert `item' into an INTEGER_32 value.
385 require
386 not_too_small: item >= {INTEGER}.Min_value
387 not_too_big: item <= {INTEGER}.Max_value
388 do
389 Result := as_integer_32
390 end
391
392 frozen to_integer_64: INTEGER_64 is
393 -- Return `item'.
394 do
395 Result := item
396 end
397
398 to_real: REAL is
399 -- Convert `item' into a REAL
400 do
401 Result := item.to_real
402 end
403
404 to_double: DOUBLE is
405 -- Convert `item' into a DOUBLE
406 do
407 Result := item.to_double
408 end
409
410 to_hex_string: STRING is
411 -- Convert `item' into an hexadecimal string.
412 local
413 i: INTEGER
414 val: INTEGER_64
415 a_digit: INTEGER
416 do
417 from
418 i := (create {PLATFORM}).Integer_64_bits // 4
419 create Result.make (i)
420 Result.fill_blank
421 val := item
422 until
423 i = 0
424 loop
425 a_digit := (val & 0x0F).to_integer
426 Result.put (a_digit.to_hex_character, i)
427 val := val |>> 4
428 i := i - 1
429 end
430 ensure
431 Result_not_void: Result /= Void
432 Result_valid_count: Result.count = (create {PLATFORM}).Integer_64_bits // 4
433 end
434
435 to_hex_character: CHARACTER is
436 -- Convert `item' into an hexadecimal character.
437 require
438 in_bounds: 0 <= item and item <= 15
439 local
440 tmp: INTEGER
441 do
442 tmp := item.to_integer
443 if tmp <= 9 then
444 Result := (tmp + ('0').code).to_character_8
445 else
446 Result := (('A').code + (tmp - 10)).to_character_8
447 end
448 ensure
449 valid_character: ("0123456789ABCDEF").has (Result)
450 end
451
452 to_character: CHARACTER is
453 -- Returns corresponding ASCII character to `item' value.
454 obsolete
455 "Use `to_character_8' instead."
456 require
457 valid_character: is_valid_character_8_code
458 do
459 Result := item.to_character_8
460 end
461
462 to_character_8: CHARACTER_8 is
463 -- Associated character in 8 bit version.
464 require
465 valid_character: is_valid_character_8_code
466 do
467 Result := item.to_character_8
468 end
469
470 to_character_32: CHARACTER_32 is
471 -- Associated character in 32 bit version.
472 require
473 valid_character: is_valid_character_32_code
474 do
475 Result := item.to_character_32
476 end
477
478 feature -- Bit operations
479
480 bit_and (i: like Current): like Current is
481 -- Bitwise and between Current' and `i'.
482 require
483 i_not_void: i /= Void
484 do
485 create Result
486 Result.set_item (item.bit_and (i.item))
487 ensure
488 bitwise_and_not_void: Result /= Void
489 end
490
491 frozen infix "&" (i: like Current): like Current is
492 -- Bitwise and between Current' and `i'.
493 require
494 i_not_void: i /= Void
495 do
496 Result := bit_and (i)
497 ensure
498 bitwise_and_not_void: Result /= Void
499 end
500
501 bit_or (i: like Current): like Current is
502 -- Bitwise or between Current' and `i'.
503 require
504 i_not_void: i /= Void
505 do
506 create Result
507 Result.set_item (item.bit_or (i.item))
508 ensure
509 bitwise_or_not_void: Result /= Void
510 end
511
512 frozen infix "|" (i: like Current): like Current is
513 -- Bitwise or between Current' and `i'.
514 require
515 i_not_void: i /= Void
516 do
517 Result := bit_or (i)
518 ensure
519 bitwise_or_not_void: Result /= Void
520 end
521
522 bit_xor (i: like Current): like Current is
523 -- Bitwise xor between Current' and `i'.
524 require
525 i_not_void: i /= Void
526 do
527 create Result
528 Result.set_item (item.bit_xor (i.item))
529 ensure
530 bitwise_xor_not_void: Result /= Void
531 end
532
533 bit_not: like Current is
534 -- One's complement of Current.
535 do
536 create Result
537 Result.set_item (item.bit_not)
538 ensure
539 bit_not_not_void: Result /= Void
540 end
541
542 frozen bit_shift (n: INTEGER): INTEGER_64 is
543 -- Shift Current from `n' position to right if `n' positive,
544 -- to left otherwise.
545 require
546 n_less_or_equal_to_64: n <= 64
547 n_greater_or_equal_to_minus_64: n >= -64
548 do
549 if n > 0 then
550 Result := bit_shift_right (n)
551 else
552 Result := bit_shift_left (- n)
553 end
554 end
555
556 bit_shift_left (n: INTEGER): like Current is
557 -- Shift Current from `n' position to left.
558 require
559 n_nonnegative: n >= 0
560 n_less_or_equal_to_64: n <= 64
561 do
562 create Result
563 Result.set_item (item.bit_shift_left (n))
564 ensure
565 bit_shift_left_not_void: Result /= Void
566 end
567
568 frozen infix "|<<" (n: INTEGER): like Current is
569 -- Shift Current from `n' position to left.
570 require
571 n_nonnegative: n >= 0
572 n_less_or_equal_to_64: n <= 64
573 do
574 Result := bit_shift_left (n)
575 ensure
576 bit_shift_left_not_void: Result /= Void
577 end
578
579 bit_shift_right (n: INTEGER): like Current is
580 -- Shift Current from `n' position to right.
581 require
582 n_nonnegative: n >= 0
583 n_less_or_equal_to_64: n <= 64
584 do
585 create Result
586 Result.set_item (item.bit_shift_right (n))
587 ensure
588 bit_shift_right_not_void: Result /= Void
589 end
590
591 frozen infix "|>>" (n: INTEGER): like Current is
592 -- Shift Current from `n' position to right.
593 require
594 n_nonnegative: n >= 0
595 n_less_or_equal_to_64: n <= 64
596 do
597 Result := bit_shift_right (n)
598 ensure
599 bit_shift_right_not_void: Result /= Void
600 end
601
602 frozen bit_test (n: INTEGER): BOOLEAN is
603 -- Test `n'-th position of Current.
604 require
605 n_nonnegative: n >= 0
606 n_less_than_64: n < 64
607 do
608 Result := item & ((1).to_integer_64 |<< n) /= 0
609 end
610
611 frozen set_bit (b: BOOLEAN; n: INTEGER): INTEGER_64 is
612 -- Copy of current with `n'-th position
613 -- set to 1 if `b', 0 otherwise.
614 require
615 n_nonnegative: n >= 0
616 n_less_than_64: n < 64
617 do
618 if b then
619 Result := item | ((1).to_integer_64 |<< n)
620 else
621 Result := item & ((1).to_integer_64 |<< n).bit_not
622 end
623 end
624
625 frozen set_bit_with_mask (b: BOOLEAN; m: INTEGER_64): INTEGER_64 is
626 -- Copy of current with all 1 bits of m set to 1
627 -- if `b', 0 otherwise.
628 do
629 if b then
630 Result := item | m
631 else
632 Result := item & m.bit_not
633 end
634 end
635
636 feature -- Output
637
638 out: STRING is
639 -- Printable representation of integer value
640 do
641 create Result.make (20)
642 Result.append_integer_64 (item)
643 end
644
645 feature {NONE} -- Implementation
646
647 abs_ref: like Current is
648 -- Absolute value
649 do
650 if item >= 0 then
651 Result := Current
652 else
653 Result := -Current
654 end
655 ensure
656 result_exists: Result /= Void
657 same_absolute_value: equal (Result, Current) or equal (Result, - Current)
658 end
659
660 invariant
661
662 sign_times_abs: sign * abs = item
663
664 end

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.23