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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 91457 - (show annotations)
Thu Feb 9 11:21:18 2006 UTC (13 years, 11 months ago) by ericb
File size: 13189 byte(s)
Fixed flat Degree 3 error.

1 indexing
2 description: "References to objects containing an integer value coded on 32 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_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 is
33 -- Integer value
34 external
35 "built_in"
36 end
37
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 "Use to_character instead"
75 require
76 valid_character_code: is_valid_character_code
77 do
78 Result := item.to_character
79 end
80
81 Min_value: INTEGER is -2147483648
82 Max_value: INTEGER is 2147483647
83 -- 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 set_item (i: INTEGER) is
103 -- Make `i' the `item' value.
104 external
105 "built_in"
106 ensure
107 item_set: item = i
108 end
109
110 feature -- Status report
111
112 divisible (other: INTEGER_REF): BOOLEAN is
113 -- 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 -- Does current object represent a character?
151 do
152 Result := item >= {CHARACTER}.Min_value and item <= {CHARACTER}.Max_value
153 end
154
155 feature -- Basic operations
156
157 abs: INTEGER is
158 -- Absolute value
159 do
160 Result := abs_ref.item
161 ensure
162 non_negative: Result >= 0
163 same_absolute_value: (Result = item) or (Result = -item)
164 end
165
166 infix "+" (other: like Current): like Current is
167 -- Sum with `other'
168 do
169 create Result
170 Result.set_item (item + other.item)
171 end
172
173 infix "-" (other: like Current): like Current is
174 -- Result of subtracting `other'
175 do
176 create Result
177 Result.set_item (item - other.item)
178 end
179
180 infix "*" (other: like Current): like Current is
181 -- Product by `other'
182 do
183 create Result
184 Result.set_item (item * other.item)
185 end
186
187 infix "/" (other: like Current): DOUBLE is
188 -- Division by `other'
189 require
190 other_exists: other /= Void
191 good_divisor: divisible (other)
192 do
193 Result := item / other.item
194 end
195
196 prefix "+": like Current is
197 -- Unary plus
198 do
199 create Result
200 Result.set_item (+ item)
201 end
202
203 prefix "-": like Current is
204 -- Unary minus
205 do
206 create Result
207 Result.set_item (- item)
208 end
209
210 infix "//" (other: like Current): like Current is
211 -- Integer division of Current by `other'
212 do
213 create Result
214 Result.set_item (item // other.item)
215 end
216
217 infix "\\" (other: like Current): like Current is
218 -- Remainder of the integer division of Current by `other'
219 require
220 other_exists: other /= Void
221 good_divisor: divisible (other)
222 do
223 create Result
224 Result.set_item (item \\ other.item)
225 ensure
226 result_exists: Result /= Void
227 end
228
229 infix "^" (other: DOUBLE): DOUBLE is
230 -- Integer power of Current by `other'
231 do
232 Result := item ^ other
233 end
234
235 infix "|..|" (other: INTEGER): INTEGER_INTERVAL is
236 -- Interval from current element to `other'
237 -- (empty if `other' less than current integer)
238 do
239 create Result.make (item, other)
240 end
241
242 feature {NONE} -- Initialization
243
244 make_from_reference (v: INTEGER_REF) is
245 -- Initialize `Current' with `v.item'.
246 require
247 v_not_void: v /= Void
248 do
249 set_item (v.item)
250 ensure
251 item_set: item = v.item
252 end
253
254 feature -- Conversion
255
256 to_reference: INTEGER_REF is
257 -- Associated reference of Current
258 do
259 create Result
260 Result.set_item (item)
261 ensure
262 to_reference_not_void: Result /= Void
263 end
264
265 frozen to_boolean: BOOLEAN is
266 -- True if not `zero'.
267 do
268 Result := item /= 0
269 end
270
271 as_natural_8: NATURAL_8 is
272 -- Convert `item' into an NATURAL_8 value.
273 do
274 Result := item.as_natural_8
275 end
276
277 as_natural_16: NATURAL_16 is
278 -- Convert `item' into an NATURAL_16 value.
279 do
280 Result := item.as_natural_16
281 end
282
283 as_natural_32: NATURAL_32 is
284 -- Convert `item' into an NATURAL_32 value.
285 do
286 Result := item.as_natural_32
287 end
288
289 as_natural_64: NATURAL_64 is
290 -- Convert `item' into an NATURAL_64 value.
291 do
292 Result := item.as_natural_64
293 end
294
295 as_integer_8: INTEGER_8 is
296 -- Convert `item' into an INTEGER_8 value.
297 do
298 Result := item.as_integer_8
299 end
300
301 as_integer_16: INTEGER_16 is
302 -- Convert `item' into an INTEGER_16 value.
303 do
304 Result := item.as_integer_16
305 end
306
307 as_integer_32: INTEGER is
308 -- Convert `item' into an INTEGER_32 value.
309 do
310 Result := item.as_integer_32
311 end
312
313 as_integer_64: INTEGER_64 is
314 -- Convert `item' into an INTEGER_64 value.
315 do
316 Result := item.as_integer_64
317 end
318
319 frozen to_natural_8: NATURAL_8 is
320 -- Convert `item' into an NATURAL_8 value.
321 require
322 item_non_negative: item >= 0
323 not_too_big: item <= {NATURAL_8}.Max_value
324 do
325 Result := as_natural_8
326 end
327
328 frozen to_natural_16: NATURAL_16 is
329 -- Convert `item' into an NATURAL_16 value.
330 require
331 item_non_negative: item >= 0
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 item_non_negative: item >= 0
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 require
348 item_non_negative: item >= 0
349 do
350 Result := as_natural_64
351 end
352
353 frozen to_integer_8: INTEGER_8 is
354 -- Convert `item' into an INTEGER_8 value.
355 require
356 not_too_small: item >= {INTEGER_8}.Min_value
357 not_too_big: item <= {INTEGER_8}.Max_value
358 do
359 Result := as_integer_8
360 end
361
362 frozen to_integer_16: INTEGER_16 is
363 -- Convert `item' into an INTEGER_16 value.
364 require
365 not_too_small: item >= {INTEGER_16}.Min_value
366 not_too_big: item <= {INTEGER_16}.Max_value
367 do
368 Result := as_integer_16
369 end
370
371 frozen to_integer, frozen to_integer_32: INTEGER is
372 -- Return `item'.
373 do
374 Result := item
375 end
376
377 frozen to_integer_64: INTEGER_64 is
378 -- Convert `item' into an INTEGER_64 value.
379 do
380 Result := as_integer_64
381 end
382
383 to_real: REAL is
384 -- Convert `item' into a REAL
385 do
386 Result := item.to_real
387 end
388
389 to_double: DOUBLE is
390 -- Convert `item' into a DOUBLE
391 do
392 Result := item.to_double
393 end
394
395 to_hex_string: STRING is
396 -- Convert `item' into an hexadecimal string.
397 local
398 i, val: INTEGER
399 a_digit: INTEGER
400 do
401 from
402 i := (create {PLATFORM}).Integer_bits // 4
403 create Result.make (i)
404 Result.fill_blank
405 val := item
406 until
407 i = 0
408 loop
409 a_digit := (val & 0xF)
410 Result.put (a_digit.to_hex_character, i)
411 val := val |>> 4
412 i := i - 1
413 end
414 ensure
415 Result_not_void: Result /= Void
416 Result_valid_count: Result.count = (create {PLATFORM}).Integer_bits // 4
417 end
418
419 to_hex_character: CHARACTER is
420 -- Convert `item' into an hexadecimal character.
421 require
422 in_bounds: 0 <= item and item <= 15
423 local
424 tmp: INTEGER
425 do
426 tmp := item
427 if tmp <= 9 then
428 Result := (tmp + ('0').code).to_character
429 else
430 Result := (('A').code + (tmp - 10)).to_character
431 end
432 ensure
433 valid_character: ("0123456789ABCDEF").has (Result)
434 end
435
436 to_character: CHARACTER is
437 -- Returns corresponding ASCII character to `item' value.
438 require
439 valid_character: is_valid_character_code
440 do
441 Result := item.to_character
442 end
443
444 feature -- Bit operations
445
446 bit_and (i: like Current): like Current is
447 -- Bitwise and between Current' and `i'.
448 require
449 i_not_void: i /= Void
450 do
451 create Result
452 Result.set_item (item.bit_and (i.item))
453 ensure
454 bitwise_and_not_void: Result /= Void
455 end
456
457 frozen infix "&" (i: like Current): like Current is
458 -- Bitwise and between Current' and `i'.
459 require
460 i_not_void: i /= Void
461 do
462 Result := bit_and (i)
463 ensure
464 bitwise_and_not_void: Result /= Void
465 end
466
467 bit_or (i: like Current): like Current is
468 -- Bitwise or between Current' and `i'.
469 require
470 i_not_void: i /= Void
471 do
472 create Result
473 Result.set_item (item.bit_or (i.item))
474 ensure
475 bitwise_or_not_void: Result /= Void
476 end
477
478 frozen infix "|" (i: like Current): like Current is
479 -- Bitwise or between Current' and `i'.
480 require
481 i_not_void: i /= Void
482 do
483 Result := bit_or (i)
484 ensure
485 bitwise_or_not_void: Result /= Void
486 end
487
488 bit_xor (i: like Current): like Current is
489 -- Bitwise xor between Current' and `i'.
490 require
491 i_not_void: i /= Void
492 do
493 create Result
494 Result.set_item (item.bit_xor (i.item))
495 ensure
496 bitwise_xor_not_void: Result /= Void
497 end
498
499 bit_not: like Current is
500 -- One's complement of Current.
501 do
502 create Result
503 Result.set_item (item.bit_not)
504 ensure
505 bit_not_not_void: Result /= Void
506 end
507
508 frozen bit_shift (n: INTEGER): INTEGER is
509 -- Shift Current from `n' position to right if `n' positive,
510 -- to left otherwise.
511 require
512 n_less_or_equal_to_32: n <= 32
513 n_greater_or_equal_to_minus_32: n >= -32
514 do
515 if n > 0 then
516 Result := bit_shift_right (n)
517 else
518 Result := bit_shift_left (- n)
519 end
520 end
521
522 bit_shift_left (n: INTEGER): like Current is
523 -- Shift Current from `n' position to left.
524 require
525 n_nonnegative: n >= 0
526 n_less_or_equal_to_32: n <= 32
527 do
528 create Result
529 Result.set_item (item.bit_shift_left (n))
530 ensure
531 bit_shift_left_not_void: Result /= Void
532 end
533
534 frozen infix "|<<" (n: INTEGER): like Current is
535 -- Shift Current from `n' position to left.
536 require
537 n_nonnegative: n >= 0
538 n_less_or_equal_to_32: n <= 32
539 do
540 Result := bit_shift_left (n)
541 ensure
542 bit_shift_left_not_void: Result /= Void
543 end
544
545 bit_shift_right (n: INTEGER): like Current is
546 -- Shift Current from `n' position to right.
547 require
548 n_nonnegative: n >= 0
549 n_less_or_equal_to_32: n <= 32
550 do
551 create Result
552 Result.set_item (item.bit_shift_right (n))
553 ensure
554 bit_shift_right_not_void: Result /= Void
555 end
556
557 frozen infix "|>>" (n: INTEGER): like Current is
558 -- Shift Current from `n' position to right.
559 require
560 n_nonnegative: n >= 0
561 n_less_or_equal_to_32: n <= 32
562 do
563 Result := bit_shift_right (n)
564 ensure
565 bit_shift_right_not_void: Result /= Void
566 end
567
568 frozen bit_test (n: INTEGER): BOOLEAN is
569 -- Test `n'-th position of Current.
570 require
571 n_nonnegative: n >= 0
572 n_less_than_32: n < 32
573 do
574 Result := item & (1 |<< n) /= 0
575 end
576
577 frozen set_bit (b: BOOLEAN; n: INTEGER): INTEGER is
578 -- Copy of current with `n'-th position
579 -- set to 1 if `b', 0 otherwise.
580 require
581 n_nonnegative: n >= 0
582 n_less_than_32: n < 32
583 do
584 if b then
585 Result := item | (1 |<< n)
586 else
587 Result := item & (1 |<< n).bit_not
588 end
589 end
590
591 frozen set_bit_with_mask (b: BOOLEAN; m: INTEGER): INTEGER is
592 -- Copy of current with all 1 bits of m set to 1
593 -- if `b', 0 otherwise.
594 do
595 if b then
596 Result := item | m
597 else
598 Result := item & m.bit_not
599 end
600 end
601
602 feature -- Output
603
604 out: STRING is
605 -- Printable representation of integer value
606 do
607 create Result.make (11)
608 Result.append_integer (item)
609 end
610
611 feature {NONE} -- Implementation
612
613 abs_ref: like Current is
614 -- Absolute value
615 do
616 if item >= 0 then
617 Result := Current
618 else
619 Result := -Current
620 end
621 ensure
622 result_exists: Result /= Void
623 same_absolute_value: equal (Result, Current) or equal (Result, - Current)
624 end
625
626 invariant
627
628 sign_times_abs: sign * abs = item
629
630 end

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.23