16 #ifndef INCLUDE_NN_NLIB_REALLOCVEC_H_ 17 #define INCLUDE_NN_NLIB_REALLOCVEC_H_ 34 typedef void* (*ReallocFunc)(
void*, size_t)NLIB_NOEXCEPT_FUNCPTR;
57 #ifdef __cpp_rvalue_references 61 realloc_(rhs.realloc_) {
71 realloc_ = rhs.realloc_;
82 realloc_(rhs.realloc_) {
92 realloc_ = rhs.realloc_;
102 size_t newsize = (size_ == 0) ? 1 : size_ * 2;
103 if (newsize < size_)
return false;
104 if (!this->Expand(newsize))
return false;
110 if (cur_ == 0)
return false;
117 NLIB_ASSERT(!this->empty());
121 NLIB_ASSERT(!this->empty());
125 NLIB_ASSERT(!this->empty());
126 return *(begin() + cur_ - 1);
129 NLIB_ASSERT(!this->empty());
130 return *(begin() + cur_ - 1);
150 if (!this->reserve(n))
return false;
167 ptrdiff_t idx = pos - begin();
168 if (!this->push_back(val))
return nullptr;
169 std::rotate(begin() + idx, &back(), end());
170 return begin() + idx;
179 ReallocFunc realloc_;
192 void* new_ptr = realloc_(ptr, new_size *
sizeof(T));
193 if (!new_ptr)
return false;
194 vec_ =
static_cast<T*
>(new_ptr);
208 #ifdef __cpp_rvalue_references 210 front_vec_(std::move(rhs.front_vec_)),
211 back_vec_(std::move(rhs.back_vec_)) {
215 front_idx_ = rhs.front_idx_;
217 front_vec_ = std::move(rhs.front_vec_);
218 back_vec_ = std::move(rhs.back_vec_);
223 : front_idx_(rhs.front_idx_),
224 front_vec_(rhs.front_vec_,
move_tag()),
225 back_vec_(rhs.back_vec_,
move_tag()) {
228 ReallocQueue& assign(ReallocQueue& rhs, move_tag)
NLIB_NOEXCEPT {
229 front_idx_ = rhs.front_idx_;
231 front_vec_.assign(rhs.front_vec_, move_tag());
232 back_vec_.assign(rhs.back_vec_, move_tag());
238 return front_idx_ == front_vec_.size() && back_vec_.empty();
242 if (front_idx_ == front_vec_.size()) {
244 if (back_vec_.empty())
return false;
246 swap(front_vec_, back_vec_);
253 if (front_idx_ == front_vec_.size()) {
255 NLIB_ASSERT(!back_vec_.empty());
257 swap(front_vec_, back_vec_);
260 return front_vec_[front_idx_];
263 if (front_idx_ == front_vec_.size()) {
265 if (back_vec_.empty())
return false;
267 swap(front_vec_, back_vec_);
270 if (v) *v = front_vec_[front_idx_];
284 typedef VecType::ReallocFunc ReallocFunc;
290 #ifdef __cpp_rvalue_references 291 #ifdef NLIB_CXX11_DEFAULTED_AND_DELETED_FUNCTIONS 292 ReallocCstringVec(ReallocCstringVec&& rhs) =
default;
293 ReallocCstringVec& operator=(ReallocCstringVec&& rhs) =
default;
295 ReallocCstringVec(ReallocCstringVec&& rhs)
NLIB_NOEXCEPT : vec_(std::move(rhs.vec_)) {}
296 ReallocCstringVec& operator=(ReallocCstringVec&& rhs)
NLIB_NOEXCEPT {
297 vec_ = std::move(rhs.vec_);
302 ReallocCstringVec(ReallocCstringVec& rhs, move_tag)
NLIB_NOEXCEPT : vec_(rhs.vec_, move_tag()) {
304 ReallocCstringVec& assign(ReallocCstringVec& rhs, move_tag)
NLIB_NOEXCEPT {
305 vec_.assign(rhs.vec_, move_tag());
310 return this->push_back(str, str +
nlib_strlen(str));
313 size_t n = endchar_ptr - startchar_ptr;
314 char* p =
static_cast<char*
>(vec_.GetRealloc()(
nullptr, n + 1));
315 if (!p)
return false;
316 nlib_memcpy(p, n, startchar_ptr, n);
318 return vec_.push_back(p);
321 if (vec_.empty())
return false;
322 vec_.GetRealloc()(vec_.back(), 0);
339 for (; it != end; ++it) {
340 vec_.GetRealloc()(*it, 0);
352 NLIB_DEFINE_STD_SWAP(NLIB_NS::ReallocCstringVec)
354 NLIB_DEFINE_STD_SWAP_T_BEGIN2(
nn, nlib)
355 NLIB_DEFINE_STD_SWAP_T1(AL, NLIB_NS::ReallocVec)
356 NLIB_DEFINE_STD_SWAP_T1(AL, NLIB_NS::ReallocQueue)
357 NLIB_DEFINE_STD_SWAP_T_END2(
nn, nlib)
359 #endif // INCLUDE_NN_NLIB_REALLOCVEC_H_ const T & front() const
上記関数のconst修飾付き版です。
T * iterator
ランダムアクセス反復子です。
ReallocQueue(ReallocFunc func) noexcept
コンストラクタで、realloc関数をユーザーが指定できます。
const_iterator end() const noexcept
末尾の次を指す読み取り専用反復子を取得します。
ReallocVec< T >::ReallocFunc ReallocFunc
reallocに相当する関数の型です。
bool push_back(const T &v) noexcept
ベクタに要素を追加します。
C++11の標準ヘッダとなるtype_traitsの代用定義です。 コンパイラや標準ライブラリによってサポートされてい...
size_t size_type
符号なし整数型(size_t)
char * front() noexcept
最初の文字列を取得します。
#define NLIB_DISALLOW_COPY_AND_ASSIGN(TypeName)
TypeName で指定されたクラスのコピーコンストラクタと代入演算子を禁止します。
ReallocVec & operator=(ReallocVec &&rhs) noexcept
ムーブ代入演算子です。
bool pop_back() noexcept
ベクタの末尾から文字列を削除します。
size_t size() const noexcept
格納されている要素数を返します。
const_iterator begin() const noexcept
先頭要素を指す読み取り専用反復子を取得します。
C文字列のベクタをreallocベースで実装しています。
bool empty() const noexcept
キューが空かどうかを判定する
PODを要素に持つキューをreallocベースで実装しています。
const char * front() const noexcept
上記関数のconst修飾付き版です。
const char * back() const noexcept
上記関数のconst修飾付き版です。
const T * const_pointer
要素への読み取り専用ポインタです。
bool reserve(size_t n) noexcept
n 個の要素までをベクタの拡張をせずに格納できるようにする。
iterator insert(iterator pos, const T &val)
要素をposの場所に挿入します。
char * back() noexcept
最後の文字列を取得します。
const_reverse_iterator crbegin() const noexcept
末尾要素を指す読み取り専用逆反復子を取得します。
bool pop() noexcept
キューの先頭要素を削除する。
std::unique_ptrに相当するクラスが定義されています。
const_iterator cbegin() const noexcept
先頭要素を指す読み取り専用反復子を取得します。
const T & const_reference
要素への読み取り専用参照です。
ReallocFunc GetRealloc() const noexcept
指定したrealloc関数を取得します。
const T * const_iterator
読み取り専用ランダムアクセス反復子です。
T & front()
最初の要素への参照を取得します。
bool resize(size_t n) noexcept
要素数を変更する
const_reverse_iterator rbegin() const noexcept
末尾要素を指す読み取り専用逆反復子を取得します。
bool reserve(size_t n) noexcept
n 個の要素までをメモリを最確保せずに格納できるようにする。
T & back()
最後の要素への参照を取得します。
bool pop(T *v) noexcept
キューの先頭要素を取得し、その後削除する。
ReallocQueue(ReallocQueue &&rhs) noexcept
ムーブコンストラクタです。
bool pop_back() noexcept
ベクタの末尾から要素を削除します。
空の構造体で、関数の引数をムーブすべきことを示すために利用されます。
ReallocQueue & operator=(ReallocQueue &&rhs) noexcept
ムーブ代入演算子です。
bool empty() const noexcept
ベクタが空かどうかを判定する
std::reverse_iterator< const_iterator > const_reverse_iterator
読み取り専用逆反復子です。
ReallocFunc GetRealloc() const noexcept
指定したrealloc関数を取得します。
ReallocCstringVec() noexcept
デフォルトコンストラクタです。 std::reallocを利用します。
T & operator[](size_t idx)
idx 番目の要素を取得します。
size_t size() const noexcept
キューのサイズを取得する
reverse_iterator rend() noexcept
先頭の前を指す逆反復子を取得します。
bool push_back(const char *str) noexcept
C文字列を末尾に追加します。
const_reverse_iterator rend() const noexcept
先頭の前を指す読み取り専用逆反復子を取得します。
constexpr ReallocVec() noexcept
デフォルトコンストラクタで、std::reallocを利用します。
ReallocCstringVec(ReallocFunc func) noexcept
コンストラクタで、realloc関数をユーザーが指定できます。
size_t capacity() const noexcept
ベクタの拡張をせずに格納できる最大の要素数を取得する
void clear() noexcept
ベクタを空にします。メモリは確保されたままであることに注意してください。
void clear() noexcept
ベクタを空にします。
bool empty() const noexcept
格納されている要素数が0ならばtrue、それ以外ならfalseを返します。
#define NLIB_NOEXCEPT
環境に合わせてnoexcept 又は同等の定義がされます。
size_t capacity() const noexcept
メモリを再確保せずに格納できる最大の要素数を取得する
iterator end() noexcept
末尾の次を指す反復子を取得します。
const T & operator[](size_t idx) const
上記関数のconst修飾付き版です。
#define NLIB_CEXPR
利用可能であればconstexprが定義されます。そうでない場合は空文字列です。
reverse_iterator rbegin() noexcept
末尾要素を指す逆反復子を取得します。
bool push(const T &val) noexcept
キューに要素を追加する。
std::reverse_iterator< iterator > reverse_iterator
逆反復子です。
bool push_back(const char *startchar_ptr, const char *endchar_ptr) noexcept
(部分)文字列を末尾に追加します。
const_reverse_iterator crend() const noexcept
先頭の前を指す読み取り専用逆反復子を取得します。
char * operator[](size_t idx) noexcept
idx番目の文字列を取得します。
void shrink_to_fit() noexcept
確保されたメモリを現在の要素数を丁度格納できるサイズにフィッティングします。
const char * operator[](size_t idx) const noexcept
上記関数のconst修飾付き版です。
iterator begin() noexcept
先頭要素を指す反復子を取得します。
ReallocFunc GetRealloc() const noexcept
指定したrealloc関数を取得します。
#define NLIB_STATIC_ASSERT(exp)
静的アサートが定義されます。利用可能であればstatic_assertを利用します。
ReallocQueue() noexcept
デフォルトコンストラクタです。 std::reallocを利用します。
ptrdiff_t difference_type
符号つき整数型(ptrdiff_t)
T & front()
キューの先頭要素を取得する。
size_t size() const noexcept
ベクタのサイズを取得する
PODを要素に持つベクタをreallocベースで実装しています。
ReallocVec(ReallocVec &&rhs) noexcept
ムーブコンストラクタです。
const_iterator cend() const noexcept
末尾の次を指す読み取り専用反復子を取得します。
const T & back() const
上記関数のconst修飾付き版です。