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
00030 #ifndef MORE_GEN_PO_MAP_H
00031 #define MORE_GEN_PO_MAP_H
00032
00033
00034 #include <more/gen/partial_ordering.h>
00035 #include <more/gen/po_algo.h>
00036
00037
00038 namespace more {
00039 namespace gen {
00040
00041
00042 namespace bits {
00043 template<typename Pair, typename SubCompare>
00044 struct compare_first_function
00045 : std::binary_function<Pair, Pair, bool>
00046 {
00047 compare_first_function(SubCompare const& cmp)
00048 : m_subcmp(cmp) {}
00049
00050 bool
00051 operator()(Pair const& x, Pair const& y)
00052 {
00053 return m_subcmp(x.first, y.first);
00054 }
00055
00056 private:
00057 SubCompare m_subcmp;
00058 };
00059
00060 template<typename Pair, typename Function>
00061 struct apply_to_first_function
00062 : std::unary_function<Pair, typename Function::result_type>
00063 {
00064 apply_to_first_function(Function const& sub)
00065 : m_sub(sub) {}
00066
00067 typename Function::result_type
00068 operator()(Pair const& x) { return m_sub(x.first); }
00069
00070 private:
00071 Function m_sub;
00072 };
00073 }
00074
00075
00076
00077
00078 template<typename Key, typename Data, typename KeyCompare>
00079 struct po_map
00080 {
00081 typedef Key key_type;
00082 typedef Data data_type;
00083 typedef std::pair<key_type, data_type> value_type;
00084
00085 typedef KeyCompare key_compare;
00086
00087 struct value_compare
00088 : std::binary_function<value_type, value_type, bool>
00089 {
00090 bool operator()(value_type const& x, value_type const& y) const
00091 {
00092 return m_key_preceq(x.first, y.first);
00093 }
00094 private:
00095 key_compare m_key_preceq;
00096 };
00097
00098 private:
00099 typedef partial_ordering<value_type> container;
00100
00101 public:
00102 typedef typename container::element iterator;
00103 typedef typename container::const_element const_iterator;
00104
00105 std::pair<iterator, bool> insert(value_type const&);
00106 void erase(iterator it) { m_po.erase(it); }
00107
00108 iterator min() { return m_po.min(); }
00109 iterator max() { return m_po.max(); }
00110
00111 const_iterator min() const { return m_po.min(); }
00112 const_iterator max() const { return m_po.max(); }
00113
00114 template<typename OutputIterator>
00115 void find_supremums(key_type const&, OutputIterator) const;
00116 template<typename OutputIterator>
00117 void find_supremums(key_type const&, OutputIterator);
00118 template<typename OutputIterator>
00119 void find_infimums(key_type const&, OutputIterator) const;
00120 template<typename OutputIterator>
00121 void find_infimums(key_type const&, OutputIterator);
00122
00123 template<typename Predicate, typename OutputIterator>
00124 void find_supremums_if(Predicate const&, OutputIterator) const;
00125 template<typename Predicate, typename OutputIterator>
00126 void find_supremums_if(Predicate const&, OutputIterator);
00127 template<typename Predicate, typename OutputIterator>
00128 void find_infimums_if(Predicate const&, OutputIterator) const;
00129 template<typename Predicate, typename OutputIterator>
00130 void find_infimums_if(Predicate const&, OutputIterator);
00131
00132 void debug_dump() { m_po.debug_dump(); }
00133
00134 private:
00135 container m_po;
00136 value_compare m_preceq;
00137 };
00138
00139
00140 template<typename Key, typename Data, typename KeyCompare>
00141 std::pair<typename po_map<Key, Data, KeyCompare>::iterator, bool>
00142 po_map<Key, Data, KeyCompare>::insert(value_type const& v)
00143 {
00144 std::list<typename container::element> infs, sups;
00145 find_infimums(v.first, back_inserter(infs));
00146 if (infs.size() == 1 && m_preceq(v, *infs.front()))
00147 return std::pair<iterator, bool>(infs.front(), false);
00148 find_supremums(v.first, back_inserter(sups));
00149
00150 typename container::element e = m_po.insert(v);
00151
00152 for (typename std::list<typename container::element>::iterator
00153 it = infs.begin();
00154 it != infs.end(); ++it)
00155 m_po.constrain_prec(*it, e);
00156 infs.clear();
00157
00158 for (typename std::list<typename container::element>::iterator
00159 it = sups.begin();
00160 it != sups.end(); ++it)
00161 m_po.constrain_prec(e, *it);
00162
00163 return std::pair<iterator, bool>(e, true);
00164 }
00165
00166 template<typename Key, typename Data, typename KeyCompare>
00167 template<typename OutputIterator>
00168 void
00169 po_map<Key, Data, KeyCompare>::
00170 find_supremums(key_type const& key, OutputIterator it_out) const
00171 {
00172 value_type v(key, data_type());
00173 gen::find_supremums_below(m_po.max(), v, m_preceq, it_out);
00174 }
00175
00176 template<typename Key, typename Data, typename KeyCompare>
00177 template<typename OutputIterator>
00178 void
00179 po_map<Key, Data, KeyCompare>::
00180 find_supremums(key_type const& key, OutputIterator it_out)
00181 {
00182 value_type v(key, data_type());
00183 gen::find_supremums_below(m_po.max(), v, m_preceq, it_out);
00184 }
00185
00186 template<typename Key, typename Data, typename KeyCompare>
00187 template<typename OutputIterator>
00188 void
00189 po_map<Key, Data, KeyCompare>::
00190 find_infimums(key_type const& key, OutputIterator it_out) const
00191 {
00192 value_type v(key, data_type());
00193 gen::find_infimums_above(m_po.min(), v, m_preceq, it_out);
00194 }
00195
00196 template<typename Key, typename Data, typename KeyCompare>
00197 template<typename OutputIterator>
00198 void
00199 po_map<Key, Data, KeyCompare>::
00200 find_infimums(key_type const& key, OutputIterator it_out)
00201 {
00202 value_type v(key, data_type());
00203 gen::find_infimums_above(m_po.min(), v, m_preceq, it_out);
00204 }
00205
00206
00207 template<typename Key, typename Data, typename KeyCompare>
00208 template<typename Predicate, typename OutputIterator>
00209 void
00210 po_map<Key, Data, KeyCompare>::
00211 find_supremums_if(Predicate const& pred, OutputIterator it_out) const
00212 {
00213 bits::apply_to_first_function<value_type, Predicate> f(pred);
00214 gen::find_supremums_below_if(m_po.max(), f, it_out);
00215 }
00216
00217 template<typename Key, typename Data, typename KeyCompare>
00218 template<typename Predicate, typename OutputIterator>
00219 void
00220 po_map<Key, Data, KeyCompare>::
00221 find_supremums_if(Predicate const& pred, OutputIterator it_out)
00222 {
00223 bits::apply_to_first_function<value_type, Predicate> f(pred);
00224 gen::find_supremums_below_if(m_po.max(), f, it_out);
00225 }
00226
00227 template<typename Key, typename Data, typename KeyCompare>
00228 template<typename Predicate, typename OutputIterator>
00229 void
00230 po_map<Key, Data, KeyCompare>::
00231 find_infimums_if(Predicate const& pred, OutputIterator it_out) const
00232 {
00233 bits::apply_to_first_function<value_type, Predicate> f(pred);
00234 gen::find_infimums_above_if(m_po.min(), f, it_out);
00235 }
00236
00237 template<typename Key, typename Data, typename KeyCompare>
00238 template<typename Predicate, typename OutputIterator>
00239 void
00240 po_map<Key, Data, KeyCompare>::
00241 find_infimums_if(Predicate const& pred, OutputIterator it_out)
00242 {
00243 bits::apply_to_first_function<value_type, Predicate> f(pred);
00244 gen::find_infimums_above_if(m_po.min(), f, it_out);
00245 }
00246
00247
00248 }}
00249
00250 #endif