16 #ifndef INCLUDE_NN_NLIB_NLIST_H_ 17 #define INCLUDE_NN_NLIB_NLIST_H_ 26 #include "nn/nlib/Swap.h" 31 template <
class T,
class AL>
45 : curblk_(
nullptr), curlevel_(0), curidx_(0), firstblk_(
nullptr) {}
46 #ifdef __cpp_rvalue_references 48 : curblk_(rhs.curblk_), curlevel_(rhs.curlevel_),
49 curidx_(rhs.curidx_), firstblk_(rhs.firstblk_) {
50 rhs.curblk_ =
nullptr;
53 rhs.firstblk_ =
nullptr;
56 curblk_ = rhs.curblk_;
57 curlevel_ = rhs.curlevel_;
58 curidx_ = rhs.curidx_;
59 firstblk_ = rhs.firstblk_;
60 rhs.curblk_ =
nullptr;
63 rhs.firstblk_ =
nullptr;
68 : curblk_(rhs.curblk_), curlevel_(rhs.curlevel_),
69 curidx_(rhs.curidx_), firstblk_(rhs.firstblk_) {
70 rhs.curblk_ =
nullptr;
73 rhs.firstblk_ =
nullptr;
76 curblk_ = rhs.curblk_;
77 curlevel_ = rhs.curlevel_;
78 curidx_ = rhs.curidx_;
79 firstblk_ = rhs.firstblk_;
80 rhs.curblk_ =
nullptr;
83 rhs.firstblk_ =
nullptr;
87 return 8 * ((1 << level) - 1);
90 return 8 * (1 << level);
93 bool ConfirmBack() const NLIB_NOEXCEPT;
94 void ClearSimple() NLIB_NOEXCEPT {
102 mutable Blk* curblk_;
103 mutable size_t curlevel_;
104 mutable size_t curidx_;
105 mutable Blk* firstblk_;
112 class NlistConstIterator;
117 class NlistBaseT :
public NlistBase {
119 typedef T& reference;
120 typedef const T& const_reference;
121 typedef NlistIterator<T> iterator;
122 typedef NlistConstIterator<T> const_iterator;
123 typedef size_t size_type;
124 typedef T value_type;
126 typedef const T* const_pointer;
127 typedef ptrdiff_t difference_type;
131 #ifdef __cpp_rvalue_references 132 #ifdef NLIB_CXX11_DEFAULTED_AND_DELETED_FUNCTIONS 133 NlistBaseT(NlistBaseT&& rhs) =
default;
134 NlistBaseT& operator=(NlistBaseT&& rhs) =
default;
136 NlistBaseT(NlistBaseT&& rhs)
NLIB_NOEXCEPT : NlistBase(std::move(rhs)) {}
138 NlistBase::operator=(std::move(rhs));
151 return static_cast<T*
>(blk->item);
154 return static_cast<const T*
>(blk->item);
158 const Blk* p = this->AtSub(n, &base);
159 return p ?
const_cast<T*
>(&(GetItem(p)[n - base])) : nullptr;
161 iterator Begin()
NLIB_NOEXCEPT {
return iterator(
this, TrueType()); }
162 iterator End()
NLIB_NOEXCEPT {
return iterator(
this, FalseType()); }
164 return const_iterator(
this, TrueType());
167 return const_iterator(
this, FalseType());
172 friend class NlistConstIterator<T>;
173 friend class NlistIterator<T>;
177 class NlistConstIterator
178 :
public std::iterator<std::forward_iterator_tag, T> {
180 typedef NlistBaseT<T> ContainerType;
181 typedef typename ContainerType::Blk Blk;
184 typedef typename std::iterator<std::forward_iterator_tag, T> BaseType;
185 typedef typename BaseType::iterator_category iterator_category;
186 typedef typename BaseType::value_type value_type;
187 typedef typename BaseType::difference_type difference_type;
188 typedef typename BaseType::pointer pointer;
189 typedef typename BaseType::reference reference;
198 NlistConstIterator(
const ContainerType* p, TrueType)
NLIB_NOEXCEPT {
199 ContainerType* rhs =
const_cast<ContainerType*
>(p);
201 blk_ = rhs->firstblk_;
202 elemptr_ = GetItem(blk_);
203 blkend_ = elemptr_ + 8;
205 NlistConstIterator(
const ContainerType* p, FalseType)
NLIB_NOEXCEPT {
206 ContainerType* rhs =
const_cast<ContainerType*
>(p);
207 idx_ = rhs->BlkBaseIdx(rhs->curlevel_) + rhs->curidx_;
209 elemptr_ = &(GetItem(blk_)[rhs->curidx_]);
210 blkend_ = GetItem(blk_) + rhs->BlkSize(rhs->curlevel_);
211 if (elemptr_ == blkend_) this->Normalize();
216 const T* operator->() const
NLIB_NOEXCEPT {
return elemptr_; }
218 NLIB_ASSERT(elemptr_ != blkend_);
220 if (++elemptr_ == blkend_) this->Normalize();
224 NlistConstIterator rval(*
this);
229 return idx_ == rhs.idx_ && elemptr_ == rhs.elemptr_;
235 difference_type Distance(NlistConstIterator to)
const NLIB_NOEXCEPT {
236 return to.idx_ - idx_;
240 static T* GetItem(Blk* blk) NLIB_NOEXCEPT {
241 return static_cast<T*
>(blk->item);
243 static const T* GetItem(
const Blk* blk) NLIB_NOEXCEPT {
244 return reinterpret_cast<const T*
>(blk->item);
246 bool Normalize() NLIB_NOEXCEPT;
253 friend class NlistBaseT<T>;
257 bool NlistConstIterator<T>::Normalize() NLIB_NOEXCEPT {
258 if (blk_ && blk_->next) {
259 Blk* next = blk_->next;
260 size_t next_size = 2 *
static_cast<size_t>(blkend_ - GetItem(blk_));
261 T* next_elemptr = GetItem(next);
262 T* next_end = next_elemptr + next_size;
264 elemptr_ = next_elemptr;
272 void NlistConstIterator<T>::Advance(difference_type n) NLIB_NOEXCEPT {
273 while (elemptr_ + n >= blkend_) {
274 n -= blkend_ - elemptr_;
275 idx_ += blkend_ - elemptr_;
277 if (!this->Normalize())
return;
284 class NlistIterator :
public NlistConstIterator<T> {
286 typedef NlistBaseT<T> ContainerType;
287 typedef typename ContainerType::Blk Blk;
288 typedef NlistConstIterator<T> MyBase;
291 typedef typename NlistConstIterator<T>::BaseType BaseType;
292 typedef typename BaseType::iterator_category iterator_category;
293 typedef typename BaseType::value_type value_type;
294 typedef typename BaseType::difference_type difference_type;
295 typedef typename BaseType::pointer pointer;
296 typedef typename BaseType::reference reference;
299 NLIB_CEXPR NlistIterator() NLIB_NOEXCEPT : MyBase() {}
302 NlistIterator(
const ContainerType* p, TrueType)
NLIB_NOEXCEPT : MyBase(p, TrueType()) {}
303 NlistIterator(
const ContainerType* p, FalseType)
NLIB_NOEXCEPT : MyBase(p, FalseType()) {}
307 const MyBase* tmp =
static_cast<const MyBase*
>(
this);
308 return const_cast<T&
>(**tmp);
310 T* operator->() const NLIB_NOEXCEPT {
311 const MyBase* tmp =
static_cast<const MyBase*
>(
this);
312 return const_cast<T*
>(tmp->operator->());
314 NlistIterator& operator++() NLIB_NOEXCEPT {
315 MyBase* tmp =
static_cast<MyBase*
>(
this);
319 NlistIterator operator++(
int) NLIB_NOEXCEPT {
320 NlistIterator rval(*
this);
324 bool operator==(
const NlistIterator& rhs)
const NLIB_NOEXCEPT {
325 const MyBase* tmp =
static_cast<const MyBase*
>(
this);
326 const MyBase* tmp2 =
static_cast<const MyBase*
>(&rhs);
327 return *tmp == *tmp2;
329 bool operator!=(
const NlistIterator& rhs)
const NLIB_NOEXCEPT {
332 difference_type Distance(NlistIterator to)
const NLIB_NOEXCEPT {
333 const MyBase* tmp =
static_cast<const MyBase*
>(
this);
334 const MyBase* tmp2 =
static_cast<const MyBase*
>(&to);
335 return tmp->Distance(*tmp2);
339 friend class NlistBaseT<T>;
342 template<
bool is_empty,
class AL>
345 NlistAlloc() NLIB_NOEXCEPT : alloc_(AL()) {}
346 explicit NlistAlloc(
const AL& al)
NLIB_NOEXCEPT : alloc_(al) {}
349 AL& _GetNlistAlloc() const NLIB_NOEXCEPT {
350 return const_cast<AL&
>(alloc_);
352 void _SwapNlistAlloc(NlistAlloc& rhs) NLIB_NOEXCEPT {
354 swap(alloc_, rhs.alloc_);
356 #ifdef __cpp_rvalue_references 357 #ifdef NLIB_CXX11_DEFAULTED_AND_DELETED_FUNCTIONS 358 NlistAlloc(NlistAlloc&& rhs) =
default;
359 NlistAlloc& operator=(NlistAlloc&& rhs) =
default;
361 NlistAlloc(NlistAlloc&& rhs)
NLIB_NOEXCEPT : alloc_(std::move(rhs.alloc_)) {}
362 NlistAlloc& operator=(NlistAlloc&& rhs) NLIB_NOEXCEPT {
363 alloc_ = std::move(rhs.alloc_);
368 NlistAlloc(NlistAlloc& rhs,
move_tag) NLIB_NOEXCEPT
371 swap(alloc_, rhs.alloc_);
373 NlistAlloc& assign(NlistAlloc& rhs,
move_tag) NLIB_NOEXCEPT {
376 swap(alloc_, rhs.alloc_);
385 class NlistAlloc <true, AL> {
387 NlistAlloc() NLIB_NOEXCEPT {}
388 explicit NlistAlloc(
const AL& al) NLIB_NOEXCEPT { NLIB_UNUSED(al); }
391 AL _GetNlistAlloc() const NLIB_NOEXCEPT {
return AL(); }
392 void _SwapNlistAlloc(NlistAlloc& rhs) NLIB_NOEXCEPT {
395 #ifdef __cpp_rvalue_references 396 NlistAlloc(NlistAlloc&&) NLIB_NOEXCEPT {}
397 NlistAlloc& operator=(NlistAlloc&&) NLIB_NOEXCEPT {
return *
this; }
399 NlistAlloc(NlistAlloc&,
move_tag) NLIB_NOEXCEPT {}
400 NlistAlloc& assign(NlistAlloc&,
move_tag) NLIB_NOEXCEPT {
return *
this; }
405 template <
class T,
class AL = std::allocator<
char> >
407 public nlist::NlistAlloc<IsEmpty<AL>::value, AL> {
432 typedef typename nlist::NlistBaseT<T> BaseType;
433 typedef typename nlist::NlistAlloc<IsEmpty<AL>::value, AL> AllocType;
434 using BaseType::curblk_;
435 using BaseType::curidx_;
436 using BaseType::curlevel_;
437 using BaseType::firstblk_;
438 using BaseType::ConfirmBack;
439 using BaseType::ClearSimple;
440 using BaseType::BlkBaseIdx;
441 using BaseType::BlkSize;
442 using BaseType::GetItem;
443 using BaseType::Begin;
445 typedef typename BaseType::Blk Blk;
460 Nlist() NLIB_NOEXCEPT : AllocType(AL()) {}
463 #ifdef __cpp_rvalue_references 464 #ifdef NLIB_CXX11_DEFAULTED_AND_DELETED_FUNCTIONS 468 : BaseType(std::move(rhs)), AllocType(std::move(rhs)) {}
472 this->DeallocBlkList(firstblk_, 0);
473 BaseType::operator=(std::move(rhs));
474 AllocType::operator=(std::move(rhs));
482 this->DeallocBlkList(firstblk_, 0);
489 swap(curblk_, rhs.curblk_);
490 swap(curlevel_, rhs.curlevel_);
491 swap(curidx_, rhs.curidx_);
492 swap(firstblk_, rhs.firstblk_);
493 AllocType::_SwapNlistAlloc(rhs);
495 size_type
size() const NLIB_NOEXCEPT {
496 return BlkBaseIdx(curlevel_) + curidx_;
500 return (
nullptr != this->LastBlock(&lv)) ? BlkBaseIdx(lv + 1) : 0;
502 bool empty() const NLIB_NOEXCEPT {
return curlevel_ == 0 && curidx_ == 0; }
504 return Resize(n,
typename IsTriviallyDefaultConstructible<T>::type());
508 void* p = this->PushBack();
509 if (!p)
return nullptr;
510 pointer ptr = MyCtor(p,
typename IsTriviallyDefaultConstructible<T>::type());
514 #ifdef __cpp_rvalue_references 515 pointer push_back(T&& rhs) NLIB_NOEXCEPT {
516 #if defined(NLIB_HAS_NATIVE_TYPETRAITS) && !defined(NLIB_HAS_TR1_TYPETRAITS) 519 void* p = this->PushBack();
520 if (!p)
return nullptr;
521 pointer ptr =
new (p) T(std::forward<T>(rhs));
526 pointer push_back(
const T& rhs) {
527 void* p = this->PushBack();
528 if (!p)
return nullptr;
529 pointer ptr =
new (p) T(rhs);
534 return this->pop_back_(
typename IsTriviallyDestructible<T>::type());
537 Clear(
typename IsTriviallyDestructible<T>::type());
540 return AllocType::_GetNlistAlloc();
544 if (curidx_ == 0) ConfirmBack();
545 return GetItem(curblk_)[curidx_ - 1];
548 if (curidx_ == 0) ConfirmBack();
549 return GetItem(curblk_)[curidx_ - 1];
551 const_reference
front()
const {
return GetItem(firstblk_)[0]; }
552 reference
front() {
return GetItem(firstblk_)[0]; }
554 NLIB_ASSERT(idx < this->size());
555 return *this->At(idx);
558 NLIB_ASSERT(idx < this->size());
559 return *this->At(idx);
563 if (!firstblk_ && !ConfirmFirstBlk())
return iterator();
566 const_iterator
begin() const NLIB_NOEXCEPT {
567 if (!firstblk_ && !ConfirmFirstBlk())
return const_iterator();
570 const_iterator
cbegin() const NLIB_NOEXCEPT {
571 if (!firstblk_ && !ConfirmFirstBlk())
return const_iterator();
575 iterator
end() NLIB_NOEXCEPT {
576 if (!firstblk_ && !ConfirmFirstBlk())
return iterator();
579 const_iterator
end() const NLIB_NOEXCEPT {
580 if (!firstblk_ && !ConfirmFirstBlk())
return const_iterator();
583 const_iterator
cend() const NLIB_NOEXCEPT {
584 if (!firstblk_ && !ConfirmFirstBlk())
return const_iterator();
589 if (!curblk_)
return;
590 Blk* p = curblk_->next;
591 curblk_->next =
nullptr;
592 DeallocBlkList(p, static_cast<int>(curlevel_ + 1));
594 #if defined(__cpp_rvalue_references) && defined(__cpp_variadic_templates) 595 template<
class... Args>
597 void* p = this->PushBack();
598 if (!p)
return nullptr;
599 pointer ptr =
new (p) T(std::forward<Args>(args)...);
605 pointer EmplaceBack(
const A1& a1) {
606 void* p = this->PushBack();
607 if (!p)
return nullptr;
608 pointer ptr =
new (p) T(
const_cast<typename RemoveConst<A1>::type&
>(a1));
612 template <
class A1,
class A2>
613 pointer EmplaceBack(
const A1& a1,
const A2& a2) {
614 void* p = this->PushBack();
615 if (!p)
return nullptr;
616 pointer ptr =
new (p) T(
const_cast<typename RemoveConst<A1>::type&
>(a1),
617 const_cast<typename RemoveConst<A2>::type&
>(a2));
621 template <
class A1,
class A2,
class A3>
622 pointer EmplaceBack(
const A1& a1,
const A2& a2,
const A3& a3) {
623 void* p = this->PushBack();
624 if (!p)
return nullptr;
625 pointer ptr =
new (p) T(
const_cast<typename RemoveConst<A1>::type&
>(a1),
626 const_cast<typename RemoveConst<A2>::type&
>(a2),
627 const_cast<typename RemoveConst<A3>::type&
>(a3));
631 template <
class A1,
class A2,
class A3,
class A4>
632 pointer EmplaceBack(
const A1& a1,
const A2& a2,
const A3& a3,
const A4& a4) {
633 void* p = this->PushBack();
634 if (!p)
return nullptr;
635 pointer ptr =
new (p) T(
const_cast<typename RemoveConst<A1>::type&
>(a1),
636 const_cast<typename RemoveConst<A2>::type&
>(a2),
637 const_cast<typename RemoveConst<A3>::type&
>(a3),
638 const_cast<typename RemoveConst<A4>::type&
>(a4));
642 template <
class A1,
class A2,
class A3,
class A4,
class A5>
644 EmplaceBack(
const A1& a1,
const A2& a2,
const A3& a3,
const A4& a4,
const A5& a5) {
645 void* p = this->PushBack();
646 if (!p)
return nullptr;
647 pointer ptr =
new (p) T(
const_cast<typename RemoveConst<A1>::type&
>(a1),
648 const_cast<typename RemoveConst<A2>::type&
>(a2),
649 const_cast<typename RemoveConst<A3>::type&
>(a3),
650 const_cast<typename RemoveConst<A4>::type&
>(a4),
651 const_cast<typename RemoveConst<A5>::type&
>(a5));
667 void DestroyBlk(Blk*,
size_t, TrueType)
const NLIB_NOEXCEPT {}
668 void DestroyBlk(Blk* p,
size_t n, FalseType)
const NLIB_NOEXCEPT;
669 void DestroyBlk(Blk* p,
size_t n)
const NLIB_NOEXCEPT {
670 DestroyBlk(p, n,
typename IsTriviallyDestructible<T>::type());
675 void Clear(TrueType) NLIB_NOEXCEPT { ClearSimple(); }
678 T* MyCtor(
void* p, TrueType) NLIB_NOEXCEPT {
679 return static_cast<T*
>(p);
681 T* MyCtor(
void* p, FalseType) {
685 bool pop_back_(FalseType) NLIB_NOEXCEPT {
687 if (!ConfirmBack())
return false;
689 reference item = GetItem(curblk_)[--curidx_];
694 bool pop_back_(TrueType) NLIB_NOEXCEPT {
696 if (!ConfirmBack())
return false;
706 template <
class T,
class AL>
709 this->DeallocBlkList(firstblk_, 0);
714 template <
class T,
class AL>
715 class ResizeRewinder {
718 void PushBack(
size_t n) {
719 for (; count_ < n; ++count_) {
724 ~ResizeRewinder() NLIB_NOEXCEPT {
725 for (; count_ > 0; --count_) {
738 template <
class T,
class AL>
741 size_t sz = this->size();
742 if (n == sz)
return true;
744 for (
size_t i = sz; i > n; --i) {
748 if (!this->reserve(n))
return false;
749 nlist::ResizeRewinder<T, AL> rewinder(*
this);
750 rewinder.PushBack(n);
755 template <
class T,
class AL>
757 size_type sz = this->size();
758 if (n == sz)
return true;
760 for (size_type i = sz; i > n; --i) {
764 if (!this->reserve(n))
return false;
768 size_t lower = this->BlkBaseIdx(curlevel_);
769 size_t upper = this->BlkBaseIdx(curlevel_ + 1);
772 curblk_ = curblk_->next;
774 upper = this->BlkBaseIdx(curlevel_ + 1);
776 curidx_ =
static_cast<size_t>(n - lower);
781 template <
class T,
class AL>
783 if (n == 0 || n <= BlkBaseIdx(curlevel_ + 1))
return true;
784 if (!firstblk_ && !this->ConfirmFirstBlk())
return false;
786 Blk* blk = this->LastBlock(&lv);
788 if (n <= BlkBaseIdx(lv + 1))
return true;
790 Blk* p = this->AllocBlk(BlkSize(lv));
791 if (!p)
return false;
797 template <
class T,
class AL>
802 size_t sz =
sizeof(Blk) + n *
sizeof(T);
805 pp = AllocType::_GetNlistAlloc().allocate(sz);
807 #ifdef __cpp_exceptions 808 NLIB_CATCH(
const std::bad_alloc&) {
return nullptr; }
810 if (!pp)
return nullptr;
811 Blk* p =
static_cast<Blk*
>(pp);
813 p->item =
reinterpret_cast<T*
>(p + 1);
817 template <
class T,
class AL>
820 size_t n = BlkSize(lv);
825 AllocType::_GetNlistAlloc().deallocate(reinterpret_cast<char*>(pp),
826 sizeof(Blk) + n *
sizeof(T));
831 template <
class T,
class AL>
836 T* base =
static_cast<T*
>(p->item);
838 for (
size_t i = 0; i < n; ++i) {
843 template <
class T,
class AL>
845 Blk* p = AllocBlk(BlkSize(0));
846 if (!p)
return false;
854 template <
class T,
class AL>
856 if (!firstblk_ && !ConfirmFirstBlk())
return nullptr;
857 if (curidx_ == BlkSize(curlevel_)) {
858 if (!curblk_->next) {
859 Blk* p = AllocBlk(BlkSize(curlevel_ + 1));
860 if (!p)
return nullptr;
864 curblk_ = curblk_->next;
871 T& item = GetItem(curblk_)[curidx_];
875 template <
class T,
class AL>
880 while (p != curblk_) {
881 size_t cnt = BlkSize(lv);
887 DestroyBlk(p, curidx_);
892 template <
class T,
class AL1,
class AL2>
895 return lhs.size() == rhs.size() && std::equal(lhs.begin(), lhs.end(), rhs.begin());
898 template <
class T,
class AL1,
class AL2>
901 return !(lhs == rhs);
904 template <
class T,
class AL1,
class AL2>
905 inline bool operator<(const Nlist<T, AL1>& lhs,
907 return std::lexicographical_compare(lhs.begin(), lhs.end(), rhs.begin(), rhs.end());
910 template <
class T,
class AL1,
class AL2>
916 template <
class T,
class AL1,
class AL2>
917 inline bool operator<=(const Nlist<T, AL1>& lhs,
919 return (!(rhs < lhs));
922 template <
class T,
class AL1,
class AL2>
925 return (!(lhs < rhs));
928 template <
class AL = std::allocator<
char> >
933 typedef AL allocator_type;
934 typedef typename ContainerType::iterator iterator;
935 typedef typename ContainerType::const_iterator const_iterator;
941 size_t size()
const NLIB_NOEXCEPT {
return list_.size(); }
942 size_t capacity()
const NLIB_NOEXCEPT {
return list_.capacity(); }
943 bool empty()
const NLIB_NOEXCEPT {
return list_.empty(); }
944 bool reserve(
size_t n) {
return list_.reserve(n); }
945 bool resize(
size_t n) {
return list_.resize(n); }
947 char** push_back(
const char* str);
948 template <
class Iterator>
949 char** push_back(Iterator first, Iterator last);
952 NLIB_DEPRECATED void swap(StringList& rhs) NLIB_NOEXCEPT { list_.swap(rhs.list_); }
953 void clear() NLIB_NOEXCEPT { list_.clear(); }
954 allocator_type get_allocator()
const {
return list_.get_allocator(); }
956 const char* back()
const {
return list_.back(); }
957 char* back() {
return list_.back(); }
958 const char* front()
const {
return list_.front(); }
959 char* front() {
return list_.front(); }
960 const char* operator[](
size_t idx)
const {
return list_[idx]; }
961 char* operator[](
size_t idx) {
return list_[idx]; }
962 iterator begin() NLIB_NOEXCEPT {
return list_.begin(); }
963 const_iterator begin()
const NLIB_NOEXCEPT {
return list_.begin(); }
964 const_iterator cbegin() NLIB_NOEXCEPT {
return list_.cbegin(); }
966 iterator end() NLIB_NOEXCEPT {
return list_.end(); }
967 const_iterator end()
const NLIB_NOEXCEPT {
return list_.end(); }
968 const_iterator cend() NLIB_NOEXCEPT {
return list_.cend(); }
970 void shrink_to_fit() { list_.shrink_to_fit(); }
971 void Clobber() NLIB_NOEXCEPT { list_.clobber(); }
973 char** Replace(iterator it,
const char* str);
974 char** Replace(
size_t idx,
const char* str) {
975 if (idx >= list_.size())
return nullptr;
976 iterator it = list_.begin();
978 return this->Replace(it, str);
982 void Dealloc(
const char* ptr)
const {
985 this->get_allocator().deallocate(const_cast<char*>(ptr), 0);
988 char* StrDup(
const char* ptr)
const;
989 char* Alloc(
size_t nbytes)
const {
990 return this->get_allocator().allocate(nbytes);
999 char* StringList<AL>::StrDup(
const char* ptr)
const {
1001 void* p = this->Alloc(nbytes);
1002 if (!p)
return nullptr;
1004 return reinterpret_cast<char*
>(p);
1008 StringList<AL>::~StringList() NLIB_NOEXCEPT {
1009 const_iterator it = this->cbegin();
1010 const_iterator it_end = this->cend();
1011 while (it != it_end) {
1018 char** StringList<AL>::push_back(
const char* str) {
1021 dup = this->StrDup(str);
1022 if (!dup)
return nullptr;
1026 char** ptr_str = list_.push_back(dup);
1027 if (!ptr_str) this->Dealloc(dup);
1032 template <
class Iterator>
1033 char** StringList<AL>::push_back(Iterator first, Iterator last) {
1034 typename std::iterator_traits<Iterator>::difference_type n = std::distance(first, last);
1035 char* str = this->Alloc(n + 1);
1036 if (!str)
return nullptr;
1037 std::copy(first, last, str);
1039 char** rval = list_.push_back(str);
1040 if (!rval) this->Dealloc(str);
1045 bool StringList<AL>::pop_back() NLIB_NOEXCEPT {
1046 if (list_.empty())
return false;
1047 Dealloc(list_.back());
1048 return list_.pop_back();
1052 char** StringList<AL>::Replace(iterator it,
const char* str) {
1055 dup = this->StrDup(str);
1056 if (!dup)
return nullptr;
1080 template <
class T,
class Distance>
1081 inline void advance(::nlib_ns::nlist::NlistIterator<T>& pos,
1083 pos.Advance(static_cast<size_t>(n));
1087 inline std::ptrdiff_t distance(const ::nlib_ns::nlist::NlistIterator<T>& first,
1088 const ::nlib_ns::nlist::NlistIterator<T>& last) {
1089 return first.Distance(last);
1095 NLIB_DEFINE_STD_SWAP_T_BEGIN2(
nn, nlib)
1096 NLIB_DEFINE_STD_SWAP_T2(T, AL, NLIB_NS::Nlist)
1097 NLIB_DEFINE_STD_SWAP_T1(AL, NLIB_NS::StringList)
1098 NLIB_DEFINE_STD_SWAP_T_END2(
nn, nlib)
1100 #endif // INCLUDE_NN_NLIB_NLIST_H_ Nlist(const AL &al) noexcept
Specifies an allocator. Creates an empty container.
void swap(Nlist &rhs) noexcept
Swaps the container.
BaseType::const_pointer const_pointer
const T*.
pointer EmplaceBack(Args &&... args)
Adds elements in-place.
reference back()
Returns a reference to the last element.
BaseType::pointer pointer
T*.
Substitute definitions for the C++11 standard header type_traits. These substitute definitions are us...
Nlist() noexcept
Instantiates the object with default parameters (default constructor). Creates an empty container...
BaseType::reference reference
T&.
#define NLIB_DISALLOW_COPY_AND_ASSIGN(TypeName)
Prohibits use of the copy constructor and assignment operator for the class specified by TypeName...
bool operator>=(const Nlist< T, AL1 > &lhs, const Nlist< T, AL2 > &rhs) noexcept
Compares two lists in dictionary order.
const_iterator begin() const noexcept
Returns the iterator pointing to the beginning of the container.
BaseType::size_type size_type
Unsigned integer type (size_t).
bool operator>(const Nlist< T, AL1 > &lhs, const Nlist< T, AL2 > &rhs) noexcept
Compares two lists in dictionary order.
iterator begin() noexcept
Returns the iterator pointing to the beginning of the container.
void shrink_to_fit() noexcept
Returns allocated memory, if possible.
allocator_type get_allocator() const noexcept
Gets the allocator.
const_reference back() const
The same as back.
const_iterator end() const noexcept
Returns the iterator pointing to the end of the container.
bool operator==(const HeapHash &rhs, const HeapHash &lhs)
Returns true if the two compared summaries are equal.
bool operator!=(const HeapHash &rhs, const HeapHash &lhs)
Returns true if the two compared summaries are not equal.
BaseType::const_iterator const_iterator
Read-only forward iterator.
Nlist & operator=(Nlist &&rhs) noexcept
Move assignment operator. This function is useful when using C++11.
Object created when MessagePack or JSON is read.
size_type capacity() const noexcept
Returns the number of allocated elements.
bool resize(size_type n) noexcept
Resizes the container.
pointer push_back()
Adds an element to the end and initializes it with the default constructor.
reference operator[](size_type idx)
Uses an index to reference an element.
Nlist(Nlist &rhs, move_tag) noexcept
Corresponds to a move constructor.
An empty structure indicating that an argument to a function needs to be moved.
void clear() noexcept
Clears the container.
#define NLIB_CATCH(x)
Defines catch(x) if exceptions are enabled. If not, defines if (true).
#define NLIB_TRY
Defines try if exceptions are enabled. If not, defines if (true).
Nlist & assign(Nlist &rhs, move_tag) noexcept
Corresponds to a move assignment operator.
reference front()
Returns a reference to the first element.
BaseType::iterator iterator
Forward iterator.
BaseType::difference_type difference_type
Signed integer type (ptrdiff_t).
#define NLIB_NOEXCEPT
Defines noexcept geared to the environment, or the equivalent.
BaseType::value_type value_type
Element type T.
#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.
TimeSpan operator*(int i, const TimeSpan &rhs) noexcept
Increases rhs by a factor of i.
bool reserve(size_type n) noexcept
Allocates memory for n number of elements.
void Clobber() noexcept
Returns the container empty without calling the element destructor and without releasing the memory f...
A container-like class similar to std::vector that can store objects that do not have copy constructo...
const_reference front() const
The same as front.
const_iterator cend() const noexcept
Returns the iterator pointing to the end of the container.
size_type size() const noexcept
Returns the number of stored elements.
#define NLIB_FINAL
Defines final if it is available for use. If not, holds an empty string.
#define NLIB_STATIC_ASSERT(exp)
Defines a static assertion. Uses static_assert if it is available for use.
bool empty() const noexcept
Checks whether the container is empty.
const_reference operator[](size_type idx) const
The same as operator[](size_t idx).
#define NLIB_ALIGNOF(tp)
Defines alignof(tp) or the equivalent.
std::allocator< char > allocator_type
Allocator type AL.
BaseType::reference const_reference
const T&.
const_iterator cbegin() const noexcept
Returns the iterator pointing to the beginning of the container.
iterator end() noexcept
Returns the iterator pointing to the end of the container.
bool pop_back() noexcept
Deletes the last element.