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

Contents of /FreeELKS/trunk/library/kernel/integer_16_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: 13967 byte(s)
Synchronized with ISE 6.0.65740
1 indexing
2 description: "References to objects containing an integer value coded on 16 bits"
3 library: "Free implementation of ELKS library"
4 copyright: "Copyright (c) 1986-2004, Eiffel Software and others"
5 license: "Eiffel Forum License v2 (see forum.txt)"
6 date: "$Date$"
7 revision: "$Revision$"
8
9 class
10 INTEGER_16_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_16 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_16 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_16 is -32768
81 Max_value: INTEGER_16 is 32767
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_16) 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_16 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_16_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_16_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 not_too_big: item <= {NATURAL_8}.Max_value
340 do
341 Result := as_natural_8
342 end
343
344 frozen to_natural_16: NATURAL_16 is
345 -- Convert `item' into an NATURAL_16 value.
346 require
347 item_non_negative: item >= 0
348 do
349 Result := as_natural_16
350 end
351
352 frozen to_natural_32: NATURAL_32 is
353 -- Convert `item' into an NATURAL_32 value.
354 require
355 item_non_negative: item >= 0
356 do
357 Result := as_natural_32
358 end
359
360 frozen to_natural_64: NATURAL_64 is
361 -- Convert `item' into an NATURAL_64 value.
362 require
363 item_non_negative: item >= 0
364 do
365 Result := as_natural_64
366 end
367
368 frozen to_integer_8: INTEGER_8 is
369 -- Convert `item' into an INTEGER_8 value.
370 require
371 not_too_small: item >= {INTEGER_8}.Min_value
372 not_too_big: item <= {INTEGER_8}.Max_value
373 do
374 Result := as_integer_8
375 end
376
377 frozen to_integer, frozen to_integer_32: INTEGER is
378 -- Convert `item' into an INTEGER_32 value.
379 do
380 Result := as_integer_32
381 end
382
383 frozen to_integer_16: INTEGER_16 is
384 -- Return `item'.
385 do
386 Result := item
387 end
388
389 frozen to_integer_64: INTEGER_64 is
390 -- Convert `item' into an INTEGER_64 value.
391 do
392 Result := as_integer_64
393 end
394
395 to_real: REAL is
396 -- Convert `item' into a REAL
397 do
398 Result := item.to_real
399 end
400
401 to_double: DOUBLE is
402 -- Convert `item' into a DOUBLE
403 do
404 Result := item.to_double
405 end
406
407 to_hex_string: STRING is
408 -- Convert `item' into an hexadecimal string.
409 local
410 i, val: INTEGER
411 a_digit: INTEGER
412 do
413 from
414 i := (create {PLATFORM}).Integer_16_bits // 4
415 create Result.make (i)
416 Result.fill_blank
417 val := item
418 until
419 i = 0
420 loop
421 a_digit := (val & 0xF)
422 Result.put (a_digit.to_hex_character, i)
423 val := val |>> 4
424 i := i - 1
425 end
426 ensure
427 Result_not_void: Result /= Void
428 Result_valid_count: Result.count = (create {PLATFORM}).Integer_16_bits // 4
429 end
430
431 to_hex_character: CHARACTER is
432 -- Convert `item' into an hexadecimal character.
433 require
434 in_bounds: 0 <= item and item <= 15
435 local
436 tmp: INTEGER
437 do
438 tmp := item
439 if tmp <= 9 then
440 Result := (tmp + ('0').code).to_character_8
441 else
442 Result := (('A').code + (tmp - 10)).to_character_8
443 end
444 ensure
445 valid_character: ("0123456789ABCDEF").has (Result)
446 end
447
448 to_character: CHARACTER is
449 -- Returns corresponding ASCII character to `item' value.
450 obsolete
451 "Use `to_character_8' instead."
452 require
453 valid_character: is_valid_character_8_code
454 do
455 Result := item.to_character_8
456 end
457
458 to_character_8: CHARACTER_8 is
459 -- Associated character in 8 bit version.
460 require
461 valid_character: is_valid_character_8_code
462 do
463 Result := item.to_character_8
464 end
465
466 to_character_32: CHARACTER_32 is
467 -- Associated character in 32 bit version.
468 require
469 valid_character: is_valid_character_32_code
470 do
471 Result := item.to_character_32
472 end
473
474 feature -- Bit operations
475
476 bit_and (i: like Current): like Current is
477 -- Bitwise and between Current' and `i'.
478 require
479 i_not_void: i /= Void
480 do
481 create Result
482 Result.set_item (item.bit_and (i.item))
483 ensure
484 bitwise_and_not_void: Result /= Void
485 end
486
487 frozen infix "&" (i: like Current): like Current is
488 -- Bitwise and between Current' and `i'.
489 require
490 i_not_void: i /= Void
491 do
492 Result := bit_and (i)
493 ensure
494 bitwise_and_not_void: Result /= Void
495 end
496
497 bit_or (i: like Current): like Current is
498 -- Bitwise or between Current' and `i'.
499 require
500 i_not_void: i /= Void
501 do
502 create Result
503 Result.set_item (item.bit_or (i.item))
504 ensure
505 bitwise_or_not_void: Result /= Void
506 end
507
508 frozen infix "|" (i: like Current): like Current is
509 -- Bitwise or between Current' and `i'.
510 require
511 i_not_void: i /= Void
512 do
513 Result := bit_or (i)
514 ensure
515 bitwise_or_not_void: Result /= Void
516 end
517
518 bit_xor (i: like Current): like Current is
519 -- Bitwise xor between Current' and `i'.
520 require
521 i_not_void: i /= Void
522 do
523 create Result
524 Result.set_item (item.bit_xor (i.item))
525 ensure
526 bitwise_xor_not_void: Result /= Void
527 end
528
529 bit_not: like Current is
530 -- One's complement of Current.
531 do
532 create Result
533 Result.set_item (item.bit_not)
534 ensure
535 bit_not_not_void: Result /= Void
536 end
537
538 frozen bit_shift (n: INTEGER): INTEGER_16 is
539 -- Shift Current from `n' position to right if `n' positive,
540 -- to left otherwise.
541 require
542 n_less_or_equal_to_16: n <= 16
543 n_greater_or_equal_to_minus_16: n >= -16
544 do
545 if n > 0 then
546 Result := bit_shift_right (n)
547 else
548 Result := bit_shift_left (- n)
549 end
550 end
551
552 bit_shift_left (n: INTEGER): like Current is
553 -- Shift Current from `n' position to left.
554 require
555 n_nonnegative: n >= 0
556 n_less_or_equal_to_16: n <= 16
557 do
558 create Result
559 Result.set_item (item.bit_shift_left (n))
560 ensure
561 bit_shift_left_not_void: Result /= Void
562 end
563
564 frozen infix "|<<" (n: INTEGER): like Current is
565 -- Shift Current from `n' position to left.
566 require
567 n_nonnegative: n >= 0
568 n_less_or_equal_to_16: n <= 16
569 do
570 Result := bit_shift_left (n)
571 ensure
572 bit_shift_left_not_void: Result /= Void
573 end
574
575 bit_shift_right (n: INTEGER): like Current is
576 -- Shift Current from `n' position to right.
577 require
578 n_nonnegative: n >= 0
579 n_less_or_equal_to_16: n <= 16
580 do
581 create Result
582 Result.set_item (item.bit_shift_right (n))
583 ensure
584 bit_shift_right_not_void: Result /= Void
585 end
586
587 frozen infix "|>>" (n: INTEGER): like Current is
588 -- Shift Current from `n' position to right.
589 require
590 n_nonnegative: n >= 0
591 n_less_or_equal_to_16: n <= 16
592 do
593 Result := bit_shift_right (n)
594 ensure
595 bit_shift_right_not_void: Result /= Void
596 end
597
598 frozen bit_test (n: INTEGER): BOOLEAN is
599 -- Test `n'-th position of Current.
600 require
601 n_nonnegative: n >= 0
602 n_less_than_16: n < 16
603 do
604 Result := item & ((1).to_integer_16 |<< n) /= 0
605 end
606
607 frozen set_bit (b: BOOLEAN; n: INTEGER): INTEGER_16 is
608 -- Copy of current with `n'-th position
609 -- set to 1 if `b', 0 otherwise.
610 require
611 n_nonnegative: n >= 0
612 n_less_than_16: n < 16
613 do
614 if b then
615 Result := item | ((1).to_integer_16 |<< n)
616 else
617 Result := item & ((1).to_integer_16 |<< n).bit_not
618 end
619 end
620
621 frozen set_bit_with_mask (b: BOOLEAN; m: INTEGER_16): INTEGER_16 is
622 -- Copy of current with all 1 bits of m set to 1
623 -- if `b', 0 otherwise.
624 do
625 if b then
626 Result := item | m
627 else
628 Result := item & m.bit_not
629 end
630 end
631
632 feature -- Output
633
634 out: STRING is
635 -- Printable representation of integer value
636 do
637 create Result.make (6)
638 Result.append_integer_16 (item)
639 end
640
641 feature {NONE} -- Implementation
642
643 abs_ref: like Current is
644 -- Absolute value
645 do
646 if item >= 0 then
647 Result := Current
648 else
649 Result := -Current
650 end
651 ensure
652 result_exists: Result /= Void
653 same_absolute_value: equal (Result, Current) or equal (Result, - Current)
654 end
655
656 invariant
657
658 sign_times_abs: sign * abs = item
659
660 end

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.23