3 #ifndef INCLUDE_NN_NLIB_REALLOCVEC_H_
4 #define INCLUDE_NN_NLIB_REALLOCVEC_H_
20 typedef void* (*ReallocFunc)(
void*, size_t);
43 NLIB_MOVE_MEMBER_HELPER_4(ReallocVec, m_Vec, m_Cur, m_Size, m_Realloc)
47 if (m_Cur == m_Size) {
48 size_t newSize = (m_Size == 0) ? 1 : m_Size * 2;
49 if (newSize < m_Size)
return false;
50 if (!this->Expand(newSize))
return false;
56 if (m_Cur == 0)
return false;
61 const T&
operator[](
size_t idx)
const {
return m_Vec[idx]; }
63 const T&
front()
const {
return *begin(); }
64 T&
back() {
return *(begin() + m_Cur - 1); }
65 const T&
back()
const {
return *(begin() + m_Cur - 1); }
75 return const_reverse_iterator(end());
79 return const_reverse_iterator(begin());
82 return const_reverse_iterator(end());
85 return const_reverse_iterator(begin());
89 swap(m_Vec, rhs.m_Vec);
90 swap(m_Cur, rhs.m_Cur);
91 swap(m_Size, rhs.m_Size);
92 swap(m_Realloc, rhs.m_Realloc);
98 return (n > m_Size) ? this->Expand(n) :
true;
101 if (!this->reserve(n))
return false;
107 if (m_Cur != m_Size) {
117 iterator
insert(iterator pos,
const T& val) {
118 ptrdiff_t idx = pos - begin();
119 if (!this->push_back(val))
return NULL;
120 std::rotate(begin() + idx, &back(), end());
121 return begin() + idx;
130 ReallocFunc m_Realloc;
143 void* newPtr = m_Realloc(ptr, newSize *
sizeof(T));
144 if (!newPtr)
return false;
145 m_Vec =
reinterpret_cast<T*
>(newPtr);
159 NLIB_MOVE_MEMBER_HELPER_3(ReallocQueue, m_FrontIdx, m_FrontVec, m_BackVec)
161 return m_FrontVec.GetRealloc();
164 return m_FrontVec.size() - m_FrontIdx + m_BackVec.size();
167 return m_FrontIdx == m_FrontVec.size() && m_BackVec.empty();
171 if (m_FrontIdx == m_FrontVec.size()) {
172 if (m_BackVec.empty())
return false;
174 m_FrontVec.swap(m_BackVec);
181 if (m_FrontIdx == m_FrontVec.size()) {
182 NLIB_ASSERT(!m_BackVec.empty());
184 m_FrontVec.swap(m_BackVec);
187 return m_FrontVec[m_FrontIdx];
190 if (m_FrontIdx == m_FrontVec.size()) {
191 if (m_BackVec.empty())
return false;
193 m_FrontVec.swap(m_BackVec);
196 if (v) *v = m_FrontVec[m_FrontIdx];
202 swap(m_FrontIdx, rhs.m_FrontIdx);
203 m_FrontVec.swap(rhs.m_FrontVec);
204 m_BackVec.swap(rhs.m_BackVec);
216 typedef VecType::ReallocFunc ReallocFunc;
222 NLIB_MOVE_MEMBER_HELPER_1(ReallocCstringVec, m_Vec)
225 return this->push_back(str, str +
nlib_strlen(str));
229 size_t n = endCharPtr - startCharPtr;
230 char* p =
reinterpret_cast<char*
>(m_Vec.GetRealloc()(NULL, n + 1));
231 if (!p)
return false;
234 if (!m_Vec.push_back(p))
return false;
238 if (m_Vec.empty())
return false;
239 m_Vec.GetRealloc()(m_Vec.back(), 0);
248 const char* front() const
NLIB_NOEXCEPT {
return m_Vec.front(); }
250 const char* back() const
NLIB_NOEXCEPT {
return m_Vec.back(); }
257 VecType::iterator it = m_Vec.begin();
258 VecType::iterator end = m_Vec.end();
259 for (; it != end; ++it) {
260 m_Vec.GetRealloc()(*it, 0);
272 NLIB_DEFINE_STD_SWAP(NLIB_NS::ReallocCstringVec)
273 #ifndef NLIB_STD_SWAP_WORKAROUND
276 NLIB_DEFINE_STD_SWAP_T_BEGIN1(
std)
279 NLIB_DEFINE_STD_SWAP_T1(AL, NLIB_NS::ReallocVec)
280 NLIB_DEFINE_STD_SWAP_T1(AL, NLIB_NS::ReallocQueue)
282 #ifndef NLIB_STD_SWAP_WORKAROUND
285 NLIB_DEFINE_STD_SWAP_T_END1(
std)
288 #endif // INCLUDE_NN_NLIB_REALLOCVEC_H_
#define NLIB_NOEXCEPT
環境に合わせてnoexcept 又は同等の定義がされます。
T * iterator
ランダムアクセスイテレータ
bool empty() const noexcept
キューが空かどうかを判定する
ReallocQueue(ReallocFunc func) noexcept
コンストラクタで、realloc関数をユーザーが指定できます。
size_t capacity() const noexcept
ベクタの拡張をせずに格納できる最大の要素数を取得する
ReallocVec< T >::ReallocFunc ReallocFunc
reallocに相当する関数の型です。
bool push_back(const T &v) noexcept
ベクタに要素を追加します。
C++11の標準ヘッダとなるtype_traitsの代用定義です。 コンパイラや標準ライブラリによってサポートされてい...
const_reverse_iterator crend() const noexcept
先頭の前を指す読み取り専用逆イテレータを取得する。
size_t size_type
符号なし整数型(size_t)
bool empty() const noexcept
ベクタが空かどうかを判定する
char * front() noexcept
最初の文字列を取得します。
#define NLIB_DISALLOW_COPY_AND_ASSIGN(TypeName)
TypeName で指定されたクラスのコピーコンストラクタと代入演算子を禁止します。
bool pop_back() noexcept
ベクタの末尾から文字列を削除します。
size_t size() const noexcept
ベクタのサイズを取得する
const_reverse_iterator rend() const noexcept
rend()を御覧ください。
C文字列のベクタをreallocベースで実装しています。
PODを要素に持つキューをreallocベースで実装しています。
const_iterator begin() const noexcept
begin()を御覧ください。
const T * const_pointer
const T*
bool reserve(size_t n) noexcept
n 個の要素までをベクタの拡張をせずに格納できるようにする。
iterator insert(iterator pos, const T &val)
要素をposの場所に挿入します。
const_iterator cend() const noexcept
末尾の次を指す読み取り専用イテレータを取得する。
const_iterator end() const noexcept
end()を御覧ください。
char * back() noexcept
最後の文字列を取得します。
size_t capacity() const noexcept
メモリを再確保せずに格納できる最大の要素数を取得する
bool pop() noexcept
キューの先頭要素を削除する。
std::unique_ptrに相当するクラスが定義されています。
const_iterator cbegin() const noexcept
先頭要素を指す読み取り専用イテレータを取得する。
const T & const_reference
const T&
const T * const_iterator
読み取り専用ランダムアクセスイテレータ
size_t size() const noexcept
キューのサイズを取得する
void swap(ReallocCstringVec &rhs) noexcept
ベクタを交換する
bool resize(size_t n) noexcept
要素数を変更する
bool reserve(size_t n) noexcept
n 個の要素までをメモリを最確保せずに格納できるようにする。
bool pop(T *v) noexcept
キューの先頭要素を取得し、その後削除する。
bool pop_back() noexcept
ベクタの末尾から要素を削除します。
void swap(ReallocQueue &rhs) noexcept
キューを交換する
const T & front() const
front()をご覧ください。
#define NLIB_CEXPR
利用可能であればconstexprが定義されます。そうでない場合は空文字列です。
std::reverse_iterator< const_iterator > const_reverse_iterator
std::reverse_iterator
ReallocCstringVec() noexcept
デフォルトコンストラクタで、std::reallocを利用します。
T & operator[](size_t idx)
idx 番目の要素を取得します。
reverse_iterator rend() noexcept
先頭の前を指す逆イテレータを取得する。
bool push_back(const char *str) noexcept
C文字列を末尾に追加します。
bool push_back(const char *startCharPtr, const char *endCharPtr) noexcept
(部分)文字列を末尾に追加します。
constexpr ReallocVec() noexcept
デフォルトコンストラクタで、std::reallocを利用します。
size_t size() const noexcept
ベクタのサイズを取得する
ReallocCstringVec(ReallocFunc func) noexcept
コンストラクタで、realloc関数をユーザーが指定できます。
const_reverse_iterator rbegin() const noexcept
rbegin()を御覧ください。
void clear() noexcept
ベクタを空にします。メモリは確保されたままであることに注意してください。
const T & back() const
back()を御覧ください。
iterator end() noexcept
末尾の次を指すイテレータを取得する。
const_reverse_iterator crbegin() const noexcept
末尾要素を指す読み取り専用逆イテレータを取得する。
reverse_iterator rbegin() noexcept
末尾要素を指す逆イテレータを取得する。
bool push(const T &val) noexcept
キューに要素を追加する。
std::reverse_iterator< iterator > reverse_iterator
std::reverse_iterator
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)
bool empty() const noexcept
ベクタが空かどうかを判定する
T & front()
キューの先頭要素を取得する。
PODを要素に持つベクタをreallocベースで実装しています。
const T & operator[](size_t idx) const
operator[](size_t idx) を御覧ください。
void swap(ReallocVec &rhs) noexcept
ベクタを交換する