TIVI-153: Add as dependency for iputils
[profile/ivi/opensp.git] / include / RangeMap.h
1 // Copyright (c) 1994 James Clark
2 // See the file COPYING for copying permission.
3
4 #ifndef RangeMap_INCLUDED
5 #define RangeMap_INCLUDED 1
6
7 #include "Vector.h"
8 #include "Boolean.h"
9 #include "ISet.h"
10 #include "types.h"
11 #include <stddef.h>
12
13 #ifdef SP_NAMESPACE
14 namespace SP_NAMESPACE {
15 #endif
16
17 template<class From, class To>
18 struct RangeMapRange {
19   From fromMin;
20   From fromMax;
21   To toMin;
22 };
23
24 template<class From, class To> class RangeMapIter;
25
26 template<class From, class To>
27 class RangeMap {
28 public:
29   RangeMap();
30   Boolean map(From, To &, From &alsoMax) const;
31   // Return 0 for no matches, 1 for 1, 2 for more than 1.
32   unsigned inverseMap(To, From &, ISet<WideChar> &, WideChar &count) const;
33   void addRange(From, From, To);
34 private:
35   Vector<RangeMapRange<From,To> > ranges_;
36   friend class RangeMapIter<From,To>;
37 };
38
39 template<class From, class To>
40 class RangeMapIter {
41 public:
42   RangeMapIter(const RangeMap<From,To> &map);
43   Boolean next(From &fromMin, From &fromMax, To &toMin) {
44     if (!count_)
45       return 0;
46     else {
47       fromMin = ptr_->fromMin;
48       fromMax = ptr_->fromMax;
49       toMin = ptr_->toMin;
50       ptr_++;
51       count_--;
52       return 1;
53     }
54   }
55 private:
56   size_t count_;
57   typename Vector<RangeMapRange<From,To> >::const_iterator ptr_;
58 };
59
60 #ifdef SP_NAMESPACE
61 }
62 #endif
63
64 #endif /* not RangeMap_INCLUDED */
65
66 #ifdef SP_DEFINE_TEMPLATES
67 #include "RangeMap.cxx"
68 #endif