Reworked Traveling Salesman Problem code for simplicity and robustness.

The TSP algorithm is newly used for planning of the printing order
of objects AND their instances.
This commit is contained in:
bubnikv
2019-09-26 16:39:50 +02:00
parent 030f84734a
commit 702c06d4b6
8 changed files with 293 additions and 316 deletions

View File

@@ -13,7 +13,7 @@ public:
{}
~MutablePriorityQueue() { clear(); }
void clear() { m_heap.clear(); }
void clear();
void reserve(size_t cnt) { m_heap.reserve(cnt); }
void push(const T &item);
void push(T &&item);
@@ -49,6 +49,17 @@ MutablePriorityQueue<T, IndexSetter, LessPredicate> make_mutable_priority_queue(
std::forward<IndexSetter>(index_setter), std::forward<LessPredicate>(less_predicate));
}
template<class T, class LessPredicate, class IndexSetter>
inline void MutablePriorityQueue<T, LessPredicate, IndexSetter>::clear()
{
#ifndef NDEBUG
for (size_t idx = 0; idx < m_heap.size(); ++ idx)
// Mark as removed from the queue.
m_index_setter(m_heap[idx], std::numeric_limits<size_t>::max());
#endif /* NDEBUG */
m_heap.clear();
}
template<class T, class LessPredicate, class IndexSetter>
inline void MutablePriorityQueue<T, LessPredicate, IndexSetter>::push(const T &item)
{
@@ -71,6 +82,10 @@ template<class T, class LessPredicate, class IndexSetter>
inline void MutablePriorityQueue<T, LessPredicate, IndexSetter>::pop()
{
assert(! m_heap.empty());
#ifndef NDEBUG
// Mark as removed from the queue.
m_index_setter(m_heap.front(), std::numeric_limits<size_t>::max());
#endif /* NDEBUG */
if (m_heap.size() > 1) {
m_heap.front() = m_heap.back();
m_heap.pop_back();
@@ -84,6 +99,10 @@ template<class T, class LessPredicate, class IndexSetter>
inline void MutablePriorityQueue<T, LessPredicate, IndexSetter>::remove(size_t idx)
{
assert(idx < m_heap.size());
#ifndef NDEBUG
// Mark as removed from the queue.
m_index_setter(m_heap[idx], std::numeric_limits<size_t>::max());
#endif /* NDEBUG */
if (idx + 1 == m_heap.size()) {
m_heap.pop_back();
return;