|
|
1.1 root 1: //
2: // nono
3: // Copyright (C) 2018 [email protected]
4: //
5:
1.1.1.3 ! root 6: #pragma once
! 7:
1.1 root 8: #include "header.h"
9: #include <vector>
10:
11: // 固定長キュー
12: // std::queue では出来ないことがいくつかあるので仕方なく車輪の再開発。
13: //
14: // T 型で上限 capacity 個のキュー。
15: // スレッドセーフではない。
16: template <typename T, int capacity>
17: class FixedQueue
18: {
19: public:
20: // コンストラクタ
21: FixedQueue()
22: : buf(capacity)
23: {
24: Clear();
25: }
26:
27: // デストラクタ
28: virtual ~FixedQueue() {
29: }
30:
31: // 空にする
32: void Clear() {
33: start = 0;
34: length = 0;
35: }
36:
37: // 追加
38: bool Enqueue(T val) {
39: if (length < capacity) {
40: // 空きがあれば追加
41: buf[(start + length) % capacity] = val;
42: length++;
43: return true;
44: } else {
45: return false;
46: }
47: }
48:
49: // 取り出し
50: // 要素が1つ以上あれば *outp に取り出して true を返す。
51: // キューが空なら false を返す。
52: bool Dequeue(T *outp) {
53: if (length > 0) {
54: // 1つ以上あれば取り出し
55: *outp = buf[start];
56: start = (start + 1) % capacity;
57: length--;
58: return true;
59: } else {
60: return false;
61: }
62: }
63:
64: // 取り出し
65: // 先頭の要素を取り出して返す。
66: // キューが空の時に呼ぶと不定値が返る。
67: T Dequeue() {
68: T val = buf[start];
69: if (length > 0) {
70: start = (start + 1) % capacity;
71: length--;
72: }
73: return val;
74: }
75:
76: // 現在の要素数を取得
77: uint Length() const {
78: return length;
79: }
80:
1.1.1.2 root 81: // 要素が一杯なら true を返す
82: bool IsFull() const {
83: return (length >= capacity);
84: }
85:
1.1 root 86: // 要素を覗き見る。
87: // idx は 0 .. length-1 までで、0 がキューの先頭。
1.1.1.3 ! root 88: T Peek(int idx) const {
1.1 root 89: // XXX 範囲チェックすべきだがとりあえず
90: return buf[(start + idx) % capacity];
91: }
92:
93: private:
94: std::vector<T> buf; // バッファ
95: uint start = 0; // 開始位置
96: uint length = 0; // 現在有効な長さ
97: };
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.