home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Environments / SmallEiffel 0.3.3 / SmallEiffel PPC / lib_std / integer.e < prev    next >
Encoding:
Text File  |  1996-06-13  |  6.2 KB  |  311 lines  |  [TEXT/EDIT]

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