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

Contents of /FreeELKS/trunk/library/kernel/integer_8_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: 11053 byte(s)
Initial revision

1 indexing
2 description: "References to objects containing an integer value coded on 8 bits"
3 status: "See notice at end of class"
4 date: "$Date$"
5 revision: "$Revision$"
6
7 class
8 INTEGER_8_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_8
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_8 is -128
76 Max_value: INTEGER_8 is 127
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_8) 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_8_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_8 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_8_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_8_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 -- Return `item'.
267 do
268 Result := item
269 end
270
271 frozen to_integer_16: INTEGER_16 is
272 -- Convert `item' into an INTEGER_16 value.
273 do
274 Result := item.to_integer_16
275 end
276
277 frozen to_integer, frozen to_integer_32: INTEGER is
278 -- Convert `item' into an INTEGER_32 value.
279 do
280 Result := item.to_integer
281 end
282
283 frozen to_integer_64: INTEGER_64 is
284 -- Convert `item' into an INTEGER_64 value.
285 do
286 Result := item.to_integer_64
287 end
288
289 frozen to_real: REAL is
290 -- Convert `item' into a REAL
291 do
292 Result := item.to_real
293 end
294
295 frozen to_double: DOUBLE is
296 -- Convert `item' into a DOUBLE
297 do
298 Result := item.to_double
299 end
300
301 to_hex_string: STRING is
302 -- Convert `item' into an hexadecimal string.
303 local
304 i, val: INTEGER
305 a_digit: INTEGER
306 do
307 from
308 i := 2
309 create Result.make (i)
310 Result.fill_blank
311 val := item
312 until
313 i = 0
314 loop
315 a_digit := (val & 15)
316 Result.put (a_digit.to_hex_character, i)
317 val := val |>> 4
318 i := i - 1
319 end
320 ensure
321 result_not_void: Result /= Void
322 result_valid_count: Result.count = 2
323 end
324
325 to_hex_character: CHARACTER is
326 -- Convert `item' into an hexadecimal character.
327 require
328 in_bounds: 0 <= item and item <= 15
329 local
330 tmp: INTEGER
331 do
332 tmp := item
333 Result := tmp.to_hex_character
334 ensure
335 valid_character: ("0123456789ABCDEF").has (Result)
336 end
337
338 frozen to_character: CHARACTER is
339 -- Returns corresponding ASCII character to `item' value.
340 require
341 valid_character: is_valid_character_code
342 do
343 Result := item.to_character
344 end
345
346 feature -- Bit operations
347
348 frozen infix "&", frozen bit_and (i: like Current): like Current is
349 -- Bitwise and between Current' and `i'.
350 require
351 i_not_void: i /= Void
352 do
353 create Result
354 Result.set_item (item & i.item)
355 ensure
356 bitwise_and_not_void: Result /= Void
357 end
358
359 frozen infix "|", frozen bit_or (i: like Current): like Current is
360 -- Bitwise or between Current' and `i'.
361 require
362 i_not_void: i /= Void
363 do
364 create Result
365 Result.set_item (item | i.item)
366 ensure
367 bitwise_or_not_void: Result /= Void
368 end
369
370 frozen bit_xor (i: like Current): like Current is
371 -- Bitwise xor between Current' and `i'.
372 require
373 i_not_void: i /= Void
374 do
375 create Result
376 Result.set_item (item.bit_xor (i.item))
377 ensure
378 bitwise_xor_not_void: Result /= Void
379 end
380
381 frozen bit_not: like Current is
382 -- One's complement of Current.
383 do
384 create Result
385 Result.set_item (item.bit_not)
386 ensure
387 bit_not_not_void: Result /= Void
388 end
389
390 frozen bit_shift (n: INTEGER): like Current is
391 -- Shift Current from `n' position to right if `n' positive,
392 -- to left otherwise.
393 require
394 n_less_or_equal_to_8: n <= 8
395 n_greater_or_equal_to_minus_8: n >= -8
396 do
397 if n > 0 then
398 Result := bit_shift_right (n)
399 else
400 Result := bit_shift_left (- n)
401 end
402 ensure
403 bit_shift_not_void: Result /= Void
404 end
405
406 frozen infix "|<<", frozen bit_shift_left (n: INTEGER): like Current is
407 -- Shift Current from `n' position to left.
408 require
409 n_nonnegative: n >= 0
410 n_less_or_equal_to_8: n <= 8
411 do
412 create Result
413 Result.set_item (item |<< n)
414 ensure
415 bit_shift_left_not_void: Result /= Void
416 end
417
418 frozen infix "|>>", frozen bit_shift_right (n: INTEGER): like Current is
419 -- Shift Current from `n' position to right.
420 require
421 n_nonnegative: n >= 0
422 n_less_or_equal_to_8: n <= 8
423 do
424 create Result
425 Result.set_item (item |>> n)
426 ensure
427 bit_shift_right_not_void: Result /= Void
428 end
429
430 frozen bit_test (n: INTEGER): BOOLEAN is
431 -- Test `n'-th position of Current.
432 require
433 n_nonnegative: n >= 0
434 n_less_than_8: n < 8
435 do
436 Result := item & ((1).to_integer_8 |<< n) /= 0
437 end
438
439 frozen set_bit (b: BOOLEAN; n: INTEGER): INTEGER_8 is
440 -- Copy of current with `n'-th position
441 -- set to 1 if `b', 0 otherwise.
442 require
443 n_nonnegative: n >= 0
444 n_less_than_8: n < 8
445 do
446 if b then
447 Result := item | ((1).to_integer_8 |<< n)
448 else
449 Result := item & ((1).to_integer_8 |<< n).bit_not
450 end
451 end
452
453 frozen set_bit_with_mask (b: BOOLEAN; m: INTEGER_8): INTEGER_8 is
454 -- Copy of current with all 1 bits of m set to 1
455 -- if `b', 0 otherwise.
456 do
457 if b then
458 Result := item | m
459 else
460 Result := item & m.bit_not
461 end
462 end
463
464 feature -- Output
465
466 out: STRING is
467 -- Printable representation of integer value
468 do
469 Result := item.out
470 end
471
472 feature {NONE} -- Implementation
473
474 abs_ref: INTEGER_8_REF is
475 -- Absolute value
476 do
477 if item >= 0 then
478 Result := Current
479 else
480 Result := -Current
481 end
482 ensure
483 result_exists: Result /= Void
484 same_absolute_value: equal (Result, Current) or equal (Result, - Current)
485 end
486
487 invariant
488
489 sign_times_abs: sign * abs = item
490
491 indexing
492
493 library: "[
494 EiffelBase: Library of reusable components for Eiffel.
495 ]"
496
497 status: "[
498 Copyright 1986-2001 Interactive Software Engineering (ISE).
499 For ISE customers the original versions are an ISE product
500 covered by the ISE Eiffel license and support agreements.
501 ]"
502
503 license: "[
504 EiffelBase may now be used by anyone as FREE SOFTWARE to
505 develop any product, public-domain or commercial, without
506 payment to ISE, under the terms of the ISE Free Eiffel Library
507 License (IFELL) at http://eiffel.com/products/base/license.html.
508 ]"
509
510 source: "[
511 Interactive Software Engineering Inc.
512 ISE Building
513 360 Storke Road, Goleta, CA 93117 USA
514 Telephone 805-685-1006, Fax 805-685-6869
515 Electronic mail <info@eiffel.com>
516 Customer support http://support.eiffel.com
517 ]"
518
519 info: "[
520 For latest info see award-winning pages: http://eiffel.com
521 ]"
522
523 end -- class INTEGER_8_REF
524
525
526

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.23