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
The const decoration version of the above function.
T * iterator
A random-access iterator.
ReallocQueue(ReallocFunc func) noexcept
Enables the user to specify the realloc function with the constructor.
const_iterator end() const noexcept
Gets the read-only iterator pointing beyond the last element.
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...
ReallocVec & operator=(ReallocVec &&rhs) noexcept
Move assignment operator.
bool pop_back() noexcept
Deletes a string from the end of the vector.
size_t size() const noexcept
Returns the number of stored elements.
const_iterator begin() const noexcept
Gets the read-only iterator pointing to the first element.
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 char * front() const noexcept
The const decoration version of the above function.
const char * back() const noexcept
The const decoration version of the above function.
const T * const_pointer
Read-only pointer to an element.
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
Read-only reference to an element.
ReallocFunc GetRealloc() const noexcept
Gets the specified realloc function.
const T * const_iterator
Read-only random-access iterator.
T & front()
Gets a reference to the first element.
T & reference
A reference to an element.
bool resize(size_t n) noexcept
Changes the number of elements.
const_reverse_iterator rbegin() const noexcept
Gets the read-only reverse iterator pointing to the last element.
bool reserve(size_t n) noexcept
Makes it possible to store as many as n elements without reallocating memory.
T & back()
Gets a reference to the last element.
bool pop(T *v) noexcept
Gets the first element in the queue and then deletes it.
ReallocQueue(ReallocQueue &&rhs) noexcept
Instantiates the object (move constructor).
bool pop_back() noexcept
Deletes an element from the end of the vector.
An empty structure indicating that an argument to a function needs to be moved.
ReallocQueue & operator=(ReallocQueue &&rhs) noexcept
Move assignment operator.
bool empty() const noexcept
Determines whether the vector is empty.
std::reverse_iterator< const_iterator > const_reverse_iterator
Read-only reverse iterator.
ReallocFunc GetRealloc() const noexcept
Gets the specified realloc function.
ReallocCstringVec() noexcept
Instantiates the object with default parameters (default constructor). Uses std::realloc.
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
Gets the read-only reverse iterator pointing ahead of the first element.
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.
void clear() noexcept
Empties the vector.
bool empty() const noexcept
Returns true if the number of stored elements is 0, or returns false otherwise.
#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
The const decoration version of the above function.
#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
A reverse 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 reverse 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.
const char * operator[](size_t idx) const noexcept
The const decoration version of the above function.
iterator begin() noexcept
Gets the iterator pointing to the first element.
ReallocFunc GetRealloc() const noexcept
Gets the specified realloc function.
#define NLIB_STATIC_ASSERT(exp)
Defines a static assertion. Uses static_assert if it is available for use.
ReallocQueue() noexcept
Instantiates the object with default parameters (default constructor). Uses std::realloc.
T * pointer
Pointer to the element.
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.
ReallocVec(ReallocVec &&rhs) noexcept
Instantiates the object (move constructor).
const_iterator cend() const noexcept
Gets the read-only iterator pointing beyond the last element.
const T & back() const
The const decoration version of the above function.