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

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

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.23