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

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

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.23