5#ifndef _COLLECTION2_BUFFER_H_
6#define _COLLECTION2_BUFFER_H_
12namespace collection2 {
20template <
typename Element,
typename Size =
size_t>
26 Element*
const internalData;
31 Size internalDataSize;
56 Buffer(Element*
const data,
const Size& dataSize);
85 return internalDataSize;
103 return count < internalDataSize;
116template <
typename Element,
typename Size>
120 internalDataSize = dataSize;
125 unsigned char maxbitPos = 0;
126 Size size = dataSize;
127 while ((size >>= 1) != 0) {
130 internalDataSize = 1 << maxbitPos;
133template <
typename Element,
typename Size>
136 if (internalDataSize == 0) {
137 return OperationResult::Overflow;
142 auto result = pop(
nullptr);
146 *(internalData + tail) = data;
148 tail = (tail + 1) & (internalDataSize - 1);
151 return OperationResult::Success;
154template <
typename Element,
typename Size>
158 return OperationResult::Empty;
162 if (data !=
nullptr) {
163 *data = *(internalData + head);
166 head = (head + 1) & (internalDataSize - 1);
169 return OperationResult::Success;
リングバッファ
Definition: buffer.hpp:21
OperationResult append(const Element &data)
バッファの末尾にデータを追加
Definition: buffer.hpp:134
bool isEmpty() const
バッファが空かどうか
Definition: buffer.hpp:111
OperationResult pop(Element *const data)
バッファの先頭からデータを取り出し
Definition: buffer.hpp:155
Buffer(Element *const data, const Size &dataSize)
内部データを扱う領域とそのサイズを指定してバッファを初期化
Definition: buffer.hpp:117
Size capacity() const
バッファの全体長を返す
Definition: buffer.hpp:84
Size amount() const
現在バッファ内にあるデータ数を返す
Definition: buffer.hpp:93
bool hasSpace() const
バッファに値を追加できるか
Definition: buffer.hpp:102
OperationResult
コレクション操作結果
Definition: common.hpp:14