Ignore:
Timestamp:
Dec 1, 2010, 10:08:29 PM (12 years ago)
Author:
Peter
Message:

use compare(Segment, Segment) as comparator in SegmentSet? which allows simplifications in SegmentSet? code. Add some tests and docs.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/yat/utility/Segment.h

    r2321 r2357  
    2323*/
    2424
     25#include "yat_assert.h"
     26
    2527#include <algorithm>
    2628#include <functional>
     
    3335     \brief a class for a Segment or Interval
    3436
    35      A class for a Segment [begin, end).
     37     A Segment is defined by its \a begin and \a end. The end is not
     38     included in the Segment, i.e., you could write the Segment as
     39     [begin, end). Type T must be comparable, optionally with a
     40     comparator Compare, which should implement <a
     41     href="http://www.sgi.com/tech/stl/StrictWeakOrdering.html">
     42     Strict Weak Ordering.</a>.   
    3643
    3744     \since new in yat 0.7
     
    8996
    9097  /**
    91      \return true if all elements in \a lhs is less than all elements
    92      in \a rhs, i.e., returns false if rhs.begin() is less than
    93      lhs.end()
     98     This function takes two Segment and compare them with comparator
     99     Compare. If all elements in \a lhs is less than all elements in
     100     \a rhs, \c true is returned. In other words, \c false is returned
     101     if \a rhs.begin() is less than \a lhs.end().
     102
     103     The exception is if both \a lhs and \a rhs are empty Segment
     104     (i.e. begin equals end) and their begins and ends are equal, in
     105     which case \c false is returned. This exception implies that
     106     compare(x,x) always returns false.
    94107
    95108     \relates Segment
     
    101114  {
    102115    Compare c;
     116    // begin <= end
     117    YAT_ASSERT(!c(lhs.end(), lhs.begin()));
     118    YAT_ASSERT(!c(rhs.end(), rhs.begin()));
     119    // take care of case when both sides are zero segments
     120    if (!c(lhs.begin(), lhs.end()) && !c(rhs.begin(), rhs.end())) {
     121      return c(lhs.begin(), rhs.begin());
     122    }
     123
    103124    return ! c(rhs.begin(), lhs.end());
    104125  }
    105126
    106127  /**
    107      \return -1 if all elements in lhs is less than all elements in
    108      rhs; +1 if all elements in rhs is less than all elements in lhs;
    109      and 0 if lhs and rhs overlap.
     128     \return -1 if compare(lhs, rhs) is \c true, +1 if compare(rhs,
     129     lhs) is \c true, and 0 otherwise
    110130
    111131     \relates Segment
     132
     133     \see compare(const Segment<T, Compare>&, const Segment<T, Compare>&)
    112134
    113135     \since new in yat 0.7
     
    115137  template<typename T, class Compare>
    116138  int compare_3way(const Segment<T, Compare>& lhs,
    117                     const Segment<T, Compare>& rhs)
     139                   const Segment<T, Compare>& rhs)
    118140  {
    119141    if (compare(lhs, rhs))
     
    125147
    126148  /**
    127      \return -1 if lhs is less than all elements in rhs; +1 if all
    128      elements in rhs is less than lhs; and 0 if lhs and rhs overlap
     149     \return -1 if \a element is less than all elements in \a segment,
     150     0 if \a element overlaps with \a segment, and 1 otherwise.
    129151
    130152     \relates Segment
     
    133155   */
    134156  template<typename T, class Compare>
    135   int compare_3way(const T& lhs,
    136                    const Segment<T, Compare>& rhs)
     157  int compare_3way(const T& element,
     158                   const Segment<T, Compare>& segment)
    137159  {
    138160    Compare comp;
    139     if (comp(lhs, rhs.begin()))
     161    if (comp(element, segment.begin()))
    140162      return -1;
    141     if (comp(lhs, rhs.end()))
     163    if (comp(element, segment.end()))
    142164      return 0;
    143165    return 1;
     
    145167
    146168  /**
    147      \return intersection of \a a and \a b. If \a a and \b do not
     169     \return intersection of \a a and \a b. If \a a and \a b do not
    148170     overlap an empty Section is returned (begin==end), but the exact
    149171     value of begin is undefined.
     
    161183
    162184    result.begin() = std::max(a.begin(), b.begin(), comp);
     185    // the first max is needed in case a and b don't overlap
    163186    result.end() = std::max(result.begin(),
    164187                            std::min(a.end(), b.end(), comp),
Note: See TracChangeset for help on using the changeset viewer.