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

Annotation of /FreeELKS/trunk/library/kernel/natural_16_ref.e

Parent Directory Parent Directory | Revision Log Revision Log


Revision 91477 - (hide annotations)
Sun Jan 14 09:47:13 2007 UTC (13 years, 1 month ago) by ericb
File size: 13361 byte(s)
Synchronized with ISE 6.0.65740
1 ericb 91447 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-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_16_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_16 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 ericb 91477 "Use `to_character_8' instead"
75 ericb 91447 require
76 ericb 91477 valid_character_code: is_valid_character_8_code
77 ericb 91447 do
78 ericb 91477 Result := item.to_character_8
79 ericb 91447 end
80    
81     Min_value: NATURAL_16 is 0
82     Max_value: NATURAL_16 is 65535
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_16) 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 ericb 91477 divisible (other: like Current): BOOLEAN is
113 ericb 91447 -- 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 ericb 91477 -- 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_32 and
161     item <= {CHARACTER_8}.Max_value.to_natural_32
162     end
163    
164     is_valid_character_32_code: BOOLEAN is
165 ericb 91447 -- Does current object represent a character?
166     do
167 ericb 91477 Result := item >= {CHARACTER_32}.Min_value and
168     item <= {CHARACTER_32}.Max_value
169 ericb 91447 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_16_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_16_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 ericb 91477
296 ericb 91447 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 ericb 91477
308 ericb 91447 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 INTEGER_8 value.
328     require
329     not_too_big: item <= {NATURAL_8}.Max_value
330     do
331     Result := as_natural_8
332     end
333    
334     frozen to_natural_16: NATURAL_16 is
335     -- Convert `item' into an NATURAL_16 value.
336     do
337     Result := item
338     end
339    
340     frozen to_natural_32: NATURAL_32 is
341     -- Convert `item' into an NATURAL_32 value.
342     do
343     Result := as_natural_32
344     end
345    
346     frozen to_natural_64: NATURAL_64 is
347     -- Convert `item' into an NATURAL_64 value.
348     do
349     Result := as_natural_64
350     end
351    
352     frozen to_integer_8: INTEGER_8 is
353     -- Convert `item' into an INTEGER_8 value.
354     require
355     not_too_big: item <= {INTEGER_8}.Max_value.to_natural_16
356     do
357     Result := as_integer_8
358     end
359    
360     frozen to_integer_16: INTEGER_16 is
361     -- Convert `item' into an INTEGER_16 value.
362     require
363     not_too_big: item <= {INTEGER_16}.Max_value.to_natural_16
364     do
365     Result := as_integer_16
366     end
367    
368     frozen to_integer_32: INTEGER is
369     -- Convert `item' into an INTEGER_32 value.
370     do
371     Result := as_integer_32
372     end
373    
374     frozen to_integer_64: INTEGER_64 is
375     -- Convert `item' into an INTEGER_64 value.
376     do
377     Result := as_integer_64
378     end
379    
380     to_real_32: REAL is
381     -- Convert `item' into a REAL
382     do
383     Result := item.to_real_32
384     end
385    
386     to_real_64: DOUBLE is
387     -- Convert `item' into a DOUBLE
388     do
389     Result := item.to_real_64
390     end
391    
392     to_hex_string: STRING is
393     -- Convert `item' into an hexadecimal string.
394     local
395     i, val: INTEGER
396     a_digit: INTEGER
397     do
398     from
399     i := (create {PLATFORM}).Integer_16_bits // 4
400     create Result.make (i)
401     Result.fill_blank
402     val := item
403     until
404     i = 0
405     loop
406     a_digit := (val & 0xF)
407     Result.put (a_digit.to_hex_character, i)
408 ericb 91477 val := val |>> 4
409 ericb 91447 i := i - 1
410     end
411     ensure
412     Result_not_void: Result /= Void
413     Result_valid_count: Result.count = (create {PLATFORM}).Integer_16_bits // 4
414     end
415    
416     to_hex_character: CHARACTER is
417     -- Convert `item' into an hexadecimal character.
418     require
419     in_bounds: 0 <= item and item <= 15
420     local
421     tmp: INTEGER
422     do
423     tmp := item.to_integer_32
424     if tmp <= 9 then
425 ericb 91477 Result := (tmp + ('0').code).to_character_8
426 ericb 91447 else
427 ericb 91477 Result := (('A').code + (tmp - 10)).to_character_8
428 ericb 91447 end
429     ensure
430     valid_character: ("0123456789ABCDEF").has (Result)
431     end
432    
433     to_character: CHARACTER is
434     -- Returns corresponding ASCII character to `item' value.
435 ericb 91477 obsolete
436     "Use `to_character_8' instead."
437 ericb 91447 require
438 ericb 91477 valid_character: is_valid_character_8_code
439 ericb 91447 do
440 ericb 91477 Result := item.to_character_8
441 ericb 91447 end
442    
443 ericb 91477 to_character_8: CHARACTER_8 is
444     -- Associated character in 8 bit version.
445     require
446     valid_character: is_valid_character_8_code
447     do
448     Result := item.to_character_8
449     end
450    
451     to_character_32: CHARACTER_32 is
452     -- Associated character in 32 bit version.
453     require
454     valid_character: is_valid_character_32_code
455     do
456     Result := item.to_character_32
457     end
458    
459 ericb 91447 feature -- Bit operations
460    
461     bit_and (i: like Current): like Current is
462     -- Bitwise and between Current' and `i'.
463     require
464     i_not_void: i /= Void
465     do
466     create Result
467     Result.set_item (item.bit_and (i.item))
468     ensure
469     bitwise_and_not_void: Result /= Void
470     end
471    
472     frozen infix "&" (i: like Current): like Current is
473     -- Bitwise and between Current' and `i'.
474     require
475     i_not_void: i /= Void
476     do
477     Result := bit_and (i)
478     ensure
479     bitwise_and_not_void: Result /= Void
480     end
481    
482     bit_or (i: like Current): like Current is
483     -- Bitwise or between Current' and `i'.
484     require
485     i_not_void: i /= Void
486     do
487     create Result
488     Result.set_item (item.bit_or (i.item))
489     ensure
490     bitwise_or_not_void: Result /= Void
491     end
492    
493     frozen infix "|" (i: like Current): like Current is
494     -- Bitwise or between Current' and `i'.
495     require
496     i_not_void: i /= Void
497     do
498     Result := bit_or (i)
499     ensure
500     bitwise_or_not_void: Result /= Void
501     end
502    
503     bit_xor (i: like Current): like Current is
504     -- Bitwise xor between Current' and `i'.
505     require
506     i_not_void: i /= Void
507     do
508     create Result
509     Result.set_item (item.bit_xor (i.item))
510     ensure
511     bitwise_xor_not_void: Result /= Void
512     end
513    
514     bit_not: like Current is
515     -- One's complement of Current.
516     do
517     create Result
518     Result.set_item (item.bit_not)
519     ensure
520     bit_not_not_void: Result /= Void
521     end
522    
523     frozen bit_shift (n: INTEGER): NATURAL_16 is
524     -- Shift Current from `n' position to right if `n' positive,
525     -- to left otherwise.
526     require
527     n_less_or_equal_to_16: n <= 16
528     n_greater_or_equal_to_minus_16: n >= -16
529     do
530     if n > 0 then
531     Result := bit_shift_right (n)
532     else
533     Result := bit_shift_left (- n)
534     end
535     end
536    
537     bit_shift_left (n: INTEGER): like Current is
538     -- Shift Current from `n' position to left.
539     require
540     n_nonnegative: n >= 0
541     n_less_or_equal_to_16: n <= 16
542     do
543     create Result
544     Result.set_item (item.bit_shift_left (n))
545     ensure
546     bit_shift_left_not_void: Result /= Void
547     end
548    
549     frozen infix "|<<" (n: INTEGER): like Current is
550     -- Shift Current from `n' position to left.
551     require
552     n_nonnegative: n >= 0
553     n_less_or_equal_to_16: n <= 16
554     do
555     Result := bit_shift_left (n)
556     ensure
557     bit_shift_left_not_void: Result /= Void
558     end
559    
560     bit_shift_right (n: INTEGER): like Current is
561     -- Shift Current from `n' position to right.
562     require
563     n_nonnegative: n >= 0
564     n_less_or_equal_to_16: n <= 16
565     do
566     create Result
567     Result.set_item (item.bit_shift_right (n))
568     ensure
569     bit_shift_right_not_void: Result /= Void
570     end
571    
572     frozen infix "|>>" (n: INTEGER): like Current is
573     -- Shift Current from `n' position to right.
574     require
575     n_nonnegative: n >= 0
576     n_less_or_equal_to_16: n <= 16
577     do
578     Result := bit_shift_right (n)
579     ensure
580     bit_shift_right_not_void: Result /= Void
581     end
582    
583     frozen bit_test (n: INTEGER): BOOLEAN is
584     -- Test `n'-th position of Current.
585     require
586     n_nonnegative: n >= 0
587     n_less_than_16: n < 16
588     do
589     Result := item & ((1).to_natural_16 |<< n) /= 0
590     end
591    
592     frozen set_bit (b: BOOLEAN; n: INTEGER): NATURAL_16 is
593     -- Copy of current with `n'-th position
594     -- set to 1 if `b', 0 otherwise.
595     require
596     n_nonnegative: n >= 0
597     n_less_than_16: n < 16
598     do
599     if b then
600     Result := item | ((1).to_natural_16 |<< n)
601     else
602     Result := item & ((1).to_natural_16 |<< n).bit_not
603     end
604     end
605    
606     frozen set_bit_with_mask (b: BOOLEAN; m: NATURAL_16): NATURAL_16 is
607     -- Copy of current with all 1 bits of m set to 1
608     -- if `b', 0 otherwise.
609     do
610     if b then
611     Result := item | m
612     else
613     Result := item & m.bit_not
614     end
615     end
616    
617     feature -- Output
618    
619     out: STRING is
620     -- Printable representation of integer value
621     do
622     create Result.make (5)
623     Result.append_natural_16 (item)
624     end
625    
626     end

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.23