home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 3 / AACD03.BIN / AACD / Programming / sofa / archive / SmallEiffel.lha / SmallEiffel / lib_std / integer.e < prev    next >
Text File  |  1999-06-05  |  8KB  |  342 lines

  1. -- This file is  free  software, which  comes  along  with  SmallEiffel. This
  2. -- software  is  distributed  in the hope that it will be useful, but WITHOUT 
  3. -- ANY  WARRANTY;  without  even  the  implied warranty of MERCHANTABILITY or
  4. -- FITNESS  FOR A PARTICULAR PURPOSE. You can modify it as you want, provided
  5. -- this header is kept unaltered, and a notification of the changes is added.
  6. -- You  are  allowed  to  redistribute  it and sell it, alone or as a part of 
  7. -- another product.
  8. --          Copyright (C) 1994-98 LORIA - UHP - CRIN - INRIA - FRANCE
  9. --            Dominique COLNET and Suzanne COLLIN - colnet@loria.fr 
  10. --                       http://SmallEiffel.loria.fr
  11. --
  12. expanded class INTEGER
  13. --
  14. -- Note: An Eiffel INTEGER is mapped as a C int or as a Java int.
  15. --
  16.  
  17. inherit
  18.    INTEGER_REF
  19.       redefine
  20.          infix "+", infix "-", infix "*", infix "/", infix "\\", 
  21.          infix "//", infix "^", infix "<", infix "<=", infix ">",
  22.          infix ">=", prefix "-", prefix "+", hash_code, 
  23.          one, zero, out_in_tagged_out_memory, fill_tagged_out_memory
  24.       end;
  25.  
  26. feature 
  27.    
  28.    one: INTEGER is 1;
  29.  
  30.    zero: INTEGER is 0;
  31.  
  32.    infix "+" (other: INTEGER): INTEGER is
  33.       external "SmallEiffel"
  34.       end;
  35.    
  36.    infix "-" (other: INTEGER): INTEGER is
  37.       external "SmallEiffel"
  38.       end;
  39.  
  40.    infix "*" (other: INTEGER): INTEGER is
  41.       external "SmallEiffel"
  42.       end;
  43.  
  44.    infix "/" (other: INTEGER): DOUBLE is
  45.       external "SmallEiffel"
  46.       end;
  47.  
  48.    infix "//" (other: INTEGER): INTEGER is
  49.       external "SmallEiffel"
  50.       end;
  51.  
  52.    infix "\\" (other: INTEGER): INTEGER is
  53.       external "SmallEiffel"
  54.       end;
  55.    
  56.    infix "^" (exp: INTEGER): INTEGER is
  57.       do
  58.          if exp = 0 then
  59.             Result := 1;
  60.          elseif exp \\ 2 = 0 then
  61.             Result := (Current * Current) ^ (exp // 2);
  62.          else
  63.             Result := Current * (Current ^ (exp - 1));
  64.          end;
  65.       end;
  66.    
  67.    abs: INTEGER is
  68.          -- Absolute value of `Current'.
  69.       do
  70.          if Current < 0 then
  71.             Result := -Current;
  72.          else
  73.             Result := Current;
  74.          end;
  75.       end;
  76.          
  77.    infix "<" (other: INTEGER): BOOLEAN is
  78.          -- Is 'Current' strictly less than 'other'?
  79.       external "SmallEiffel"
  80.       end;
  81.  
  82.    infix "<=" (other: INTEGER): BOOLEAN is
  83.          -- Is 'Current' less or equal 'other'?
  84.       external "SmallEiffel"
  85.       end;
  86.  
  87.    infix ">" (other: INTEGER): BOOLEAN is
  88.          -- Is 'Current' strictly greater than 'other'?
  89.       external "SmallEiffel"
  90.       end;
  91.  
  92.    infix ">=" (other: INTEGER): BOOLEAN is
  93.          -- Is 'Current' greater or equal than 'other'?
  94.       external "SmallEiffel"
  95.       end;
  96.  
  97.    prefix "+": INTEGER is
  98.       do
  99.          Result := Current
  100.       end;
  101.  
  102.    prefix "-": INTEGER is
  103.       external "SmallEiffel"
  104.       end;
  105.    
  106.    odd: BOOLEAN is
  107.          -- Is odd ?
  108.       do
  109.          Result := (Current \\ 2) = 1;
  110.       end;
  111.  
  112.    even: BOOLEAN is
  113.          -- Is even ?
  114.       do
  115.          Result := (Current \\ 2) = 0;
  116.       end;
  117.  
  118.    sqrt: DOUBLE is
  119.          -- Compute the square routine.
  120.       do
  121.          Result := to_double.sqrt;
  122.       end;
  123.  
  124.    log: DOUBLE is
  125.          -- (ANSI C log).
  126.       do
  127.          Result := to_double.log;
  128.       end;
  129.  
  130.    gcd(other: INTEGER): INTEGER is
  131.          -- Great Common Divisor of `Current' and `other'.
  132.       require
  133.          Current >= 0;
  134.          other >= 0;
  135.       do
  136.          if other = 0 then 
  137.             Result := Current
  138.          else
  139.             Result := other.gcd(Current \\ other);
  140.          end;    
  141.       ensure
  142.          Result = other.gcd(Current);
  143.       end;
  144.    
  145. feature -- Conversions :
  146.    
  147.    to_real: REAL is
  148.       do
  149.          Result := Current;
  150.       end;
  151.    
  152.    to_double: DOUBLE is
  153.       do
  154.          Result := Current;
  155.       end;
  156.    
  157.    to_string: STRING is
  158.          -- Convert the INTEGER into a new allocated STRING. 
  159.          -- Note: see also `append_in' to save memory.
  160.       do
  161.          !!Result.make(0);
  162.          append_in(Result);
  163.       end; 
  164.  
  165.    to_boolean: BOOLEAN is
  166.          -- Return false for 0, otherwise true.
  167.       do
  168.          Result := Current /= 0;
  169.       ensure 
  170.          Result = (Current /= 0)
  171.       end;
  172.  
  173.    to_bit: BIT Integer_bits is
  174.          -- Portable BIT_N conversion.
  175.       external "SmallEiffel"
  176.       end;
  177.  
  178.    append_in(str: STRING) is
  179.          -- Append the equivalent of `to_string' at the end of 
  180.          -- `str'. Thus you can save memory because no other
  181.          -- STRING is allocate for the job.
  182.       require
  183.          str /= Void;
  184.       local
  185.          val, i: INTEGER;
  186.       do
  187.          if Current = 0 then
  188.             str.extend('0');
  189.          else
  190.             if Current > 0 then
  191.                from
  192.                   i := str.count + 1;
  193.                   val := Current;
  194.                until
  195.                   val = 0
  196.                loop
  197.                   str.extend((val \\ 10).digit);
  198.                   val := val // 10;
  199.                end;
  200.             else
  201.                str.extend('-');
  202.                from
  203.                   i := str.count + 1;
  204.                   val := Current;
  205.                until
  206.                   val = 0
  207.                loop
  208.                   str.extend((-(val \\ 10)).digit);
  209.                   val := val // 10;
  210.                end;
  211.             end;
  212.             from  
  213.                val := str.count;
  214.             until
  215.                i >= val
  216.             loop
  217.                str.swap(i,val);
  218.                val := val - 1;
  219.                i := i + 1;
  220.             end;
  221.          end;
  222.       end; 
  223.    
  224.    to_string_format(s: INTEGER): STRING is
  225.          -- Same as `to_string' but the result is on `s' character and the 
  226.          -- number is right aligned. 
  227.          -- Note: see `append_in_format' to save memory.
  228.       require
  229.          to_string.count <= s;
  230.       do
  231.          from  
  232.             tmp_string.clear;
  233.             append_in(tmp_string);
  234.          until
  235.             tmp_string.count >= s
  236.          loop
  237.             tmp_string.add_first(' ');
  238.          end;
  239.          Result := tmp_string.twin;
  240.       ensure
  241.          Result.count = s;
  242.       end; 
  243.  
  244.    append_in_format(str: STRING; s: INTEGER) is
  245.          -- Append the equivalent of `to_string_format' at the end of 
  246.          -- `str'. Thus you can save memory because no other
  247.          -- STRING is allocate for the job.
  248.       do
  249.          from
  250.             tmp_string.clear;
  251.             append_in(tmp_string);
  252.          until
  253.             tmp_string.count >= s
  254.          loop
  255.             tmp_string.add_first(' ');
  256.          end;
  257.          str.append(tmp_string);
  258.       ensure
  259.          str.count >= (old str.count) + s;
  260.       end;
  261.    
  262.    digit: CHARACTER is
  263.          -- Gives the corresponding CHARACTER for range 0..9.
  264.       require
  265.          in_range(0,9)
  266.       do
  267.          Result := (Current + ('0').code).to_character;
  268.       ensure
  269.          ("0123456789").has(Result);
  270.          Result.value = Current
  271.       end;
  272.       
  273.    hexadecimal_digit: CHARACTER is
  274.          -- Gives the corresponding CHARACTER for range 0..15.
  275.       require
  276.          in_range(0,15)
  277.       do
  278.          if Current <= 9 then
  279.             Result := digit;
  280.          else
  281.             Result := (('A').code + (Current - 10)).to_character;
  282.          end;
  283.       ensure
  284.          ("0123456789ABCDEF").has(Result)
  285.       end;
  286.       
  287.    to_character: CHARACTER is
  288.          -- Return the coresponding ASCII character.
  289.       require
  290.          Current >= 0;
  291.       external "SmallEiffel"
  292.       end;
  293.    
  294.    to_octal: INTEGER is
  295.          -- Gives coresponding octal value.
  296.       do
  297.          if Current = 0 then
  298.          elseif Current < 0 then
  299.             Result := -((-Current).to_octal);
  300.          else
  301.             from  
  302.                tmp_string.clear;
  303.                Result := Current;
  304.             until
  305.                Result = 0
  306.             loop
  307.                tmp_string.extend((Result \\ 8).digit);
  308.                Result := Result // 8;
  309.             end;
  310.             tmp_string.reverse;
  311.             Result := tmp_string.to_integer;
  312.          end;
  313.       end;
  314.    
  315. feature -- Object Printing :
  316.  
  317.    out_in_tagged_out_memory, fill_tagged_out_memory is
  318.       do
  319.          Current.append_in(tagged_out_memory);
  320.       end;
  321.  
  322. feature -- Hashing :
  323.    
  324.    hash_code: INTEGER is
  325.       do
  326.          if Current < 0 then
  327.             Result := -(Current + 1);
  328.          else
  329.             Result := Current;
  330.          end
  331.       end;
  332.  
  333. feature {NONE}
  334.    
  335.    tmp_string: STRING is 
  336.       once
  337.          !!Result.make(128);
  338.       end;
  339.          
  340. end -- INTEGER
  341.  
  342.