16 #ifndef INCLUDE_NN_NLIB_REALLOCVEC_H_ 17 #define INCLUDE_NN_NLIB_REALLOCVEC_H_ 33 typedef void* (*ReallocFunc)(
void*, size_t) NLIB_NOEXCEPT_FUNCPTR;
56 NLIB_MOVE_MEMBER_HELPER_4(
ReallocVec, vec_, cur_, size_, realloc_)
61 size_t newsize = (size_ == 0) ? 1 : size_ * 2;
62 if (newsize < size_)
return false;
63 if (!this->Expand(newsize))
return false;
69 if (cur_ == 0)
return false;
74 const T&
operator[](
size_t idx)
const {
return vec_[idx]; }
76 const T&
front()
const {
return *begin(); }
77 T&
back() {
return *(begin() + cur_ - 1); }
78 const T&
back()
const {
return *(begin() + cur_ - 1); }
102 swap(vec_, rhs.vec_);
103 swap(cur_, rhs.cur_);
104 swap(size_, rhs.size_);
105 swap(realloc_, rhs.realloc_);
111 return (n > size_) ? this->Expand(n) :
true;
114 if (!this->reserve(n))
return false;
131 ptrdiff_t idx = pos - begin();
132 if (!this->push_back(val))
return NULL;
133 std::rotate(begin() + idx, &back(), end());
134 return begin() + idx;
143 ReallocFunc realloc_;
156 void* new_ptr = realloc_(ptr, new_size *
sizeof(T));
157 if (!new_ptr)
return false;
158 vec_ =
reinterpret_cast<T*
>(new_ptr);
172 NLIB_MOVE_MEMBER_HELPER_3(
ReallocQueue, front_idx_, front_vec_, back_vec_)
174 return front_vec_.GetRealloc();
177 return front_vec_.size() - front_idx_ + back_vec_.size();
180 return front_idx_ == front_vec_.size() && back_vec_.empty();
184 if (front_idx_ == front_vec_.size()) {
185 if (back_vec_.empty())
return false;
187 front_vec_.swap(back_vec_);
194 if (front_idx_ == front_vec_.size()) {
195 NLIB_ASSERT(!back_vec_.empty());
197 front_vec_.swap(back_vec_);
200 return front_vec_[front_idx_];
203 if (front_idx_ == front_vec_.size()) {
204 if (back_vec_.empty())
return false;
206 front_vec_.swap(back_vec_);
209 if (v) *v = front_vec_[front_idx_];
215 swap(front_idx_, rhs.front_idx_);
216 front_vec_.swap(rhs.front_vec_);
217 back_vec_.swap(rhs.back_vec_);
229 typedef VecType::ReallocFunc ReallocFunc;
238 return this->push_back(str, str +
nlib_strlen(str));
242 size_t n = endchar_ptr - startchar_ptr;
243 char* p =
reinterpret_cast<char*
>(vec_.GetRealloc()(NULL, n + 1));
244 if (!p)
return false;
247 if (!vec_.push_back(p))
return false;
251 if (vec_.empty())
return false;
252 vec_.GetRealloc()(vec_.back(), 0);
261 const char* front()
const NLIB_NOEXCEPT {
return vec_.front(); }
263 const char* back()
const NLIB_NOEXCEPT {
return vec_.back(); }
272 for (; it != end; ++it) {
273 vec_.GetRealloc()(*it, 0);
285 NLIB_DEFINE_STD_SWAP(NLIB_NS::ReallocCstringVec)
286 #ifndef NLIB_STD_SWAP_WORKAROUND 289 NLIB_DEFINE_STD_SWAP_T_BEGIN1(
std)
292 NLIB_DEFINE_STD_SWAP_T1(AL, NLIB_NS::ReallocVec)
293 NLIB_DEFINE_STD_SWAP_T1(AL, NLIB_NS::ReallocQueue)
295 #ifndef NLIB_STD_SWAP_WORKAROUND 298 NLIB_DEFINE_STD_SWAP_T_END1(
std)
301 #endif // INCLUDE_NN_NLIB_REALLOCVEC_H_ const T & front() const
front()をご覧ください。
T * iterator
ランダムアクセスイテレータ
ReallocQueue(ReallocFunc func) noexcept
コンストラクタで、realloc関数をユーザーが指定できます。
const_iterator end() const noexcept
end()を御覧ください。
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 で指定されたクラスのコピーコンストラクタと代入演算子を禁止します。
bool pop_back() noexcept
ベクタの末尾から文字列を削除します。
size_t size() const noexcept
ベクタのサイズを取得する
const_iterator begin() const noexcept
begin()を御覧ください。
C文字列のベクタをreallocベースで実装しています。
bool empty() const noexcept
キューが空かどうかを判定する
PODを要素に持つキューをreallocベースで実装しています。
const T * const_pointer
const T*
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
const T&
const T * const_iterator
読み取り専用ランダムアクセスイテレータ
void swap(ReallocCstringVec &rhs) noexcept
ベクタを交換する
bool resize(size_t n) noexcept
要素数を変更する
const_reverse_iterator rbegin() const noexcept
rbegin()を御覧ください。
bool reserve(size_t n) noexcept
n 個の要素までをメモリを最確保せずに格納できるようにする。
bool pop(T *v) noexcept
キューの先頭要素を取得し、その後削除する。
bool pop_back() noexcept
ベクタの末尾から要素を削除します。
void swap(ReallocQueue &rhs) noexcept
キューを交換する
bool empty() const noexcept
ベクタが空かどうかを判定する
std::reverse_iterator< const_iterator > const_reverse_iterator
std::reverse_iterator<const_iterator>
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
rend()を御覧ください。
constexpr ReallocVec() noexcept
デフォルトコンストラクタで、std::reallocを利用します。
ReallocCstringVec(ReallocFunc func) noexcept
コンストラクタで、realloc関数をユーザーが指定できます。
size_t capacity() const noexcept
ベクタの拡張をせずに格納できる最大の要素数を取得する
void clear() noexcept
ベクタを空にします。メモリは確保されたままであることに注意してください。
bool empty() const noexcept
ベクタが空かどうかを判定する
#define NLIB_NOEXCEPT
環境に合わせてnoexcept 又は同等の定義がされます。
size_t capacity() const noexcept
メモリを再確保せずに格納できる最大の要素数を取得する
iterator end() noexcept
末尾の次を指すイテレータを取得する。
const T & operator[](size_t idx) const
operator[](size_t idx) を御覧ください。
#define NLIB_CEXPR
利用可能であればconstexprが定義されます。そうでない場合は空文字列です。
reverse_iterator rbegin() noexcept
末尾要素を指す逆イテレータを取得する。
bool push(const T &val) noexcept
キューに要素を追加する。
std::reverse_iterator< iterator > reverse_iterator
std::reverse_iterator<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
確保されたメモリを現在の要素数を丁度格納できるサイズにフィッティングします。
iterator begin() noexcept
先頭要素を指すイテレータを取得する。
#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ベースで実装しています。
const_iterator cend() const noexcept
末尾の次を指す読み取り専用イテレータを取得する。
void swap(ReallocVec &rhs) noexcept
ベクタを交換する
const T & back() const
back()を御覧ください。