#ifndef CEPH_INTERVAL_SET_H
#define CEPH_INTERVAL_SET_H
+#include <iterator>
#include <map>
#include <ostream>
using namespace std;
map<T,T> m; // map start -> len
int64_t _size;
+ class const_iterator : public std::iterator <std::forward_iterator_tag, T>
+ {
+ public:
+ explicit const_iterator(typename std::map<T,T>::const_iterator iter)
+ : _iter(iter)
+ { }
+
+ // For the copy constructor and assignment operator, the compiler-generated functions, which
+ // perform simple bitwise copying, should be fine.
+
+ bool operator==(const const_iterator& rhs) const {
+ return (_iter == rhs._iter);
+ }
+
+ bool operator!=(const const_iterator& rhs) const {
+ return (_iter != rhs._iter);
+ }
+
+ // Dereference this iterator to get a pair.
+ pair < T, T > &operator*() {
+ return *_iter;
+ }
+
+ // Return the interval start.
+ const T& get_start() const {
+ return _iter->first;
+ }
+
+ // Return the interval length.
+ const T& get_len() const {
+ return _iter->second;
+ }
+
+ // Preincrement
+ const_iterator &operator++()
+ {
+ ++_iter;
+ return *this;
+ }
+
+ // Postincrement
+ const_iterator operator++(int)
+ {
+ const_iterator prev(_iter);
+ ++_iter;
+ return prev;
+ }
+
+ protected:
+ typename map<T,T>::const_iterator _iter;
+ };
+
interval_set() : _size(0) {}
+ typename interval_set<T>::const_iterator begin() const
+ {
+ return typename interval_set<T>::const_iterator(m.begin());
+ }
+
+ typename interval_set<T>::const_iterator end() const
+ {
+ return typename interval_set<T>::const_iterator(m.end());
+ }
+
// helpers
private:
typename map<T,T>::const_iterator find_inc(T start) const {
};
+
template<class T>
inline ostream& operator<<(ostream& out, const interval_set<T> &s) {
out << "[";
- for (typename map<T,T>::const_iterator i = s.m.begin();
- i != s.m.end();
- i++) {
- if (i != s.m.begin()) out << ",";
- out << i->first << "~" << i->second;
+ const char *prequel = "";
+ for (typename interval_set<T>::const_iterator i = s.begin();
+ i != s.end();
+ ++i)
+ {
+ out << prequel << i.get_start() << "~" << i.get_len();
+ prequel = ",";
}
out << "]";
return out;