File:  [Isaki's NoNo m68k/m88k emulator] / nono / vm / eventqueue.h
Revision 1.1.1.1 (vendor branch): download - view: text, annotated - select for diffs
Wed Apr 29 17:04:52 2026 UTC (2 months, 3 weeks ago) by root
Branches: MAIN, Isaki
CVS tags: v014, v013, v012, v011, v010, HEAD
nono 0.1.6

//
// nono
// Copyright (C) 2021 nono project
// Licensed under nono-license.txt
//

#pragma once

#include <queue>

//
// イベントキュー
//

// 比較関数
template <class T>
class event_comp_t {
 public:
	inline bool operator()(const T& a, const T& b) const
	{
		return a->vtime > b->vtime;
	}
};
using event_comp = event_comp_t<Event *>;

// イベントキュー
class EventQueue
	: public std::priority_queue<Event *, std::vector<Event *>, event_comp>
{
	using inherited =
		std::priority_queue<Event *, std::vector<Event *>, event_comp>;

 public:
	EventQueue()
		: inherited((event_comp()))
	{
	}

	~EventQueue()
	{
	}

	// キューから ev と(このオブジェクトのポインタが)一致する要素を外し
	// true を返す。なければ何もせず false を返す。
	// (独自メソッドなので大文字で始めている)
	bool Erase(Event *ev)
	{
		for (auto it = c.begin(); it != c.end(); ++it) {
			Event *e = *it;
			if (e == ev) {
				// 見つけた要素を末尾に移動
				std::pop_heap(it, c.end(), comp);
				// 末尾を削除
				c.pop_back();
				// 再構築
				std::make_heap(c.begin(), c.end(), comp);
				return true;
			}
		}
		return false;
	}
};

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.