1 #ifndef __GfxTL_LIMITEDHEAP_HEADER__
2 #define __GfxTL_LIMITEDHEAP_HEADER__
13 class PredicateT = std::less<T>,
14 template <
class>
class ContainerT = FlatCopyVector>
19 typedef typename ContainerT<T>::size_type
size_type;
29 BaseType::reserve(limit);
34 BaseType::reserve(_limit);
38 m_pred(pred), _limit(limit), _instances(0)
40 BaseType::reserve(limit);
47 BaseType::reserve(limit + 1);
79 for (
size_type i = BaseType::size() >> 1; i > 0; --i)
88 if (!BaseType::size())
92 for (
size_type i = BaseType::size() - 1; i > 0; --i)
94 T tmp = BaseType::at(i);
95 BaseType::at(i) = BaseType::at(0);
96 BaseType::at(0) = tmp;
104 if (BaseType::size() < _limit)
113 if (BaseType::size() < _limit)
115 BaseType::push_back(t);
116 if (BaseType::size() == _limit)
119 _instances = CountInstances(0);
124 if (m_pred(BaseType::front(), t))
128 if (m_pred(t, BaseType::front()))
132 if (BaseType::size() + 1 - _instances >= _limit)
135 for (
size_type i = 0; i < _instances; ++i)
137 std::swap(BaseType::front(), BaseType::back());
138 BaseType::pop_back();
139 Heapify(0, BaseType::size());
141 BaseType::push_back(t);
143 _instances = CountInstances(0);
146 BaseType::push_back(t);
150 BaseType::front() = t;
151 Heapify(0, BaseType::size());
152 _instances = CountInstances(0);
156 BaseType::push_back(t);
168 while ((
max = (i << 1) + 1) < iend)
170 if (
max + 1 < iend && m_pred(BaseType::at(
max), BaseType::at(
max + 1)))
174 if (!m_pred(BaseType::at(i), BaseType::at(
max)))
187 if (BaseType::size() == 1)
192 while (i && m_pred(BaseType::at(j = ((i + 1) >> 1) - 1), BaseType::at(i)))
194 std::swap(BaseType::at(i), BaseType::at(j));
203 if (child >= BaseType::size())
208 if (!m_pred(BaseType::at(child), BaseType::at(i)))
210 instances = CountInstances(child);
213 if (child >= BaseType::size())
215 return 1 + instances;
217 if (!m_pred(BaseType::at(child), BaseType::at(i)))
219 instances += CountInstances(child);
221 return 1 + instances;
230 template <
class T,
template <
class>
class ContainerT =
FlatCopyVector>
244 BaseType::reserve(limit);
251 BaseType::reserve(limit + 1);
264 for (
size_type i = BaseType::size() >> 1; i > 0; --i)
266 Heapify(i - 1, iend);
273 if (!BaseType::size())
277 for (
size_type i = BaseType::size() - 1; i > 0; --i)
279 T tmp = BaseType::at(i);
280 BaseType::at(i) = BaseType::at(0);
281 BaseType::at(0) = tmp;
289 if (BaseType::size() < _limit)
298 if (BaseType::size() < _limit)
300 BaseType::push_back(t);
301 if (BaseType::size() == _limit)
308 if (t >= BaseType::front())
312 BaseType::front() = t;
313 Heapify(0, BaseType::size());
322 while ((
max = (i << 1) + 1) < iend)
324 if (
max + 1 < iend && BaseType::at(
max) < BaseType::at(
max + 1))
328 if (BaseType::at(i) >= BaseType::at(
max))
340 if (BaseType::size() == 1)
345 while (i && BaseType::at(i) > BaseType::at(j = ((i + 1) >> 1) - 1))
347 std::swap(BaseType::at(i), BaseType::at(j));