|
|
1.1 root 1: //
2: // nono
3: // Copyright (C) 2021 nono project
4: // Licensed under nono-license.txt
5: //
6:
7: #pragma once
8:
9: #include <queue>
10:
11: //
12: // イベントキュー
13: //
14:
15: // 比較関数
16: template <class T>
17: class event_comp_t {
18: public:
19: inline bool operator()(const T& a, const T& b) const
20: {
21: return a->vtime > b->vtime;
22: }
23: };
24: using event_comp = event_comp_t<Event *>;
25:
26: // イベントキュー
27: class EventQueue
28: : public std::priority_queue<Event *, std::vector<Event *>, event_comp>
29: {
30: using inherited =
31: std::priority_queue<Event *, std::vector<Event *>, event_comp>;
32:
33: public:
34: EventQueue()
35: : inherited((event_comp()))
36: {
37: }
38:
39: ~EventQueue()
40: {
41: }
42:
43: // キューから ev と(このオブジェクトのポインタが)一致する要素を外し
44: // true を返す。なければ何もせず false を返す。
45: // (独自メソッドなので大文字で始めている)
46: bool Erase(Event *ev)
47: {
48: for (auto it = c.begin(); it != c.end(); ++it) {
49: Event *e = *it;
50: if (e == ev) {
51: // 見つけた要素を末尾に移動
52: std::pop_heap(it, c.end(), comp);
53: // 末尾を削除
54: c.pop_back();
55: // 再構築
56: std::make_heap(c.begin(), c.end(), comp);
57: return true;
58: }
59: }
60: return false;
61: }
62: };
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.