home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Environments / Small Eiffel 0.4.8 / lib_std / integer.e < prev    next >
Encoding:
Text File  |  1997-04-13  |  6.5 KB  |  327 lines  |  [TEXT/ttxt]

  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 "\\", 
  12.      infix "//", infix "^", infix "<", infix "<=", infix ">",
  13.      infix ">=", compare, prefix "-", prefix "+", hash_code, 
  14.      one, zero, out_in_tagged_out_memory, fill_tagged_out_memory
  15.       end;
  16.  
  17. feature 
  18.    
  19.    infix "+"(other: INTEGER): INTEGER is
  20.      -- Add `other' to Current.
  21.       external "CSE"
  22.       end;
  23.    
  24.    infix "-" (other: INTEGER): INTEGER is
  25.      -- Subtract `other' from Current.
  26.       external "CSE"
  27.       end;
  28.  
  29.    infix "*" (other: INTEGER): INTEGER is
  30.      -- Multiply `other' by Current.
  31.       external "CSE"
  32.       end;
  33.  
  34.    infix "/" (other: INTEGER): DOUBLE is
  35.      -- Divide Current by `other'.
  36.       external "CSE"
  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' strictly less than 'other'?
  75.       external "CSE"
  76.       end;
  77.  
  78.    infix "<=" (other: INTEGER): BOOLEAN is
  79.      -- Is 'Current' less or equal 'other'?
  80.       external "CSE"
  81.       end;
  82.  
  83.    infix ">" (other: INTEGER): BOOLEAN is
  84.      -- Is 'Current' strictly greater than 'other'?
  85.       external "CSE"
  86.       end;
  87.  
  88.    infix ">=" (other: INTEGER): BOOLEAN is
  89.      -- Is 'Current' greater or equal than '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.    odd: BOOLEAN is
  113.       do
  114.      Result := (Current \\ 2) = 1;
  115.       end;
  116.  
  117.    even: BOOLEAN is
  118.       do
  119.      Result := (Current \\ 2) = 0;
  120.       end;
  121.  
  122.    hash_code: INTEGER is
  123.       do
  124.      if Current < 0 then
  125.         Result := -Current
  126.      else
  127.         Result := Current
  128.      end
  129.       end;
  130.     
  131.    one: INTEGER is 1;
  132.  
  133.    zero: INTEGER is 0;
  134.  
  135.    sqrt: DOUBLE is
  136.       -- Compute the square routine.
  137.       do
  138.      Result := to_double.sqrt;
  139.       end;
  140.    
  141.   gcd(other: INTEGER): INTEGER is
  142.      -- Great Common Divisor of `Current' and `other'.
  143.       require
  144.      Current >= 0;
  145.          other >= 0;
  146.       do
  147.      if other = 0 then 
  148.         Result := Current
  149.          else
  150.             Result := other.gcd(Current \\ other);
  151.          end;    
  152.       ensure
  153.          Result = other.gcd(Current);
  154.       end;
  155.    
  156. feature -- Conversions :
  157.    
  158.    to_real: REAL is
  159.       do
  160.      Result := Current;
  161.       end;
  162.    
  163.    to_double: DOUBLE is
  164.       do
  165.      Result := Current;
  166.       end;
  167.    
  168.    to_string: STRING is
  169.      -- Convert the INTEGER into a new allocated STRING. 
  170.      -- Note: see `append_in' to save memory.
  171.       do
  172.      !!Result.make(0);
  173.      append_in(Result);
  174.       end; 
  175.  
  176.    to_boolean: BOOLEAN is
  177.       do
  178.      if Current /= 0 then
  179.         Result := true;
  180.      end;
  181.       end;
  182.  
  183.    to_bit: BIT Integer_bits is
  184.       do
  185.          c_inline_c("R=C;");
  186.       end;
  187.  
  188.    append_in(str: STRING) is
  189.      -- Append the equivalent of `to_string' at the end of 
  190.      -- `str'. Thus you can save memory because no other
  191.      -- STRING is allocate for the job.
  192.       require
  193.      str /= Void;
  194.       local
  195.      val, i: INTEGER;
  196.       do
  197.      if Current = 0 then
  198.         str.extend('0');
  199.      else
  200.         if Current > 0 then
  201.            from
  202.           i := str.count + 1;
  203.           val := Current;
  204.            until
  205.           val = 0
  206.            loop
  207.           str.extend((val \\ 10).digit);
  208.           val := val // 10;
  209.            end;
  210.         else
  211.            str.extend('-');
  212.            from
  213.           i := str.count + 1;
  214.           val := Current;
  215.            until
  216.           val = 0
  217.            loop
  218.           str.extend((-(val \\ 10)).digit);
  219.           val := val // 10;
  220.            end;
  221.         end;
  222.         from  
  223.            val := str.count;
  224.         until
  225.            i >= val
  226.         loop
  227.            str.swap(i,val);
  228.            val := val - 1;
  229.            i := i + 1;
  230.         end;
  231.      end;
  232.       end; 
  233.    
  234.    to_string_format(s: INTEGER): STRING is
  235.      -- Same as `to_string' but the result is on `s' character and the 
  236.      -- number is right aligned. 
  237.      -- Note: see `append_in_format' to save memory.
  238.       require
  239.      to_string.count <= s;
  240.       do
  241.      from  
  242.         tmp_string.clear;
  243.         append_in(tmp_string);
  244.      until
  245.         tmp_string.count >= s
  246.      loop
  247.         tmp_string.add_first(' ');
  248.      end;
  249.      Result := clone(tmp_string);
  250.       ensure
  251.      Result.count = s;
  252.       end; 
  253.  
  254.    append_in_format(str: STRING; s: INTEGER) is
  255.      -- Append the equivalent of `to_string_format' at the end of 
  256.      -- `str'. Thus you can save memory because no other
  257.      -- STRING is allocate for the job.
  258.       do
  259.      from
  260.         tmp_string.clear;
  261.         append_in(tmp_string);
  262.      until
  263.         tmp_string.count >= s
  264.      loop
  265.         tmp_string.add_first(' ');
  266.      end;
  267.      str.append(tmp_string);
  268.       ensure
  269.      str.count >= (old str.count) + s;
  270.       end;
  271.    
  272.    digit: CHARACTER is
  273.      -- Gives the corresponding CHARACTER for range 0..9.
  274.       require
  275.      0 <= Current;
  276.      Current <= 9
  277.       do
  278.      Result := (Current + ('0').code).to_character;
  279.       ensure
  280.      ("0123456789").has(Result);
  281.      Result.value = Current
  282.       end;
  283.       
  284.    to_character: CHARACTER is
  285.      -- Return the coresponding ASCII character.
  286.       require
  287.      Current >= 0;
  288.       external "CSE"
  289.       end;
  290.    
  291.    to_octal: INTEGER is
  292.       -- Gives the octal value.
  293.       do
  294.      if Current = 0 then
  295.      elseif Current < 0 then
  296.         Result := -((-Current).to_octal);
  297.      else
  298.         from  
  299.            tmp_string.clear;
  300.            Result := Current;
  301.         until
  302.            Result = 0
  303.         loop
  304.            tmp_string.extend((Result \\ 8).digit);
  305.            Result := Result // 8;
  306.         end;        
  307.         tmp_string.reverse;
  308.         Result := tmp_string.to_integer;
  309.      end;
  310.       end;
  311.    
  312. feature -- Object Printing :
  313.  
  314.    out_in_tagged_out_memory, fill_tagged_out_memory is
  315.       do
  316.      Current.append_in(tagged_out_memory);
  317.       end;
  318.  
  319. feature {NONE}
  320.    
  321.    tmp_string: STRING is 
  322.       once
  323.      !!Result.make(128);
  324.       end;
  325.      
  326. end -- INTEGER
  327.