Annotation of nono/lib/fixedqueue.h, revision 1.1.1.4

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

unix.superglobalmegacorp.com

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