home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 3: Developer Tools / Linux Cubed Series 3 - Developer Tools.iso / devel / lang / eiffel / smalleif.97 / se.t / SmallEiffel / lib_std / integer.e < prev    next >
Encoding:
Text File  |  1996-05-02  |  6.0 KB  |  298 lines

  1. -- Part of SmallEiffel -- Read DISCLAIMER file -- Copyright (C) 
  2. -- Dominique COLNET and Suzanne COLLIN -- colnet@loria.fr
  3. --
  4. expanded class INTEGER
  5. --
  6. -- Note : corresponding C type is "int"
  7. --
  8. inherit
  9.    INTEGER_REF
  10.       redefine
  11.      infix "+", infix "-", infix "*", infix "/", infix "\\", infix "//", 
  12.      infix "^", infix "<", infix "<=", infix ">", infix ">=", compare, 
  13.      prefix "-", prefix "+", hash_code, one, zero, print_on
  14.       end;
  15.  
  16. feature 
  17.    
  18.    infix "+"(other: INTEGER): INTEGER is
  19.      -- Add `other' to Current.
  20.       external "CSE"
  21.       end;
  22.    
  23.    infix "-" (other: INTEGER): INTEGER is
  24.      -- Subtract `other' from Current.
  25.       external "CSE"
  26.       end;
  27.  
  28.    infix "*" (other: INTEGER): INTEGER is
  29.      -- Multiply `other' by Current.
  30.       external "CSE"
  31.       end;
  32.  
  33.    infix "/" (other: INTEGER): DOUBLE is
  34.      -- Divide Current by `other'.
  35.       do
  36.      Result := to_double / other.to_double;
  37.       end;
  38.  
  39.    infix "//" (other: INTEGER): INTEGER is
  40.      -- Divide Current by `other'.
  41.      -- Note : Integer division
  42.       external "CSE"
  43.       end;
  44.  
  45.    infix "\\" (other: INTEGER): INTEGER is
  46.      -- Remainder of division of Current by `other'.
  47.       external "CSE"
  48.       end;
  49.    
  50.    infix "^" (exp: INTEGER): INTEGER is
  51.      -- Raise Current to `exp'-th power.
  52.      --     
  53.      -- Note : written by Bruno MATHIEU
  54.       do
  55.      if exp = 0 then
  56.         Result := 1;
  57.      elseif exp \\ 2 = 0 then
  58.         Result := (Current * Current) ^ (exp // 2);
  59.      else
  60.         Result := Current * (Current ^ (exp - 1));
  61.      end;
  62.       end; -- infix "^" 
  63.    
  64.    abs: INTEGER is
  65.       do
  66.      if Current < 0 then
  67.         Result := -Current;
  68.      else
  69.         Result := Current;
  70.      end;
  71.       end;
  72.          
  73.    infix "<" (other: INTEGER): BOOLEAN is
  74.      -- Is Current smaller than `other'?
  75.       external "CSE"
  76.       end;
  77.  
  78.    infix "<=" (other: INTEGER): BOOLEAN is
  79.      -- Is Current smaller or equal to `other'?
  80.       external "CSE"
  81.       end;
  82.  
  83.    infix ">" (other: INTEGER): BOOLEAN is
  84.      -- Is Current greater than `other'?
  85.       external "CSE"
  86.       end;
  87.  
  88.    infix ">=" (other: INTEGER): BOOLEAN is
  89.      -- Is Current greater or equal to `other'?
  90.       external "CSE"
  91.       end;
  92.  
  93.    prefix "+": INTEGER is
  94.       do
  95.      Result := Current
  96.       end;
  97.  
  98.    prefix "-": INTEGER is
  99.      -- Unary minus of Current
  100.       external "CSE"
  101.       end;
  102.    
  103.    compare(other: INTEGER): INTEGER is
  104.      -- Compare Current with `other'.
  105.      -- '<' <==> Result < 0
  106.      -- '>' <==> Result > 0
  107.      -- Otherwise Result = 0
  108.       do
  109.      Result := Current - other
  110.       end;
  111.  
  112.    hash_code: INTEGER is
  113.       do
  114.      if Current < 0 then
  115.         Result := -Current
  116.      else
  117.         Result := Current
  118.      end
  119.       end;
  120.     
  121.    one: INTEGER is
  122.       do
  123.      Result := 1
  124.       end;
  125.  
  126.    zero: INTEGER is
  127.       do
  128.      Result := 0
  129.       end;
  130.  
  131.    sqrt: DOUBLE is
  132.       -- Compute the square routine.
  133.       do
  134.      Result := to_real.sqrt;
  135.       end;
  136.    
  137.   gcd(other: INTEGER): INTEGER is
  138.      -- Great Common Divisor of `Current' and `other'.
  139.       require
  140.      Current >= 0;
  141.          other >= 0;
  142.       do
  143.      if other = 0 then 
  144.         Result := Current
  145.          else
  146.             Result := other.gcd(Current \\ other);
  147.          end;    
  148.       ensure
  149.          Result = other.gcd(Current);
  150.       end;
  151.    
  152.    print_on(file: STD_FILE_WRITE) is
  153.       do
  154.      file.put_integer(Current);
  155.       end;
  156.    
  157. feature -- Conversions :
  158.    
  159.    to_real: REAL is
  160.       do
  161.      Result := Current;
  162.       end;
  163.    
  164.    to_double: DOUBLE is
  165.       do
  166.      Result := Current;
  167.       end;
  168.    
  169.    to_string: STRING is
  170.      -- Convert the INTEGER into a new allocated STRING. 
  171.      -- Note: see `append_in' to save memory.
  172.       do
  173.      !!Result.make(0);
  174.      append_in(Result);
  175.       end; 
  176.  
  177.    append_in(str: STRING) is
  178.      -- Append the equivalent of `to_string' at the end of 
  179.      -- `str'. Thus you can save memory because no other
  180.      -- STRING is allocate for the job.
  181.       require
  182.      str /= Void;
  183.       local
  184.      val, i: INTEGER;
  185.       do
  186.      if Current = 0 then
  187.         str.extend('0');
  188.      else
  189.         if Current < 0 then
  190.            str.extend('-');
  191.            (- Current).append_in(str);
  192.         else
  193.            from
  194.           i := str.count + 1;
  195.           val := Current;
  196.            until
  197.           val = 0
  198.            loop
  199.           str.extend((val \\ 10).digit);
  200.           val := val // 10;
  201.            end;
  202.            from  
  203.           val := str.count;
  204.            until
  205.           i >= val
  206.            loop
  207.           str.swap(i,val);
  208.           val := val - 1;
  209.           i := i + 1;
  210.            end;        
  211.         end;
  212.      end;
  213.       end; 
  214.    
  215.    to_string_format(s: INTEGER): STRING is
  216.      -- Same as `to_string' but the result is on `s' character and the 
  217.      -- number is right aligned. 
  218.      -- Note: see `append_in_format' to save memory.
  219.       require
  220.      to_string.count <= s;
  221.       do
  222.      from  
  223.         tmp_string.clear;
  224.         append_in(tmp_string);
  225.      until
  226.         tmp_string.count >= s
  227.      loop
  228.         tmp_string.add_first(' ');
  229.      end;
  230.      Result := clone(tmp_string);
  231.       ensure
  232.      Result.count = s;
  233.       end; 
  234.  
  235.    append_in_format(str: STRING; s: INTEGER) is
  236.      -- Append the equivalent of `to_string_format' at the end of 
  237.      -- `str'. Thus you can save memory because no other
  238.      -- STRING is allocate for the job.
  239.       do
  240.      from
  241.         tmp_string.clear;
  242.         append_in(tmp_string);
  243.      until
  244.         tmp_string.count >= s
  245.      loop
  246.         tmp_string.add_first(' ');
  247.      end;
  248.      str.append(tmp_string);
  249.       ensure
  250.      str.count >= (old str.count) + s;
  251.       end;
  252.    
  253.    digit: CHARACTER is
  254.      -- Gives the corresponding CHARACTER for range 0..9.
  255.       require
  256.      0 <= Current;
  257.      Current <= 9;
  258.       do
  259.      Result := ("0123456789").item(Current + 1);
  260.       ensure
  261.      ("0123456789").has(Result);
  262.      Result.value = Current;
  263.       end;
  264.       
  265.    to_character: CHARACTER is
  266.      -- Return the coresponding ASCII character.
  267.       require
  268.      Current >= 0;
  269.       external "CSE"
  270.       end;
  271.    
  272.    to_octal: INTEGER is
  273.       -- Gives the octal value.
  274.       do
  275.      if Current = 0 then
  276.      elseif Current < 0 then
  277.         Result := -((-Current).to_octal);
  278.      else
  279.         from  
  280.            tmp_string.clear;
  281.            Result := Current;
  282.         until
  283.            Result = 0
  284.         loop
  285.            tmp_string.extend((Result \\ 8).digit);
  286.            Result := Result // 8;
  287.         end;        
  288.         tmp_string.reverse;
  289.         Result := tmp_string.to_integer;
  290.      end;
  291.       end;
  292.    
  293. feature {NONE}
  294.    
  295.    tmp_string: STRING is "0000000000000000000";
  296.      
  297. end -- INTEGER
  298.