Author: aconway
Date: Wed Apr 23 08:31:57 2008
New Revision: 650906

URL: http://svn.apache.org/viewvc?rev=650906&view=rev
Log:

 - SequenceSet implemented on RangeSet.
 - Reduced #include dependencides on SequenceSet.h

Modified:
    incubator/qpid/trunk/qpid/cpp/src/qpid/DataDir.cpp
    incubator/qpid/trunk/qpid/cpp/src/qpid/RangeSet.h
    incubator/qpid/trunk/qpid/cpp/src/qpid/amqp_0_10/built_in_types.h
    incubator/qpid/trunk/qpid/cpp/src/qpid/broker/SessionAdapter.cpp
    incubator/qpid/trunk/qpid/cpp/src/qpid/broker/SessionAdapter.h
    incubator/qpid/trunk/qpid/cpp/src/qpid/broker/SessionHandler.h
    incubator/qpid/trunk/qpid/cpp/src/qpid/client/Results.cpp
    incubator/qpid/trunk/qpid/cpp/src/qpid/client/Results.h
    incubator/qpid/trunk/qpid/cpp/src/qpid/framing/SequenceSet.cpp
    incubator/qpid/trunk/qpid/cpp/src/qpid/framing/SequenceSet.h
    incubator/qpid/trunk/qpid/cpp/src/tests/Makefile.am
    incubator/qpid/trunk/qpid/cpp/src/tests/RangeSet.cpp
    incubator/qpid/trunk/qpid/cpp/src/tests/SequenceSet.cpp

Modified: incubator/qpid/trunk/qpid/cpp/src/qpid/DataDir.cpp
URL: 
http://svn.apache.org/viewvc/incubator/qpid/trunk/qpid/cpp/src/qpid/DataDir.cpp?rev=650906&r1=650905&r2=650906&view=diff
==============================================================================
--- incubator/qpid/trunk/qpid/cpp/src/qpid/DataDir.cpp (original)
+++ incubator/qpid/trunk/qpid/cpp/src/qpid/DataDir.cpp Wed Apr 23 08:31:57 2008
@@ -54,10 +54,8 @@
             throw Exception ("Data directory is locked by another process");
         if (errno == EACCES)
             throw Exception ("Insufficient privileges for data directory");
-
-        std::ostringstream oss;
-        oss << "Error locking data directory: errno=" << errno;
-        throw Exception (oss.str ());
+        throw Exception(
+            QPID_MSG("Error locking " << lockFile << ": " << strError(errno)));
     }
 
     QPID_LOG (info, "Locked data directory: " << dirPath);

Modified: incubator/qpid/trunk/qpid/cpp/src/qpid/RangeSet.h
URL: 
http://svn.apache.org/viewvc/incubator/qpid/trunk/qpid/cpp/src/qpid/RangeSet.h?rev=650906&r1=650905&r2=650906&view=diff
==============================================================================
--- incubator/qpid/trunk/qpid/cpp/src/qpid/RangeSet.h (original)
+++ incubator/qpid/trunk/qpid/cpp/src/qpid/RangeSet.h Wed Apr 23 08:31:57 2008
@@ -30,17 +30,27 @@
 
 namespace qpid {
 
-/** A range of values, used in RangeSet */
+/** A range of values, used in RangeSet.
+ * Range(begin, end) includes begin but excludes end.
+ * Range::makeClosed(first,last) includes both first and last.
+ */
 template <class T>
 class Range {
   public:
+    static Range makeClosed(const T& first, T last) { return Range(first, 
++last); }
+    
     Range() : begin_(), end_() {}
     explicit Range(const T& t) : begin_(t), end_(t) { ++end_; }
     Range(const T& b, const T& e) : begin_(b), end_(e) { assert(b <= e); }
 
     T begin() const { return begin_; }
+    /** End of _open_ range, i.e. !contains(end()) */
     T end() const { return end_; }
 
+    T first() const { assert(!empty()); return begin_; }
+    /** Last in closed range, i.e. contains(end()) */
+    T last() const { assert(!empty()); T ret=end_; return --ret; }
+
     void begin(const T& t) { begin_ = t; }
     void end(const T& t) { end_ = t; }
 
@@ -48,6 +58,7 @@
 
     bool contains(const T& x) const { return begin_ <= x && x < end_; }
     bool contains(const Range& r) const { return begin_ <= r.begin_ && r.end_ 
<= end_; }
+    bool strictContains(const Range& r) const { return begin_ < r.begin_ && 
r.end_ < end_; }
 
     bool operator==(const Range& x) { return begin_ == x.begin_ && end_== 
x.end_; }
 
@@ -66,6 +77,8 @@
     }
 
     operator bool() const { return !empty(); }
+
+    template <class S> void serialize(S& s) { s(begin_)(end_); }
         
   private:
     T begin_, end_;
@@ -83,7 +96,10 @@
                        boost::additive2<RangeSet<T>, Range<T>,
                                         boost::additive2<RangeSet<T>, T> > >
 {
+  public:
     typedef qpid::Range<T> Range;
+
+  private:
     typedef InlineVector<Range, 3> Ranges; // TODO aconway 2008-04-21: what's 
the optimial inlined value?
 
   public:
@@ -116,8 +132,8 @@
     typedef iterator const_iterator;
     
     RangeSet() {}
-    explicit RangeSet(const Range& r) { ranges.push_back(r); }
-    RangeSet(const T& a, const T& b) { ranges.push_back(Range(a,b)); }
+    explicit RangeSet(const Range& r) { *this += r; }
+    RangeSet(const T& a, const T& b) { *this += Range(a,b); }
     
     bool contiguous() const { return ranges.size() <= 1; }
 
@@ -129,12 +145,15 @@
 
     bool operator==(const RangeSet<T>&) const;
 
+    void addRange (const Range&);
+    void addSet (const RangeSet&);
+
+    RangeSet& operator+=(const T& t) { return *this += Range(t); }
+    RangeSet& operator+=(const Range& r) { addRange(r); return *this; }
+    RangeSet& operator+=(const RangeSet& s) { addSet(s); return *this; }
+
     void removeRange (const Range&);
     void removeSet (const RangeSet&);
-    
-    RangeSet& operator+=(const T& t) { return *this += Range(t); }
-    RangeSet& operator+=(const Range&);
-    RangeSet& operator+=(const RangeSet&) { return *this; };
 
     RangeSet& operator-=(const T& t)  { return *this -= Range(t); }
     RangeSet& operator-=(const Range& r) { removeRange(r); return *this; }
@@ -143,16 +162,28 @@
     T front() const { return ranges.front().begin(); }
     T back() const { return ranges.back().end(); }
 
+    // Iterate over elements in the set.
     iterator begin() const;
     iterator end() const;
-    
-    bool empty() const { return ranges.empty(); }
 
+    // Iterate over ranges in the set.
+    typedef typename Ranges::const_iterator RangeIterator;
+    RangeIterator rangesBegin() const { return ranges.begin(); }
+    RangeIterator rangesEnd() const { return ranges.end(); }
+    size_t rangesSize() const { return ranges.size(); }
+
+    bool empty() const { return ranges.empty(); }
+    void clear() { ranges.clear(); }
+    
     /** Return the largest contiguous range containing x.
      * Returns the empty range [x,x) if x is not in the set.
      */
     Range rangeContaining(const T&) const;
 
+    template <class S> void serialize(S& s) { s.split(*this); 
s(ranges.begin(), ranges.end()); }
+    template <class S> void encode(S& s) const { 
s(uint16_t(ranges.size()*sizeof(Range))); }
+    template <class S> void decode(S& s) { uint16_t sz; s(sz); 
ranges.resize(sz/sizeof(Range)); }
+    
  private:
     Ranges ranges;
 
@@ -188,10 +219,13 @@
     return i != ranges.end() && i->contains(r);
 }
 
-template <class T> RangeSet<T>& RangeSet<T>::operator+=(const Range& r) {
+template <class T> void RangeSet<T>::addRange(const Range& r) {
+    if (r.empty()) return;
     typename Ranges::iterator i =
         std::lower_bound(ranges.begin(), ranges.end(), r.begin());
-    if (i != ranges.end() && i->touching(r)) {
+    if (i == ranges.end() || !i->touching(r)) 
+        ranges.insert(i, r);
+    else {
         i->merge(r);
         typename Ranges::iterator j = i;
         if (++j != ranges.end() && i->touching(*j)) {
@@ -199,19 +233,23 @@
             ranges.erase(j);
         }
     }
-    else
-        ranges.insert(i, r);
-    return *this;
+}
+
+
+template <class T> void RangeSet<T>::addSet(const RangeSet<T>& s) {
+    std::for_each(s.ranges.begin(), s.ranges.end(), 
+                  boost::bind((RangeSet<T>& (RangeSet<T>::*)(const 
Range&))&RangeSet<T>::operator+=, this, _1));
 }
 
 template <class T> void RangeSet<T>::removeRange(const Range& r) {
+    if (r.empty()) return;
     typename Ranges::iterator i,j;
     i = std::lower_bound(ranges.begin(), ranges.end(), r.begin());
     if (i == ranges.end() || i->begin() >= r.end())
         return;                 // Outside of set
     if (*i == r)                // Erase i
         ranges.erase(i);
-    else if (i->contains(r)) {  // Split i
+    else if (i->strictContains(r)) {  // Split i
         Range i1(i->begin(), r.begin());
         Range i2(r.end(), i->end());
         *i = i2;
@@ -272,6 +310,7 @@
         std::lower_bound(ranges.begin(), ranges.end(), t);
     return (i != ranges.end() && i->contains(t)) ? *i : Range(t,t);
 }
+
 
 } // namespace qpid
 

Modified: incubator/qpid/trunk/qpid/cpp/src/qpid/amqp_0_10/built_in_types.h
URL: 
http://svn.apache.org/viewvc/incubator/qpid/trunk/qpid/cpp/src/qpid/amqp_0_10/built_in_types.h?rev=650906&r1=650905&r2=650906&view=diff
==============================================================================
--- incubator/qpid/trunk/qpid/cpp/src/qpid/amqp_0_10/built_in_types.h (original)
+++ incubator/qpid/trunk/qpid/cpp/src/qpid/amqp_0_10/built_in_types.h Wed Apr 
23 08:31:57 2008
@@ -22,8 +22,6 @@
  */
 
 #include "qpid/Serializer.h"
-#include "qpid/framing/SequenceNumber.h"
-#include "qpid/framing/SequenceSet.h"
 #include "qpid/framing/Uuid.h"
 #include "qpid/sys/Time.h"
 #include "Decimal.h"
@@ -38,6 +36,12 @@
 /[EMAIL PROTECTED] Mapping from built-in AMQP types to C++ types */
 
 namespace qpid {
+
+namespace framing {
+class SequenceNumber;
+class SequenceSet;
+}
+
 namespace amqp_0_10 {
 
 /** Wrapper that behaves like type T but is a distinct type for
@@ -143,6 +147,7 @@
 class Struct32;
 class List;
 class UnknownType;
+
 template <class T> struct  ArrayDomain;
 typedef ArrayDomain<UnknownType> Array;
 

Modified: incubator/qpid/trunk/qpid/cpp/src/qpid/broker/SessionAdapter.cpp
URL: 
http://svn.apache.org/viewvc/incubator/qpid/trunk/qpid/cpp/src/qpid/broker/SessionAdapter.cpp?rev=650906&r1=650905&r2=650906&view=diff
==============================================================================
--- incubator/qpid/trunk/qpid/cpp/src/qpid/broker/SessionAdapter.cpp (original)
+++ incubator/qpid/trunk/qpid/cpp/src/qpid/broker/SessionAdapter.cpp Wed Apr 23 
08:31:57 2008
@@ -25,6 +25,7 @@
 #include "qpid/framing/constants.h"
 #include "qpid/log/Statement.h"
 #include "qpid/amqp_0_10/exceptions.h"
+#include "qpid/framing/SequenceSet.h"
 #include <boost/format.hpp>
 #include <boost/cast.hpp>
 #include <boost/bind.hpp>

Modified: incubator/qpid/trunk/qpid/cpp/src/qpid/broker/SessionAdapter.h
URL: 
http://svn.apache.org/viewvc/incubator/qpid/trunk/qpid/cpp/src/qpid/broker/SessionAdapter.h?rev=650906&r1=650905&r2=650906&view=diff
==============================================================================
--- incubator/qpid/trunk/qpid/cpp/src/qpid/broker/SessionAdapter.h (original)
+++ incubator/qpid/trunk/qpid/cpp/src/qpid/broker/SessionAdapter.h Wed Apr 23 
08:31:57 2008
@@ -26,7 +26,6 @@
 #include "qpid/Exception.h"
 #include "qpid/framing/AMQP_ServerOperations.h"
 #include "qpid/framing/reply_exceptions.h"
-#include "qpid/framing/SequenceSet.h"
 #include "qpid/framing/StructHelper.h"
 
 #include <vector>

Modified: incubator/qpid/trunk/qpid/cpp/src/qpid/broker/SessionHandler.h
URL: 
http://svn.apache.org/viewvc/incubator/qpid/trunk/qpid/cpp/src/qpid/broker/SessionHandler.h?rev=650906&r1=650905&r2=650906&view=diff
==============================================================================
--- incubator/qpid/trunk/qpid/cpp/src/qpid/broker/SessionHandler.h (original)
+++ incubator/qpid/trunk/qpid/cpp/src/qpid/broker/SessionHandler.h Wed Apr 23 
08:31:57 2008
@@ -30,7 +30,7 @@
 #include "qpid/framing/Array.h"
 #include "qpid/framing/ChannelHandler.h"
 #include "qpid/framing/SequenceNumber.h"
-#include "qpid/framing/SequenceSet.h"
+
 
 #include <boost/noncopyable.hpp>
 

Modified: incubator/qpid/trunk/qpid/cpp/src/qpid/client/Results.cpp
URL: 
http://svn.apache.org/viewvc/incubator/qpid/trunk/qpid/cpp/src/qpid/client/Results.cpp?rev=650906&r1=650905&r2=650906&view=diff
==============================================================================
--- incubator/qpid/trunk/qpid/cpp/src/qpid/client/Results.cpp (original)
+++ incubator/qpid/trunk/qpid/cpp/src/qpid/client/Results.cpp Wed Apr 23 
08:31:57 2008
@@ -21,6 +21,7 @@
 
 #include "Results.h"
 #include "FutureResult.h"
+#include "qpid/framing/SequenceSet.h"
 
 using namespace qpid::framing;
 using namespace boost;

Modified: incubator/qpid/trunk/qpid/cpp/src/qpid/client/Results.h
URL: 
http://svn.apache.org/viewvc/incubator/qpid/trunk/qpid/cpp/src/qpid/client/Results.h?rev=650906&r1=650905&r2=650906&view=diff
==============================================================================
--- incubator/qpid/trunk/qpid/cpp/src/qpid/client/Results.h (original)
+++ incubator/qpid/trunk/qpid/cpp/src/qpid/client/Results.h Wed Apr 23 08:31:57 
2008
@@ -20,7 +20,6 @@
  */
 
 #include "qpid/framing/SequenceNumber.h"
-#include "qpid/framing/SequenceSet.h"
 #include <map>
 #include <boost/shared_ptr.hpp>
 

Modified: incubator/qpid/trunk/qpid/cpp/src/qpid/framing/SequenceSet.cpp
URL: 
http://svn.apache.org/viewvc/incubator/qpid/trunk/qpid/cpp/src/qpid/framing/SequenceSet.cpp?rev=650906&r1=650905&r2=650906&view=diff
==============================================================================
--- incubator/qpid/trunk/qpid/cpp/src/qpid/framing/SequenceSet.cpp (original)
+++ incubator/qpid/trunk/qpid/cpp/src/qpid/framing/SequenceSet.cpp Wed Apr 23 
08:31:57 2008
@@ -20,11 +20,16 @@
  */
 
 #include "SequenceSet.h"
+#include "Buffer.h"
+#include "qpid/framing/reply_exceptions.h"
 
 using namespace qpid::framing;
 using std::max;
 using std::min;
 
+namespace qpid {
+namespace framing {
+
 namespace {
 //each range contains 2 numbers, 4 bytes each
 uint16_t RANGE_SIZE = 2 * 4; 
@@ -32,9 +37,10 @@
 
 void SequenceSet::encode(Buffer& buffer) const
 {
-    buffer.putShort(ranges.size() * RANGE_SIZE);
-    for (Ranges::const_iterator i = ranges.begin(); i != ranges.end(); i++) {
-        i->encode(buffer);
+    buffer.putShort(rangesSize() * RANGE_SIZE);
+    for (RangeIterator i = rangesBegin(); i != rangesEnd(); i++) {
+        buffer.putLong(i->first().getValue());
+        buffer.putLong(i->last().getValue());
     }
 }
 
@@ -42,189 +48,53 @@
 {
     uint16_t size = buffer.getShort();
     uint16_t count = size / RANGE_SIZE;//number of ranges
-    if (size % RANGE_SIZE) throw FrameErrorException(QPID_MSG("Invalid size 
for sequence set: " << size)); 
+    if (size % RANGE_SIZE)
+        throw FrameErrorException(QPID_MSG("Invalid size for sequence set: " 
<< size)); 
 
     for (uint16_t i = 0; i < count; i++) {
         add(SequenceNumber(buffer.getLong()), 
SequenceNumber(buffer.getLong()));
     }
 }
 
-uint32_t SequenceSet::size() const
-{
-    return 2 /*size field*/ + (ranges.size() * RANGE_SIZE);
+uint32_t SequenceSet::size() const {
+    return 2 /*size field*/ + (rangesSize() * RANGE_SIZE);
 }
 
-bool SequenceSet::contains(const SequenceNumber& point) const
-{
-    for (Ranges::const_iterator i = ranges.begin(); i != ranges.end(); i++) {
-        if (i->contains(point)) return true;
-    }
-    return false;
+bool SequenceSet::contains(const SequenceNumber& s) const {
+    return RangeSet<SequenceNumber>::contains(s);
 }
 
-void SequenceSet::add(const SequenceNumber& s)
-{
-    add(s, s);
-}
+void SequenceSet::add(const SequenceNumber& s) { *this += s; }
 
-void SequenceSet::add(const SequenceNumber& start, const SequenceNumber& end)
-{
-    if (start > end) {
-        add(end, start);
-    } else {
-        Range r(start, end);
-        Ranges::iterator merged = ranges.end();
-        Ranges::iterator i = ranges.begin();
-        while (i != ranges.end() && merged == ranges.end() && i->start < 
start) {
-            if (i->merge(r)) merged = i;            
-            i++;
-        }
-        if (merged == ranges.end()) {
-            i = merged = ranges.insert(i, r);
-            i++;
-        }
-        while (i != ranges.end() && merged->merge(*i)) {
-            i = ranges.erase(i);
-        }
-    }
+void SequenceSet::add(const SequenceNumber& start, const SequenceNumber& 
finish) {
+    *this += Range::makeClosed(std::min(start,finish), std::max(start, 
finish));
 }
 
-void SequenceSet::add(const SequenceSet& set)
-{
-    for (Ranges::const_iterator i = set.ranges.begin(); i != set.ranges.end(); 
i++) {
-        add(i->start, i->end);
-    }
-}
+void SequenceSet::add(const SequenceSet& set) { *this += set; }
 
-void SequenceSet::remove(const SequenceSet& set)
-{
-    for (Ranges::const_iterator i = set.ranges.begin(); i != set.ranges.end(); 
i++) {
-        remove(i->start, i->end);
-    }    
-}
+void SequenceSet::remove(const SequenceSet& set) { *this -= set; }
 
-void SequenceSet::remove(const SequenceNumber& start, const SequenceNumber& 
end)
-{
-    if (start > end) {
-        remove(end, start);
-    } else {
-        Ranges::iterator i = ranges.begin(); 
-        while (i != ranges.end() && i->start < start) {
-            if (start <= i->end) {
-                if (end > i->end) {
-                    //i.e. start is within the range pointed to by i, but end 
is not
-                    i->end = (uint32_t)start - 1;
-                } else {
-                    //whole of range to be deleted is contained within that 
pointed to be i
-                    if (end == i->end) {
-                        //just shrink range pointed to by i
-                        i->end = (uint32_t)start - 1;
-                    } else {
-                        //need to split the range pointed to by i
-                        Range r(i->start, (uint32_t)start - 1);
-                        i->start = end + 1;
-                        ranges.insert(i, r);
-                    }
-                    return;//no need to go any further
-                }
-            }
-            i++;
-        }
-        Ranges::iterator j = i;
-        while (j != ranges.end() && j->end < end) {            
-            j++;
-        }
-        if (j->start <= end){
-            j->start = end + 1;
-        }
-        ranges.erase(i, j);        
-    }
+void SequenceSet::remove(const SequenceNumber& start, const SequenceNumber& 
finish) {
+    *this -= Range::makeClosed(std::min(start,finish), std::max(start, 
finish));
 }
 
-void SequenceSet::remove(const SequenceNumber& s)
-{
-    for (Ranges::iterator i = ranges.begin(); i != ranges.end() && s >= 
i->start; i++) {
-        if (i->contains(s)) {
-            if (i->start == i->end) {
-                //range is just a single number, so we can delete the whole 
range
-                i = ranges.erase(i);
-            } else if (i->start == s) {
-                //move the start forward to exclude s
-                ++(i->start);
-            } else if (i->end == s) {
-                //move the end backward to exclude s
-                --(i->end);
-            } else {
-                //need to split range pointed to by i
-                Range r(i->start, (uint32_t)s - 1);
-                i->start = s + 1;
-                i = ranges.insert(i, r);
-            }
-            break;
-        }
-    }    
-}
+void SequenceSet::remove(const SequenceNumber& s) { *this -= s; }
 
-bool SequenceSet::empty() const
-{
-    return ranges.empty();
-}
 
-void SequenceSet::clear()
-{
-    return ranges.clear();
-}
-
-bool SequenceSet::Range::contains(SequenceNumber i) const 
-{ 
-    return i >= start && i <= end; 
-}
-
-bool SequenceSet::Range::intersects(const Range& r) const 
-{ 
-    return r.contains(start) || r.contains(end) || contains(r.start) || 
contains(r.end); 
-}
-
-bool SequenceSet::Range::merge(const Range& r) 
-{ 
-    if (intersects(r) || mergeable(r.end) || r.mergeable(end)) {
-        start = min(start, r.start); 
-        end = max(end, r.end); 
-        return true;
-    } else {
-        return false;
-    }
-}
-
-bool SequenceSet::Range::mergeable(const SequenceNumber& s) const
-{ 
-    if (contains(s) || start - s == 1 || s - end == 1) {
-        return true;
-    } else {
-        return false;
+struct RangePrinter {
+    std::ostream& out;
+    RangePrinter(std::ostream& o) : out(o) {}
+    void operator()(SequenceNumber i, SequenceNumber j) {
+        out << "[" << i.getValue() << "," << j.getValue() << "] ";
     }
-}
+};
 
-void SequenceSet::Range::encode(Buffer& buffer) const
-{
-    buffer.putLong(start);
-    buffer.putLong(end);
+std::ostream& operator<<(std::ostream& o, const SequenceSet& s) {
+    RangePrinter print(o);
+    o << "{ ";
+    s.for_each(print);
+    return o << "}";
 }
 
-SequenceSet::Range::Range(SequenceNumber s, SequenceNumber e) : start(s), 
end(e) {}
+}} // namespace qpid::framing
 
-namespace qpid{
-namespace framing{
-
-std::ostream& operator<<(std::ostream& out, const SequenceSet& set) {
-    out << "{";
-    for (SequenceSet::Ranges::const_iterator i = set.ranges.begin(); i != 
set.ranges.end(); i++) {
-        if (i != set.ranges.begin()) out << ", "; 
-        out << i->start.getValue() << "-" << i->end.getValue();
-    }
-    out << "}";
-    return out;
-}
-
-}
-}

Modified: incubator/qpid/trunk/qpid/cpp/src/qpid/framing/SequenceSet.h
URL: 
http://svn.apache.org/viewvc/incubator/qpid/trunk/qpid/cpp/src/qpid/framing/SequenceSet.h?rev=650906&r1=650905&r2=650906&view=diff
==============================================================================
--- incubator/qpid/trunk/qpid/cpp/src/qpid/framing/SequenceSet.h (original)
+++ incubator/qpid/trunk/qpid/cpp/src/qpid/framing/SequenceSet.h Wed Apr 23 
08:31:57 2008
@@ -21,38 +21,18 @@
 #ifndef _framing_SequenceSet_h
 #define _framing_SequenceSet_h
 
-#include "amqp_types.h"
-#include "Buffer.h"
 #include "SequenceNumber.h"
-#include "qpid/framing/reply_exceptions.h"
-#include <ostream>
-#include <list>
+#include "qpid/RangeSet.h"
 
 namespace qpid {
 namespace framing {
+class Buffer;
 
-class SequenceSet {
-    struct Range {
-        SequenceNumber start;
-        SequenceNumber end;
-        
-        Range(SequenceNumber s=0, SequenceNumber e=0);
-        bool contains(SequenceNumber i) const;
-        bool intersects(const Range& r) const;
-        bool merge(const Range& r);
-        bool mergeable(const SequenceNumber& r) const;
-        void encode(Buffer& buffer) const;
-
-        template <class S> void serialize(S& s) { s(start)(end); }
-    };
-
-    typedef std::list<Range> Ranges;
-    Ranges ranges;
-
+class SequenceSet : public RangeSet<SequenceNumber> {
   public:
     SequenceSet() {}
     explicit SequenceSet(const SequenceNumber& s) { add(s); }
-
+    
     void encode(Buffer& buffer) const;
     void decode(Buffer& buffer);
     uint32_t size() const;   
@@ -65,25 +45,15 @@
     void remove(const SequenceNumber& start, const SequenceNumber& end);
     void remove(const SequenceSet& set);
 
-    void clear();
-    bool empty() const;
-
-    template <class T>
-    T for_each(T& t) const
-    {
-        for (Ranges::const_iterator i = ranges.begin(); i != ranges.end(); 
i++) {
-            t(i->start, i->end);
+    template <class T> T for_each(T& t) const {
+        for (RangeIterator i = rangesBegin(); i != rangesEnd(); i++) {
+            t(i->first(), i->last());
         }
         return t;
     }
 
-    template <class S> void serialize(S& s) { s.split(*this); 
s(ranges.begin(), ranges.end()); }
-    template <class S> void encode(S& s) const { 
s(uint16_t(ranges.size()*sizeof(Range))); }
-    template <class S> void decode(S& s) { uint16_t sz; s(sz); 
ranges.resize(sz/sizeof(Range)); }
-    
   friend std::ostream& operator<<(std::ostream&, const SequenceSet&);
 };    
-
 
 }} // namespace qpid::framing
 

Modified: incubator/qpid/trunk/qpid/cpp/src/tests/Makefile.am
URL: 
http://svn.apache.org/viewvc/incubator/qpid/trunk/qpid/cpp/src/tests/Makefile.am?rev=650906&r1=650905&r2=650906&view=diff
==============================================================================
--- incubator/qpid/trunk/qpid/cpp/src/tests/Makefile.am (original)
+++ incubator/qpid/trunk/qpid/cpp/src/tests/Makefile.am Wed Apr 23 08:31:57 2008
@@ -1,4 +1,4 @@
-AM_CXXFLAGS = $(WARNING_CFLAGS) $(CPPUNIT_CXXFLAGS)   $(APR_CXXFLAGS) 
-DBOOST_TEST_DYN_LINK
+AM_CXXFLAG = $(WARNING_CFLAGS) $(CPPUNIT_CXXFLAGS)   $(APR_CXXFLAGS) 
-DBOOST_TEST_DYN_LINK
 INCLUDES =  -I$(srcdir)/.. -I$(srcdir)/../gen -I$(top_builddir)/src/gen
 
 [EMAIL PROTECTED]@

Modified: incubator/qpid/trunk/qpid/cpp/src/tests/RangeSet.cpp
URL: 
http://svn.apache.org/viewvc/incubator/qpid/trunk/qpid/cpp/src/tests/RangeSet.cpp?rev=650906&r1=650905&r2=650906&view=diff
==============================================================================
--- incubator/qpid/trunk/qpid/cpp/src/tests/RangeSet.cpp (original)
+++ incubator/qpid/trunk/qpid/cpp/src/tests/RangeSet.cpp Wed Apr 23 08:31:57 
2008
@@ -63,6 +63,22 @@
     BOOST_CHECK(r.back() == 6);
 }
 
+QPID_AUTO_TEST_CASE(testRangeSetAddSet) {
+    TestRangeSet r;
+    TestRangeSet s = TestRangeSet(0,3)+TestRange(5,10);
+    r += s;
+    BOOST_CHECK_EQUAL(r,s);
+    r += TestRangeSet(3,5) + TestRange(7,12) + 15;
+    BOOST_CHECK_EQUAL(r, TestRangeSet(0,12) + 15);
+
+    r.clear();
+    BOOST_CHECK(r.empty());
+    r += TestRange::makeClosed(6,10);
+    BOOST_CHECK_EQUAL(r, TestRangeSet(6,11));
+    r += TestRangeSet(2,6)+8;
+    BOOST_CHECK_EQUAL(r, TestRangeSet(2,11));
+}
+
 QPID_AUTO_TEST_CASE(testRangeSetIterate) {
     TestRangeSet r;
     (((r += 1) += 10) += TestRange(4,7)) += 2;
@@ -74,12 +90,14 @@
 }
 
 QPID_AUTO_TEST_CASE(testRangeSetRemove) {
+    // points
     BOOST_CHECK_EQUAL(TestRangeSet(0,5)-3, TestRangeSet(0,3)+TestRange(4,5));
     BOOST_CHECK_EQUAL(TestRangeSet(1,5)-5, TestRangeSet(1,5));
     BOOST_CHECK_EQUAL(TestRangeSet(1,5)-0, TestRangeSet(1,5));
 
     TestRangeSet r(TestRangeSet(0,5)+TestRange(10,15)+TestRange(20,25));
 
+    // TestRanges
     BOOST_CHECK_EQUAL(r-TestRange(0,5), TestRangeSet(10,15)+TestRange(20,25));
     BOOST_CHECK_EQUAL(r-TestRange(10,15), TestRangeSet(0,5)+TestRange(20,25));
     BOOST_CHECK_EQUAL(r-TestRange(20,25), TestRangeSet(0,5)+TestRange(10,15));
@@ -89,16 +107,22 @@
     BOOST_CHECK_EQUAL(r-TestRange(-5, 7), 
TestRangeSet(10,15)+TestRange(20,25));
     BOOST_CHECK_EQUAL(r-TestRange(8,19), TestRangeSet(0,5)+TestRange(20,25));
     BOOST_CHECK_EQUAL(r-TestRange(17,30), TestRangeSet(0,5)+TestRange(10,15));
+    BOOST_CHECK_EQUAL(r-TestRange(17,30), TestRangeSet(0,5)+TestRange(10,15));
 
     BOOST_CHECK_EQUAL(r-TestRange(-5, 5), 
TestRangeSet(10,15)+TestRange(20,25));
     BOOST_CHECK_EQUAL(r-TestRange(10,19), TestRangeSet(0,5)+TestRange(20,25));
     BOOST_CHECK_EQUAL(r-TestRange(18,25), TestRangeSet(0,5)+TestRange(10,15));
+    BOOST_CHECK_EQUAL(r-TestRange(23,25), 
TestRangeSet(0,5)+TestRange(10,15)+TestRange(20,23));
 
     BOOST_CHECK_EQUAL(r-TestRange(-3, 3), 
TestRangeSet(3,5)+TestRange(10,15)+TestRange(20,25));
     BOOST_CHECK_EQUAL(r-TestRange(3, 7), 
TestRangeSet(0,2)+TestRange(10,15)+TestRange(20,25));
     BOOST_CHECK_EQUAL(r-TestRange(3, 12), 
TestRangeSet(0,3)+TestRange(12,15)+TestRange(20,25));
     BOOST_CHECK_EQUAL(r-TestRange(3, 22), 
TestRangeSet(12,15)+TestRange(22,25));
     BOOST_CHECK_EQUAL(r-TestRange(12, 22), 
TestRangeSet(0,5)+TestRange(10,11)+TestRange(22,25));
+
+    // Sets
+    BOOST_CHECK_EQUAL(r-(TestRangeSet(-1,6)+TestRange(11,14)+TestRange(23,25)),
+                      TestRangeSet(10,11)+TestRange(14,15)+TestRange(20,23));
 }
 
 QPID_AUTO_TEST_CASE(testRangeContaining) {

Modified: incubator/qpid/trunk/qpid/cpp/src/tests/SequenceSet.cpp
URL: 
http://svn.apache.org/viewvc/incubator/qpid/trunk/qpid/cpp/src/tests/SequenceSet.cpp?rev=650906&r1=650905&r2=650906&view=diff
==============================================================================
--- incubator/qpid/trunk/qpid/cpp/src/tests/SequenceSet.cpp (original)
+++ incubator/qpid/trunk/qpid/cpp/src/tests/SequenceSet.cpp Wed Apr 23 08:31:57 
2008
@@ -80,7 +80,7 @@
         BOOST_CHECK(!t.contains(i));
 
     for (uint32_t i = 2; i <= 10; i++)
-        BOOST_CHECK(t.contains(i));
+        BOOST_CHECK_MESSAGE(t.contains(i), t << " contains " << i);
 
     RangeExpectations().expect(2, 10).check(t);
 }


Reply via email to