/[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 91424 - (show annotations)
Tue Oct 26 18:39:32 2004 UTC (15 years, 2 months ago) by manus_eiffel
File size: 11384 byte(s)
Initial revision

1 indexing
2 description: "References to objects containing an integer value coded on 16 bits"
3 status: "See notice at end of class"
4 date: "$Date$"
5 revision: "$Revision$"
6
7 class
8 INTEGER_16_REF
9
10 inherit
11 NUMERIC
12 rename
13 infix "/" as infix "//"
14 redefine
15 out, is_equal
16 end
17
18 COMPARABLE
19 redefine
20 out, is_equal
21 end
22
23 HASHABLE
24 redefine
25 is_hashable, out, is_equal
26 end
27
28 feature -- Access
29
30 item: INTEGER_16
31 -- Integer value
32
33 hash_code: INTEGER is
34 -- Hash code value
35 do
36 Result := item.to_integer.hash_code
37 end
38
39 sign: INTEGER is
40 -- Sign value (0, -1 or 1)
41 do
42 if item > 0 then
43 Result := 1
44 elseif item < 0 then
45 Result := -1
46 end
47 ensure
48 three_way: Result = three_way_comparison (zero)
49 end
50
51 one: like Current is
52 -- Neutral element for "*" and "/"
53 do
54 create Result
55 Result.set_item (1)
56 end
57
58 zero: like Current is
59 -- Neutral element for "+" and "-"
60 do
61 create Result
62 Result.set_item (0)
63 end
64
65 ascii_char: CHARACTER is
66 -- Returns corresponding ASCII character to `item' value.
67 obsolete
68 "Use to_character instead"
69 require
70 valid_character_code: is_valid_character_code
71 do
72 Result := item.to_character
73 end
74
75 Min_value: INTEGER_16 is -32768
76 Max_value: INTEGER_16 is 32767
77 -- Minimum and Maximum value hold in `item'.
78
79 feature -- Comparison
80
81 infix "<" (other: like Current): BOOLEAN is
82 -- Is current integer less than `other'?
83 do
84 Result := item < other.item
85 end
86
87 is_equal (other: like Current): BOOLEAN is
88 -- Is `other' attached to an object of the same type
89 -- as current object and identical to it?
90 do
91 Result := other.item = item
92 end
93
94 feature -- Element change
95
96 set_item (i: INTEGER_16) is
97 -- Make `i' the `item' value.
98 do
99 item := i
100 ensure
101 item_set: item = i
102 end
103
104 feature -- Status report
105
106 divisible (other: INTEGER_16_REF): BOOLEAN is
107 -- May current object be divided by `other'?
108 do
109 Result := other.item /= 0
110 ensure then
111 value: Result = (other.item /= 0)
112 end
113
114 exponentiable (other: NUMERIC): BOOLEAN is
115 -- May current object be elevated to the power `other'?
116 local
117 integer_value: INTEGER_REF
118 double_value: DOUBLE_REF
119 real_value: REAL_REF
120 do
121 integer_value ?= other
122 real_value ?= other
123 double_value ?= other
124 if integer_value /= Void then
125 Result := integer_value.item >= 0 or item /= 0
126 elseif real_value /= Void then
127 Result := real_value.item >= 0.0 or item /= 0
128 elseif double_value /= Void then
129 Result := double_value.item >= 0.0 or item /= 0
130 end
131 ensure then
132 safe_values: ((other.conforms_to (0) and item /= 0) or
133 (other.conforms_to (0.0) and item > 0)) implies Result
134 end
135
136 is_hashable: BOOLEAN is
137 -- May current object be hashed?
138 -- (True if it is not its type's default.)
139 do
140 Result := item /= 0
141 end
142
143 is_valid_character_code: BOOLEAN is
144 -- Does current object represent a character?
145 do
146 Result := item >= feature {CHARACTER}.Min_value and item <= feature {CHARACTER}.Max_value
147 end
148
149 feature -- Basic operations
150
151 abs: INTEGER_16 is
152 -- Absolute value
153 do
154 Result := abs_ref.item
155 ensure
156 non_negative: Result >= 0
157 same_absolute_value: (Result = item) or (Result = -item)
158 end
159
160 infix "+" (other: like Current): like Current is
161 -- Sum with `other'
162 do
163 create Result
164 Result.set_item (item + other.item)
165 end
166
167 infix "-" (other: like Current): like Current is
168 -- Result of subtracting `other'
169 do
170 create Result
171 Result.set_item (item - other.item)
172 end
173
174 infix "*" (other: like Current): like Current is
175 -- Product by `other'
176 do
177 create Result
178 Result.set_item (item * other.item)
179 end
180
181 infix "/" (other: like Current): DOUBLE is
182 -- Division by `other'
183 require
184 other_exists: other /= Void
185 good_divisor: divisible (other)
186 do
187 Result := item / other.item
188 end
189
190 prefix "+": like Current is
191 -- Unary plus
192 do
193 create Result
194 Result.set_item (+ item)
195 end
196
197 prefix "-": like Current is
198 -- Unary minus
199 do
200 create Result
201 Result.set_item (- item)
202 end
203
204 infix "//" (other: like Current): like Current is
205 -- Integer division of Current by `other'
206 do
207 create Result
208 Result.set_item (item // other.item)
209 end
210
211 infix "\\" (other: like Current): like Current is
212 -- Remainder of the integer division of Current by `other'
213 require
214 other_exists: other /= Void
215 good_divisor: divisible (other)
216 do
217 create Result
218 Result.set_item (item \\ other.item)
219 ensure
220 result_exists: Result /= Void
221 end
222
223 infix "^" (other: DOUBLE): DOUBLE is
224 -- Integer power of Current by `other'
225 do
226 Result := item ^ other
227 end
228
229 infix "|..|" (other: INTEGER): INTEGER_INTERVAL is
230 -- Interval from current element to `other'
231 -- (empty if `other' less than current integer)
232 do
233 create Result.make (item, other)
234 end
235
236 feature {NONE} -- Conversion
237
238 make_from_reference (v: INTEGER_16_REF) is
239 -- Initialize `Current' with `v.item'.
240 require
241 v_not_void: v /= Void
242 do
243 item := v.item
244 ensure
245 item_set: item = v.item
246 end
247
248 feature -- Conversion
249
250 to_reference: INTEGER_16_REF is
251 -- Associated reference of Current
252 do
253 create Result
254 Result.set_item (item)
255 ensure
256 to_reference_not_void: Result /= Void
257 end
258
259 frozen to_boolean: BOOLEAN is
260 -- True if not `zero'.
261 do
262 Result := item /= 0
263 end
264
265 frozen to_integer_8: INTEGER_8 is
266 -- Convert `item' into an INTEGER_8 value.
267 require
268 not_too_small: item >= feature {INTEGER_8}.Min_value
269 not_too_big: item <= feature {INTEGER_8}.Max_value
270 do
271 Result := item.to_integer_8
272 end
273
274 frozen to_integer, frozen to_integer_32: INTEGER is
275 -- Convert `item' into an INTEGER_32 value.
276 do
277 Result := item.to_integer
278 end
279
280 frozen to_integer_16: INTEGER_16 is
281 -- Return `item'.
282 do
283 Result := item
284 end
285
286 frozen to_integer_64: INTEGER_64 is
287 -- Convert `item' into an INTEGER_64 value.
288 do
289 Result := item.to_integer_64
290 end
291
292 frozen to_real: REAL is
293 -- Convert `item' into a REAL
294 do
295 Result := item.to_real
296 end
297
298 frozen to_double: DOUBLE is
299 -- Convert `item' into a DOUBLE
300 do
301 Result := item.to_double
302 end
303
304 to_hex_string: STRING is
305 -- Convert `item' into an hexadecimal string.
306 local
307 i, val: INTEGER
308 a_digit: INTEGER
309 do
310 from
311 i := (create {PLATFORM}).Integer_16_bits // 4
312 create Result.make (i)
313 Result.fill_blank
314 val := item
315 until
316 i = 0
317 loop
318 a_digit := (val & 0xF)
319 Result.put (a_digit.to_hex_character, i)
320 val := val |>> 4
321 i := i - 1
322 end
323 ensure
324 Result_not_void: Result /= Void
325 Result_valid_count: Result.count = (create {PLATFORM}).Integer_16_bits // 4
326 end
327
328 to_hex_character: CHARACTER is
329 -- Convert `item' into an hexadecimal character.
330 require
331 in_bounds: 0 <= item and item <= 15
332 local
333 tmp: INTEGER
334 do
335 tmp := item
336 if tmp <= 9 then
337 Result := (tmp + ('0').code).to_character
338 else
339 Result := (('A').code + (tmp - 10)).to_character
340 end
341 ensure
342 valid_character: ("0123456789ABCDEF").has (Result)
343 end
344
345 frozen to_character: CHARACTER is
346 -- Returns corresponding ASCII character to `item' value.
347 require
348 valid_character: is_valid_character_code
349 do
350 Result := item.to_character
351 end
352
353 feature -- Bit operations
354
355 frozen infix "&", frozen bit_and (i: like Current): like Current is
356 -- Bitwise and between Current' and `i'.
357 require
358 i_not_void: i /= Void
359 do
360 create Result
361 Result.set_item (item & i.item)
362 ensure
363 bitwise_and_not_void: Result /= Void
364 end
365
366 frozen infix "|", frozen bit_or (i: like Current): like Current is
367 -- Bitwise or between Current' and `i'.
368 require
369 i_not_void: i /= Void
370 do
371 create Result
372 Result.set_item (item | i.item)
373 ensure
374 bitwise_or_not_void: Result /= Void
375 end
376
377 frozen bit_xor (i: like Current): like Current is
378 -- Bitwise xor between Current' and `i'.
379 require
380 i_not_void: i /= Void
381 do
382 create Result
383 Result.set_item (item.bit_xor (i.item))
384 ensure
385 bitwise_xor_not_void: Result /= Void
386 end
387
388 frozen bit_not: like Current is
389 -- One's complement of Current.
390 do
391 create Result
392 Result.set_item (item.bit_not)
393 ensure
394 bit_not_not_void: Result /= Void
395 end
396
397 frozen bit_shift (n: INTEGER): like Current is
398 -- Shift Current from `n' position to right if `n' positive,
399 -- to left otherwise.
400 require
401 n_less_or_equal_to_16: n <= 16
402 n_greater_or_equal_to_minus_16: n >= -16
403 do
404 if n > 0 then
405 Result := bit_shift_right (n)
406 else
407 Result := bit_shift_left (- n)
408 end
409 ensure
410 bit_shift_not_void: Result /= Void
411 end
412
413 frozen infix "|<<", frozen bit_shift_left (n: INTEGER): like Current is
414 -- Shift Current from `n' position to left.
415 require
416 n_nonnegative: n >= 0
417 n_less_or_equal_to_16: n <= 16
418 do
419 create Result
420 Result.set_item (item |<< n)
421 ensure
422 bit_shift_left_not_void: Result /= Void
423 end
424
425 frozen infix "|>>", frozen bit_shift_right (n: INTEGER): like Current is
426 -- Shift Current from `n' position to right.
427 require
428 n_nonnegative: n >= 0
429 n_less_or_equal_to_16: n <= 16
430 do
431 create Result
432 Result.set_item (item |>> n)
433 ensure
434 bit_shift_right_not_void: Result /= Void
435 end
436
437 frozen bit_test (n: INTEGER): BOOLEAN is
438 -- Test `n'-th position of Current.
439 require
440 n_nonnegative: n >= 0
441 n_less_than_16: n < 16
442 do
443 Result := item & ((1).to_integer_16 |<< n) /= 0
444 end
445
446 frozen set_bit (b: BOOLEAN; n: INTEGER): INTEGER_16 is
447 -- Copy of current with `n'-th position
448 -- set to 1 if `b', 0 otherwise.
449 require
450 n_nonnegative: n >= 0
451 n_less_than_16: n < 16
452 do
453 if b then
454 Result := item | ((1).to_integer_16 |<< n)
455 else
456 Result := item & ((1).to_integer_16 |<< n).bit_not
457 end
458 end
459
460 frozen set_bit_with_mask (b: BOOLEAN; m: INTEGER_16): INTEGER_16 is
461 -- Copy of current with all 1 bits of m set to 1
462 -- if `b', 0 otherwise.
463 do
464 if b then
465 Result := item | m
466 else
467 Result := item & m.bit_not
468 end
469 end
470
471 feature -- Output
472
473 out: STRING is
474 -- Printable representation of integer value
475 do
476 Result := item.out
477 end
478
479 feature {NONE} -- Implementation
480
481 abs_ref: INTEGER_16_REF is
482 -- Absolute value
483 do
484 if item >= 0 then
485 Result := Current
486 else
487 Result := -Current
488 end
489 ensure
490 result_exists: Result /= Void
491 same_absolute_value: equal (Result, Current) or equal (Result, - Current)
492 end
493
494 invariant
495
496 sign_times_abs: sign * abs = item
497
498 indexing
499
500 library: "[
501 EiffelBase: Library of reusable components for Eiffel.
502 ]"
503
504 status: "[
505 Copyright 1986-2001 Interactive Software Engineering (ISE).
506 For ISE customers the original versions are an ISE product
507 covered by the ISE Eiffel license and support agreements.
508 ]"
509
510 license: "[
511 EiffelBase may now be used by anyone as FREE SOFTWARE to
512 develop any product, public-domain or commercial, without
513 payment to ISE, under the terms of the ISE Free Eiffel Library
514 License (IFELL) at http://eiffel.com/products/base/license.html.
515 ]"
516
517 source: "[
518 Interactive Software Engineering Inc.
519 ISE Building
520 360 Storke Road, Goleta, CA 93117 USA
521 Telephone 805-685-1006, Fax 805-685-6869
522 Electronic mail <info@eiffel.com>
523 Customer support http://support.eiffel.com
524 ]"
525
526 info: "[
527 For latest info see award-winning pages: http://eiffel.com
528 ]"
529
530 end -- class INTEGER_16_REF
531
532
533

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.23