Simplify updating queue entries

The erase/insert pattern can be replaced by a single method that
updates an existing entry's timer, or inserting if it doesn't exist.

This has much less impact than the libtorrent change it's based on,
but might as well be consistent.
This commit is contained in:
kannibalox
2024-10-17 16:16:17 -04:00
committed by Jari Sundell
parent 38b39bdafc
commit 6e976616a0
6 changed files with 32 additions and 19 deletions
+22 -4
View File
@@ -127,14 +127,32 @@ priority_queue_erase(priority_queue_default* queue, priority_item* item) {
if (!item->is_valid())
throw torrent::internal_error("priority_queue_erase(...) called on an invalid item.");
// Clear time before erasing to force it to the top.
// Unqueue it before erasing.
item->clear_time();
if (!queue->erase(item))
throw torrent::internal_error("priority_queue_erase(...) could not find item in queue.");
}
if (queue->find(item) != queue->end())
throw torrent::internal_error("priority_queue_erase(...) item still in queue.");
inline void
priority_queue_update(priority_queue_default* queue, priority_item* item, timer t) {
if (t == timer())
throw torrent::internal_error("priority_queue_update(...) received a bad timer.");
if (!item->is_valid())
throw torrent::internal_error("priority_queue_update(...) called on an invalid item.");
if (queue->find(item) == queue->end()) {
if (item->is_queued())
throw torrent::internal_error("priority_queue_update(...) cannot insert an already queued item.");
item->set_time(t);
queue->push(item);
} else {
item->set_time(t);
queue->update();
}
}
}