--- nono/lib/fixedqueue.h 2026/04/29 17:04:51 1.1.1.6 +++ nono/lib/fixedqueue.h 2026/04/29 17:05:22 1.1.1.9 @@ -4,6 +4,10 @@ // Licensed under nono-license.txt // +// +// 固定長キュー +// + #pragma once #include "header.h" @@ -17,8 +21,7 @@ // T 型で上限 capacity 個のキュー。 // スレッドセーフではない。 template -class FixedQueue - : private std::vector +class FixedQueue : private std::vector { using inherited = std::vector; public: @@ -40,7 +43,7 @@ class FixedQueue // 空きがあれば追加して true を返す。 // 空きがなければ何もせず false を返す。 - bool Enqueue(T val) { + bool Enqueue(const T& val) { if (IsFull()) { return false; } else { @@ -51,7 +54,7 @@ class FixedQueue } // 空きがなければ古いのを捨てて追加する。 - void EnqueueForce(T val) { + void EnqueueForce(const T& val) { if (IsFull()) { Dequeue(); } @@ -114,6 +117,19 @@ class FixedQueue return capacity; } + // 先頭側から追加する。 + // 空きがなければ何もせず false を返す。 + bool PushFront(const T& val) { + if (IsFull()) { + return false; + } else { + start = (start + capacity - 1) % capacity; + (*this)[start] = val; + length++; + return true; + } + } + private: uint start {}; // 開始位置 uint length {}; // 現在有効な長さ