#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
{
public:
typedef typename RangeMap::iterator iterator;
+ typedef typename RangeMap::const_iterator const_iterator;
+
+ template <class U>
+ 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>
- const iterator
+ iterator
find(const Range<U> &r)
{
iterator i;
return tree.end();
}
- i--;
+ --i;
if (i->first.start <= r.end && i->first.end >= r.start)
return i;
}
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));
return false;
}
-
template <class U,class W>
iterator
insert(const Range<U> &r, const W d)
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()
{
}
size_t
- size()
+ size() const
{
return tree.size();
}
bool
- empty()
+ empty() const
{
return tree.empty();
}