]> git-server-git.apps.pok.os.sepia.ceph.com Git - ceph.git/commitdiff
Implement interval_set::const_iterator
authorColin Patrick McCabe <cmccabe@alumni.cmu.edu>
Wed, 15 Sep 2010 22:23:15 +0000 (15:23 -0700)
committerColin Patrick McCabe <cmccabe@alumni.cmu.edu>
Mon, 27 Sep 2010 21:47:18 +0000 (14:47 -0700)
src/include/interval_set.h

index a8e7fc53e41e32435dc177227ed2ed0bbf138137..4617a724415941aa031642199d29f152db033e47 100644 (file)
@@ -16,6 +16,7 @@
 #ifndef CEPH_INTERVAL_SET_H
 #define CEPH_INTERVAL_SET_H
 
+#include <iterator>
 #include <map>
 #include <ostream>
 using namespace std;
@@ -36,8 +37,70 @@ class interval_set {
   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 {
@@ -373,14 +436,17 @@ class interval_set {
 
 };
 
+
 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;