00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029 #ifndef CONSTRAINT_INDEX_H
00030 #define CONSTRAINT_INDEX_H
00031
00032 #include "fwd_table.h"
00033 #include "ft_allocator.h"
00034
00044 template<class T>
00045 class constraint_index {
00046 private:
00047 class node {
00048 public:
00049 T value;
00050 Constraint * c;
00051
00052 node(const T & v, Constraint * d): value(v), c(d) {}
00053
00054 bool operator < (const T & x) const { return value < x; }
00055 bool operator > (const T & x) const { return value > x; }
00056 bool operator == (const T & x) const { return value == x; }
00057 };
00058
00059 typedef vector<node> v_index;
00060 typedef map<T, Constraint *> m_index;
00061
00062 v_index lt_map;
00063 v_index gt_map;
00064 m_index eq_map;
00065
00066 public:
00067 Constraint * add_lt(const T & v, FTAllocator & ftmemory) {
00068 v_index::iterator i = lower_bound(lt_map.begin(), lt_map.end(), v);
00069 if (i == lt_map.end() || (*i).value != v)
00070 i = lt_map.insert(i, node(v, new (ftmemory)Constraint()));
00071
00072 return (*i).c;
00073 }
00074
00075 Constraint * add_gt(const T & v, FTAllocator & ftmemory) {
00076 v_index::iterator i = lower_bound(gt_map.begin(), gt_map.end(), v);
00077 if (i == gt_map.end() || (*i).value != v)
00078 i = gt_map.insert(i, node(v, new (ftmemory)Constraint()));
00079
00080 return (*i).c;
00081 }
00082
00083 Constraint * add_eq(const T & v, FTAllocator & ftmemory) {
00084 pair<m_index::iterator, bool> pib;
00085 pib = eq_map.insert(m_index::value_type(v, NULL));
00086 if (pib.second)
00087 (*pib.first).second = new (ftmemory)Constraint();
00088
00089 return (*pib.first).second;
00090 }
00091
00092 bool match(const T & v, CProcessor & p) const {
00093
00094
00095
00096 m_index::const_iterator i = eq_map.find(v);
00097 if (i != eq_map.end())
00098 if (p.process_constraint((*i).second))
00099 return true;
00100
00101
00102
00103 v_index::const_iterator vi;
00104 for(vi = gt_map.begin(); vi != gt_map.end() && *vi < v; ++vi)
00105 if (p.process_constraint((*vi).c))
00106 return true;
00107
00108
00109
00110 v_index::const_reverse_iterator vri;
00111 for(vri = lt_map.rbegin(); vri != lt_map.rend() && *vri > v; ++vri)
00112 if (p.process_constraint((*vri).c))
00113 return true;
00114 return false;
00115 }
00116 };
00117
00118 #endif