annotate DEPENDENCIES/generic/include/boost/heap/detail/heap_comparison.hpp @ 125:34e428693f5d vext

Vext -> Repoint
author Chris Cannam
date Thu, 14 Jun 2018 11:15:39 +0100
parents 2665513ce2d3
children
rev   line source
Chris@16 1 // boost heap: heap node helper classes
Chris@16 2 //
Chris@16 3 // Copyright (C) 2010 Tim Blechmann
Chris@16 4 //
Chris@16 5 // Distributed under the Boost Software License, Version 1.0. (See
Chris@16 6 // accompanying file LICENSE_1_0.txt or copy at
Chris@16 7 // http://www.boost.org/LICENSE_1_0.txt)
Chris@16 8
Chris@16 9 #ifndef BOOST_HEAP_DETAIL_HEAP_COMPARISON_HPP
Chris@16 10 #define BOOST_HEAP_DETAIL_HEAP_COMPARISON_HPP
Chris@16 11
Chris@16 12 #include <boost/assert.hpp>
Chris@16 13 #include <boost/static_assert.hpp>
Chris@16 14 #include <boost/concept/assert.hpp>
Chris@16 15 #include <boost/heap/heap_concepts.hpp>
Chris@16 16
Chris@16 17 #ifdef BOOST_HEAP_SANITYCHECKS
Chris@16 18 #define BOOST_HEAP_ASSERT BOOST_ASSERT
Chris@16 19 #else
Chris@16 20 #define BOOST_HEAP_ASSERT(expression)
Chris@16 21 #endif
Chris@16 22
Chris@16 23 namespace boost {
Chris@16 24 namespace heap {
Chris@16 25 namespace detail {
Chris@16 26
Chris@16 27 template <typename Heap1, typename Heap2>
Chris@16 28 bool value_equality(Heap1 const & lhs, Heap2 const & rhs,
Chris@16 29 typename Heap1::value_type lval, typename Heap2::value_type rval)
Chris@16 30 {
Chris@16 31 typename Heap1::value_compare const & cmp = lhs.value_comp();
Chris@16 32 bool ret = !(cmp(lval, rval)) && !(cmp(rval, lval));
Chris@16 33
Chris@16 34 // if this assertion is triggered, the value_compare objects of lhs and rhs return different values
Chris@16 35 BOOST_ASSERT((ret == (!(rhs.value_comp()(lval, rval)) && !(rhs.value_comp()(rval, lval)))));
Chris@16 36
Chris@16 37 return ret;
Chris@16 38 }
Chris@16 39
Chris@16 40 template <typename Heap1, typename Heap2>
Chris@16 41 bool value_compare(Heap1 const & lhs, Heap2 const & rhs,
Chris@16 42 typename Heap1::value_type lval, typename Heap2::value_type rval)
Chris@16 43 {
Chris@16 44 typename Heap1::value_compare const & cmp = lhs.value_comp();
Chris@16 45 bool ret = cmp(lval, rval);
Chris@16 46
Chris@16 47 // if this assertion is triggered, the value_compare objects of lhs and rhs return different values
Chris@16 48 BOOST_ASSERT((ret == rhs.value_comp()(lval, rval)));
Chris@16 49 return ret;
Chris@16 50 }
Chris@16 51
Chris@16 52 struct heap_equivalence_copy
Chris@16 53 {
Chris@16 54 template <typename Heap1, typename Heap2>
Chris@16 55 bool operator()(Heap1 const & lhs, Heap2 const & rhs)
Chris@16 56 {
Chris@16 57 BOOST_CONCEPT_ASSERT((boost::heap::PriorityQueue<Heap1>));
Chris@16 58 BOOST_CONCEPT_ASSERT((boost::heap::PriorityQueue<Heap2>));
Chris@16 59
Chris@16 60 // if this assertion is triggered, the value_compare types are incompatible
Chris@16 61 BOOST_STATIC_ASSERT((boost::is_same<typename Heap1::value_compare, typename Heap2::value_compare>::value));
Chris@16 62
Chris@16 63 if (Heap1::constant_time_size && Heap2::constant_time_size)
Chris@16 64 if (lhs.size() != rhs.size())
Chris@16 65 return false;
Chris@16 66
Chris@16 67 if (lhs.empty() && rhs.empty())
Chris@16 68 return true;
Chris@16 69
Chris@16 70 Heap1 lhs_copy(lhs);
Chris@16 71 Heap2 rhs_copy(rhs);
Chris@16 72
Chris@16 73 while (true) {
Chris@16 74 if (!value_equality(lhs_copy, rhs_copy, lhs_copy.top(), rhs_copy.top()))
Chris@16 75 return false;
Chris@16 76
Chris@16 77 lhs_copy.pop();
Chris@16 78 rhs_copy.pop();
Chris@16 79
Chris@16 80 if (lhs_copy.empty() && rhs_copy.empty())
Chris@16 81 return true;
Chris@16 82
Chris@16 83 if (lhs_copy.empty())
Chris@16 84 return false;
Chris@16 85
Chris@16 86 if (rhs_copy.empty())
Chris@16 87 return false;
Chris@16 88 }
Chris@16 89 }
Chris@16 90 };
Chris@16 91
Chris@16 92
Chris@16 93 struct heap_equivalence_iteration
Chris@16 94 {
Chris@16 95 template <typename Heap1, typename Heap2>
Chris@16 96 bool operator()(Heap1 const & lhs, Heap2 const & rhs)
Chris@16 97 {
Chris@16 98 BOOST_CONCEPT_ASSERT((boost::heap::PriorityQueue<Heap1>));
Chris@16 99 BOOST_CONCEPT_ASSERT((boost::heap::PriorityQueue<Heap2>));
Chris@16 100
Chris@16 101 // if this assertion is triggered, the value_compare types are incompatible
Chris@16 102 BOOST_STATIC_ASSERT((boost::is_same<typename Heap1::value_compare, typename Heap2::value_compare>::value));
Chris@16 103
Chris@16 104 if (Heap1::constant_time_size && Heap2::constant_time_size)
Chris@16 105 if (lhs.size() != rhs.size())
Chris@16 106 return false;
Chris@16 107
Chris@16 108 if (lhs.empty() && rhs.empty())
Chris@16 109 return true;
Chris@16 110
Chris@16 111 typename Heap1::ordered_iterator it1 = lhs.ordered_begin();
Chris@16 112 typename Heap1::ordered_iterator it1_end = lhs.ordered_end();
Chris@16 113 typename Heap1::ordered_iterator it2 = rhs.ordered_begin();
Chris@16 114 typename Heap1::ordered_iterator it2_end = rhs.ordered_end();
Chris@16 115 while (true) {
Chris@16 116 if (!value_equality(lhs, rhs, *it1, *it2))
Chris@16 117 return false;
Chris@16 118
Chris@16 119 ++it1;
Chris@16 120 ++it2;
Chris@16 121
Chris@16 122 if (it1 == it1_end && it2 == it2_end)
Chris@16 123 return true;
Chris@16 124
Chris@16 125 if (it1 == it1_end || it2 == it2_end)
Chris@16 126 return false;
Chris@16 127 }
Chris@16 128 }
Chris@16 129 };
Chris@16 130
Chris@16 131
Chris@16 132 template <typename Heap1,
Chris@16 133 typename Heap2
Chris@16 134 >
Chris@16 135 bool heap_equality(Heap1 const & lhs, Heap2 const & rhs)
Chris@16 136 {
Chris@16 137 const bool use_ordered_iterators = Heap1::has_ordered_iterators && Heap2::has_ordered_iterators;
Chris@16 138
Chris@16 139 typedef typename boost::mpl::if_c<use_ordered_iterators,
Chris@16 140 heap_equivalence_iteration,
Chris@16 141 heap_equivalence_copy
Chris@16 142 >::type equivalence_check;
Chris@16 143
Chris@16 144 equivalence_check eq_check;
Chris@16 145 return eq_check(lhs, rhs);
Chris@16 146 }
Chris@16 147
Chris@16 148
Chris@16 149 struct heap_compare_iteration
Chris@16 150 {
Chris@16 151 template <typename Heap1,
Chris@16 152 typename Heap2
Chris@16 153 >
Chris@16 154 bool operator()(Heap1 const & lhs, Heap2 const & rhs)
Chris@16 155 {
Chris@16 156 typename Heap1::size_type left_size = lhs.size();
Chris@16 157 typename Heap2::size_type right_size = rhs.size();
Chris@16 158 if (left_size < right_size)
Chris@16 159 return true;
Chris@16 160
Chris@16 161 if (left_size > right_size)
Chris@16 162 return false;
Chris@16 163
Chris@16 164 typename Heap1::ordered_iterator it1 = lhs.ordered_begin();
Chris@16 165 typename Heap1::ordered_iterator it1_end = lhs.ordered_end();
Chris@16 166 typename Heap1::ordered_iterator it2 = rhs.ordered_begin();
Chris@16 167 typename Heap1::ordered_iterator it2_end = rhs.ordered_end();
Chris@16 168 while (true) {
Chris@16 169 if (value_compare(lhs, rhs, *it1, *it2))
Chris@16 170 return true;
Chris@16 171
Chris@16 172 if (value_compare(lhs, rhs, *it2, *it1))
Chris@16 173 return false;
Chris@16 174
Chris@16 175 ++it1;
Chris@16 176 ++it2;
Chris@16 177
Chris@16 178 if (it1 == it1_end && it2 == it2_end)
Chris@16 179 return true;
Chris@16 180
Chris@16 181 if (it1 == it1_end || it2 == it2_end)
Chris@16 182 return false;
Chris@16 183 }
Chris@16 184 }
Chris@16 185 };
Chris@16 186
Chris@16 187 struct heap_compare_copy
Chris@16 188 {
Chris@16 189 template <typename Heap1,
Chris@16 190 typename Heap2
Chris@16 191 >
Chris@16 192 bool operator()(Heap1 const & lhs, Heap2 const & rhs)
Chris@16 193 {
Chris@16 194 typename Heap1::size_type left_size = lhs.size();
Chris@16 195 typename Heap2::size_type right_size = rhs.size();
Chris@16 196 if (left_size < right_size)
Chris@16 197 return true;
Chris@16 198
Chris@16 199 if (left_size > right_size)
Chris@16 200 return false;
Chris@16 201
Chris@16 202 Heap1 lhs_copy(lhs);
Chris@16 203 Heap2 rhs_copy(rhs);
Chris@16 204
Chris@16 205 while (true) {
Chris@16 206 if (value_compare(lhs_copy, rhs_copy, lhs_copy.top(), rhs_copy.top()))
Chris@16 207 return true;
Chris@16 208
Chris@16 209 if (value_compare(lhs_copy, rhs_copy, rhs_copy.top(), lhs_copy.top()))
Chris@16 210 return false;
Chris@16 211
Chris@16 212 lhs_copy.pop();
Chris@16 213 rhs_copy.pop();
Chris@16 214
Chris@16 215 if (lhs_copy.empty() && rhs_copy.empty())
Chris@16 216 return false;
Chris@16 217 }
Chris@16 218 }
Chris@16 219 };
Chris@16 220
Chris@16 221 template <typename Heap1,
Chris@16 222 typename Heap2
Chris@16 223 >
Chris@16 224 bool heap_compare(Heap1 const & lhs, Heap2 const & rhs)
Chris@16 225 {
Chris@16 226 const bool use_ordered_iterators = Heap1::has_ordered_iterators && Heap2::has_ordered_iterators;
Chris@16 227
Chris@16 228 typedef typename boost::mpl::if_c<use_ordered_iterators,
Chris@16 229 heap_compare_iteration,
Chris@16 230 heap_compare_copy
Chris@16 231 >::type compare_check;
Chris@16 232
Chris@16 233 compare_check check_object;
Chris@16 234 return check_object(lhs, rhs);
Chris@16 235 }
Chris@16 236
Chris@16 237
Chris@16 238 } /* namespace detail */
Chris@16 239 } /* namespace heap */
Chris@16 240 } /* namespace boost */
Chris@16 241
Chris@16 242
Chris@16 243 #undef BOOST_HEAP_ASSERT
Chris@16 244
Chris@16 245 #endif // BOOST_HEAP_DETAIL_HEAP_COMPARISON_HPP