changeset 50d1b7dbd23c in /z/repo/gem5
details: http://repo.gem5.org/gem5?cmd=changeset;node=50d1b7dbd23c
description:
        range_map: Enable const find and iteration

        This patch adds const access functions to the range_map to enable its 
use
        in a const context, similar to the STL container classes.

diffstat:

 src/base/range_map.hh |  61 +++++++++++++++++++++++++++++++++++++++++++++-----
 1 files changed, 55 insertions(+), 6 deletions(-)

diffs (126 lines):

diff -r a16ba72db7d0 -r 50d1b7dbd23c src/base/range_map.hh
--- a/src/base/range_map.hh     Mon Mar 26 05:35:24 2012 -0400
+++ b/src/base/range_map.hh     Mon Mar 26 05:37:00 2012 -0400
@@ -36,6 +36,12 @@
 
 #include "base/range.hh"
 
+/**
+ * The range_map uses an STL map to implement an interval tree. The
+ * type of both the key (range) and the value are template
+ * parameters. It can, for example, be used for address decoding,
+ * using a range of addresses to map to ports.
+ */
 template <class T,class V>
 class range_map
 {
@@ -45,9 +51,34 @@
 
   public:
     typedef typename RangeMap::iterator iterator;
+    typedef typename RangeMap::const_iterator const_iterator;
 
     template <class U>
-    const iterator
+    const_iterator
+    find(const Range<U> &r) const
+    {
+        const_iterator i;
+
+        i = tree.upper_bound(r);
+
+        if (i == tree.begin()) {
+            if (i->first.start <= r.end && i->first.end >= r.start)
+                return i;
+            else
+                // Nothing could match, so return end()
+                return tree.end();
+        }
+
+        --i;
+
+        if (i->first.start <= r.end && i->first.end >= r.start)
+            return i;
+
+        return tree.end();
+    }
+
+    template <class U>
+    iterator
     find(const Range<U> &r)
     {
         iterator i;
@@ -62,7 +93,7 @@
                 return tree.end();
         }
 
-        i--;
+        --i;
 
         if (i->first.start <= r.end && i->first.end >= r.start)
             return i;
@@ -71,7 +102,14 @@
     }
 
     template <class U>
-    const iterator
+    const_iterator
+    find(const U &r) const
+    {
+        return find(RangeSize(r, 1));
+    }
+
+    template <class U>
+    iterator
     find(const U &r)
     {
         return find(RangeSize(r, 1));
@@ -88,7 +126,6 @@
         return false;
     }
 
-
     template <class U,class W>
     iterator
     insert(const Range<U> &r, const W d)
@@ -123,12 +160,24 @@
         tree.erase(tree.begin(), tree.end());
     }
 
+    const_iterator
+    begin() const
+    {
+        return tree.begin();
+    }
+
     iterator
     begin()
     {
         return tree.begin();
     }
 
+    const_iterator
+    end() const
+    {
+        return tree.end();
+    }
+
     iterator
     end()
     {
@@ -136,13 +185,13 @@
     }
 
     size_t
-    size()
+    size() const
     {
         return tree.size();
     }
 
     bool
-    empty()
+    empty() const
     {
         return tree.empty();
     }
_______________________________________________
gem5-dev mailing list
[email protected]
http://m5sim.org/mailman/listinfo/gem5-dev

Reply via email to