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

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

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.23