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

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

unix.superglobalmegacorp.com

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