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

Annotation of /FreeELKS/trunk/library/kernel/integer_32_ref.e

Parent Directory Parent Directory | Revision Log Revision Log


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

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.23