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

  1. -- Part of SmallEiffel -- Read DISCLAIMER file -- Copyright (C) 
  2. -- Dominique COLNET and Suzanne COLLIN -- colnet@loria.fr
  3. --
  4. deferred class COMPARABLE
  5.    --
  6.    -- All class handling COMPARABLE object with a total order relation
  7.    -- should inherit from this class.
  8.    --
  9.    
  10. inherit 
  11.    ANY 
  12.       redefine 
  13.      is_equal 
  14.       end;
  15.    
  16. feature 
  17.    
  18.    infix "<" (other: like Current): BOOLEAN is
  19.      -- Is 'Current' strictly less than 'other'?
  20.       require
  21.      other_exists: other /= Void
  22.       deferred
  23.       ensure
  24.      asymmetric: Result implies not (other < Current);
  25.       end;
  26.    
  27.    infix "<=" (other: like Current): BOOLEAN is
  28.      -- Is 'Current' less or equal 'other'?
  29.       require
  30.      other_exists: other /= Void
  31.       do
  32.      Result := not (Current > other)
  33.       ensure
  34.      definition: Result = (Current < other) or is_equal(other);
  35.       end;
  36.  
  37.    infix ">" (other: like Current): BOOLEAN is
  38.      -- Is 'Current' strictly greater than 'other'?
  39.       require
  40.      other_exists: other /= Void
  41.       do
  42.      Result := (other < Current)
  43.       ensure
  44.      definition: Result = (other < Current)
  45.       end;
  46.  
  47.    infix ">=" (other: like Current): BOOLEAN is
  48.      -- Is 'Current' greater or equal than 'other'?
  49.       require
  50.      other_exists: other /= Void
  51.       do
  52.      Result := not (Current < other)
  53.       ensure
  54.      definition: Result = (other <= Current)
  55.       end;
  56.    
  57.    is_equal(other: like Current): BOOLEAN is
  58.       do
  59.      if Current < other then
  60.      elseif other < Current then
  61.      else
  62.         Result := true;
  63.      end;
  64.       ensure then
  65.      trichotomy: Result = (not (Current < other) 
  66.                    and not (other < Current));
  67.       end;
  68.       
  69.    compare(other: like Current): INTEGER is
  70.      -- Compare 'Current' with 'other'.
  71.      -- '<' <==> Result < 0
  72.      -- '>' <==> Result > 0
  73.      -- Otherwise Result = 0.
  74.       require
  75.      other /= Void
  76.       do
  77.      if Current < other then
  78.         Result := -1
  79.      elseif other < Current then
  80.         Result := 1
  81.      end
  82.       ensure
  83.      (Result < 0) = (Current < other);
  84.      (Result = 0) = not (Current < other or Current > other);
  85.      (Result > 0) = (Current > other)
  86.       end;
  87.    
  88.    min(other: like Current): like Current is 
  89.      -- Minimum of 'Current' and 'other'.
  90.       require
  91.      other /= Void
  92.       do
  93.      if Current < other then
  94.         Result := Current
  95.      else
  96.         Result := other
  97.      end;
  98.       ensure
  99.      Result <= Current and then Result <= other;
  100.      compare(Result) = 0 or else other.compare(Result) = 0
  101.       end;
  102.  
  103.    max(other: like Current): like Current is 
  104.      -- Maximum of 'Current' and 'other'.
  105.       require
  106.      other /= Void
  107.       do
  108.      if other < Current then
  109.         Result := Current;
  110.      else
  111.         Result := other;
  112.      end;
  113.       ensure
  114.      Result >= Current and then Result >= other;
  115.      compare(Result) = 0 or else other.compare(Result) = 0
  116.       end;
  117.  
  118. end -- COMPARABLE
  119.