Apache Qpid - AMQP Messaging for Java JMS, C++, Python, Ruby, and .NET Apache Qpid Documentation
qpid/RangeSet.h
Go to the documentation of this file.
00001 #ifndef QPID_RANGESET_H
00002 #define QPID_RANGESET_H
00003 
00004 /*
00005  *
00006  * Licensed to the Apache Software Foundation (ASF) under one
00007  * or more contributor license agreements.  See the NOTICE file
00008  * distributed with this work for additional information
00009  * regarding copyright ownership.  The ASF licenses this file
00010  * to you under the Apache License, Version 2.0 (the
00011  * "License"); you may not use this file except in compliance
00012  * with the License.  You may obtain a copy of the License at
00013  *
00014  *   http://www.apache.org/licenses/LICENSE-2.0
00015  *
00016  * Unless required by applicable law or agreed to in writing,
00017  * software distributed under the License is distributed on an
00018  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
00019  * KIND, either express or implied.  See the License for the
00020  * specific language governing permissions and limitations
00021  * under the License.
00022  *
00023  */
00024 
00025 #include "qpid/InlineVector.h"
00026 #include <boost/iterator/iterator_facade.hpp>
00027 #include <boost/operators.hpp>
00028 #include <boost/bind.hpp>
00029 #include <algorithm>
00030 #include <numeric>
00031 
00032 namespace qpid {
00033 
00038 template <class T>
00039 class Range {
00040   public:
00041     static Range makeClosed(const T& first, T last) { return Range(first, ++last); }
00042 
00043     Range() : begin_(), end_() {}
00044     explicit Range(const T& t) : begin_(t), end_(t) { ++end_; }
00045     Range(const T& b, const T& e) : begin_(b), end_(e) { assert(b <= e); }
00046 
00047     T begin() const { return begin_; }
00049     T end() const { return end_; }
00050 
00051     T first() const { assert(!empty()); return begin_; }
00053     T last() const { assert(!empty()); T ret=end_; return --ret; }
00054 
00055     void begin(const T& t) { begin_ = t; }
00056     void end(const T& t) { end_ = t; }
00057     size_t size() const { return end_ - begin_; }
00058     bool empty() const { return begin_ == end_; }
00059 
00060     bool contains(const T& x) const { return begin_ <= x && x < end_; }
00061     bool contains(const Range& r) const { return begin_ <= r.begin_ && r.end_ <= end_; }
00062     bool strictContains(const Range& r) const { return begin_ < r.begin_ && r.end_ < end_; }
00063 
00064     bool operator==(const Range& x) { return begin_ == x.begin_ && end_== x.end_; }
00065 
00066     bool operator<(const T& t) const { return end_ < t; }
00067     bool operator<(const Range<T>& r) const { return end_ < r.begin_; }
00068 
00070     bool touching(const Range& r) const {
00071         return std::max(begin_, r.begin_) <= std::min(end_, r.end_);
00072     }
00073 
00075     void merge(const Range& r) {
00076         assert(touching(r));
00077         begin_ = std::min(begin_, r.begin_);
00078         end_ = std::max(end_, r.end_);
00079     }
00080 
00081     operator bool() const { return !empty(); }
00082 
00083     template <class S> void serialize(S& s) { s(begin_)(end_); }
00084 
00085   private:
00086     T begin_, end_;
00087 };
00088 
00089 
00095 template <class T>
00096 class RangeSet
00097     : boost::additive1<RangeSet<T>,
00098                        boost::additive2<RangeSet<T>, Range<T>,
00099                                         boost::additive2<RangeSet<T>, T> > >
00100 {
00101     typedef InlineVector<Range<T>, 3> Ranges; // TODO aconway 2008-04-21: what's the optimial inlined value?
00102 
00103   public:
00104 
00105     class iterator : public boost::iterator_facade<
00106         iterator,
00107         const T,
00108         boost::forward_traversal_tag>
00109     {
00110       public:
00111         iterator() : ranges(), iter(), value() {}
00112 
00113       private:
00114         typedef typename Ranges::const_iterator RangesIter;
00115         iterator(const Ranges& r, const RangesIter& i, const T& t)
00116             : ranges(&r), iter(i), value(t) {}
00117 
00118         void increment();
00119         bool equal(const iterator& i) const;
00120         const T& dereference() const { return value; }
00121 
00122         const Ranges* ranges;
00123         RangesIter iter;
00124         T value;
00125 
00126       friend class RangeSet<T>;
00127       friend class boost::iterator_core_access;
00128     };
00129 
00130     typedef iterator const_iterator;
00131 
00132     RangeSet() {}
00133     explicit RangeSet(const Range<T>& r) { *this += r; }
00134     RangeSet(const T& a, const T& b) { *this += Range<T>(a,b); }
00135 
00136     bool contiguous() const { return ranges.size() <= 1; }
00137 
00138     bool contains(const T& t) const;
00139     bool contains(const Range<T>&) const;
00140 
00142     Range<T> toRange() const;
00143 
00144     bool operator==(const RangeSet<T>&) const;
00145 
00146     void addRange (const Range<T>&);
00147     void addSet (const RangeSet<T>&);
00148 
00149     RangeSet<T>& operator+=(const T& t) { return *this += Range<T>(t); }
00150     RangeSet<T>& operator+=(const Range<T>& r) { addRange(r); return *this; }
00151     RangeSet<T>& operator+=(const RangeSet<T>& s) { addSet(s); return *this; }
00152 
00153     void removeRange (const Range<T>&);
00154     void removeSet (const RangeSet<T>&);
00155 
00156     RangeSet<T>& operator-=(const T& t)  { return *this -= Range<T>(t); }
00157     RangeSet<T>& operator-=(const Range<T>& r) { removeRange(r); return *this; }
00158     RangeSet<T>& operator-=(const RangeSet<T>& s) { removeSet(s); return *this; }
00159 
00160     T front() const { return ranges.front().begin(); }
00161     T back() const { return ranges.back().end(); }
00162 
00163     // Iterate over elements in the set.
00164     iterator begin() const;
00165     iterator end() const;
00166 
00167     // Iterate over ranges in the set.
00168     typedef typename Ranges::const_iterator RangeIterator;
00169     RangeIterator rangesBegin() const { return ranges.begin(); }
00170     RangeIterator rangesEnd() const { return ranges.end(); }
00171     size_t rangesSize() const { return ranges.size(); }
00172 
00173     // The difference between the start and end of this range set
00174     uint32_t span() const;
00175 
00176     size_t size() const;
00177     bool empty() const { return ranges.empty(); }
00178     void clear() { ranges.clear(); }
00179 
00183     Range<T> rangeContaining(const T&) const;
00184 
00185     template <class S> void serialize(S& s) { s.split(*this); s(ranges.begin(), ranges.end()); }
00186     template <class S> void encode(S& s) const { s(uint16_t(ranges.size()*sizeof(Range<T>))); }
00187     template <class S> void decode(S& s) { uint16_t sz; s(sz); ranges.resize(sz/sizeof(Range<T>)); }
00188 
00189  private:
00190     static size_t accumulateSize(size_t s, const Range<T>& r) { return s+r.size(); }
00191     Ranges ranges;
00192 
00193   template <class U> friend std::ostream& operator<<(std::ostream& o, const RangeSet<U>& r);
00194 
00195   friend class iterator;
00196 };
00197 
00198 template <class T>
00199 std::ostream& operator<<(std::ostream& o, const Range<T>& r) {
00200     return o << "[" << r.begin() << "," << r.end() << ")";
00201 }
00202 
00203 template <class T>
00204 std::ostream& operator<<(std::ostream& o, const RangeSet<T>& rs) {
00205     std::ostream_iterator<Range<T> > i(o, " ");
00206     o << "{ ";
00207     std::copy(rs.ranges.begin(), rs.ranges.end(), i);
00208     return o << "}";
00209 }
00210 
00211 template <class T>
00212 bool RangeSet<T>::contains(const T& t) const {
00213     typename Ranges::const_iterator i =
00214         std::lower_bound(ranges.begin(), ranges.end(), Range<T>(t));
00215     return i != ranges.end() && i->contains(t);
00216 }
00217 
00218 template <class T>
00219 bool RangeSet<T>::contains(const Range<T>& r) const {
00220     typename Ranges::const_iterator i =
00221         std::lower_bound(ranges.begin(), ranges.end(), r);
00222     return i != ranges.end() && i->contains(r);
00223 }
00224 
00225 template <class T> void RangeSet<T>::addRange(const Range<T>& r) {
00226     if (r.empty()) return;
00227     typename Ranges::iterator i =
00228         std::lower_bound(ranges.begin(), ranges.end(), r);
00229     if (i == ranges.end() || !i->touching(r))
00230         ranges.insert(i, r);
00231     else {
00232         i->merge(r);
00233         typename Ranges::iterator j = i;
00234         if (++j != ranges.end() && i->touching(*j)) {
00235             i->merge(*j);
00236             ranges.erase(j);
00237         }
00238     }
00239 }
00240 
00241 
00242 template <class T> void RangeSet<T>::addSet(const RangeSet<T>& s) {
00243     typedef RangeSet<T>& (RangeSet<T>::*RangeSetRangeOp)(const Range<T>&);
00244     std::for_each(s.ranges.begin(), s.ranges.end(),
00245                   boost::bind((RangeSetRangeOp)&RangeSet<T>::operator+=, this, _1));
00246 }
00247 
00248 template <class T> void RangeSet<T>::removeRange(const Range<T>& r) {
00249     if (r.empty()) return;
00250     typename Ranges::iterator i,j;
00251     i = std::lower_bound(ranges.begin(), ranges.end(), r);
00252     if (i == ranges.end() || i->begin() >= r.end())
00253         return;                 // Outside of set
00254     if (*i == r)                // Erase i
00255         ranges.erase(i);
00256     else if (i->strictContains(r)) {  // Split i
00257         Range<T> i1(i->begin(), r.begin());
00258         Range<T> i2(r.end(), i->end());
00259         *i = i2;
00260         ranges.insert(i, i1);
00261     } else {
00262         if (i->begin() < r.begin()) { // Truncate i
00263             i->end(r.begin());
00264             ++i;
00265         }
00266         for (j = i; j != ranges.end() && r.contains(*j); ++j)
00267             ;                   // Ranges to erase.
00268         if (j != ranges.end() && r.end() > j->begin())
00269             j->begin(r.end()); // Truncate j
00270         ranges.erase(i,j);
00271     }
00272 }
00273 
00274 template <class T> void RangeSet<T>::removeSet(const RangeSet<T>& r) {
00275     std::for_each(
00276         r.ranges.begin(), r.ranges.end(),
00277         boost::bind(&RangeSet<T>::removeRange, this, _1));
00278 }
00279 
00280 template <class T> Range<T> RangeSet<T>::toRange() const {
00281     assert(contiguous());
00282     return empty() ? Range<T>() :  ranges.front();
00283 }
00284 
00285 template <class T> void RangeSet<T>::iterator::increment() {
00286     assert(ranges && iter != ranges->end());
00287     if (!iter->contains(++value)) {
00288         ++iter;
00289         if (iter == ranges->end())
00290             *this=iterator();   // end() iterator
00291         else
00292             value=iter->begin();
00293     }
00294 }
00295 
00296 template <class T> bool RangeSet<T>::operator==(const RangeSet<T>& r) const {
00297     return ranges.size() == r.ranges.size() && std::equal(ranges.begin(), ranges.end(), r.ranges.begin());
00298 }
00299 
00300 template <class T> typename RangeSet<T>::iterator RangeSet<T>::begin() const {
00301     return empty() ? end() : iterator(ranges, ranges.begin(), front());
00302 }
00303 
00304 template <class T> typename RangeSet<T>::iterator RangeSet<T>::end() const {
00305     return iterator();
00306 }
00307 
00308 template <class T> bool RangeSet<T>::iterator::equal(const iterator& i) const {
00309     return ranges==i.ranges && (ranges==0 || value==i.value);
00310 }
00311 
00312 template <class T> Range<T> RangeSet<T>::rangeContaining(const T& t) const {
00313     typename Ranges::const_iterator i =
00314         std::lower_bound(ranges.begin(), ranges.end(), Range<T>(t));
00315     return (i != ranges.end() && i->contains(t)) ? *i : Range<T>(t,t);
00316 }
00317 
00318 template <class T> uint32_t RangeSet<T>::span() const {
00319     if (ranges.empty()) return 0;
00320     return ranges.back().last() - ranges.front().first();
00321 }
00322 
00323 template <class T> size_t RangeSet<T>::size() const {
00324     return std::accumulate(rangesBegin(), rangesEnd(), 0, &RangeSet<T>::accumulateSize);
00325 }
00326 
00327 } // namespace qpid
00328 
00329 
00330 #endif  

Qpid C++ API Reference
Generated on Mon Aug 22 2011 for Qpid C++ Client API by doxygen 1.7.4