home *** CD-ROM | disk | FTP | other *** search
- -- Part of SmallEiffel -- Read DISCLAIMER file -- Copyright (C)
- -- Dominique COLNET and Suzanne COLLIN -- colnet@loria.fr
- --
- expanded class INTEGER
- --
- -- Note : corresponding C type is "int"
- --
- inherit
- INTEGER_REF
- redefine
- infix "+", infix "-", infix "*", infix "/", infix "\\", infix "//",
- infix "^", infix "<", infix "<=", infix ">", infix ">=", compare,
- prefix "-", prefix "+", hash_code, one, zero, print_on
- end;
-
- feature
-
- infix "+"(other: INTEGER): INTEGER is
- -- Add `other' to Current.
- external "CSE"
- end;
-
- infix "-" (other: INTEGER): INTEGER is
- -- Subtract `other' from Current.
- external "CSE"
- end;
-
- infix "*" (other: INTEGER): INTEGER is
- -- Multiply `other' by Current.
- external "CSE"
- end;
-
- infix "/" (other: INTEGER): DOUBLE is
- -- Divide Current by `other'.
- do
- Result := to_double / other.to_double;
- end;
-
- infix "//" (other: INTEGER): INTEGER is
- -- Divide Current by `other'.
- -- Note : Integer division
- external "CSE"
- end;
-
- infix "\\" (other: INTEGER): INTEGER is
- -- Remainder of division of Current by `other'.
- external "CSE"
- end;
-
- infix "^" (exp: INTEGER): INTEGER is
- -- Raise Current to `exp'-th power.
- --
- -- Note : written by Bruno MATHIEU
- do
- if exp = 0 then
- Result := 1;
- elseif exp \\ 2 = 0 then
- Result := (Current * Current) ^ (exp // 2);
- else
- Result := Current * (Current ^ (exp - 1));
- end;
- end; -- infix "^"
-
- abs: INTEGER is
- do
- if Current < 0 then
- Result := -Current;
- else
- Result := Current;
- end;
- end;
-
- infix "<" (other: INTEGER): BOOLEAN is
- -- Is Current smaller than `other'?
- external "CSE"
- end;
-
- infix "<=" (other: INTEGER): BOOLEAN is
- -- Is Current smaller or equal to `other'?
- external "CSE"
- end;
-
- infix ">" (other: INTEGER): BOOLEAN is
- -- Is Current greater than `other'?
- external "CSE"
- end;
-
- infix ">=" (other: INTEGER): BOOLEAN is
- -- Is Current greater or equal to `other'?
- external "CSE"
- end;
-
- prefix "+": INTEGER is
- do
- Result := Current
- end;
-
- prefix "-": INTEGER is
- -- Unary minus of Current
- external "CSE"
- end;
-
- compare(other: INTEGER): INTEGER is
- -- Compare Current with `other'.
- -- '<' <==> Result < 0
- -- '>' <==> Result > 0
- -- Otherwise Result = 0
- do
- Result := Current - other
- end;
-
- hash_code: INTEGER is
- do
- if Current < 0 then
- Result := -Current
- else
- Result := Current
- end
- end;
-
- one: INTEGER is
- do
- Result := 1
- end;
-
- zero: INTEGER is
- do
- Result := 0
- end;
-
- sqrt: DOUBLE is
- -- Compute the square routine.
- do
- Result := to_real.sqrt;
- end;
-
- gcd(other: INTEGER): INTEGER is
- -- Great Common Divisor of `Current' and `other'.
- require
- Current >= 0;
- other >= 0;
- do
- if other = 0 then
- Result := Current
- else
- Result := other.gcd(Current \\ other);
- end;
- ensure
- Result = other.gcd(Current);
- end;
-
- print_on(file: STD_FILE_WRITE) is
- do
- file.put_integer(Current);
- end;
-
- feature -- Conversions :
-
- to_real: REAL is
- do
- Result := Current;
- end;
-
- to_double: DOUBLE is
- do
- Result := Current;
- end;
-
- to_string: STRING is
- -- Convert the INTEGER into a new allocated STRING.
- -- Note: see `append_in' to save memory.
- do
- !!Result.make(0);
- append_in(Result);
- end;
-
- append_in(str: STRING) is
- -- Append the equivalent of `to_string' at the end of
- -- `str'. Thus you can save memory because no other
- -- STRING is allocate for the job.
- require
- str /= Void;
- local
- val, i: INTEGER;
- do
- if Current = 0 then
- str.extend('0');
- else
- if Current < 0 then
- str.extend('-');
- (- Current).append_in(str);
- else
- from
- i := str.count + 1;
- val := Current;
- until
- val = 0
- loop
- str.extend((val \\ 10).digit);
- val := val // 10;
- end;
- from
- val := str.count;
- until
- i >= val
- loop
- str.swap(i,val);
- val := val - 1;
- i := i + 1;
- end;
- end;
- end;
- end;
-
- to_string_format(s: INTEGER): STRING is
- -- Same as `to_string' but the result is on `s' character and the
- -- number is right aligned.
- -- Note: see `append_in_format' to save memory.
- require
- to_string.count <= s;
- do
- from
- tmp_string.clear;
- append_in(tmp_string);
- until
- tmp_string.count >= s
- loop
- tmp_string.add_first(' ');
- end;
- Result := clone(tmp_string);
- ensure
- Result.count = s;
- end;
-
- append_in_format(str: STRING; s: INTEGER) is
- -- Append the equivalent of `to_string_format' at the end of
- -- `str'. Thus you can save memory because no other
- -- STRING is allocate for the job.
- do
- from
- tmp_string.clear;
- append_in(tmp_string);
- until
- tmp_string.count >= s
- loop
- tmp_string.add_first(' ');
- end;
- str.append(tmp_string);
- ensure
- str.count >= (old str.count) + s;
- end;
-
- digit: CHARACTER is
- -- Gives the corresponding CHARACTER for range 0..9.
- require
- 0 <= Current;
- Current <= 9;
- do
- Result := ("0123456789").item(Current + 1);
- ensure
- ("0123456789").has(Result);
- Result.value = Current;
- end;
-
- to_character: CHARACTER is
- -- Return the coresponding ASCII character.
- require
- Current >= 0;
- external "CSE"
- end;
-
- to_octal: INTEGER is
- -- Gives the octal value.
- do
- if Current = 0 then
- elseif Current < 0 then
- Result := -((-Current).to_octal);
- else
- from
- tmp_string.clear;
- Result := Current;
- until
- Result = 0
- loop
- tmp_string.extend((Result \\ 8).digit);
- Result := Result // 8;
- end;
- tmp_string.reverse;
- Result := tmp_string.to_integer;
- end;
- end;
-
- feature {NONE}
-
- tmp_string: STRING is "0000000000000000000";
-
- end -- INTEGER
-