Zhenkai Zhu | 97019eb | 2013-01-08 00:21:43 -0800 | [diff] [blame^] | 1 | #include "event-scheduler.h" |
| 2 | |
| 3 | Task::Task(const Callback &callback, void *arg, const Tag &tag) |
| 4 | : m_callback(callback) |
| 5 | , m_arg(arg) |
| 6 | , m_tag(tag) |
| 7 | { |
| 8 | } |
| 9 | |
| 10 | Task::Task(const Task &other) |
| 11 | : m_callback(other.m_callback) |
| 12 | , m_arg(other.m_arg) |
| 13 | , m_tag(other.m_tag) |
| 14 | { |
| 15 | } |
| 16 | |
| 17 | Task & |
| 18 | Task::operator=(const Task &other) |
| 19 | { |
| 20 | m_callback = other.m_callback; |
| 21 | m_arg = other.m_arg; |
| 22 | m_tag = other.m_tag; |
| 23 | return (*this); |
| 24 | } |
| 25 | |
| 26 | RandomIntervalGenerator::RandomIntervalGenerator(double interval, double percent, Direction direction = UP) |
| 27 | : m_interval(interval) |
| 28 | , m_rng(time(NULL)) |
| 29 | , m_percent(percent) |
| 30 | , m_dist(0.0, fractional(percent)) |
| 31 | , m_random(m_rng, m_dist) |
| 32 | { |
| 33 | } |
| 34 | |
| 35 | double |
| 36 | RandomIntervalGenerator::nextInterval() |
| 37 | { |
| 38 | double percent = m_random(); |
| 39 | double interval = m_interval; |
| 40 | switch (m_direction) |
| 41 | { |
| 42 | case UP: interval = m_interval * (1.0 + percent); break; |
| 43 | case DOWN: interval = m_interval * (1.0 - percent); break; |
| 44 | case EVEN: interval = m_interval * (1.0 - m_percent/2.0 + percent); break; |
| 45 | default: break |
| 46 | } |
| 47 | |
| 48 | return interval; |
| 49 | } |