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

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

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

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.23