* Cleaned up rak::priority_queue.

* Added download time left display, need to tweak it.

* Removed libtorrent/src/download/download_setup.cc.


git-svn-id: svn://rakshasa.no/libtorrent/trunk/rtorrent@607 e378c898-3ddf-0310-93e7-cc216c733640
This commit is contained in:
rakshasa
2005-12-09 05:22:36 +00:00
parent 12ef47da3c
commit 7cc377b550
11 changed files with 102 additions and 77 deletions
+46 -10
View File
@@ -46,7 +46,7 @@
namespace rak {
template <typename Value, typename Compare, typename Equal, typename Remove>
template <typename Value, typename Compare, typename Equal>
class priority_queue : public std::vector<Value> {
public:
typedef std::vector<Value> base_type;
@@ -59,8 +59,8 @@ public:
using base_type::size;
using base_type::empty;
priority_queue(Compare l = Compare(), Equal e = Equal(), Remove r = Remove())
: m_compare(l), m_equal(e), m_remove(r) {}
priority_queue(Compare l = Compare(), Equal e = Equal())
: m_compare(l), m_equal(e) {}
const_reference top() const {
return base_type::front();
@@ -76,16 +76,23 @@ public:
std::push_heap(base_type::begin(), base_type::end(), m_compare);
}
// Removes 'value' from the queue. The Remove functor must change the
// priority of the value such that it comes before any other in the
// queue.
void erase(value_type value) {
iterator itr = std::find_if(base_type::begin(), base_type::end(), std::bind2nd(m_equal, value));
template <typename Key>
iterator find(const Key& key) {
return std::find_if(base_type::begin(), base_type::end(), std::bind2nd(m_equal, key));
}
template <typename Key>
void erase(const Key& key) {
erase(find(key));
}
// Removes 'itr' from the queue. This assumes 'itr' has been
// modified such that it has a higher priority than any other
// element in the queue.
void erase(iterator itr) {
if (itr == base_type::end())
return;
m_remove(*itr);
std::push_heap(base_type::begin(), ++itr, m_compare);
pop();
}
@@ -93,9 +100,38 @@ public:
private:
Compare m_compare;
Equal m_equal;
Remove m_remove;
};
// Iterate while the top node has higher priority, as 'Compare'
// returns false.
template <typename Queue, typename Compare>
class queue_pop_iterator
: public std::iterator<std::forward_iterator_tag, void, void, void, void> {
public:
typedef Queue container_type;
queue_pop_iterator() : m_queue(NULL) {}
queue_pop_iterator(Queue* q, Compare c) : m_queue(q), m_compare(c) {}
queue_pop_iterator& operator ++ () { m_queue->pop(); return *this; }
queue_pop_iterator& operator ++ (int) { m_queue->pop(); return *this; }
typename container_type::const_reference operator * () { return m_queue->top(); }
bool operator != (const queue_pop_iterator& itr) { return !m_queue->empty() && !m_compare(m_queue->top()); }
bool operator == (const queue_pop_iterator& itr) { return m_queue->empty() || m_compare(m_queue->top()); }
private:
Queue* m_queue;
Compare m_compare;
};
template <typename Queue, typename Compare>
inline queue_pop_iterator<Queue, Compare>
queue_popper(Queue& queue, Compare comp) {
return queue_pop_iterator<Queue, Compare>(&queue, comp);
}
}
#endif
+9 -56
View File
@@ -47,15 +47,17 @@ namespace rak {
class priority_item {
public:
bool is_queued() const { return m_time != timer(); }
bool is_queued() const { return m_time != timer(); }
void call() { m_slot(); }
void set_slot(function<void> s) { m_slot = s; }
void call() { m_slot(); }
void set_slot(function<void> s) { m_slot = s; }
const timer& time() const { return m_time; }
void clear_time() { m_time = timer(); }
const timer& time() const { return m_time; }
priority_item* prepare(const timer& t);
priority_item* clear() { m_time = timer(); return this; }
bool compare(const timer& t) const { return m_time > t; }
private:
timer m_time;
@@ -72,62 +74,13 @@ priority_item::prepare(const timer& t) {
}
struct priority_compare {
bool operator () (const priority_item* p1, const priority_item* p2) const {
bool operator () (const priority_item* const p1, const priority_item* const p2) const {
return p1->time() > p2->time();
}
};
struct priority_erase {
void operator () (priority_item* p1) const {
p1->clear_time();
}
};
typedef std::equal_to<priority_item*> priority_equal;
typedef priority_queue<priority_item*, priority_compare, priority_equal, priority_erase> priority_queue_default;
template <typename Queue, typename Compare>
class queue_pop_iterator
: public std::iterator<std::forward_iterator_tag, void, void, void, void> {
public:
typedef Queue container_type;
queue_pop_iterator() : m_queue(NULL) {}
queue_pop_iterator(Queue* q, Compare c) : m_queue(q), m_compare(c) {}
queue_pop_iterator& operator ++ () { m_queue->pop(); return *this; }
queue_pop_iterator& operator ++ (int) { m_queue->pop(); return *this; }
typename container_type::const_reference operator * () { return m_queue->top(); }
bool operator != (const queue_pop_iterator& itr) { return !m_queue->empty() && m_compare(m_queue->top()); }
bool operator == (const queue_pop_iterator& itr) { return m_queue->empty() || !m_compare(m_queue->top()); }
private:
Queue* m_queue;
Compare m_compare;
};
struct priority_ready {
priority_ready() {}
priority_ready(timer t) : m_timer(t) {}
bool operator () (const priority_item* p1) const {
return p1->time() <= m_timer;
}
timer m_timer;
};
inline queue_pop_iterator<priority_queue_default, priority_ready>
queue_popper(priority_queue_default& queue, priority_ready comp) {
return queue_pop_iterator<priority_queue_default, priority_ready>(&queue, comp);
}
inline queue_pop_iterator<priority_queue_default, priority_ready>
queue_popper() {
return queue_pop_iterator<priority_queue_default, priority_ready>();
}
typedef priority_queue<priority_item*, priority_compare, priority_equal> priority_queue_default;
}
+1 -1
View File
@@ -89,7 +89,7 @@ Control::initialize() {
void
Control::cleanup() {
taskScheduler.erase(&m_taskShutdown);
taskScheduler.erase(m_taskShutdown.clear());
m_inputStdin->remove(m_core->get_poll_manager()->get_torrent_poll());
+2 -2
View File
@@ -65,8 +65,8 @@ DownloadFactory::DownloadFactory(const std::string& uri, Manager* m) :
}
DownloadFactory::~DownloadFactory() {
taskScheduler.erase(&m_taskLoad);
taskScheduler.erase(&m_taskCommit);
taskScheduler.erase(m_taskLoad.clear());
taskScheduler.erase(m_taskCommit.clear());
delete m_stream;
m_stream = NULL;
+4 -2
View File
@@ -100,8 +100,10 @@ Manager::do_update() {
std::list<rak::priority_item*> workQueue;
std::copy(rak::queue_popper(displayScheduler, rak::priority_ready(cachedTime)), rak::queue_popper(), std::back_inserter(workQueue));
std::for_each(workQueue.begin(), workQueue.end(), std::mem_fun(&rak::priority_item::clear_time));
std::copy(rak::queue_popper(displayScheduler, rak::bind2nd(std::mem_fun(&rak::priority_item::compare), cachedTime)),
rak::queue_popper(displayScheduler, rak::bind2nd(std::mem_fun(&rak::priority_item::compare), rak::timer())),
std::back_inserter(workQueue));
std::for_each(workQueue.begin(), workQueue.end(), std::mem_fun(&rak::priority_item::clear));
std::for_each(workQueue.begin(), workQueue.end(), std::mem_fun(&rak::priority_item::call));
std::for_each(begin(), end(), rak::if_then(std::mem_fun(&Window::is_active), std::mem_fun(&Window::refresh)));
+30
View File
@@ -71,6 +71,18 @@ print_hhmmss(char* buf, unsigned int length, time_t t) {
return buf + std::min(s, length);
}
char*
print_hhhhmmss(char* buf, unsigned int length, time_t t) {
std::tm *u = std::localtime(&t);
if (u == NULL)
return "inv_time";
unsigned int s = snprintf(buf, length, "%2u:%02u:%02u", u->tm_hour, u->tm_min, u->tm_sec);
return buf + std::min(s, length);
}
char*
print_ddmmyyyy(char* buf, unsigned int length, time_t t) {
std::tm *u = std::gmtime(&t);
@@ -110,6 +122,9 @@ print_download_info(char* buf, unsigned int length, core::Download* d) {
(double)d->get_download().down_rate()->rate() / (1 << 10),
(double)d->get_download().up_rate()->total() / (1 << 20)));
buf += std::max(0, snprintf(buf, length, " Left: "));
buf = print_download_time_left(buf, length, d);
return buf;
}
@@ -138,6 +153,21 @@ print_download_status(char* buf, unsigned int length, core::Download* d) {
return buf;
}
char*
print_download_time_left(char* buf, unsigned int length, core::Download* d) {
uint32_t rate;
if (!d->get_download().is_active() ||
(rate = d->get_download().down_rate()->rate()) < 512) {
buf += std::max(0, snprintf(buf, length, "--:--:--"));
return buf;
}
time_t remaining = (d->get_download().bytes_total() - d->get_download().bytes_done()) / (rate & ~(uint32_t)(512 - 1));
return print_hhhhmmss(buf, length, remaining);
}
// char*
// print_entry_tags(char* buf, unsigned int length) {
+2
View File
@@ -57,11 +57,13 @@ namespace display {
char* print_string(char* buf, unsigned int length, char* str);
char* print_hhmmss(char* buf, unsigned int length, time_t t);
char* print_hhhhmmss(char* buf, unsigned int length, time_t t);
char* print_ddmmyyyy(char* buf, unsigned int length, time_t t);
char* print_download_title(char* buf, unsigned int length, core::Download* d);
char* print_download_info(char* buf, unsigned int length, core::Download* d);
char* print_download_status(char* buf, unsigned int length, core::Download* d);
char* print_download_time_left(char* buf, unsigned int length, core::Download* d);
char* print_entry_tags(char* buf, unsigned int length);
char* print_entry_file(char* buf, unsigned int length, const torrent::Entry& entry);
+2 -2
View File
@@ -54,7 +54,7 @@ Window::Window(Canvas* c, bool d, int h) :
}
Window::~Window() {
displayScheduler.erase(&m_taskUpdate);
displayScheduler.erase(m_taskUpdate.clear());
delete m_canvas;
}
@@ -63,7 +63,7 @@ Window::set_active(bool a) {
if (a)
mark_dirty();
else
displayScheduler.erase(&m_taskUpdate);
displayScheduler.erase(m_taskUpdate.clear());
m_active = a;
}
+1 -1
View File
@@ -92,7 +92,7 @@ protected:
inline void
Window::mark_dirty() {
displayScheduler.erase(&m_taskUpdate);
displayScheduler.erase(m_taskUpdate.clear());
displayScheduler.push(m_taskUpdate.prepare(cachedTime));
}
+4 -2
View File
@@ -223,8 +223,10 @@ main(int argc, char** argv) {
std::list<rak::priority_item*> workQueue;
std::copy(rak::queue_popper(taskScheduler, rak::priority_ready(cachedTime)), rak::queue_popper(), std::back_inserter(workQueue));
std::for_each(workQueue.begin(), workQueue.end(), std::mem_fun(&rak::priority_item::clear_time));
std::copy(rak::queue_popper(taskScheduler, rak::bind2nd(std::mem_fun(&rak::priority_item::compare), cachedTime)),
rak::queue_popper(taskScheduler, rak::bind2nd(std::mem_fun(&rak::priority_item::compare), rak::timer())),
std::back_inserter(workQueue));
std::for_each(workQueue.begin(), workQueue.end(), std::mem_fun(&rak::priority_item::clear));
std::for_each(workQueue.begin(), workQueue.end(), std::mem_fun(&rak::priority_item::call));
// This needs to be called every second or so. Currently done by
+1 -1
View File
@@ -138,7 +138,7 @@ DownloadList::disable() {
disable_display();
taskScheduler.erase(&m_taskUpdate);
taskScheduler.erase(m_taskUpdate.clear());
m_control->display()->erase(m_window);
m_control->display()->erase(m_windowTitle);