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, vec_, cur_, size_, realloc_)
48 size_t newsize = (size_ == 0) ? 1 : size_ * 2;
49 if (newsize < size_)
return false;
50 if (!this->Expand(newsize))
return false;
56 if (cur_ == 0)
return false;
61 const T&
operator[](
size_t idx)
const {
return vec_[idx]; }
63 const T&
front()
const {
return *begin(); }
64 T&
back() {
return *(begin() + cur_ - 1); }
65 const T&
back()
const {
return *(begin() + cur_ - 1); }
91 swap(size_, rhs.size_);
92 swap(realloc_, rhs.realloc_);
98 return (n > size_) ? this->Expand(n) :
true;
101 if (!this->reserve(n))
return false;
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 realloc_;
143 void* new_ptr = realloc_(ptr, new_size *
sizeof(T));
144 if (!new_ptr)
return false;
145 vec_ =
reinterpret_cast<T*
>(new_ptr);
159 NLIB_MOVE_MEMBER_HELPER_3(
ReallocQueue, front_idx_, front_vec_, back_vec_)
161 return front_vec_.GetRealloc();
164 return front_vec_.size() - front_idx_ + back_vec_.size();
167 return front_idx_ == front_vec_.size() && back_vec_.empty();
171 if (front_idx_ == front_vec_.size()) {
172 if (back_vec_.empty())
return false;
174 front_vec_.swap(back_vec_);
181 if (front_idx_ == front_vec_.size()) {
182 NLIB_ASSERT(!back_vec_.empty());
184 front_vec_.swap(back_vec_);
187 return front_vec_[front_idx_];
190 if (front_idx_ == front_vec_.size()) {
191 if (back_vec_.empty())
return false;
193 front_vec_.swap(back_vec_);
196 if (v) *v = front_vec_[front_idx_];
202 swap(front_idx_, rhs.front_idx_);
203 front_vec_.swap(rhs.front_vec_);
204 back_vec_.swap(rhs.back_vec_);
216 typedef VecType::ReallocFunc ReallocFunc;
225 return this->push_back(str, str +
nlib_strlen(str));
229 size_t n = endchar_ptr - startchar_ptr;
230 char* p =
reinterpret_cast<char*
>(vec_.GetRealloc()(NULL, n + 1));
231 if (!p)
return false;
234 if (!vec_.push_back(p))
return false;
238 if (vec_.empty())
return false;
239 vec_.GetRealloc()(vec_.back(), 0);
248 const char* front()
const NLIB_NOEXCEPT {
return vec_.front(); }
250 const char* back()
const NLIB_NOEXCEPT {
return vec_.back(); }
259 for (; it != end; ++it) {
260 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_ const T & front() const
See front.
T * iterator
Random-access iterator.
ReallocQueue(ReallocFunc func) noexcept
Enables the user to specify the realloc function with the constructor.
const_iterator end() const noexcept
See end.
ReallocVec< T >::ReallocFunc ReallocFunc
The type for functions corresponding to realloc.
T value_type
Element type T.
bool push_back(const T &v) noexcept
Adds an element to the vector.
Substitute definitions for the C++11 standard header type_traits. These substitute definitions are us...
size_t size_type
Unsigned integer type (size_t).
char * front() noexcept
Gets the first string.
#define NLIB_DISALLOW_COPY_AND_ASSIGN(TypeName)
Prohibits use of the copy constructor and assignment operator for the class specified by TypeName...
bool pop_back() noexcept
Deletes a string from the end of the vector.
size_t size() const noexcept
Gets the size of the vector.
const_iterator begin() const noexcept
See begin.
The class for realloc-based implementations of C string vectors.
bool empty() const noexcept
Determines whether the queue is empty.
The class for realloc-based implementations of queues with POD-type elements.
const T * const_pointer
const T*.
bool reserve(size_t n) noexcept
Makes it possible to store as many as n elements without expanding the vector.
iterator insert(iterator pos, const T &val)
Inserts an element in the position specified by pos.
char * back() noexcept
Gets the last string.
const_reverse_iterator crbegin() const noexcept
Gets the read-only reverse iterator pointing to the last element.
bool pop() noexcept
Deletes the first element in the queue.
Defines that class that is corresponding to std::unique_ptr.
const_iterator cbegin() const noexcept
Gets the read-only iterator pointing to the first element.
const T & const_reference
const T&.
const T * const_iterator
Read-only random-access iterator.
T & front()
Gets the first element.
void swap(ReallocCstringVec &rhs) noexcept
Swaps vectors.
bool resize(size_t n) noexcept
Changes the number of elements.
const_reverse_iterator rbegin() const noexcept
See rbegin.
bool reserve(size_t n) noexcept
Makes it possible to store as many as n elements without reallocating memory.
T & back()
Gets the last element.
bool pop(T *v) noexcept
Gets the first element in the queue and then deletes it.
bool pop_back() noexcept
Deletes an element from the end of the vector.
void swap(ReallocQueue &rhs) noexcept
Swaps queues.
bool empty() const noexcept
Determines whether the vector is empty.
std::reverse_iterator< const_iterator > const_reverse_iterator
std::reverse_iterator<const_iterator>
ReallocCstringVec() noexcept
Uses std::realloc with the default constructor.
T & operator[](size_t idx)
Gets the nth element, where n is specified by idx.
size_t size() const noexcept
Gets the size of the queue.
reverse_iterator rend() noexcept
Gets the reverse iterator pointing ahead of the first element.
bool push_back(const char *str) noexcept
Adds a C string to the end.
const_reverse_iterator rend() const noexcept
See rend.
constexpr ReallocVec() noexcept
Uses std::realloc with the default constructor.
ReallocCstringVec(ReallocFunc func) noexcept
Enables the user to specify the realloc function with the constructor.
size_t capacity() const noexcept
Gets the maximum number of elements that can be stored without expanding the vector.
void clear() noexcept
Empties the vector. Note that the memory remains allocated.
bool empty() const noexcept
Determines whether the vector is empty.
#define NLIB_NOEXCEPT
Defines noexcept geared to the environment, or the equivalent.
size_t capacity() const noexcept
Gets the maximum number of elements that can be stored without reallocating memory.
iterator end() noexcept
Gets the iterator pointing beyond the last element.
const T & operator[](size_t idx) const
See operator[](size_t idx) =.
#define NLIB_CEXPR
Defines constexpr if it is available for use. If not, holds an empty string.
A file that contains the configuration information for each development environment.
reverse_iterator rbegin() noexcept
Gets the reverse iterator pointing to the last element.
bool push(const T &val) noexcept
Adds an element to the queue.
std::reverse_iterator< iterator > reverse_iterator
std::reverse_iterator<iterator>
bool push_back(const char *startchar_ptr, const char *endchar_ptr) noexcept
Adds a (sub)string to the end.
const_reverse_iterator crend() const noexcept
Gets the read-only iterator pointing ahead of the first element.
char * operator[](size_t idx) noexcept
Gets the nth string, where n is specified by idx.
void shrink_to_fit() noexcept
Adjusts the size of allocated memory to exactly fit the current number of elements.
iterator begin() noexcept
Gets the iterator pointing to the first element.
#define NLIB_STATIC_ASSERT(exp)
Defines a static assertion. Uses static_assert if it is available for use.
ReallocQueue() noexcept
Uses std::realloc with the default constructor.
ptrdiff_t difference_type
Signed integer type (ptrdiff_t).
T & front()
Gets the first element in the queue.
size_t size() const noexcept
Gets the size of the vector.
The class for realloc-based implementations of vectors with POD-type elements.
const_iterator cend() const noexcept
Gets the read-only iterator pointing beyond the last element.
void swap(ReallocVec &rhs) noexcept
Swaps vectors.
const T & back() const
See back.