|
@@ -6,56 +6,70 @@
|
|
|
|
|
|
using namespace rotor_light;
|
|
|
|
|
|
-TimeEvent &TimeEvent::operator=(const TimeEvent &other) {
|
|
|
- callback = other.callback;
|
|
|
- data = other.data;
|
|
|
- when = other.when;
|
|
|
- return *this;
|
|
|
-}
|
|
|
-
|
|
|
PlannerBase::PlannerBase(TimeEvent *events_, int32_t events_count_)
|
|
|
- : events{events_}, events_count{events_count_}, last_event{-1} {}
|
|
|
+ : events{events_}, events_count{events_count_}, last_event{-1}, next_id{1} {
|
|
|
+}
|
|
|
|
|
|
-bool PlannerBase::add_event(TimePoint when, Callback callback, void *data) {
|
|
|
- if (last_event + 1 < events_count) {
|
|
|
- int i = 0;
|
|
|
- // find the insertion point (skip previous events)
|
|
|
- for (; i <= last_event; ++i) {
|
|
|
- auto &event = events[i];
|
|
|
- if (event.when <= when) {
|
|
|
- continue;
|
|
|
- }
|
|
|
- break;
|
|
|
+EventId PlannerBase::add_event(TimePoint when, Callback callback, void *data) {
|
|
|
+ if (last_event + 1 >= events_count) {
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+ EventId future_id = next_id + 1;
|
|
|
+ bool recheck = false;
|
|
|
+ int i = last_event + 1;
|
|
|
+ // find the insertion point (skip previous events)
|
|
|
+ for (int j = 0; j <= last_event; ++j) {
|
|
|
+ auto &event = events[j];
|
|
|
+ if (event.event_id == future_id) {
|
|
|
+ ++future_id;
|
|
|
+ recheck = true;
|
|
|
}
|
|
|
- // copy next events into the next position
|
|
|
- for (int j = last_event; j >= i; --j) {
|
|
|
- events[j + 1] = events[j];
|
|
|
+ if (event.when <= when) {
|
|
|
+ continue;
|
|
|
+ } else {
|
|
|
+ i = j;
|
|
|
}
|
|
|
- events[i] = TimeEvent{callback, data, when};
|
|
|
- ++last_event;
|
|
|
- return true;
|
|
|
}
|
|
|
- return false;
|
|
|
+ while (recheck || !future_id) {
|
|
|
+ recheck = false;
|
|
|
+ if (!future_id) {
|
|
|
+ ++future_id;
|
|
|
+ }
|
|
|
+ for (int j = 0; j <= last_event; ++j) {
|
|
|
+ auto &event = events[j];
|
|
|
+ if (event.event_id == future_id) {
|
|
|
+ ++future_id;
|
|
|
+ recheck = true;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ // copy next events into the next position
|
|
|
+ for (int j = last_event; j >= i; --j) {
|
|
|
+ events[j + 1] = events[j];
|
|
|
+ }
|
|
|
+ events[i] = TimeEvent{callback, data, when, next_id};
|
|
|
+ next_id = future_id;
|
|
|
+ ++last_event;
|
|
|
+ return events[i].event_id;
|
|
|
}
|
|
|
|
|
|
-void PlannerBase::remove_event(TimePoint when, void *data) {
|
|
|
+void PlannerBase::remove_event(EventId event_id) {
|
|
|
int from = -1;
|
|
|
- int till = -1;
|
|
|
for (int i = 0; i <= last_event; ++i) {
|
|
|
auto &e = events[i];
|
|
|
- if (e.when == when && e.data == data) {
|
|
|
+ if (e.event_id == event_id) {
|
|
|
if (from == -1) {
|
|
|
from = i;
|
|
|
+ break;
|
|
|
}
|
|
|
- till = i;
|
|
|
}
|
|
|
}
|
|
|
- if (till >= 0) {
|
|
|
- auto delta = 1 + till - from;
|
|
|
- for (int i = from; (i <= till) && (i + delta) <= last_event; ++i) {
|
|
|
- events[i] = events[i + delta];
|
|
|
+ if (from >= 0) {
|
|
|
+ for (int i = from; (i + 1) <= last_event; ++i) {
|
|
|
+ events[i] = events[i + 1];
|
|
|
}
|
|
|
- last_event -= delta;
|
|
|
+ --last_event;
|
|
|
}
|
|
|
}
|
|
|
|