addr_range_map.hh revision 8902
1/*
2 * Copyright (c) 2006 The Regents of The University of Michigan
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met: redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer;
9 * redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution;
12 * neither the name of the copyright holders nor the names of its
13 * contributors may be used to endorse or promote products derived from
14 * this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 *
28 * Authors: Ali Saidi
29 */
30
31#ifndef __BASE_RANGE_MAP_HH__
32#define __BASE_RANGE_MAP_HH__
33
34#include <map>
35#include <utility>
36
37#include "base/range.hh"
38
39template <class T,class V>
40class range_map
41{
42  private:
43    typedef std::map<Range<T>,V> RangeMap;
44    RangeMap tree;
45
46  public:
47    typedef typename RangeMap::iterator iterator;
48
49    template <class U>
50    const iterator
51    find(const Range<U> &r)
52    {
53        iterator i;
54
55        i = tree.upper_bound(r);
56
57        if (i == tree.begin()) {
58            if (i->first.start <= r.end && i->first.end >= r.start)
59                return i;
60            else
61                // Nothing could match, so return end()
62                return tree.end();
63        }
64
65        i--;
66
67        if (i->first.start <= r.end && i->first.end >= r.start)
68            return i;
69
70        return tree.end();
71    }
72
73    template <class U>
74    const iterator
75    find(const U &r)
76    {
77        return find(RangeSize(r, 1));
78    }
79
80    template <class U>
81    bool
82    intersect(const Range<U> &r)
83    {
84        iterator i;
85        i = find(r);
86        if (i != tree.end())
87            return true;
88        return false;
89    }
90
91
92    template <class U,class W>
93    iterator
94    insert(const Range<U> &r, const W d)
95    {
96        if (intersect(r))
97            return tree.end();
98
99        return tree.insert(std::make_pair(r, d)).first;
100    }
101
102    size_t
103    erase(T k)
104    {
105        return tree.erase(k);
106    }
107
108    void
109    erase(iterator p)
110    {
111        tree.erase(p);
112    }
113
114    void
115    erase(iterator p, iterator q)
116    {
117        tree.erase(p,q);
118    }
119
120    void
121    clear()
122    {
123        tree.erase(tree.begin(), tree.end());
124    }
125
126    iterator
127    begin()
128    {
129        return tree.begin();
130    }
131
132    iterator
133    end()
134    {
135        return tree.end();
136    }
137
138    size_t
139    size()
140    {
141        return tree.size();
142    }
143
144    bool
145    empty()
146    {
147        return tree.empty();
148    }
149};
150
151
152template <class T,class V>
153class range_multimap
154{
155  private:
156    typedef std::multimap<Range<T>,V> RangeMap;
157    RangeMap tree;
158
159  public:
160    typedef typename RangeMap::iterator iterator;
161
162    template <class U>
163    std::pair<iterator,iterator> find(const Range<U> &r)
164    {
165        iterator i;
166        iterator j;
167
168        i = tree.lower_bound(r);
169
170        if (i == tree.begin()) {
171          if (i->first.start <= r.end && i->first.end >= r.start)
172                return std::make_pair<iterator, iterator>(i,i);
173          else
174            // Nothing could match, so return end()
175            return std::make_pair<iterator, iterator>(tree.end(), tree.end());
176        }
177        i--;
178
179        if (i->first.start <= r.end && i->first.end >= r.start) {
180            // we have at least one match
181            j = i;
182
183            i--;
184            while (i->first.start <= r.end && i->first.end >=
185                    r.start) {
186                if (i == tree.begin())
187                    break;
188                i--;
189            }
190            if (i == tree.begin() && i->first.start <= r.end && i->first.end >=
191                                        r.start)
192                return std::make_pair<iterator, iterator>(i,j);
193            i++;
194            return std::make_pair<iterator, iterator>(i,j);
195
196        }
197
198        return std::make_pair<iterator, iterator>(tree.end(), tree.end());
199    }
200
201    template <class U>
202    bool
203    intersect(const Range<U> &r)
204    {
205        std::pair<iterator,iterator> p;
206        p = find(r);
207        if (p.first != tree.end())
208            return true;
209        return false;
210    }
211
212
213    template <class U,class W>
214    iterator
215    insert(const Range<U> &r, const W d)
216    {
217        std::pair<iterator,iterator> p;
218        p = find(r);
219        if ((p.first->first.start == r.start && p.first->first.end == r.end) ||
220                p.first == tree.end())
221            return tree.insert(std::make_pair<Range<T>,V>(r, d));
222        else
223            return tree.end();
224    }
225
226    size_t
227    erase(T k)
228    {
229        return tree.erase(k);
230    }
231
232    void
233    erase(iterator p)
234    {
235        tree.erase(p);
236    }
237
238    void
239    erase(iterator p, iterator q)
240    {
241        tree.erase(p,q);
242    }
243
244    void
245    clear()
246    {
247        tree.erase(tree.begin(), tree.end());
248    }
249
250    iterator
251    begin()
252    {
253        return tree.begin();
254    }
255
256    iterator
257    end()
258    {
259        return tree.end();
260    }
261
262    size_t
263    size()
264    {
265        return tree.size();
266    }
267
268    bool
269    empty()
270    {
271        return tree.empty();
272    }
273};
274
275#endif //__BASE_RANGE_MAP_HH__
276