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

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

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.23