home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 3 / AACD03.BIN / AACD / Programming / sofa / archive / SmallEiffel.lha / SmallEiffel / lib_std / comparable.e < prev    next >
Text File  |  1999-06-05  |  4KB  |  116 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. deferred class COMPARABLE
  13.    --
  14.    -- All classes handling COMPARABLE objects with a total order relation
  15.    -- should inherit from this class.
  16.    --
  17.    
  18. feature 
  19.    
  20.    infix "<" (other: like Current): BOOLEAN is
  21.          -- Is `Current' strictly less than `other'?
  22.       require
  23.          other_exists: other /= Void
  24.       deferred
  25.       ensure
  26.          asymmetric: Result implies not (other < Current);
  27.       end;
  28.    
  29.    infix "<=" (other: like Current): BOOLEAN is
  30.          -- Is `Current' less than or equal `other'?
  31.       require
  32.          other_exists: other /= Void
  33.       do
  34.          Result := not (Current > other)
  35.       ensure
  36.          definition: Result = ((Current < other) or is_equal(other));
  37.       end;
  38.  
  39.    infix ">" (other: like Current): BOOLEAN is
  40.          -- Is `Current' strictly greater than `other'?
  41.       require
  42.          other_exists: other /= Void
  43.       do
  44.          Result := (other < Current)
  45.       ensure
  46.          definition: Result = (other < Current)
  47.       end;
  48.  
  49.    infix ">=" (other: like Current): BOOLEAN is
  50.          -- Is `Current' greater than or equal than `other'?
  51.       require
  52.          other_exists: other /= Void
  53.       do
  54.          Result := not (Current < other)
  55.       ensure
  56.          definition: Result = (other <= Current)
  57.       end;
  58.    
  59.    in_range(lower, upper: like Current): BOOLEAN is
  60.          -- Return true if `Current' is in range [`lower'..`upper']
  61.       do
  62.          Result := (Current >= lower) and then (Current <= upper) 
  63.       ensure
  64.          Result = (Current >= lower and Current <= upper)
  65.       end;
  66.  
  67.    compare, three_way_comparison(other: like Current): INTEGER is
  68.       -- If current object equal to `other', 0;
  69.       -- if smaller,  -1; if greater, 1.
  70.       require
  71.          other_exists: other /= Void
  72.       do
  73.          if Current < other then
  74.             Result := -1
  75.          elseif other < Current then
  76.             Result := 1
  77.          end
  78.       ensure
  79.          equal_zero      : (Result =  0) = is_equal(other)
  80.          smaller_negative: (Result = -1) = (Current < other)
  81.          greater_positive: (Result =  1) = (Current > other)
  82.       end
  83.  
  84.    min(other: like Current): like Current is 
  85.          -- Minimum of `Current' and `other'.
  86.       require
  87.          other /= Void
  88.       do
  89.          if Current < other then
  90.             Result := Current
  91.          else
  92.             Result := other
  93.          end;
  94.       ensure
  95.          Result <= Current and then Result <= other;
  96.          compare(Result) = 0 or else other.compare(Result) = 0
  97.       end;
  98.  
  99.    max(other: like Current): like Current is 
  100.          -- Maximum of `Current' and `other'.
  101.       require
  102.          other /= Void
  103.       do
  104.          if other < Current then
  105.             Result := Current;
  106.          else
  107.             Result := other;
  108.          end;
  109.       ensure
  110.          Result >= Current and then Result >= other;
  111.          compare(Result) = 0 or else other.compare(Result) = 0
  112.       end;
  113.  
  114. end -- COMPARABLE
  115.  
  116.