diff options
author | David Chisnall <theraven@FreeBSD.org> | 2011-11-22 17:30:41 +0000 |
---|---|---|
committer | David Chisnall <theraven@FreeBSD.org> | 2011-11-22 17:30:41 +0000 |
commit | 1828c5696f7bf5850943ea6c660a493a5e648669 (patch) | |
tree | 5c73f10d9b3740138364680a74334d50b222cd6d | |
download | src-1828c5696f7bf5850943ea6c660a493a5e648669.tar.gz src-1828c5696f7bf5850943ea6c660a493a5e648669.zip |
Import libcxxrt / libc++ into a vendor branch.vendor/libc++/r145065
Approved by: dim (mentor)
Notes
Notes:
svn path=/vendor/libc++/dist/; revision=227825
svn path=/vendor/libc++/r145065/; revision=227826; tag=vendor/libc++/r145065
127 files changed, 112279 insertions, 0 deletions
diff --git a/include/__bit_reference b/include/__bit_reference new file mode 100644 index 000000000000..46213332c3e8 --- /dev/null +++ b/include/__bit_reference @@ -0,0 +1,1247 @@ +// -*- C++ -*- +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#ifndef _LIBCPP___BIT_REFERENCE +#define _LIBCPP___BIT_REFERENCE + +#include <__config> +#include <algorithm> + +#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) +#pragma GCC system_header +#endif + +_LIBCPP_BEGIN_NAMESPACE_STD + +template <class _C, bool _IsConst> class __bit_iterator; +template <class _C> class __bit_const_reference; + +template <class _Tp> +struct __has_storage_type +{ + static const bool value = false; +}; + +template <class _C, bool = __has_storage_type<_C>::value> +class __bit_reference +{ + typedef typename _C::__storage_type __storage_type; + typedef typename _C::__storage_pointer __storage_pointer; + + __storage_pointer __seg_; + __storage_type __mask_; + +#if defined(__clang__) + friend typename _C::__self; +#else + friend class _C::__self; +#endif + friend class __bit_const_reference<_C>; + friend class __bit_iterator<_C, false>; +public: + _LIBCPP_INLINE_VISIBILITY operator bool() const _NOEXCEPT + {return static_cast<bool>(*__seg_ & __mask_);} + _LIBCPP_INLINE_VISIBILITY bool operator ~() const _NOEXCEPT + {return !static_cast<bool>(*this);} + + _LIBCPP_INLINE_VISIBILITY + __bit_reference& operator=(bool __x) _NOEXCEPT + { + if (__x) + *__seg_ |= __mask_; + else + *__seg_ &= ~__mask_; + return *this; + } + + _LIBCPP_INLINE_VISIBILITY + __bit_reference& operator=(const __bit_reference& __x) _NOEXCEPT + {return operator=(static_cast<bool>(__x));} + + _LIBCPP_INLINE_VISIBILITY void flip() _NOEXCEPT {*__seg_ ^= __mask_;} + _LIBCPP_INLINE_VISIBILITY __bit_iterator<_C, false> operator&() const _NOEXCEPT + {return __bit_iterator<_C, false>(__seg_, static_cast<unsigned>(__ctz(__mask_)));} +private: + _LIBCPP_INLINE_VISIBILITY + __bit_reference(__storage_pointer __s, __storage_type __m) _NOEXCEPT + : __seg_(__s), __mask_(__m) {} +}; + +template <class _C> +class __bit_reference<_C, false> +{ +}; + +template <class _C, class _D> +_LIBCPP_INLINE_VISIBILITY inline +void +swap(__bit_reference<_C> __x, __bit_reference<_D> __y) _NOEXCEPT +{ + bool __t = __x; + __x = __y; + __y = __t; +} + +template <class _C> +_LIBCPP_INLINE_VISIBILITY inline +void +swap(__bit_reference<_C> __x, bool& __y) _NOEXCEPT +{ + bool __t = __x; + __x = __y; + __y = __t; +} + +template <class _C> +_LIBCPP_INLINE_VISIBILITY inline +void +swap(bool& __x, __bit_reference<_C> __y) _NOEXCEPT +{ + bool __t = __x; + __x = __y; + __y = __t; +} + +template <class _C> +class __bit_const_reference +{ + typedef typename _C::__storage_type __storage_type; + typedef typename _C::__const_storage_pointer __storage_pointer; + + __storage_pointer __seg_; + __storage_type __mask_; + +#if defined(__clang__) + friend typename _C::__self; +#else + friend class _C::__self; +#endif + friend class __bit_iterator<_C, true>; +public: + _LIBCPP_INLINE_VISIBILITY + __bit_const_reference(const __bit_reference<_C>& __x) _NOEXCEPT + : __seg_(__x.__seg_), __mask_(__x.__mask_) {} + + _LIBCPP_INLINE_VISIBILITY operator bool() const _NOEXCEPT + {return static_cast<bool>(*__seg_ & __mask_);} + + _LIBCPP_INLINE_VISIBILITY __bit_iterator<_C, true> operator&() const _NOEXCEPT + {return __bit_iterator<_C, true>(__seg_, static_cast<unsigned>(__ctz(__mask_)));} +private: + _LIBCPP_INLINE_VISIBILITY + __bit_const_reference(__storage_pointer __s, __storage_type __m) _NOEXCEPT + : __seg_(__s), __mask_(__m) {} + + __bit_const_reference& operator=(const __bit_const_reference& __x); +}; + +// find + +template <class _C> +__bit_iterator<_C, false> +__find_bool_true(__bit_iterator<_C, false> __first, typename _C::size_type __n) +{ + typedef __bit_iterator<_C, false> _It; + typedef typename _It::__storage_type __storage_type; + static const unsigned __bits_per_word = _It::__bits_per_word; + // do first partial word + if (__first.__ctz_ != 0) + { + __storage_type __clz_f = static_cast<__storage_type>(__bits_per_word - __first.__ctz_); + __storage_type __dn = _VSTD::min(__clz_f, __n); + __storage_type __m = (~__storage_type(0) << __first.__ctz_) & (~__storage_type(0) >> (__clz_f - __dn)); + __storage_type __b = *__first.__seg_ & __m; + if (__b) + return _It(__first.__seg_, static_cast<unsigned>(_VSTD::__ctz(__b))); + __n -= __dn; + ++__first.__seg_; + } + // do middle whole words + for (; __n >= __bits_per_word; ++__first.__seg_, __n -= __bits_per_word) + if (*__first.__seg_) + return _It(__first.__seg_, static_cast<unsigned>(_VSTD::__ctz(*__first.__seg_))); + // do last partial word + if (__n > 0) + { + __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n); + __storage_type __b = *__first.__seg_ & __m; + if (__b) + return _It(__first.__seg_, static_cast<unsigned>(_VSTD::__ctz(__b))); + } + return _It(__first.__seg_, static_cast<unsigned>(__n)); +} + +template <class _C> +__bit_iterator<_C, false> +__find_bool_false(__bit_iterator<_C, false> __first, typename _C::size_type __n) +{ + typedef __bit_iterator<_C, false> _It; + typedef typename _It::__storage_type __storage_type; + static const unsigned __bits_per_word = _It::__bits_per_word; + // do first partial word + if (__first.__ctz_ != 0) + { + __storage_type __clz_f = static_cast<__storage_type>(__bits_per_word - __first.__ctz_); + __storage_type __dn = _VSTD::min(__clz_f, __n); + __storage_type __m = (~__storage_type(0) << __first.__ctz_) & (~__storage_type(0) >> (__clz_f - __dn)); + __storage_type __b = ~(*__first.__seg_ & __m); + if (__b) + return _It(__first.__seg_, static_cast<unsigned>(_VSTD::__ctz(__b))); + __n -= __dn; + ++__first.__seg_; + } + // do middle whole words + for (; __n >= __bits_per_word; ++__first.__seg_, __n -= __bits_per_word) + { + __storage_type __b = ~*__first.__seg_; + if (__b) + return _It(__first.__seg_, static_cast<unsigned>(_VSTD::__ctz(__b))); + } + // do last partial word + if (__n > 0) + { + __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n); + __storage_type __b = ~(*__first.__seg_ & __m); + if (__b) + return _It(__first.__seg_, static_cast<unsigned>(_VSTD::__ctz(__b))); + } + return _It(__first.__seg_, static_cast<unsigned>(__n)); +} + +template <class _C, class _Tp> +inline _LIBCPP_INLINE_VISIBILITY +__bit_iterator<_C, false> +find(__bit_iterator<_C, false> __first, __bit_iterator<_C, false> __last, const _Tp& __value_) +{ + if (static_cast<bool>(__value_)) + return __find_bool_true(__first, static_cast<typename _C::size_type>(__last - __first)); + return __find_bool_false(__first, static_cast<typename _C::size_type>(__last - __first)); +} + +// count + +template <class _C> +typename __bit_iterator<_C, false>::difference_type +__count_bool_true(__bit_iterator<_C, false> __first, typename _C::size_type __n) +{ + typedef __bit_iterator<_C, false> _It; + typedef typename _It::__storage_type __storage_type; + typedef typename _It::difference_type difference_type; + static const unsigned __bits_per_word = _It::__bits_per_word; + difference_type __r = 0; + // do first partial word + if (__first.__ctz_ != 0) + { + __storage_type __clz_f = static_cast<__storage_type>(__bits_per_word - __first.__ctz_); + __storage_type __dn = _VSTD::min(__clz_f, __n); + __storage_type __m = (~__storage_type(0) << __first.__ctz_) & (~__storage_type(0) >> (__clz_f - __dn)); + __r = _VSTD::__pop_count(*__first.__seg_ & __m); + __n -= __dn; + ++__first.__seg_; + } + // do middle whole words + for (; __n >= __bits_per_word; ++__first.__seg_, __n -= __bits_per_word) + __r += _VSTD::__pop_count(*__first.__seg_); + // do last partial word + if (__n > 0) + { + __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n); + __r += _VSTD::__pop_count(*__first.__seg_ & __m); + } + return __r; +} + +template <class _C> +typename __bit_iterator<_C, false>::difference_type +__count_bool_false(__bit_iterator<_C, false> __first, typename _C::size_type __n) +{ + typedef __bit_iterator<_C, false> _It; + typedef typename _It::__storage_type __storage_type; + typedef typename _It::difference_type difference_type; + static const unsigned __bits_per_word = _It::__bits_per_word; + difference_type __r = 0; + // do first partial word + if (__first.__ctz_ != 0) + { + __storage_type __clz_f = static_cast<__storage_type>(__bits_per_word - __first.__ctz_); + __storage_type __dn = _VSTD::min(__clz_f, __n); + __storage_type __m = (~__storage_type(0) << __first.__ctz_) & (~__storage_type(0) >> (__clz_f - __dn)); + __r = _VSTD::__pop_count(~(*__first.__seg_ & __m)); + __n -= __dn; + ++__first.__seg_; + } + // do middle whole words + for (; __n >= __bits_per_word; ++__first.__seg_, __n -= __bits_per_word) + __r += _VSTD::__pop_count(~*__first.__seg_); + // do last partial word + if (__n > 0) + { + __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n); + __r += _VSTD::__pop_count(~(*__first.__seg_ & __m)); + } + return __r; +} + +template <class _C, class _Tp> +inline _LIBCPP_INLINE_VISIBILITY +typename __bit_iterator<_C, false>::difference_type +count(__bit_iterator<_C, false> __first, __bit_iterator<_C, false> __last, const _Tp& __value_) +{ + if (static_cast<bool>(__value_)) + return __count_bool_true(__first, static_cast<typename _C::size_type>(__last - __first)); + return __count_bool_false(__first, static_cast<typename _C::size_type>(__last - __first)); +} + +// fill_n + +template <class _C> +void +__fill_n_false(__bit_iterator<_C, false> __first, typename _C::size_type __n) +{ + typedef __bit_iterator<_C, false> _It; + typedef typename _It::__storage_type __storage_type; + static const unsigned __bits_per_word = _It::__bits_per_word; + // do first partial word + if (__first.__ctz_ != 0) + { + __storage_type __clz_f = static_cast<__storage_type>(__bits_per_word - __first.__ctz_); + __storage_type __dn = _VSTD::min(__clz_f, __n); + __storage_type __m = (~__storage_type(0) << __first.__ctz_) & (~__storage_type(0) >> (__clz_f - __dn)); + *__first.__seg_ &= ~__m; + __n -= __dn; + ++__first.__seg_; + } + // do middle whole words + __storage_type __nw = __n / __bits_per_word; + _VSTD::memset(__first.__seg_, 0, __nw * sizeof(__storage_type)); + __n -= __nw * __bits_per_word; + // do last partial word + if (__n > 0) + { + __first.__seg_ += __nw; + __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n); + *__first.__seg_ &= ~__m; + } +} + +template <class _C> +void +__fill_n_true(__bit_iterator<_C, false> __first, typename _C::size_type __n) +{ + typedef __bit_iterator<_C, false> _It; + typedef typename _It::__storage_type __storage_type; + static const unsigned __bits_per_word = _It::__bits_per_word; + // do first partial word + if (__first.__ctz_ != 0) + { + __storage_type __clz_f = static_cast<__storage_type>(__bits_per_word - __first.__ctz_); + __storage_type __dn = _VSTD::min(__clz_f, __n); + __storage_type __m = (~__storage_type(0) << __first.__ctz_) & (~__storage_type(0) >> (__clz_f - __dn)); + *__first.__seg_ |= __m; + __n -= __dn; + ++__first.__seg_; + } + // do middle whole words + __storage_type __nw = __n / __bits_per_word; + _VSTD::memset(__first.__seg_, -1, __nw * sizeof(__storage_type)); + __n -= __nw * __bits_per_word; + // do last partial word + if (__n > 0) + { + __first.__seg_ += __nw; + __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n); + *__first.__seg_ |= __m; + } +} + +template <class _C> +_LIBCPP_INLINE_VISIBILITY inline +void +fill_n(__bit_iterator<_C, false> __first, typename _C::size_type __n, bool __value_) +{ + if (__n > 0) + { + if (__value_) + __fill_n_true(__first, __n); + else + __fill_n_false(__first, __n); + } +} + +// fill + +template <class _C> +inline _LIBCPP_INLINE_VISIBILITY +void +fill(__bit_iterator<_C, false> __first, __bit_iterator<_C, false> __last, bool __value_) +{ + _VSTD::fill_n(__first, static_cast<typename _C::size_type>(__last - __first), __value_); +} + +// copy + +template <class _C, bool _IsConst> +__bit_iterator<_C, false> +__copy_aligned(__bit_iterator<_C, _IsConst> __first, __bit_iterator<_C, _IsConst> __last, + __bit_iterator<_C, false> __result) +{ + typedef __bit_iterator<_C, _IsConst> _In; + typedef typename _In::difference_type difference_type; + typedef typename _In::__storage_type __storage_type; + static const unsigned __bits_per_word = _In::__bits_per_word; + difference_type __n = __last - __first; + if (__n > 0) + { + // do first word + if (__first.__ctz_ != 0) + { + unsigned __clz = __bits_per_word - __first.__ctz_; + difference_type __dn = _VSTD::min(static_cast<difference_type>(__clz), __n); + __n -= __dn; + __storage_type __m = (~__storage_type(0) << __first.__ctz_) & (~__storage_type(0) >> (__clz - __dn)); + __storage_type __b = *__first.__seg_ & __m; + *__result.__seg_ &= ~__m; + *__result.__seg_ |= __b; + __result.__seg_ += (__dn + __result.__ctz_) / __bits_per_word; + __result.__ctz_ = static_cast<unsigned>((__dn + __result.__ctz_) % __bits_per_word); + ++__first.__seg_; + // __first.__ctz_ = 0; + } + // __first.__ctz_ == 0; + // do middle words + __storage_type __nw = __n / __bits_per_word; + _VSTD::memmove(__result.__seg_, __first.__seg_, __nw * sizeof(__storage_type)); + __n -= __nw * __bits_per_word; + __result.__seg_ += __nw; + // do last word + if (__n > 0) + { + __first.__seg_ += __nw; + __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n); + __storage_type __b = *__first.__seg_ & __m; + *__result.__seg_ &= ~__m; + *__result.__seg_ |= __b; + __result.__ctz_ = static_cast<unsigned>(__n); + } + } + return __result; +} + +template <class _C, bool _IsConst> +__bit_iterator<_C, false> +__copy_unaligned(__bit_iterator<_C, _IsConst> __first, __bit_iterator<_C, _IsConst> __last, + __bit_iterator<_C, false> __result) +{ + typedef __bit_iterator<_C, _IsConst> _In; + typedef typename _In::difference_type difference_type; + typedef typename _In::__storage_type __storage_type; + static const unsigned __bits_per_word = _In::__bits_per_word; + difference_type __n = __last - __first; + if (__n > 0) + { + // do first word + if (__first.__ctz_ != 0) + { + unsigned __clz_f = __bits_per_word - __first.__ctz_; + difference_type __dn = _VSTD::min(static_cast<difference_type>(__clz_f), __n); + __n -= __dn; + __storage_type __m = (~__storage_type(0) << __first.__ctz_) & (~__storage_type(0) >> (__clz_f - __dn)); + __storage_type __b = *__first.__seg_ & __m; + unsigned __clz_r = __bits_per_word - __result.__ctz_; + __storage_type __ddn = _VSTD::min<__storage_type>(__dn, __clz_r); + __m = (~__storage_type(0) << __result.__ctz_) & (~__storage_type(0) >> (__clz_r - __ddn)); + *__result.__seg_ &= ~__m; + if (__result.__ctz_ > __first.__ctz_) + *__result.__seg_ |= __b << (__result.__ctz_ - __first.__ctz_); + else + *__result.__seg_ |= __b >> (__first.__ctz_ - __result.__ctz_); + __result.__seg_ += (__ddn + __result.__ctz_) / __bits_per_word; + __result.__ctz_ = static_cast<unsigned>((__ddn + __result.__ctz_) % __bits_per_word); + __dn -= __ddn; + if (__dn > 0) + { + __m = ~__storage_type(0) >> (__bits_per_word - __dn); + *__result.__seg_ &= ~__m; + *__result.__seg_ |= __b >> (__first.__ctz_ + __ddn); + __result.__ctz_ = static_cast<unsigned>(__dn); + } + ++__first.__seg_; + // __first.__ctz_ = 0; + } + // __first.__ctz_ == 0; + // do middle words + unsigned __clz_r = __bits_per_word - __result.__ctz_; + __storage_type __m = ~__storage_type(0) << __result.__ctz_; + for (; __n >= __bits_per_word; __n -= __bits_per_word, ++__first.__seg_) + { + __storage_type __b = *__first.__seg_; + *__result.__seg_ &= ~__m; + *__result.__seg_ |= __b << __result.__ctz_; + ++__result.__seg_; + *__result.__seg_ &= __m; + *__result.__seg_ |= __b >> __clz_r; + } + // do last word + if (__n > 0) + { + __m = ~__storage_type(0) >> (__bits_per_word - __n); + __storage_type __b = *__first.__seg_ & __m; + __storage_type __dn = _VSTD::min(__n, static_cast<difference_type>(__clz_r)); + __m = (~__storage_type(0) << __result.__ctz_) & (~__storage_type(0) >> (__clz_r - __dn)); + *__result.__seg_ &= ~__m; + *__result.__seg_ |= __b << __result.__ctz_; + __result.__seg_ += (__dn + __result.__ctz_) / __bits_per_word; + __result.__ctz_ = static_cast<unsigned>((__dn + __result.__ctz_) % __bits_per_word); + __n -= __dn; + if (__n > 0) + { + __m = ~__storage_type(0) >> (__bits_per_word - __n); + *__result.__seg_ &= ~__m; + *__result.__seg_ |= __b >> __dn; + __result.__ctz_ = static_cast<unsigned>(__n); + } + } + } + return __result; +} + +template <class _C, bool _IsConst> +inline _LIBCPP_INLINE_VISIBILITY +__bit_iterator<_C, false> +copy(__bit_iterator<_C, _IsConst> __first, __bit_iterator<_C, _IsConst> __last, __bit_iterator<_C, false> __result) +{ + if (__first.__ctz_ == __result.__ctz_) + return __copy_aligned(__first, __last, __result); + return __copy_unaligned(__first, __last, __result); +} + +// copy_backward + +template <class _C, bool _IsConst> +__bit_iterator<_C, false> +__copy_backward_aligned(__bit_iterator<_C, _IsConst> __first, __bit_iterator<_C, _IsConst> __last, + __bit_iterator<_C, false> __result) +{ + typedef __bit_iterator<_C, _IsConst> _In; + typedef typename _In::difference_type difference_type; + typedef typename _In::__storage_type __storage_type; + static const unsigned __bits_per_word = _In::__bits_per_word; + difference_type __n = __last - __first; + if (__n > 0) + { + // do first word + if (__last.__ctz_ != 0) + { + difference_type __dn = _VSTD::min(static_cast<difference_type>(__last.__ctz_), __n); + __n -= __dn; + unsigned __clz = __bits_per_word - __last.__ctz_; + __storage_type __m = (~__storage_type(0) << (__last.__ctz_ - __dn)) & (~__storage_type(0) >> __clz); + __storage_type __b = *__last.__seg_ & __m; + *__result.__seg_ &= ~__m; + *__result.__seg_ |= __b; + __result.__ctz_ = static_cast<unsigned>(((-__dn & (__bits_per_word - 1)) + + __result.__ctz_) % __bits_per_word); + // __last.__ctz_ = 0 + } + // __last.__ctz_ == 0 || __n == 0 + // __result.__ctz_ == 0 || __n == 0 + // do middle words + __storage_type __nw = __n / __bits_per_word; + __result.__seg_ -= __nw; + __last.__seg_ -= __nw; + _VSTD::memmove(__result.__seg_, __last.__seg_, __nw * sizeof(__storage_type)); + __n -= __nw * __bits_per_word; + // do last word + if (__n > 0) + { + __storage_type __m = ~__storage_type(0) << (__bits_per_word - __n); + __storage_type __b = *--__last.__seg_ & __m; + *--__result.__seg_ &= ~__m; + *__result.__seg_ |= __b; + __result.__ctz_ = static_cast<unsigned>(-__n & (__bits_per_word - 1)); + } + } + return __result; +} + +template <class _C, bool _IsConst> +__bit_iterator<_C, false> +__copy_backward_unaligned(__bit_iterator<_C, _IsConst> __first, __bit_iterator<_C, _IsConst> __last, + __bit_iterator<_C, false> __result) +{ + typedef __bit_iterator<_C, _IsConst> _In; + typedef typename _In::difference_type difference_type; + typedef typename _In::__storage_type __storage_type; + static const unsigned __bits_per_word = _In::__bits_per_word; + difference_type __n = __last - __first; + if (__n > 0) + { + // do first word + if (__last.__ctz_ != 0) + { + difference_type __dn = _VSTD::min(static_cast<difference_type>(__last.__ctz_), __n); + __n -= __dn; + unsigned __clz_l = __bits_per_word - __last.__ctz_; + __storage_type __m = (~__storage_type(0) << (__last.__ctz_ - __dn)) & (~__storage_type(0) >> __clz_l); + __storage_type __b = *__last.__seg_ & __m; + unsigned __clz_r = __bits_per_word - __result.__ctz_; + __storage_type __ddn = _VSTD::min(__dn, static_cast<difference_type>(__result.__ctz_)); + if (__ddn > 0) + { + __m = (~__storage_type(0) << (__result.__ctz_ - __ddn)) & (~__storage_type(0) >> __clz_r); + *__result.__seg_ &= ~__m; + if (__result.__ctz_ > __last.__ctz_) + *__result.__seg_ |= __b << (__result.__ctz_ - __last.__ctz_); + else + *__result.__seg_ |= __b >> (__last.__ctz_ - __result.__ctz_); + __result.__ctz_ = static_cast<unsigned>(((-__ddn & (__bits_per_word - 1)) + + __result.__ctz_) % __bits_per_word); + __dn -= __ddn; + } + if (__dn > 0) + { + // __result.__ctz_ == 0 + --__result.__seg_; + __result.__ctz_ = static_cast<unsigned>(-__dn & (__bits_per_word - 1)); + __m = ~__storage_type(0) << __result.__ctz_; + *__result.__seg_ &= ~__m; + __last.__ctz_ -= __dn + __ddn; + *__result.__seg_ |= __b << (__result.__ctz_ - __last.__ctz_); + } + // __last.__ctz_ = 0 + } + // __last.__ctz_ == 0 || __n == 0 + // __result.__ctz_ != 0 || __n == 0 + // do middle words + unsigned __clz_r = __bits_per_word - __result.__ctz_; + __storage_type __m = ~__storage_type(0) >> __clz_r; + for (; __n >= __bits_per_word; __n -= __bits_per_word) + { + __storage_type __b = *--__last.__seg_; + *__result.__seg_ &= ~__m; + *__result.__seg_ |= __b >> __clz_r; + *--__result.__seg_ &= __m; + *__result.__seg_ |= __b << __result.__ctz_; + } + // do last word + if (__n > 0) + { + __m = ~__storage_type(0) << (__bits_per_word - __n); + __storage_type __b = *--__last.__seg_ & __m; + unsigned __clz_r = __bits_per_word - __result.__ctz_; + __storage_type __dn = _VSTD::min(__n, static_cast<difference_type>(__result.__ctz_)); + __m = (~__storage_type(0) << (__result.__ctz_ - __dn)) & (~__storage_type(0) >> __clz_r); + *__result.__seg_ &= ~__m; + *__result.__seg_ |= __b >> (__bits_per_word - __result.__ctz_); + __result.__ctz_ = static_cast<unsigned>(((-__dn & (__bits_per_word - 1)) + + __result.__ctz_) % __bits_per_word); + __n -= __dn; + if (__n > 0) + { + // __result.__ctz_ == 0 + --__result.__seg_; + __result.__ctz_ = static_cast<unsigned>(-__n & (__bits_per_word - 1)); + __m = ~__storage_type(0) << __result.__ctz_; + *__result.__seg_ &= ~__m; + *__result.__seg_ |= __b << (__result.__ctz_ - (__bits_per_word - __n - __dn)); + } + } + } + return __result; +} + +template <class _C, bool _IsConst> +inline _LIBCPP_INLINE_VISIBILITY +__bit_iterator<_C, false> +copy_backward(__bit_iterator<_C, _IsConst> __first, __bit_iterator<_C, _IsConst> __last, __bit_iterator<_C, false> __result) +{ + if (__last.__ctz_ == __result.__ctz_) + return __copy_backward_aligned(__first, __last, __result); + return __copy_backward_unaligned(__first, __last, __result); +} + +// move + +template <class _C, bool _IsConst> +inline _LIBCPP_INLINE_VISIBILITY +__bit_iterator<_C, false> +move(__bit_iterator<_C, _IsConst> __first, __bit_iterator<_C, _IsConst> __last, __bit_iterator<_C, false> __result) +{ + return _VSTD::copy(__first, __last, __result); +} + +// move_backward + +template <class _C, bool _IsConst> +inline _LIBCPP_INLINE_VISIBILITY +__bit_iterator<_C, false> +move_backward(__bit_iterator<_C, _IsConst> __first, __bit_iterator<_C, _IsConst> __last, __bit_iterator<_C, false> __result) +{ + return _VSTD::copy(__first, __last, __result); +} + +// swap_ranges + +template <class __C1, class __C2> +__bit_iterator<__C2, false> +__swap_ranges_aligned(__bit_iterator<__C1, false> __first, __bit_iterator<__C1, false> __last, + __bit_iterator<__C2, false> __result) +{ + typedef __bit_iterator<__C1, false> _I1; + typedef typename _I1::difference_type difference_type; + typedef typename _I1::__storage_type __storage_type; + static const unsigned __bits_per_word = _I1::__bits_per_word; + difference_type __n = __last - __first; + if (__n > 0) + { + // do first word + if (__first.__ctz_ != 0) + { + unsigned __clz = __bits_per_word - __first.__ctz_; + difference_type __dn = _VSTD::min(static_cast<difference_type>(__clz), __n); + __n -= __dn; + __storage_type __m = (~__storage_type(0) << __first.__ctz_) & (~__storage_type(0) >> (__clz - __dn)); + __storage_type __b1 = *__first.__seg_ & __m; + *__first.__seg_ &= ~__m; + __storage_type __b2 = *__result.__seg_ & __m; + *__result.__seg_ &= ~__m; + *__result.__seg_ |= __b1; + *__first.__seg_ |= __b2; + __result.__seg_ += (__dn + __result.__ctz_) / __bits_per_word; + __result.__ctz_ = static_cast<unsigned>((__dn + __result.__ctz_) % __bits_per_word); + ++__first.__seg_; + // __first.__ctz_ = 0; + } + // __first.__ctz_ == 0; + // do middle words + for (; __n >= __bits_per_word; __n -= __bits_per_word, ++__first.__seg_, ++__result.__seg_) + swap(*__first.__seg_, *__result.__seg_); + // do last word + if (__n > 0) + { + __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n); + __storage_type __b1 = *__first.__seg_ & __m; + *__first.__seg_ &= ~__m; + __storage_type __b2 = *__result.__seg_ & __m; + *__result.__seg_ &= ~__m; + *__result.__seg_ |= __b1; + *__first.__seg_ |= __b2; + __result.__ctz_ = static_cast<unsigned>(__n); + } + } + return __result; +} + +template <class __C1, class __C2> +__bit_iterator<__C2, false> +__swap_ranges_unaligned(__bit_iterator<__C1, false> __first, __bit_iterator<__C1, false> __last, + __bit_iterator<__C2, false> __result) +{ + typedef __bit_iterator<__C1, false> _I1; + typedef typename _I1::difference_type difference_type; + typedef typename _I1::__storage_type __storage_type; + static const unsigned __bits_per_word = _I1::__bits_per_word; + difference_type __n = __last - __first; + if (__n > 0) + { + // do first word + if (__first.__ctz_ != 0) + { + unsigned __clz_f = __bits_per_word - __first.__ctz_; + difference_type __dn = _VSTD::min(static_cast<difference_type>(__clz_f), __n); + __n -= __dn; + __storage_type __m = (~__storage_type(0) << __first.__ctz_) & (~__storage_type(0) >> (__clz_f - __dn)); + __storage_type __b1 = *__first.__seg_ & __m; + *__first.__seg_ &= ~__m; + unsigned __clz_r = __bits_per_word - __result.__ctz_; + __storage_type __ddn = _VSTD::min<__storage_type>(__dn, __clz_r); + __m = (~__storage_type(0) << __result.__ctz_) & (~__storage_type(0) >> (__clz_r - __ddn)); + __storage_type __b2 = *__result.__seg_ & __m; + *__result.__seg_ &= ~__m; + if (__result.__ctz_ > __first.__ctz_) + { + unsigned __s = __result.__ctz_ - __first.__ctz_; + *__result.__seg_ |= __b1 << __s; + *__first.__seg_ |= __b2 >> __s; + } + else + { + unsigned __s = __first.__ctz_ - __result.__ctz_; + *__result.__seg_ |= __b1 >> __s; + *__first.__seg_ |= __b2 << __s; + } + __result.__seg_ += (__ddn + __result.__ctz_) / __bits_per_word; + __result.__ctz_ = static_cast<unsigned>((__ddn + __result.__ctz_) % __bits_per_word); + __dn -= __ddn; + if (__dn > 0) + { + __m = ~__storage_type(0) >> (__bits_per_word - __dn); + __b2 = *__result.__seg_ & __m; + *__result.__seg_ &= ~__m; + unsigned __s = __first.__ctz_ + __ddn; + *__result.__seg_ |= __b1 >> __s; + *__first.__seg_ |= __b2 << __s; + __result.__ctz_ = static_cast<unsigned>(__dn); + } + ++__first.__seg_; + // __first.__ctz_ = 0; + } + // __first.__ctz_ == 0; + // do middle words + __storage_type __m = ~__storage_type(0) << __result.__ctz_; + unsigned __clz_r = __bits_per_word - __result.__ctz_; + for (; __n >= __bits_per_word; __n -= __bits_per_word, ++__first.__seg_) + { + __storage_type __b1 = *__first.__seg_; + __storage_type __b2 = *__result.__seg_ & __m; + *__result.__seg_ &= ~__m; + *__result.__seg_ |= __b1 << __result.__ctz_; + *__first.__seg_ = __b2 >> __result.__ctz_; + ++__result.__seg_; + __b2 = *__result.__seg_ & ~__m; + *__result.__seg_ &= __m; + *__result.__seg_ |= __b1 >> __clz_r; + *__first.__seg_ |= __b2 << __clz_r; + } + // do last word + if (__n > 0) + { + __m = ~__storage_type(0) >> (__bits_per_word - __n); + __storage_type __b1 = *__first.__seg_ & __m; + *__first.__seg_ &= ~__m; + __storage_type __dn = _VSTD::min<__storage_type>(__n, __clz_r); + __m = (~__storage_type(0) << __result.__ctz_) & (~__storage_type(0) >> (__clz_r - __dn)); + __storage_type __b2 = *__result.__seg_ & __m; + *__result.__seg_ &= ~__m; + *__result.__seg_ |= __b1 << __result.__ctz_; + *__first.__seg_ |= __b2 >> __result.__ctz_; + __result.__seg_ += (__dn + __result.__ctz_) / __bits_per_word; + __result.__ctz_ = static_cast<unsigned>((__dn + __result.__ctz_) % __bits_per_word); + __n -= __dn; + if (__n > 0) + { + __m = ~__storage_type(0) >> (__bits_per_word - __n); + __b2 = *__result.__seg_ & __m; + *__result.__seg_ &= ~__m; + *__result.__seg_ |= __b1 >> __dn; + *__first.__seg_ |= __b2 << __dn; + __result.__ctz_ = static_cast<unsigned>(__n); + } + } + } + return __result; +} + +template <class __C1, class __C2> +inline _LIBCPP_INLINE_VISIBILITY +__bit_iterator<__C2, false> +swap_ranges(__bit_iterator<__C1, false> __first1, __bit_iterator<__C1, false> __last1, + __bit_iterator<__C2, false> __first2) +{ + if (__first1.__ctz_ == __first2.__ctz_) + return __swap_ranges_aligned(__first1, __last1, __first2); + return __swap_ranges_unaligned(__first1, __last1, __first2); +} + +// rotate + +template <class _C> +struct __bit_array +{ + typedef typename _C::difference_type difference_type; + typedef typename _C::__storage_type __storage_type; + typedef typename _C::iterator iterator; + static const unsigned __bits_per_word = _C::__bits_per_word; + static const unsigned _N = 4; + + difference_type __size_; + __storage_type __word_[_N]; + + _LIBCPP_INLINE_VISIBILITY static difference_type capacity() + {return static_cast<difference_type>(_N * __bits_per_word);} + _LIBCPP_INLINE_VISIBILITY explicit __bit_array(difference_type __s) : __size_(__s) {} + _LIBCPP_INLINE_VISIBILITY iterator begin() {return iterator(__word_, 0);} + _LIBCPP_INLINE_VISIBILITY iterator end() {return iterator(__word_ + __size_ / __bits_per_word, + static_cast<unsigned>(__size_ % __bits_per_word));} +}; + +template <class _C> +__bit_iterator<_C, false> +rotate(__bit_iterator<_C, false> __first, __bit_iterator<_C, false> __middle, __bit_iterator<_C, false> __last) +{ + typedef __bit_iterator<_C, false> _I1; + typedef typename _I1::difference_type difference_type; + typedef typename _I1::__storage_type __storage_type; + static const unsigned __bits_per_word = _I1::__bits_per_word; + difference_type __d1 = __middle - __first; + difference_type __d2 = __last - __middle; + _I1 __r = __first + __d2; + while (__d1 != 0 && __d2 != 0) + { + if (__d1 <= __d2) + { + if (__d1 <= __bit_array<_C>::capacity()) + { + __bit_array<_C> __b(__d1); + _VSTD::copy(__first, __middle, __b.begin()); + _VSTD::copy(__b.begin(), __b.end(), _VSTD::copy(__middle, __last, __first)); + break; + } + else + { + __bit_iterator<_C, false> __mp = _VSTD::swap_ranges(__first, __middle, __middle); + __first = __middle; + __middle = __mp; + __d2 -= __d1; + } + } + else + { + if (__d2 <= __bit_array<_C>::capacity()) + { + __bit_array<_C> __b(__d2); + _VSTD::copy(__middle, __last, __b.begin()); + _VSTD::copy_backward(__b.begin(), __b.end(), _VSTD::copy_backward(__first, __middle, __last)); + break; + } + else + { + __bit_iterator<_C, false> __mp = __first + __d2; + _VSTD::swap_ranges(__first, __mp, __middle); + __first = __mp; + __d1 -= __d2; + } + } + } + return __r; +} + +// equal + +template <class _C> +bool +__equal_unaligned(__bit_iterator<_C, true> __first1, __bit_iterator<_C, true> __last1, + __bit_iterator<_C, true> __first2) +{ + typedef __bit_iterator<_C, true> _It; + typedef typename _It::difference_type difference_type; + typedef typename _It::__storage_type __storage_type; + static const unsigned __bits_per_word = _It::__bits_per_word; + difference_type __n = __last1 - __first1; + if (__n > 0) + { + // do first word + if (__first1.__ctz_ != 0) + { + unsigned __clz_f = __bits_per_word - __first1.__ctz_; + difference_type __dn = _VSTD::min(static_cast<difference_type>(__clz_f), __n); + __n -= __dn; + __storage_type __m = (~__storage_type(0) << __first1.__ctz_) & (~__storage_type(0) >> (__clz_f - __dn)); + __storage_type __b = *__first1.__seg_ & __m; + unsigned __clz_r = __bits_per_word - __first2.__ctz_; + __storage_type __ddn = _VSTD::min<__storage_type>(__dn, __clz_r); + __m = (~__storage_type(0) << __first2.__ctz_) & (~__storage_type(0) >> (__clz_r - __ddn)); + if (__first2.__ctz_ > __first1.__ctz_) + if ((*__first2.__seg_ & __m) != (__b << (__first2.__ctz_ - __first1.__ctz_))) + return false; + else + if ((*__first2.__seg_ & __m) != (__b >> (__first1.__ctz_ - __first2.__ctz_))) + return false; + __first2.__seg_ += (__ddn + __first2.__ctz_) / __bits_per_word; + __first2.__ctz_ = static_cast<unsigned>((__ddn + __first2.__ctz_) % __bits_per_word); + __dn -= __ddn; + if (__dn > 0) + { + __m = ~__storage_type(0) >> (__bits_per_word - __dn); + if ((*__first2.__seg_ & __m) != (__b >> (__first1.__ctz_ + __ddn))) + return false; + __first2.__ctz_ = static_cast<unsigned>(__dn); + } + ++__first1.__seg_; + // __first1.__ctz_ = 0; + } + // __first1.__ctz_ == 0; + // do middle words + unsigned __clz_r = __bits_per_word - __first2.__ctz_; + __storage_type __m = ~__storage_type(0) << __first2.__ctz_; + for (; __n >= __bits_per_word; __n -= __bits_per_word, ++__first1.__seg_) + { + __storage_type __b = *__first1.__seg_; + if ((*__first2.__seg_ & __m) != (__b << __first2.__ctz_)) + return false; + ++__first2.__seg_; + if ((*__first2.__seg_ & ~__m) != (__b >> __clz_r)) + return false; + } + // do last word + if (__n > 0) + { + __m = ~__storage_type(0) >> (__bits_per_word - __n); + __storage_type __b = *__first1.__seg_ & __m; + __storage_type __dn = _VSTD::min(__n, static_cast<difference_type>(__clz_r)); + __m = (~__storage_type(0) << __first2.__ctz_) & (~__storage_type(0) >> (__clz_r - __dn)); + if ((*__first2.__seg_ & __m) != (__b << __first2.__ctz_)) + return false; + __first2.__seg_ += (__dn + __first2.__ctz_) / __bits_per_word; + __first2.__ctz_ = static_cast<unsigned>((__dn + __first2.__ctz_) % __bits_per_word); + __n -= __dn; + if (__n > 0) + { + __m = ~__storage_type(0) >> (__bits_per_word - __n); + if ((*__first2.__seg_ & __m) != (__b >> __dn)) + return false; + } + } + } + return true; +} + +template <class _C> +bool +__equal_aligned(__bit_iterator<_C, true> __first1, __bit_iterator<_C, true> __last1, + __bit_iterator<_C, true> __first2) +{ + typedef __bit_iterator<_C, true> _It; + typedef typename _It::difference_type difference_type; + typedef typename _It::__storage_type __storage_type; + static const unsigned __bits_per_word = _It::__bits_per_word; + difference_type __n = __last1 - __first1; + if (__n > 0) + { + // do first word + if (__first1.__ctz_ != 0) + { + unsigned __clz = __bits_per_word - __first1.__ctz_; + difference_type __dn = _VSTD::min(static_cast<difference_type>(__clz), __n); + __n -= __dn; + __storage_type __m = (~__storage_type(0) << __first1.__ctz_) & (~__storage_type(0) >> (__clz - __dn)); + if ((*__first2.__seg_ & __m) != (*__first1.__seg_ & __m)) + return false; + ++__first2.__seg_; + ++__first1.__seg_; + // __first1.__ctz_ = 0; + // __first2.__ctz_ = 0; + } + // __first1.__ctz_ == 0; + // __first2.__ctz_ == 0; + // do middle words + for (; __n >= __bits_per_word; __n -= __bits_per_word, ++__first1.__seg_, ++__first2.__seg_) + if (*__first2.__seg_ != *__first1.__seg_) + return false; + // do last word + if (__n > 0) + { + __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n); + if ((*__first2.__seg_ & __m) != (*__first1.__seg_ & __m)) + return false; + } + } + return true; +} + +template <class _C, bool _IC1, bool _IC2> +inline _LIBCPP_INLINE_VISIBILITY +bool +equal(__bit_iterator<_C, _IC1> __first1, __bit_iterator<_C, _IC1> __last1, __bit_iterator<_C, _IC2> __first2) +{ + if (__first1.__ctz_ == __first2.__ctz_) + return __equal_aligned(__first1, __last1, __first2); + return __equal_unaligned(__first1, __last1, __first2); +} + +template <class _C, bool _IsConst> +class __bit_iterator +{ +public: + typedef typename _C::difference_type difference_type; + typedef bool value_type; + typedef __bit_iterator pointer; + typedef typename conditional<_IsConst, __bit_const_reference<_C>, __bit_reference<_C> >::type reference; + typedef random_access_iterator_tag iterator_category; + +private: + typedef typename _C::__storage_type __storage_type; + typedef typename conditional<_IsConst, typename _C::__const_storage_pointer, + typename _C::__storage_pointer>::type __storage_pointer; + static const unsigned __bits_per_word = _C::__bits_per_word; + + __storage_pointer __seg_; + unsigned __ctz_; + +public: + _LIBCPP_INLINE_VISIBILITY __bit_iterator() _NOEXCEPT {} + + _LIBCPP_INLINE_VISIBILITY + __bit_iterator(const __bit_iterator<_C, false>& __it) _NOEXCEPT + : __seg_(__it.__seg_), __ctz_(__it.__ctz_) {} + + _LIBCPP_INLINE_VISIBILITY reference operator*() const _NOEXCEPT + {return reference(__seg_, __storage_type(1) << __ctz_);} + + _LIBCPP_INLINE_VISIBILITY __bit_iterator& operator++() + { + if (__ctz_ != __bits_per_word-1) + ++__ctz_; + else + { + __ctz_ = 0; + ++__seg_; + } + return *this; + } + + _LIBCPP_INLINE_VISIBILITY __bit_iterator operator++(int) + { + __bit_iterator __tmp = *this; + ++(*this); + return __tmp; + } + + _LIBCPP_INLINE_VISIBILITY __bit_iterator& operator--() + { + if (__ctz_ != 0) + --__ctz_; + else + { + __ctz_ = __bits_per_word - 1; + --__seg_; + } + return *this; + } + + _LIBCPP_INLINE_VISIBILITY __bit_iterator operator--(int) + { + __bit_iterator __tmp = *this; + --(*this); + return __tmp; + } + + _LIBCPP_INLINE_VISIBILITY __bit_iterator& operator+=(difference_type __n) + { + if (__n >= 0) + __seg_ += (__n + __ctz_) / __bits_per_word; + else + __seg_ += static_cast<difference_type>(__n - __bits_per_word + __ctz_ + 1) + / static_cast<difference_type>(__bits_per_word); + __n &= (__bits_per_word - 1); + __ctz_ = static_cast<unsigned>((__n + __ctz_) % __bits_per_word); + return *this; + } + + _LIBCPP_INLINE_VISIBILITY __bit_iterator& operator-=(difference_type __n) + { + return *this += -__n; + } + + _LIBCPP_INLINE_VISIBILITY __bit_iterator operator+(difference_type __n) const + { + __bit_iterator __t(*this); + __t += __n; + return __t; + } + + _LIBCPP_INLINE_VISIBILITY __bit_iterator operator-(difference_type __n) const + { + __bit_iterator __t(*this); + __t -= __n; + return __t; + } + + _LIBCPP_INLINE_VISIBILITY + friend __bit_iterator operator+(difference_type __n, const __bit_iterator& __it) {return __it + __n;} + + _LIBCPP_INLINE_VISIBILITY + friend difference_type operator-(const __bit_iterator& __x, const __bit_iterator& __y) + {return (__x.__seg_ - __y.__seg_) * __bits_per_word + __x.__ctz_ - __y.__ctz_;} + + _LIBCPP_INLINE_VISIBILITY reference operator[](difference_type __n) const {return *(*this + __n);} + + _LIBCPP_INLINE_VISIBILITY friend bool operator==(const __bit_iterator& __x, const __bit_iterator& __y) + {return __x.__seg_ == __y.__seg_ && __x.__ctz_ == __y.__ctz_;} + + _LIBCPP_INLINE_VISIBILITY friend bool operator!=(const __bit_iterator& __x, const __bit_iterator& __y) + {return !(__x == __y);} + + _LIBCPP_INLINE_VISIBILITY friend bool operator<(const __bit_iterator& __x, const __bit_iterator& __y) + {return __x.__seg_ < __y.__seg_ || (__x.__seg_ == __y.__seg_ && __x.__ctz_ < __y.__ctz_);} + + _LIBCPP_INLINE_VISIBILITY friend bool operator>(const __bit_iterator& __x, const __bit_iterator& __y) + {return __y < __x;} + + _LIBCPP_INLINE_VISIBILITY friend bool operator<=(const __bit_iterator& __x, const __bit_iterator& __y) + {return !(__y < __x);} + + _LIBCPP_INLINE_VISIBILITY friend bool operator>=(const __bit_iterator& __x, const __bit_iterator& __y) + {return !(__x < __y);} + +private: + _LIBCPP_INLINE_VISIBILITY + __bit_iterator(__storage_pointer __s, unsigned __ctz) _NOEXCEPT + : __seg_(__s), __ctz_(__ctz) {} + +#if defined(__clang__) + friend typename _C::__self; +#else + friend class _C::__self; +#endif + friend class __bit_reference<_C>; + friend class __bit_const_reference<_C>; + friend class __bit_iterator<_C, true>; + template <class _D> friend struct __bit_array; + template <class _D> friend void __fill_n_false(__bit_iterator<_D, false> __first, typename _D::size_type __n); + template <class _D> friend void __fill_n_true(__bit_iterator<_D, false> __first, typename _D::size_type __n); + template <class _D, bool _IC> friend __bit_iterator<_D, false> __copy_aligned(__bit_iterator<_D, _IC> __first, + __bit_iterator<_D, _IC> __last, + __bit_iterator<_D, false> __result); + template <class _D, bool _IC> friend __bit_iterator<_D, false> __copy_unaligned(__bit_iterator<_D, _IC> __first, + __bit_iterator<_D, _IC> __last, + __bit_iterator<_D, false> __result); + template <class _D, bool _IC> friend __bit_iterator<_D, false> copy(__bit_iterator<_D, _IC> __first, + __bit_iterator<_D, _IC> __last, + __bit_iterator<_D, false> __result); + template <class _D, bool _IC> friend __bit_iterator<_D, false> __copy_backward_aligned(__bit_iterator<_D, _IC> __first, + __bit_iterator<_D, _IC> __last, + __bit_iterator<_D, false> __result); + template <class _D, bool _IC> friend __bit_iterator<_D, false> __copy_backward_unaligned(__bit_iterator<_D, _IC> __first, + __bit_iterator<_D, _IC> __last, + __bit_iterator<_D, false> __result); + template <class _D, bool _IC> friend __bit_iterator<_D, false> copy_backward(__bit_iterator<_D, _IC> __first, + __bit_iterator<_D, _IC> __last, + __bit_iterator<_D, false> __result); + template <class __C1, class __C2>friend __bit_iterator<__C2, false> __swap_ranges_aligned(__bit_iterator<__C1, false>, + __bit_iterator<__C1, false>, + __bit_iterator<__C2, false>); + template <class __C1, class __C2>friend __bit_iterator<__C2, false> __swap_ranges_unaligned(__bit_iterator<__C1, false>, + __bit_iterator<__C1, false>, + __bit_iterator<__C2, false>); + template <class __C1, class __C2>friend __bit_iterator<__C2, false> swap_ranges(__bit_iterator<__C1, false>, + __bit_iterator<__C1, false>, + __bit_iterator<__C2, false>); + template <class _D> friend __bit_iterator<_D, false> rotate(__bit_iterator<_D, false>, + __bit_iterator<_D, false>, + __bit_iterator<_D, false>); + template <class _D> friend bool __equal_aligned(__bit_iterator<_D, true>, + __bit_iterator<_D, true>, + __bit_iterator<_D, true>); + template <class _D> friend bool __equal_unaligned(__bit_iterator<_D, true>, + __bit_iterator<_D, true>, + __bit_iterator<_D, true>); + template <class _D, bool _IC1, bool _IC2> friend bool equal(__bit_iterator<_D, _IC1>, + __bit_iterator<_D, _IC1>, + __bit_iterator<_D, _IC2>); + template <class _D> friend __bit_iterator<_D, false> __find_bool_true(__bit_iterator<_D, false>, + typename _D::size_type); + template <class _D> friend __bit_iterator<_D, false> __find_bool_false(__bit_iterator<_D, false>, + typename _D::size_type); +}; + +_LIBCPP_END_NAMESPACE_STD + +#endif // _LIBCPP___BIT_REFERENCE diff --git a/include/__config b/include/__config new file mode 100644 index 000000000000..0f6c77dd9817 --- /dev/null +++ b/include/__config @@ -0,0 +1,405 @@ +// -*- C++ -*- +//===--------------------------- __config ---------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#ifndef _LIBCPP_CONFIG +#define _LIBCPP_CONFIG + +#if !_MSC_VER // explicit macro necessary because it is only defined below in this file +#pragma GCC system_header +#endif + +#define _LIBCPP_VERSION 1001 + +#define _LIBCPP_ABI_VERSION 1 + +#define _LIBCPP_CONCAT1(_LIBCPP_X,_LIBCPP_Y) _LIBCPP_X##_LIBCPP_Y +#define _LIBCPP_CONCAT(_LIBCPP_X,_LIBCPP_Y) _LIBCPP_CONCAT1(_LIBCPP_X,_LIBCPP_Y) + +#define _LIBCPP_NAMESPACE _LIBCPP_CONCAT(__,_LIBCPP_ABI_VERSION) + +#ifdef __LITTLE_ENDIAN__ +#if __LITTLE_ENDIAN__ +#define _LIBCPP_LITTLE_ENDIAN 1 +#define _LIBCPP_BIG_ENDIAN 0 +#endif // __LITTLE_ENDIAN__ +#endif // __LITTLE_ENDIAN__ + +#ifdef __BIG_ENDIAN__ +#if __BIG_ENDIAN__ +#define _LIBCPP_LITTLE_ENDIAN 0 +#define _LIBCPP_BIG_ENDIAN 1 +#endif // __BIG_ENDIAN__ +#endif // __BIG_ENDIAN__ + +#ifdef __FreeBSD__ +# include <sys/endian.h> +# if _BYTE_ORDER == _LITTLE_ENDIAN +# define _LIBCPP_LITTLE_ENDIAN 1 +# define _LIBCPP_BIG_ENDIAN 0 +# else // _BYTE_ORDER == _LITTLE_ENDIAN +# define _LIBCPP_LITTLE_ENDIAN 0 +# define _LIBCPP_BIG_ENDIAN 1 +# endif // _BYTE_ORDER == _LITTLE_ENDIAN +#endif // __FreeBSD__ + +#ifdef _WIN32 +# define _LIBCPP_LITTLE_ENDIAN 1 +# define _LIBCPP_BIG_ENDIAN 0 +// Compiler intrinsics (GCC or MSVC) +# if (defined(_MSC_VER) && _MSC_VER >= 1400) || (__GNUC__ >= 4 && __GNUC_MINOR__ > 3) +# define _LIBCP_HAS_IS_BASE_OF +# endif +#endif // _WIN32 + +#if !defined(_LIBCPP_LITTLE_ENDIAN) || !defined(_LIBCPP_BIG_ENDIAN) +# include <endian.h> +# if __BYTE_ORDER == __LITTLE_ENDIAN +# define _LIBCPP_LITTLE_ENDIAN 1 +# define _LIBCPP_BIG_ENDIAN 0 +# elif __BYTE_ORDER == __BIG_ENDIAN +# define _LIBCPP_LITTLE_ENDIAN 0 +# define _LIBCPP_BIG_ENDIAN 1 +# else // __BYTE_ORDER == __BIG_ENDIAN +# error unable to determine endian +# endif +#endif // !defined(_LIBCPP_LITTLE_ENDIAN) || !defined(_LIBCPP_BIG_ENDIAN) + +#if _WIN32 + +// only really useful for a DLL +#ifdef _LIBCPP_DLL // this should be a compiler builtin define ideally... +# ifdef cxx_EXPORTS +# define _LIBCPP_HIDDEN +# define _LIBCPP_VISIBLE __declspec(dllexport) +# else +# define _LIBCPP_HIDDEN +# define _LIBCPP_VISIBLE __declspec(dllimport) +# endif +#else +# define _LIBCPP_HIDDEN +# define _LIBCPP_VISIBLE +#endif + +#ifndef _LIBCPP_INLINE_VISIBILITY +# if _MSC_VER +# define _LIBCPP_INLINE_VISIBILITY __forceinline +# else // MinGW GCC and Clang +# define _LIBCPP_INLINE_VISIBILITY __attribute__ ((__always_inline__)) +# endif +#endif + +#ifndef _LIBCPP_EXCEPTION_ABI +#define _LIBCPP_EXCEPTION_ABI _LIBCPP_VISIBLE +#endif + +#ifndef _LIBCPP_ALWAYS_INLINE +# if _MSC_VER +# define _LIBCPP_ALWAYS_INLINE __forceinline +# endif +#endif + +#endif // _WIN32 + +#ifndef _LIBCPP_HIDDEN +#define _LIBCPP_HIDDEN __attribute__ ((__visibility__("hidden"))) +#endif + +#ifndef _LIBCPP_VISIBLE +#define _LIBCPP_VISIBLE __attribute__ ((__visibility__("default"))) +#endif + +#ifndef _LIBCPP_INLINE_VISIBILITY +#define _LIBCPP_INLINE_VISIBILITY __attribute__ ((__visibility__("hidden"), __always_inline__)) +#endif + +#ifndef _LIBCPP_EXCEPTION_ABI +#define _LIBCPP_EXCEPTION_ABI __attribute__ ((__visibility__("default"))) +#endif + +#ifndef _LIBCPP_CANTTHROW +#define _LIBCPP_CANTTHROW __attribute__ ((__nothrow__)) +#endif + +#ifndef _LIBCPP_ALWAYS_INLINE +#define _LIBCPP_ALWAYS_INLINE __attribute__ ((__visibility__("hidden"), __always_inline__)) +#endif + +#if defined(__clang__) + +#if __has_feature(cxx_alignas) +# define _ALIGNAS(x) alignas(x) +#else +# define _ALIGNAS(x) __attribute__((__aligned__(x))) +#endif + +#if !__has_feature(cxx_alias_templates) +#define _LIBCPP_HAS_NO_TEMPLATE_ALIASES +#endif + +#ifndef __GXX_EXPERIMENTAL_CXX0X__ +#ifdef __linux__ +#define _LIBCPP_HAS_NO_UNICODE_CHARS +#else +typedef __char16_t char16_t; +typedef __char32_t char32_t; +#endif +#endif + +#if !(__has_feature(cxx_exceptions)) +#define _LIBCPP_NO_EXCEPTIONS +#endif + +#if !(__has_feature(cxx_rtti)) +#define _LIBCPP_NO_RTTI +#endif + +#if !(__has_feature(cxx_decltype)) +#define _LIBCPP_HAS_NO_DECLTYPE +#endif + +#if __has_feature(cxx_attributes) +# define _ATTRIBUTE(x) [[x]] +#else +# define _ATTRIBUTE(x) __attribute__ ((x)) +#endif + +#define _LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS + +#if !(__has_feature(cxx_deleted_functions)) +#define _LIBCPP_HAS_NO_DELETED_FUNCTIONS +#endif // !(__has_feature(cxx_deleted_functions)) + +#if !(__has_feature(cxx_lambdas)) +#define _LIBCPP_HAS_NO_LAMBDAS +#endif + +#if !(__has_feature(cxx_nullptr)) +#define _LIBCPP_HAS_NO_NULLPTR +#endif + +#if !(__has_feature(cxx_rvalue_references)) +#define _LIBCPP_HAS_NO_RVALUE_REFERENCES +#endif + +#if !(__has_feature(cxx_static_assert)) +#define _LIBCPP_HAS_NO_STATIC_ASSERT +#endif + +#if !(__has_feature(cxx_auto_type)) +#define _LIBCPP_HAS_NO_AUTO_TYPE +#endif + +#if !(__has_feature(cxx_access_control_sfinae)) || !__has_feature(cxx_trailing_return) +#define _LIBCPP_HAS_NO_ADVANCED_SFINAE +#endif + +#if !(__has_feature(cxx_variadic_templates)) +#define _LIBCPP_HAS_NO_VARIADICS +#endif + +#if !(__has_feature(cxx_trailing_return)) +#define _LIBCPP_HAS_NO_TRAILING_RETURN +#endif + +#if !(__has_feature(cxx_generalized_initializers)) +#define _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS +#endif + +#if __has_feature(is_base_of) +# define _LIBCP_HAS_IS_BASE_OF +#endif + +// Objective-C++ features (opt-in) +#if __has_feature(objc_arc) +#define _LIBCPP_HAS_OBJC_ARC +#endif + +#if __has_feature(objc_arc_weak) +#define _LIBCPP_HAS_OBJC_ARC_WEAK +#endif + +#if !(__has_feature(cxx_constexpr)) +#define _LIBCPP_HAS_NO_CONSTEXPR +#endif + +#if (__has_feature(cxx_noexcept)) +# define _NOEXCEPT noexcept +# define _NOEXCEPT_(x) noexcept(x) +#else +# define _NOEXCEPT throw() +# define _NOEXCEPT_(x) +#endif + +#if __has_feature(underlying_type) +# define _LIBCXX_UNDERLYING_TYPE(T) __underlying_type(T) +#endif + +// Inline namespaces are available in Clang regardless of C++ dialect. +#define _LIBCPP_BEGIN_NAMESPACE_STD namespace std {inline namespace _LIBCPP_NAMESPACE { +#define _LIBCPP_END_NAMESPACE_STD } } +#define _VSTD std::_LIBCPP_NAMESPACE + +namespace std { + inline namespace _LIBCPP_NAMESPACE { + } +} + +#elif defined(__GNUC__) + +#define _ALIGNAS(x) __attribute__((__aligned__(x))) + +#define _ATTRIBUTE(x) __attribute__((x)) + +#if !__EXCEPTIONS +#define _LIBCPP_NO_EXCEPTIONS +#endif + +#define _LIBCPP_HAS_NO_TEMPLATE_ALIASES +#define _LIBCPP_HAS_NO_CONSTEXPR + +#define _NOEXCEPT throw() +#define _NOEXCEPT_(x) + +#ifndef __GXX_EXPERIMENTAL_CXX0X__ + +#define _LIBCPP_HAS_NO_ADVANCED_SFINAE +#define _LIBCPP_HAS_NO_DECLTYPE +#define _LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS +#define _LIBCPP_HAS_NO_DELETED_FUNCTIONS +#define _LIBCPP_HAS_NO_NULLPTR +#define _LIBCPP_HAS_NO_STATIC_ASSERT +#define _LIBCPP_HAS_NO_UNICODE_CHARS +#define _LIBCPP_HAS_NO_VARIADICS +#define _LIBCPP_HAS_NO_RVALUE_REFERENCES +#define _LIBCPP_HAS_NO_ALWAYS_INLINE_VARIADICS + +#else // __GXX_EXPERIMENTAL_CXX0X__ + +#define _LIBCPP_HAS_NO_TRAILING_RETURN +#define _LIBCPP_HAS_NO_ALWAYS_INLINE_VARIADICS + +#if !(__GNUC__ >= 4 && __GNUC_MINOR__ >= 3) +#define _LIBCPP_HAS_NO_RVALUE_REFERENCES +#endif + +#if !(__GNUC__ >= 4 && __GNUC_MINOR__ >= 3) +#define _LIBCPP_HAS_NO_STATIC_ASSERT +#endif + +#if !(__GNUC__ >= 4 && __GNUC_MINOR__ >= 4) +#define _LIBCPP_HAS_NO_ADVANCED_SFINAE +#define _LIBCPP_HAS_NO_DECLTYPE +#define _LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS +#define _LIBCPP_HAS_NO_DELETED_FUNCTIONS +#define _LIBCPP_HAS_NO_UNICODE_CHARS +#define _LIBCPP_HAS_NO_VARIADICS +#define _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS +#endif // !(__GNUC__ >= 4 && __GNUC_MINOR__ >= 4) + +#if !(__GNUC__ >= 4 && __GNUC_MINOR__ >= 6) +#define _LIBCPP_HAS_NO_NULLPTR +#endif + +#endif // __GXX_EXPERIMENTAL_CXX0X__ + +#define _LIBCPP_BEGIN_NAMESPACE_STD namespace std { namespace _LIBCPP_NAMESPACE { +#define _LIBCPP_END_NAMESPACE_STD } } +#define _VSTD std::_LIBCPP_NAMESPACE + +namespace std { +namespace _LIBCPP_NAMESPACE { +} +using namespace _LIBCPP_NAMESPACE __attribute__((__strong__)); +} + +#elif defined(_MSC_VER) + +#define _LIBCPP_HAS_NO_TEMPLATE_ALIASES +#define _LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER +#define _LIBCPP_HAS_NO_CONSTEXPR +#define _LIBCPP_HAS_NO_UNICODE_CHARS +#define _LIBCPP_HAS_NO_DELETED_FUNCTIONS +#define __alignof__ __alignof +#define _ATTRIBUTE __declspec +#define _ALIGNAS(x) __declspec(align(x)) +#define _LIBCPP_HAS_NO_VARIADICS + +#define _NOEXCEPT throw() +#define _NOEXCEPT_(x) + +#define _LIBCPP_BEGIN_NAMESPACE_STD namespace std { +#define _LIBCPP_END_NAMESPACE_STD } +#define _VSTD std + +namespace std { +} + +#endif // __clang__ || __GNUC___ || _MSC_VER + +#ifdef _LIBCPP_HAS_NO_UNICODE_CHARS +typedef unsigned short char16_t; +typedef unsigned int char32_t; +#endif // _LIBCPP_HAS_NO_UNICODE_CHARS + +#ifdef _LIBCPP_HAS_NO_STATIC_ASSERT + +template <bool> struct __static_assert_test; +template <> struct __static_assert_test<true> {}; +template <unsigned> struct __static_assert_check {}; +#define static_assert(__b, __m) \ + typedef __static_assert_check<sizeof(__static_assert_test<(__b)>)> \ + _LIBCPP_CONCAT(__t, __LINE__) + +#endif // _LIBCPP_HAS_NO_STATIC_ASSERT + +#ifdef _LIBCPP_HAS_NO_DECLTYPE +#define decltype(x) __typeof__(x) +#endif + +#ifdef _LIBCPP_HAS_NO_CONSTEXPR +#define constexpr const +#endif + +#ifndef __has_feature +#define __has_feature(__x) 0 +#endif + +#if __APPLE__ || __FreeBSD__ || _WIN32 +#define _LIBCPP_LOCALE__L_EXTENSIONS 1 +#endif +#if __FreeBSD__ +#define _DECLARE_C99_LDBL_MATH 1 +#endif + +#if __APPLE__ || __FreeBSD__ +#define _LIBCPP_HAS_DEFAULTRUNELOCALE +#endif + +#if __APPLE__ || __FreeBSD__ +#define _LIBCPP_WCTYPE_IS_MASK +#endif + +#ifdef _LIBCPP_DEBUG2 +# if _LIBCPP_DEBUG2 == 0 +# define _LIBCPP_DEBUG_LEVEL 1 +# elif _LIBCPP_DEBUG2 == 1 +# define _LIBCPP_DEBUG_LEVEL 2 +# else +# error Supported values for _LIBCPP_DEBUG2 are 0 and 1 +# endif +#endif + +#ifdef _LIBCPP_DEBUG2 +# include <__debug> +#else +# define _LIBCPP_ASSERT(x, m) ((void)0) +#endif + +#endif // _LIBCPP_CONFIG diff --git a/include/__debug b/include/__debug new file mode 100644 index 000000000000..cd3bd3a98d98 --- /dev/null +++ b/include/__debug @@ -0,0 +1,191 @@ +// -*- C++ -*- +//===--------------------------- __debug ----------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#ifndef _LIBCPP_DEBUG_H +#define _LIBCPP_DEBUG_H + +#if _LIBCPP_DEBUG_LEVEL >= 1 + +# include <cstdlib> +# include <cstdio> +# include <cstddef> +# define _LIBCPP_ASSERT(x, m) ((x) ? (void)0 : (_VSTD::printf("%s\n", m), _VSTD::abort())) + +#endif + +#if _LIBCPP_DEBUG_LEVEL >= 2 + +_LIBCPP_BEGIN_NAMESPACE_STD + +struct _LIBCPP_VISIBLE __c_node; + +struct _LIBCPP_VISIBLE __i_node +{ + void* __i_; + __i_node* __next_; + __c_node* __c_; + + __i_node(const __i_node&) = delete; + __i_node& operator=(const __i_node&) = delete; + _LIBCPP_INLINE_VISIBILITY + __i_node(void* __i, __i_node* __next, __c_node* __c) + : __i_(__i), __next_(__next), __c_(__c) {} + ~__i_node(); +}; + +struct _LIBCPP_VISIBLE __c_node +{ + void* __c_; + __c_node* __next_; + __i_node** beg_; + __i_node** end_; + __i_node** cap_; + + __c_node(const __c_node&) = delete; + __c_node& operator=(const __c_node&) = delete; + _LIBCPP_INLINE_VISIBILITY + __c_node(void* __c, __c_node* __next) + : __c_(__c), __next_(__next), beg_(nullptr), end_(nullptr), cap_(nullptr) {} + virtual ~__c_node(); + + virtual bool __dereferenceable(const void*) const = 0; + virtual bool __decrementable(const void*) const = 0; + virtual bool __addable(const void*, ptrdiff_t) const = 0; + virtual bool __subscriptable(const void*, ptrdiff_t) const = 0; + + void __add(__i_node* __i); + _LIBCPP_HIDDEN void __remove(__i_node* __i); +}; + +template <class _Cont> +struct _C_node + : public __c_node +{ + _C_node(void* __c, __c_node* __n) + : __c_node(__c, __n) {} + + virtual bool __dereferenceable(const void*) const; + virtual bool __decrementable(const void*) const; + virtual bool __addable(const void*, ptrdiff_t) const; + virtual bool __subscriptable(const void*, ptrdiff_t) const; +}; + +template <class _Cont> +bool +_C_node<_Cont>::__dereferenceable(const void* __i) const +{ + typedef typename _Cont::const_iterator iterator; + const iterator* __j = static_cast<const iterator*>(__i); + _Cont* _C = static_cast<_Cont*>(__c_); + return _C->__dereferenceable(__j); +} + +template <class _Cont> +bool +_C_node<_Cont>::__decrementable(const void* __i) const +{ + typedef typename _Cont::const_iterator iterator; + const iterator* __j = static_cast<const iterator*>(__i); + _Cont* _C = static_cast<_Cont*>(__c_); + return _C->__decrementable(__j); +} + +template <class _Cont> +bool +_C_node<_Cont>::__addable(const void* __i, ptrdiff_t __n) const +{ + typedef typename _Cont::const_iterator iterator; + const iterator* __j = static_cast<const iterator*>(__i); + _Cont* _C = static_cast<_Cont*>(__c_); + return _C->__addable(__j, __n); +} + +template <class _Cont> +bool +_C_node<_Cont>::__subscriptable(const void* __i, ptrdiff_t __n) const +{ + typedef typename _Cont::const_iterator iterator; + const iterator* __j = static_cast<const iterator*>(__i); + _Cont* _C = static_cast<_Cont*>(__c_); + return _C->__subscriptable(__j, __n); +} + +class _LIBCPP_VISIBLE __libcpp_db +{ + __c_node** __cbeg_; + __c_node** __cend_; + size_t __csz_; + __i_node** __ibeg_; + __i_node** __iend_; + size_t __isz_; + + __libcpp_db(); +public: + __libcpp_db(const __libcpp_db&) = delete; + __libcpp_db& operator=(const __libcpp_db&) = delete; + ~__libcpp_db(); + + class __db_c_iterator; + class __db_c_const_iterator; + class __db_i_iterator; + class __db_i_const_iterator; + + __db_c_const_iterator __c_end() const; + __db_i_const_iterator __i_end() const; + + template <class _Cont> + _LIBCPP_INLINE_VISIBILITY + void __insert_c(_Cont* __c) + { + __c_node* __n = __insert_c(static_cast<void*>(__c)); + ::new(__n) _C_node<_Cont>(__n->__c_, __n->__next_); + } + + void __insert_i(void* __i); + __c_node* __insert_c(void* __c); + void __erase_c(void* __c); + + void __insert_ic(void* __i, const void* __c); + void __iterator_copy(void* __i, const void* __i0); + void __erase_i(void* __i); + + void* __find_c_from_i(void* __i) const; + void __invalidate_all(void* __c); + __c_node* __find_c_and_lock(void* __c) const; + __c_node* __find_c(void* __c) const; + void unlock() const; + + void swap(void* __c1, void* __c2); + + + bool __dereferenceable(const void* __i) const; + bool __decrementable(const void* __i) const; + bool __addable(const void* __i, ptrdiff_t __n) const; + bool __subscriptable(const void* __i, ptrdiff_t __n) const; + bool __comparable(const void* __i, const void* __j) const; +private: + _LIBCPP_HIDDEN + __i_node* __insert_iterator(void* __i); + _LIBCPP_HIDDEN + __i_node* __find_iterator(const void* __i) const; + + friend _LIBCPP_VISIBLE __libcpp_db* __get_db(); +}; + +_LIBCPP_VISIBLE __libcpp_db* __get_db(); +_LIBCPP_VISIBLE const __libcpp_db* __get_const_db(); + + +_LIBCPP_END_NAMESPACE_STD + +#endif + +#endif // _LIBCPP_DEBUG_H + diff --git a/include/__functional_03 b/include/__functional_03 new file mode 100644 index 000000000000..5d30ce2b7502 --- /dev/null +++ b/include/__functional_03 @@ -0,0 +1,2130 @@ +// -*- C++ -*- +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#ifndef _LIBCPP_FUNCTIONAL_03 +#define _LIBCPP_FUNCTIONAL_03 + +// manual variadic expansion for <functional> + +#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) +#pragma GCC system_header +#endif + +template <class _Tp> +class __mem_fn + : public __weak_result_type<_Tp> +{ +public: + // types + typedef _Tp type; +private: + type __f_; + +public: + _LIBCPP_INLINE_VISIBILITY __mem_fn(type __f) : __f_(__f) {} + + // invoke + + typename __invoke_return<type>::type + operator() () + { + return __invoke(__f_); + } + + template <class _A0> + typename __invoke_return0<type, _A0>::type + operator() (_A0& __a0) + { + return __invoke(__f_, __a0); + } + + template <class _A0, class _A1> + typename __invoke_return1<type, _A0, _A1>::type + operator() (_A0& __a0, _A1& __a1) + { + return __invoke(__f_, __a0, __a1); + } + + template <class _A0, class _A1, class _A2> + typename __invoke_return2<type, _A0, _A1, _A2>::type + operator() (_A0& __a0, _A1& __a1, _A2& __a2) + { + return __invoke(__f_, __a0, __a1, __a2); + } +}; + +template<class _R, class _T> +inline _LIBCPP_INLINE_VISIBILITY +__mem_fn<_R _T::*> +mem_fn(_R _T::* __pm) +{ + return __mem_fn<_R _T::*>(__pm); +} + +template<class _R, class _T> +inline _LIBCPP_INLINE_VISIBILITY +__mem_fn<_R (_T::*)()> +mem_fn(_R (_T::* __pm)()) +{ + return __mem_fn<_R (_T::*)()>(__pm); +} + +template<class _R, class _T, class _A0> +inline _LIBCPP_INLINE_VISIBILITY +__mem_fn<_R (_T::*)(_A0)> +mem_fn(_R (_T::* __pm)(_A0)) +{ + return __mem_fn<_R (_T::*)(_A0)>(__pm); +} + +template<class _R, class _T, class _A0, class _A1> +inline _LIBCPP_INLINE_VISIBILITY +__mem_fn<_R (_T::*)(_A0, _A1)> +mem_fn(_R (_T::* __pm)(_A0, _A1)) +{ + return __mem_fn<_R (_T::*)(_A0, _A1)>(__pm); +} + +template<class _R, class _T, class _A0, class _A1, class _A2> +inline _LIBCPP_INLINE_VISIBILITY +__mem_fn<_R (_T::*)(_A0, _A1, _A2)> +mem_fn(_R (_T::* __pm)(_A0, _A1, _A2)) +{ + return __mem_fn<_R (_T::*)(_A0, _A1, _A2)>(__pm); +} + +template<class _R, class _T> +inline _LIBCPP_INLINE_VISIBILITY +__mem_fn<_R (_T::*)()> +mem_fn(_R (_T::* __pm)() const) +{ + return __mem_fn<_R (_T::*)()>(__pm); +} + +template<class _R, class _T, class _A0> +inline _LIBCPP_INLINE_VISIBILITY +__mem_fn<_R (_T::*)(_A0)> +mem_fn(_R (_T::* __pm)(_A0) const) +{ + return __mem_fn<_R (_T::*)(_A0)>(__pm); +} + +template<class _R, class _T, class _A0, class _A1> +inline _LIBCPP_INLINE_VISIBILITY +__mem_fn<_R (_T::*)(_A0, _A1)> +mem_fn(_R (_T::* __pm)(_A0, _A1) const) +{ + return __mem_fn<_R (_T::*)(_A0, _A1)>(__pm); +} + +template<class _R, class _T, class _A0, class _A1, class _A2> +inline _LIBCPP_INLINE_VISIBILITY +__mem_fn<_R (_T::*)(_A0, _A1, _A2)> +mem_fn(_R (_T::* __pm)(_A0, _A1, _A2) const) +{ + return __mem_fn<_R (_T::*)(_A0, _A1, _A2)>(__pm); +} + +template<class _R, class _T> +inline _LIBCPP_INLINE_VISIBILITY +__mem_fn<_R (_T::*)()> +mem_fn(_R (_T::* __pm)() volatile) +{ + return __mem_fn<_R (_T::*)()>(__pm); +} + +template<class _R, class _T, class _A0> +inline _LIBCPP_INLINE_VISIBILITY +__mem_fn<_R (_T::*)(_A0)> +mem_fn(_R (_T::* __pm)(_A0) volatile) +{ + return __mem_fn<_R (_T::*)(_A0)>(__pm); +} + +template<class _R, class _T, class _A0, class _A1> +inline _LIBCPP_INLINE_VISIBILITY +__mem_fn<_R (_T::*)(_A0, _A1)> +mem_fn(_R (_T::* __pm)(_A0, _A1) volatile) +{ + return __mem_fn<_R (_T::*)(_A0, _A1)>(__pm); +} + +template<class _R, class _T, class _A0, class _A1, class _A2> +inline _LIBCPP_INLINE_VISIBILITY +__mem_fn<_R (_T::*)(_A0, _A1, _A2)> +mem_fn(_R (_T::* __pm)(_A0, _A1, _A2) volatile) +{ + return __mem_fn<_R (_T::*)(_A0, _A1, _A2)>(__pm); +} + +template<class _R, class _T> +inline _LIBCPP_INLINE_VISIBILITY +__mem_fn<_R (_T::*)()> +mem_fn(_R (_T::* __pm)() const volatile) +{ + return __mem_fn<_R (_T::*)()>(__pm); +} + +template<class _R, class _T, class _A0> +inline _LIBCPP_INLINE_VISIBILITY +__mem_fn<_R (_T::*)(_A0)> +mem_fn(_R (_T::* __pm)(_A0) const volatile) +{ + return __mem_fn<_R (_T::*)(_A0)>(__pm); +} + +template<class _R, class _T, class _A0, class _A1> +inline _LIBCPP_INLINE_VISIBILITY +__mem_fn<_R (_T::*)(_A0, _A1)> +mem_fn(_R (_T::* __pm)(_A0, _A1) const volatile) +{ + return __mem_fn<_R (_T::*)(_A0, _A1)>(__pm); +} + +template<class _R, class _T, class _A0, class _A1, class _A2> +inline _LIBCPP_INLINE_VISIBILITY +__mem_fn<_R (_T::*)(_A0, _A1, _A2)> +mem_fn(_R (_T::* __pm)(_A0, _A1, _A2) const volatile) +{ + return __mem_fn<_R (_T::*)(_A0, _A1, _A2)>(__pm); +} + +// bad_function_call + +class _LIBCPP_EXCEPTION_ABI bad_function_call + : public exception +{ +}; + +template<class _Fp> class _LIBCPP_VISIBLE function; // undefined + +namespace __function +{ + +template<class _F> +struct __maybe_derive_from_unary_function +{ +}; + +template<class _R, class _A1> +struct __maybe_derive_from_unary_function<_R(_A1)> + : public unary_function<_A1, _R> +{ +}; + +template<class _F> +struct __maybe_derive_from_binary_function +{ +}; + +template<class _R, class _A1, class _A2> +struct __maybe_derive_from_binary_function<_R(_A1, _A2)> + : public binary_function<_A1, _A2, _R> +{ +}; + +template<class _Fp> class __base; + +template<class _R> +class __base<_R()> +{ + __base(const __base&); + __base& operator=(const __base&); +public: + __base() {} + virtual ~__base() {} + virtual __base* __clone() const = 0; + virtual void __clone(__base*) const = 0; + virtual void destroy() = 0; + virtual void destroy_deallocate() = 0; + virtual _R operator()() = 0; +#ifndef _LIBCPP_NO_RTTI + virtual const void* target(const type_info&) const = 0; + virtual const std::type_info& target_type() const = 0; +#endif // _LIBCPP_NO_RTTI +}; + +template<class _R, class _A0> +class __base<_R(_A0)> +{ + __base(const __base&); + __base& operator=(const __base&); +public: + __base() {} + virtual ~__base() {} + virtual __base* __clone() const = 0; + virtual void __clone(__base*) const = 0; + virtual void destroy() = 0; + virtual void destroy_deallocate() = 0; + virtual _R operator()(_A0) = 0; +#ifndef _LIBCPP_NO_RTTI + virtual const void* target(const type_info&) const = 0; + virtual const std::type_info& target_type() const = 0; +#endif // _LIBCPP_NO_RTTI +}; + +template<class _R, class _A0, class _A1> +class __base<_R(_A0, _A1)> +{ + __base(const __base&); + __base& operator=(const __base&); +public: + __base() {} + virtual ~__base() {} + virtual __base* __clone() const = 0; + virtual void __clone(__base*) const = 0; + virtual void destroy() = 0; + virtual void destroy_deallocate() = 0; + virtual _R operator()(_A0, _A1) = 0; +#ifndef _LIBCPP_NO_RTTI + virtual const void* target(const type_info&) const = 0; + virtual const std::type_info& target_type() const = 0; +#endif // _LIBCPP_NO_RTTI +}; + +template<class _R, class _A0, class _A1, class _A2> +class __base<_R(_A0, _A1, _A2)> +{ + __base(const __base&); + __base& operator=(const __base&); +public: + __base() {} + virtual ~__base() {} + virtual __base* __clone() const = 0; + virtual void __clone(__base*) const = 0; + virtual void destroy() = 0; + virtual void destroy_deallocate() = 0; + virtual _R operator()(_A0, _A1, _A2) = 0; +#ifndef _LIBCPP_NO_RTTI + virtual const void* target(const type_info&) const = 0; + virtual const std::type_info& target_type() const = 0; +#endif // _LIBCPP_NO_RTTI +}; + +template<class _FD, class _Alloc, class _FB> class __func; + +template<class _F, class _Alloc, class _R> +class __func<_F, _Alloc, _R()> + : public __base<_R()> +{ + __compressed_pair<_F, _Alloc> __f_; +public: + explicit __func(_F __f) : __f_(_VSTD::move(__f)) {} + explicit __func(_F __f, _Alloc __a) : __f_(_VSTD::move(__f), _VSTD::move(__a)) {} + virtual __base<_R()>* __clone() const; + virtual void __clone(__base<_R()>*) const; + virtual void destroy(); + virtual void destroy_deallocate(); + virtual _R operator()(); +#ifndef _LIBCPP_NO_RTTI + virtual const void* target(const type_info&) const; + virtual const std::type_info& target_type() const; +#endif // _LIBCPP_NO_RTTI +}; + +template<class _F, class _Alloc, class _R> +__base<_R()>* +__func<_F, _Alloc, _R()>::__clone() const +{ + typedef typename _Alloc::template rebind<__func>::other _A; + _A __a(__f_.second()); + typedef __allocator_destructor<_A> _D; + unique_ptr<__func, _D> __hold(__a.allocate(1), _D(__a, 1)); + ::new (__hold.get()) __func(__f_.first(), _Alloc(__a)); + return __hold.release(); +} + +template<class _F, class _Alloc, class _R> +void +__func<_F, _Alloc, _R()>::__clone(__base<_R()>* __p) const +{ + ::new (__p) __func(__f_.first(), __f_.second()); +} + +template<class _F, class _Alloc, class _R> +void +__func<_F, _Alloc, _R()>::destroy() +{ + __f_.~__compressed_pair<_F, _Alloc>(); +} + +template<class _F, class _Alloc, class _R> +void +__func<_F, _Alloc, _R()>::destroy_deallocate() +{ + typedef typename _Alloc::template rebind<__func>::other _A; + _A __a(__f_.second()); + __f_.~__compressed_pair<_F, _Alloc>(); + __a.deallocate(this, 1); +} + +template<class _F, class _Alloc, class _R> +_R +__func<_F, _Alloc, _R()>::operator()() +{ + return __invoke(__f_.first()); +} + +#ifndef _LIBCPP_NO_RTTI + +template<class _F, class _Alloc, class _R> +const void* +__func<_F, _Alloc, _R()>::target(const type_info& __ti) const +{ + if (__ti == typeid(_F)) + return &__f_.first(); + return (const void*)0; +} + +template<class _F, class _Alloc, class _R> +const std::type_info& +__func<_F, _Alloc, _R()>::target_type() const +{ + return typeid(_F); +} + +#endif // _LIBCPP_NO_RTTI + +template<class _F, class _Alloc, class _R, class _A0> +class __func<_F, _Alloc, _R(_A0)> + : public __base<_R(_A0)> +{ + __compressed_pair<_F, _Alloc> __f_; +public: + _LIBCPP_INLINE_VISIBILITY explicit __func(_F __f) : __f_(_VSTD::move(__f)) {} + _LIBCPP_INLINE_VISIBILITY explicit __func(_F __f, _Alloc __a) + : __f_(_VSTD::move(__f), _VSTD::move(__a)) {} + virtual __base<_R(_A0)>* __clone() const; + virtual void __clone(__base<_R(_A0)>*) const; + virtual void destroy(); + virtual void destroy_deallocate(); + virtual _R operator()(_A0); +#ifndef _LIBCPP_NO_RTTI + virtual const void* target(const type_info&) const; + virtual const std::type_info& target_type() const; +#endif // _LIBCPP_NO_RTTI +}; + +template<class _F, class _Alloc, class _R, class _A0> +__base<_R(_A0)>* +__func<_F, _Alloc, _R(_A0)>::__clone() const +{ + typedef typename _Alloc::template rebind<__func>::other _A; + _A __a(__f_.second()); + typedef __allocator_destructor<_A> _D; + unique_ptr<__func, _D> __hold(__a.allocate(1), _D(__a, 1)); + ::new (__hold.get()) __func(__f_.first(), _Alloc(__a)); + return __hold.release(); +} + +template<class _F, class _Alloc, class _R, class _A0> +void +__func<_F, _Alloc, _R(_A0)>::__clone(__base<_R(_A0)>* __p) const +{ + ::new (__p) __func(__f_.first(), __f_.second()); +} + +template<class _F, class _Alloc, class _R, class _A0> +void +__func<_F, _Alloc, _R(_A0)>::destroy() +{ + __f_.~__compressed_pair<_F, _Alloc>(); +} + +template<class _F, class _Alloc, class _R, class _A0> +void +__func<_F, _Alloc, _R(_A0)>::destroy_deallocate() +{ + typedef typename _Alloc::template rebind<__func>::other _A; + _A __a(__f_.second()); + __f_.~__compressed_pair<_F, _Alloc>(); + __a.deallocate(this, 1); +} + +template<class _F, class _Alloc, class _R, class _A0> +_R +__func<_F, _Alloc, _R(_A0)>::operator()(_A0 __a0) +{ + return __invoke(__f_.first(), __a0); +} + +#ifndef _LIBCPP_NO_RTTI + +template<class _F, class _Alloc, class _R, class _A0> +const void* +__func<_F, _Alloc, _R(_A0)>::target(const type_info& __ti) const +{ + if (__ti == typeid(_F)) + return &__f_.first(); + return (const void*)0; +} + +template<class _F, class _Alloc, class _R, class _A0> +const std::type_info& +__func<_F, _Alloc, _R(_A0)>::target_type() const +{ + return typeid(_F); +} + +#endif // _LIBCPP_NO_RTTI + +template<class _F, class _Alloc, class _R, class _A0, class _A1> +class __func<_F, _Alloc, _R(_A0, _A1)> + : public __base<_R(_A0, _A1)> +{ + __compressed_pair<_F, _Alloc> __f_; +public: + _LIBCPP_INLINE_VISIBILITY explicit __func(_F __f) : __f_(_VSTD::move(__f)) {} + _LIBCPP_INLINE_VISIBILITY explicit __func(_F __f, _Alloc __a) + : __f_(_VSTD::move(__f), _VSTD::move(__a)) {} + virtual __base<_R(_A0, _A1)>* __clone() const; + virtual void __clone(__base<_R(_A0, _A1)>*) const; + virtual void destroy(); + virtual void destroy_deallocate(); + virtual _R operator()(_A0, _A1); +#ifndef _LIBCPP_NO_RTTI + virtual const void* target(const type_info&) const; + virtual const std::type_info& target_type() const; +#endif // _LIBCPP_NO_RTTI +}; + +template<class _F, class _Alloc, class _R, class _A0, class _A1> +__base<_R(_A0, _A1)>* +__func<_F, _Alloc, _R(_A0, _A1)>::__clone() const +{ + typedef typename _Alloc::template rebind<__func>::other _A; + _A __a(__f_.second()); + typedef __allocator_destructor<_A> _D; + unique_ptr<__func, _D> __hold(__a.allocate(1), _D(__a, 1)); + ::new (__hold.get()) __func(__f_.first(), _Alloc(__a)); + return __hold.release(); +} + +template<class _F, class _Alloc, class _R, class _A0, class _A1> +void +__func<_F, _Alloc, _R(_A0, _A1)>::__clone(__base<_R(_A0, _A1)>* __p) const +{ + ::new (__p) __func(__f_.first(), __f_.second()); +} + +template<class _F, class _Alloc, class _R, class _A0, class _A1> +void +__func<_F, _Alloc, _R(_A0, _A1)>::destroy() +{ + __f_.~__compressed_pair<_F, _Alloc>(); +} + +template<class _F, class _Alloc, class _R, class _A0, class _A1> +void +__func<_F, _Alloc, _R(_A0, _A1)>::destroy_deallocate() +{ + typedef typename _Alloc::template rebind<__func>::other _A; + _A __a(__f_.second()); + __f_.~__compressed_pair<_F, _Alloc>(); + __a.deallocate(this, 1); +} + +template<class _F, class _Alloc, class _R, class _A0, class _A1> +_R +__func<_F, _Alloc, _R(_A0, _A1)>::operator()(_A0 __a0, _A1 __a1) +{ + return __invoke(__f_.first(), __a0, __a1); +} + +#ifndef _LIBCPP_NO_RTTI + +template<class _F, class _Alloc, class _R, class _A0, class _A1> +const void* +__func<_F, _Alloc, _R(_A0, _A1)>::target(const type_info& __ti) const +{ + if (__ti == typeid(_F)) + return &__f_.first(); + return (const void*)0; +} + +template<class _F, class _Alloc, class _R, class _A0, class _A1> +const std::type_info& +__func<_F, _Alloc, _R(_A0, _A1)>::target_type() const +{ + return typeid(_F); +} + +#endif // _LIBCPP_NO_RTTI + +template<class _F, class _Alloc, class _R, class _A0, class _A1, class _A2> +class __func<_F, _Alloc, _R(_A0, _A1, _A2)> + : public __base<_R(_A0, _A1, _A2)> +{ + __compressed_pair<_F, _Alloc> __f_; +public: + _LIBCPP_INLINE_VISIBILITY explicit __func(_F __f) : __f_(_VSTD::move(__f)) {} + _LIBCPP_INLINE_VISIBILITY explicit __func(_F __f, _Alloc __a) + : __f_(_VSTD::move(__f), _VSTD::move(__a)) {} + virtual __base<_R(_A0, _A1, _A2)>* __clone() const; + virtual void __clone(__base<_R(_A0, _A1, _A2)>*) const; + virtual void destroy(); + virtual void destroy_deallocate(); + virtual _R operator()(_A0, _A1, _A2); +#ifndef _LIBCPP_NO_RTTI + virtual const void* target(const type_info&) const; + virtual const std::type_info& target_type() const; +#endif // _LIBCPP_NO_RTTI +}; + +template<class _F, class _Alloc, class _R, class _A0, class _A1, class _A2> +__base<_R(_A0, _A1, _A2)>* +__func<_F, _Alloc, _R(_A0, _A1, _A2)>::__clone() const +{ + typedef typename _Alloc::template rebind<__func>::other _A; + _A __a(__f_.second()); + typedef __allocator_destructor<_A> _D; + unique_ptr<__func, _D> __hold(__a.allocate(1), _D(__a, 1)); + ::new (__hold.get()) __func(__f_.first(), _Alloc(__a)); + return __hold.release(); +} + +template<class _F, class _Alloc, class _R, class _A0, class _A1, class _A2> +void +__func<_F, _Alloc, _R(_A0, _A1, _A2)>::__clone(__base<_R(_A0, _A1, _A2)>* __p) const +{ + ::new (__p) __func(__f_.first(), __f_.second()); +} + +template<class _F, class _Alloc, class _R, class _A0, class _A1, class _A2> +void +__func<_F, _Alloc, _R(_A0, _A1, _A2)>::destroy() +{ + __f_.~__compressed_pair<_F, _Alloc>(); +} + +template<class _F, class _Alloc, class _R, class _A0, class _A1, class _A2> +void +__func<_F, _Alloc, _R(_A0, _A1, _A2)>::destroy_deallocate() +{ + typedef typename _Alloc::template rebind<__func>::other _A; + _A __a(__f_.second()); + __f_.~__compressed_pair<_F, _Alloc>(); + __a.deallocate(this, 1); +} + +template<class _F, class _Alloc, class _R, class _A0, class _A1, class _A2> +_R +__func<_F, _Alloc, _R(_A0, _A1, _A2)>::operator()(_A0 __a0, _A1 __a1, _A2 __a2) +{ + return __invoke(__f_.first(), __a0, __a1, __a2); +} + +#ifndef _LIBCPP_NO_RTTI + +template<class _F, class _Alloc, class _R, class _A0, class _A1, class _A2> +const void* +__func<_F, _Alloc, _R(_A0, _A1, _A2)>::target(const type_info& __ti) const +{ + if (__ti == typeid(_F)) + return &__f_.first(); + return (const void*)0; +} + +template<class _F, class _Alloc, class _R, class _A0, class _A1, class _A2> +const std::type_info& +__func<_F, _Alloc, _R(_A0, _A1, _A2)>::target_type() const +{ + return typeid(_F); +} + +#endif // _LIBCPP_NO_RTTI + +} // __function + +template<class _R> +class _LIBCPP_VISIBLE function<_R()> +{ + typedef __function::__base<_R()> __base; + aligned_storage<3*sizeof(void*)>::type __buf_; + __base* __f_; + + template <class _F> + static bool __not_null(const _F&) {return true;} + template <class _R2> + static bool __not_null(const function<_R()>& __p) {return __p;} +public: + typedef _R result_type; + + // 20.7.16.2.1, construct/copy/destroy: + _LIBCPP_INLINE_VISIBILITY explicit function() : __f_(0) {} + _LIBCPP_INLINE_VISIBILITY function(nullptr_t) : __f_(0) {} + function(const function&); + template<class _F> + function(_F, + typename enable_if<!is_integral<_F>::value>::type* = 0); + + template<class _Alloc> + _LIBCPP_INLINE_VISIBILITY + function(allocator_arg_t, const _Alloc&) : __f_(0) {} + template<class _Alloc> + _LIBCPP_INLINE_VISIBILITY + function(allocator_arg_t, const _Alloc&, nullptr_t) : __f_(0) {} + template<class _Alloc> + function(allocator_arg_t, const _Alloc&, const function&); + template<class _F, class _Alloc> + function(allocator_arg_t, const _Alloc& __a, _F __f, + typename enable_if<!is_integral<_F>::value>::type* = 0); + + function& operator=(const function&); + function& operator=(nullptr_t); + template<class _F> + typename enable_if + < + !is_integral<_F>::value, + function& + >::type + operator=(_F); + + ~function(); + + // 20.7.16.2.2, function modifiers: + void swap(function&); + template<class _F, class _Alloc> + _LIBCPP_INLINE_VISIBILITY + void assign(_F __f, const _Alloc& __a) + {function(allocator_arg, __a, __f).swap(*this);} + + // 20.7.16.2.3, function capacity: + _LIBCPP_INLINE_VISIBILITY operator bool() const {return __f_;} + +private: + // deleted overloads close possible hole in the type system + template<class _R2> + bool operator==(const function<_R2()>&) const;// = delete; + template<class _R2> + bool operator!=(const function<_R2()>&) const;// = delete; +public: + // 20.7.16.2.4, function invocation: + _R operator()() const; + +#ifndef _LIBCPP_NO_RTTI + // 20.7.16.2.5, function target access: + const std::type_info& target_type() const; + template <typename _T> _T* target(); + template <typename _T> const _T* target() const; +#endif // _LIBCPP_NO_RTTI +}; + +template<class _R> +function<_R()>::function(const function& __f) +{ + if (__f.__f_ == 0) + __f_ = 0; + else if (__f.__f_ == (const __base*)&__f.__buf_) + { + __f_ = (__base*)&__buf_; + __f.__f_->__clone(__f_); + } + else + __f_ = __f.__f_->__clone(); +} + +template<class _R> +template<class _Alloc> +function<_R()>::function(allocator_arg_t, const _Alloc&, const function& __f) +{ + if (__f.__f_ == 0) + __f_ = 0; + else if (__f.__f_ == (const __base*)&__f.__buf_) + { + __f_ = (__base*)&__buf_; + __f.__f_->__clone(__f_); + } + else + __f_ = __f.__f_->__clone(); +} + +template<class _R> +template <class _F> +function<_R()>::function(_F __f, + typename enable_if<!is_integral<_F>::value>::type*) + : __f_(0) +{ + if (__not_null(__f)) + { + typedef __function::__func<_F, allocator<_F>, _R()> _FF; + if (sizeof(_FF) <= sizeof(__buf_)) + { + __f_ = (__base*)&__buf_; + ::new (__f_) _FF(__f); + } + else + { + typedef allocator<_FF> _A; + _A __a; + typedef __allocator_destructor<_A> _D; + unique_ptr<__base, _D> __hold(__a.allocate(1), _D(__a, 1)); + ::new (__hold.get()) _FF(__f, allocator<_F>(__a)); + __f_ = __hold.release(); + } + } +} + +template<class _R> +template <class _F, class _Alloc> +function<_R()>::function(allocator_arg_t, const _Alloc& __a0, _F __f, + typename enable_if<!is_integral<_F>::value>::type*) + : __f_(0) +{ + typedef allocator_traits<_Alloc> __alloc_traits; + if (__not_null(__f)) + { + typedef __function::__func<_F, _Alloc, _R()> _FF; + if (sizeof(_FF) <= sizeof(__buf_)) + { + __f_ = (__base*)&__buf_; + ::new (__f_) _FF(__f); + } + else + { + typedef typename __alloc_traits::template +#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES + rebind_alloc<_FF> +#else + rebind_alloc<_FF>::other +#endif + _A; + _A __a(__a0); + typedef __allocator_destructor<_A> _D; + unique_ptr<__base, _D> __hold(__a.allocate(1), _D(__a, 1)); + ::new (__hold.get()) _FF(__f, _Alloc(__a)); + __f_ = __hold.release(); + } + } +} + +template<class _R> +function<_R()>& +function<_R()>::operator=(const function& __f) +{ + function(__f).swap(*this); + return *this; +} + +template<class _R> +function<_R()>& +function<_R()>::operator=(nullptr_t) +{ + if (__f_ == (__base*)&__buf_) + __f_->destroy(); + else if (__f_) + __f_->destroy_deallocate(); + __f_ = 0; +} + +template<class _R> +template <class _F> +typename enable_if +< + !is_integral<_F>::value, + function<_R()>& +>::type +function<_R()>::operator=(_F __f) +{ + function(_VSTD::move(__f)).swap(*this); + return *this; +} + +template<class _R> +function<_R()>::~function() +{ + if (__f_ == (__base*)&__buf_) + __f_->destroy(); + else if (__f_) + __f_->destroy_deallocate(); +} + +template<class _R> +void +function<_R()>::swap(function& __f) +{ + if (__f_ == (__base*)&__buf_ && __f.__f_ == (__base*)&__f.__buf_) + { + typename aligned_storage<sizeof(__buf_)>::type __tempbuf; + __base* __t = (__base*)&__tempbuf; + __f_->__clone(__t); + __f_->destroy(); + __f_ = 0; + __f.__f_->__clone((__base*)&__buf_); + __f.__f_->destroy(); + __f.__f_ = 0; + __f_ = (__base*)&__buf_; + __t->__clone((__base*)&__f.__buf_); + __t->destroy(); + __f.__f_ = (__base*)&__f.__buf_; + } + else if (__f_ == (__base*)&__buf_) + { + __f_->__clone((__base*)&__f.__buf_); + __f_->destroy(); + __f_ = __f.__f_; + __f.__f_ = (__base*)&__f.__buf_; + } + else if (__f.__f_ == (__base*)&__f.__buf_) + { + __f.__f_->__clone((__base*)&__buf_); + __f.__f_->destroy(); + __f.__f_ = __f_; + __f_ = (__base*)&__buf_; + } + else + _VSTD::swap(__f_, __f.__f_); +} + +template<class _R> +_R +function<_R()>::operator()() const +{ +#ifndef _LIBCPP_NO_EXCEPTIONS + if (__f_ == 0) + throw bad_function_call(); +#endif // _LIBCPP_NO_EXCEPTIONS + return (*__f_)(); +} + +#ifndef _LIBCPP_NO_RTTI + +template<class _R> +const std::type_info& +function<_R()>::target_type() const +{ + if (__f_ == 0) + return typeid(void); + return __f_->target_type(); +} + +template<class _R> +template <typename _T> +_T* +function<_R()>::target() +{ + if (__f_ == 0) + return (_T*)0; + return (_T*)__f_->target(typeid(_T)); +} + +template<class _R> +template <typename _T> +const _T* +function<_R()>::target() const +{ + if (__f_ == 0) + return (const _T*)0; + return (const _T*)__f_->target(typeid(_T)); +} + +#endif // _LIBCPP_NO_RTTI + +template<class _R, class _A0> +class _LIBCPP_VISIBLE function<_R(_A0)> + : public unary_function<_A0, _R> +{ + typedef __function::__base<_R(_A0)> __base; + aligned_storage<3*sizeof(void*)>::type __buf_; + __base* __f_; + + template <class _F> + _LIBCPP_INLINE_VISIBILITY + static bool __not_null(const _F&) {return true;} + template <class _R2, class _B0> + _LIBCPP_INLINE_VISIBILITY + static bool __not_null(_R2 (*__p)(_B0)) {return __p;} + template <class _R2, class _C> + _LIBCPP_INLINE_VISIBILITY + static bool __not_null(_R2 (_C::*__p)()) {return __p;} + template <class _R2, class _C> + _LIBCPP_INLINE_VISIBILITY + static bool __not_null(_R2 (_C::*__p)() const) {return __p;} + template <class _R2, class _C> + _LIBCPP_INLINE_VISIBILITY + static bool __not_null(_R2 (_C::*__p)() volatile) {return __p;} + template <class _R2, class _C> + _LIBCPP_INLINE_VISIBILITY + static bool __not_null(_R2 (_C::*__p)() const volatile) {return __p;} + template <class _R2, class _B0> + _LIBCPP_INLINE_VISIBILITY + static bool __not_null(const function<_R(_B0)>& __p) {return __p;} +public: + typedef _R result_type; + + // 20.7.16.2.1, construct/copy/destroy: + _LIBCPP_INLINE_VISIBILITY explicit function() : __f_(0) {} + _LIBCPP_INLINE_VISIBILITY function(nullptr_t) : __f_(0) {} + function(const function&); + template<class _F> + function(_F, + typename enable_if<!is_integral<_F>::value>::type* = 0); + + template<class _Alloc> + _LIBCPP_INLINE_VISIBILITY + function(allocator_arg_t, const _Alloc&) : __f_(0) {} + template<class _Alloc> + _LIBCPP_INLINE_VISIBILITY + function(allocator_arg_t, const _Alloc&, nullptr_t) : __f_(0) {} + template<class _Alloc> + function(allocator_arg_t, const _Alloc&, const function&); + template<class _F, class _Alloc> + function(allocator_arg_t, const _Alloc& __a, _F __f, + typename enable_if<!is_integral<_F>::value>::type* = 0); + + function& operator=(const function&); + function& operator=(nullptr_t); + template<class _F> + typename enable_if + < + !is_integral<_F>::value, + function& + >::type + operator=(_F); + + ~function(); + + // 20.7.16.2.2, function modifiers: + void swap(function&); + template<class _F, class _Alloc> + _LIBCPP_INLINE_VISIBILITY + void assign(_F __f, const _Alloc& __a) + {function(allocator_arg, __a, __f).swap(*this);} + + // 20.7.16.2.3, function capacity: + _LIBCPP_INLINE_VISIBILITY operator bool() const {return __f_;} + +private: + // deleted overloads close possible hole in the type system + template<class _R2, class _B0> + bool operator==(const function<_R2(_B0)>&) const;// = delete; + template<class _R2, class _B0> + bool operator!=(const function<_R2(_B0)>&) const;// = delete; +public: + // 20.7.16.2.4, function invocation: + _R operator()(_A0) const; + +#ifndef _LIBCPP_NO_RTTI + // 20.7.16.2.5, function target access: + const std::type_info& target_type() const; + template <typename _T> _T* target(); + template <typename _T> const _T* target() const; +#endif // _LIBCPP_NO_RTTI +}; + +template<class _R, class _A0> +function<_R(_A0)>::function(const function& __f) +{ + if (__f.__f_ == 0) + __f_ = 0; + else if (__f.__f_ == (const __base*)&__f.__buf_) + { + __f_ = (__base*)&__buf_; + __f.__f_->__clone(__f_); + } + else + __f_ = __f.__f_->__clone(); +} + +template<class _R, class _A0> +template<class _Alloc> +function<_R(_A0)>::function(allocator_arg_t, const _Alloc&, const function& __f) +{ + if (__f.__f_ == 0) + __f_ = 0; + else if (__f.__f_ == (const __base*)&__f.__buf_) + { + __f_ = (__base*)&__buf_; + __f.__f_->__clone(__f_); + } + else + __f_ = __f.__f_->__clone(); +} + +template<class _R, class _A0> +template <class _F> +function<_R(_A0)>::function(_F __f, + typename enable_if<!is_integral<_F>::value>::type*) + : __f_(0) +{ + if (__not_null(__f)) + { + typedef __function::__func<_F, allocator<_F>, _R(_A0)> _FF; + if (sizeof(_FF) <= sizeof(__buf_)) + { + __f_ = (__base*)&__buf_; + ::new (__f_) _FF(__f); + } + else + { + typedef allocator<_FF> _A; + _A __a; + typedef __allocator_destructor<_A> _D; + unique_ptr<__base, _D> __hold(__a.allocate(1), _D(__a, 1)); + ::new (__hold.get()) _FF(__f, allocator<_F>(__a)); + __f_ = __hold.release(); + } + } +} + +template<class _R, class _A0> +template <class _F, class _Alloc> +function<_R(_A0)>::function(allocator_arg_t, const _Alloc& __a0, _F __f, + typename enable_if<!is_integral<_F>::value>::type*) + : __f_(0) +{ + typedef allocator_traits<_Alloc> __alloc_traits; + if (__not_null(__f)) + { + typedef __function::__func<_F, _Alloc, _R(_A0)> _FF; + if (sizeof(_FF) <= sizeof(__buf_)) + { + __f_ = (__base*)&__buf_; + ::new (__f_) _FF(__f); + } + else + { + typedef typename __alloc_traits::template +#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES + rebind_alloc<_FF> +#else + rebind_alloc<_FF>::other +#endif + _A; + _A __a(__a0); + typedef __allocator_destructor<_A> _D; + unique_ptr<__base, _D> __hold(__a.allocate(1), _D(__a, 1)); + ::new (__hold.get()) _FF(__f, _Alloc(__a)); + __f_ = __hold.release(); + } + } +} + +template<class _R, class _A0> +function<_R(_A0)>& +function<_R(_A0)>::operator=(const function& __f) +{ + function(__f).swap(*this); + return *this; +} + +template<class _R, class _A0> +function<_R(_A0)>& +function<_R(_A0)>::operator=(nullptr_t) +{ + if (__f_ == (__base*)&__buf_) + __f_->destroy(); + else if (__f_) + __f_->destroy_deallocate(); + __f_ = 0; +} + +template<class _R, class _A0> +template <class _F> +typename enable_if +< + !is_integral<_F>::value, + function<_R(_A0)>& +>::type +function<_R(_A0)>::operator=(_F __f) +{ + function(_VSTD::move(__f)).swap(*this); + return *this; +} + +template<class _R, class _A0> +function<_R(_A0)>::~function() +{ + if (__f_ == (__base*)&__buf_) + __f_->destroy(); + else if (__f_) + __f_->destroy_deallocate(); +} + +template<class _R, class _A0> +void +function<_R(_A0)>::swap(function& __f) +{ + if (__f_ == (__base*)&__buf_ && __f.__f_ == (__base*)&__f.__buf_) + { + typename aligned_storage<sizeof(__buf_)>::type __tempbuf; + __base* __t = (__base*)&__tempbuf; + __f_->__clone(__t); + __f_->destroy(); + __f_ = 0; + __f.__f_->__clone((__base*)&__buf_); + __f.__f_->destroy(); + __f.__f_ = 0; + __f_ = (__base*)&__buf_; + __t->__clone((__base*)&__f.__buf_); + __t->destroy(); + __f.__f_ = (__base*)&__f.__buf_; + } + else if (__f_ == (__base*)&__buf_) + { + __f_->__clone((__base*)&__f.__buf_); + __f_->destroy(); + __f_ = __f.__f_; + __f.__f_ = (__base*)&__f.__buf_; + } + else if (__f.__f_ == (__base*)&__f.__buf_) + { + __f.__f_->__clone((__base*)&__buf_); + __f.__f_->destroy(); + __f.__f_ = __f_; + __f_ = (__base*)&__buf_; + } + else + _VSTD::swap(__f_, __f.__f_); +} + +template<class _R, class _A0> +_R +function<_R(_A0)>::operator()(_A0 __a0) const +{ +#ifndef _LIBCPP_NO_EXCEPTIONS + if (__f_ == 0) + throw bad_function_call(); +#endif // _LIBCPP_NO_EXCEPTIONS + return (*__f_)(__a0); +} + +#ifndef _LIBCPP_NO_RTTI + +template<class _R, class _A0> +const std::type_info& +function<_R(_A0)>::target_type() const +{ + if (__f_ == 0) + return typeid(void); + return __f_->target_type(); +} + +template<class _R, class _A0> +template <typename _T> +_T* +function<_R(_A0)>::target() +{ + if (__f_ == 0) + return (_T*)0; + return (_T*)__f_->target(typeid(_T)); +} + +template<class _R, class _A0> +template <typename _T> +const _T* +function<_R(_A0)>::target() const +{ + if (__f_ == 0) + return (const _T*)0; + return (const _T*)__f_->target(typeid(_T)); +} + +#endif // _LIBCPP_NO_RTTI + +template<class _R, class _A0, class _A1> +class _LIBCPP_VISIBLE function<_R(_A0, _A1)> + : public binary_function<_A0, _A1, _R> +{ + typedef __function::__base<_R(_A0, _A1)> __base; + aligned_storage<3*sizeof(void*)>::type __buf_; + __base* __f_; + + template <class _F> + _LIBCPP_INLINE_VISIBILITY + static bool __not_null(const _F&) {return true;} + template <class _R2, class _B0, class _B1> + _LIBCPP_INLINE_VISIBILITY + static bool __not_null(_R2 (*__p)(_B0, _B1)) {return __p;} + template <class _R2, class _C, class _B1> + _LIBCPP_INLINE_VISIBILITY + static bool __not_null(_R2 (_C::*__p)(_B1)) {return __p;} + template <class _R2, class _C, class _B1> + _LIBCPP_INLINE_VISIBILITY + static bool __not_null(_R2 (_C::*__p)(_B1) const) {return __p;} + template <class _R2, class _C, class _B1> + _LIBCPP_INLINE_VISIBILITY + static bool __not_null(_R2 (_C::*__p)(_B1) volatile) {return __p;} + template <class _R2, class _C, class _B1> + _LIBCPP_INLINE_VISIBILITY + static bool __not_null(_R2 (_C::*__p)(_B1) const volatile) {return __p;} + template <class _R2, class _B0, class _B1> + _LIBCPP_INLINE_VISIBILITY + static bool __not_null(const function<_R(_B0, _B1)>& __p) {return __p;} +public: + typedef _R result_type; + + // 20.7.16.2.1, construct/copy/destroy: + _LIBCPP_INLINE_VISIBILITY explicit function() : __f_(0) {} + _LIBCPP_INLINE_VISIBILITY function(nullptr_t) : __f_(0) {} + function(const function&); + template<class _F> + function(_F, + typename enable_if<!is_integral<_F>::value>::type* = 0); + + template<class _Alloc> + _LIBCPP_INLINE_VISIBILITY + function(allocator_arg_t, const _Alloc&) : __f_(0) {} + template<class _Alloc> + _LIBCPP_INLINE_VISIBILITY + function(allocator_arg_t, const _Alloc&, nullptr_t) : __f_(0) {} + template<class _Alloc> + function(allocator_arg_t, const _Alloc&, const function&); + template<class _F, class _Alloc> + function(allocator_arg_t, const _Alloc& __a, _F __f, + typename enable_if<!is_integral<_F>::value>::type* = 0); + + function& operator=(const function&); + function& operator=(nullptr_t); + template<class _F> + typename enable_if + < + !is_integral<_F>::value, + function& + >::type + operator=(_F); + + ~function(); + + // 20.7.16.2.2, function modifiers: + void swap(function&); + template<class _F, class _Alloc> + _LIBCPP_INLINE_VISIBILITY + void assign(_F __f, const _Alloc& __a) + {function(allocator_arg, __a, __f).swap(*this);} + + // 20.7.16.2.3, function capacity: + operator bool() const {return __f_;} + +private: + // deleted overloads close possible hole in the type system + template<class _R2, class _B0, class _B1> + bool operator==(const function<_R2(_B0, _B1)>&) const;// = delete; + template<class _R2, class _B0, class _B1> + bool operator!=(const function<_R2(_B0, _B1)>&) const;// = delete; +public: + // 20.7.16.2.4, function invocation: + _R operator()(_A0, _A1) const; + +#ifndef _LIBCPP_NO_RTTI + // 20.7.16.2.5, function target access: + const std::type_info& target_type() const; + template <typename _T> _T* target(); + template <typename _T> const _T* target() const; +#endif // _LIBCPP_NO_RTTI +}; + +template<class _R, class _A0, class _A1> +function<_R(_A0, _A1)>::function(const function& __f) +{ + if (__f.__f_ == 0) + __f_ = 0; + else if (__f.__f_ == (const __base*)&__f.__buf_) + { + __f_ = (__base*)&__buf_; + __f.__f_->__clone(__f_); + } + else + __f_ = __f.__f_->__clone(); +} + +template<class _R, class _A0, class _A1> +template<class _Alloc> +function<_R(_A0, _A1)>::function(allocator_arg_t, const _Alloc&, const function& __f) +{ + if (__f.__f_ == 0) + __f_ = 0; + else if (__f.__f_ == (const __base*)&__f.__buf_) + { + __f_ = (__base*)&__buf_; + __f.__f_->__clone(__f_); + } + else + __f_ = __f.__f_->__clone(); +} + +template<class _R, class _A0, class _A1> +template <class _F> +function<_R(_A0, _A1)>::function(_F __f, + typename enable_if<!is_integral<_F>::value>::type*) + : __f_(0) +{ + if (__not_null(__f)) + { + typedef __function::__func<_F, allocator<_F>, _R(_A0, _A1)> _FF; + if (sizeof(_FF) <= sizeof(__buf_)) + { + __f_ = (__base*)&__buf_; + ::new (__f_) _FF(__f); + } + else + { + typedef allocator<_FF> _A; + _A __a; + typedef __allocator_destructor<_A> _D; + unique_ptr<__base, _D> __hold(__a.allocate(1), _D(__a, 1)); + ::new (__hold.get()) _FF(__f, allocator<_F>(__a)); + __f_ = __hold.release(); + } + } +} + +template<class _R, class _A0, class _A1> +template <class _F, class _Alloc> +function<_R(_A0, _A1)>::function(allocator_arg_t, const _Alloc& __a0, _F __f, + typename enable_if<!is_integral<_F>::value>::type*) + : __f_(0) +{ + typedef allocator_traits<_Alloc> __alloc_traits; + if (__not_null(__f)) + { + typedef __function::__func<_F, _Alloc, _R(_A0, _A1)> _FF; + if (sizeof(_FF) <= sizeof(__buf_)) + { + __f_ = (__base*)&__buf_; + ::new (__f_) _FF(__f); + } + else + { + typedef typename __alloc_traits::template +#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES + rebind_alloc<_FF> +#else + rebind_alloc<_FF>::other +#endif + _A; + _A __a(__a0); + typedef __allocator_destructor<_A> _D; + unique_ptr<__base, _D> __hold(__a.allocate(1), _D(__a, 1)); + ::new (__hold.get()) _FF(__f, _Alloc(__a)); + __f_ = __hold.release(); + } + } +} + +template<class _R, class _A0, class _A1> +function<_R(_A0, _A1)>& +function<_R(_A0, _A1)>::operator=(const function& __f) +{ + function(__f).swap(*this); + return *this; +} + +template<class _R, class _A0, class _A1> +function<_R(_A0, _A1)>& +function<_R(_A0, _A1)>::operator=(nullptr_t) +{ + if (__f_ == (__base*)&__buf_) + __f_->destroy(); + else if (__f_) + __f_->destroy_deallocate(); + __f_ = 0; +} + +template<class _R, class _A0, class _A1> +template <class _F> +typename enable_if +< + !is_integral<_F>::value, + function<_R(_A0, _A1)>& +>::type +function<_R(_A0, _A1)>::operator=(_F __f) +{ + function(_VSTD::move(__f)).swap(*this); + return *this; +} + +template<class _R, class _A0, class _A1> +function<_R(_A0, _A1)>::~function() +{ + if (__f_ == (__base*)&__buf_) + __f_->destroy(); + else if (__f_) + __f_->destroy_deallocate(); +} + +template<class _R, class _A0, class _A1> +void +function<_R(_A0, _A1)>::swap(function& __f) +{ + if (__f_ == (__base*)&__buf_ && __f.__f_ == (__base*)&__f.__buf_) + { + typename aligned_storage<sizeof(__buf_)>::type __tempbuf; + __base* __t = (__base*)&__tempbuf; + __f_->__clone(__t); + __f_->destroy(); + __f_ = 0; + __f.__f_->__clone((__base*)&__buf_); + __f.__f_->destroy(); + __f.__f_ = 0; + __f_ = (__base*)&__buf_; + __t->__clone((__base*)&__f.__buf_); + __t->destroy(); + __f.__f_ = (__base*)&__f.__buf_; + } + else if (__f_ == (__base*)&__buf_) + { + __f_->__clone((__base*)&__f.__buf_); + __f_->destroy(); + __f_ = __f.__f_; + __f.__f_ = (__base*)&__f.__buf_; + } + else if (__f.__f_ == (__base*)&__f.__buf_) + { + __f.__f_->__clone((__base*)&__buf_); + __f.__f_->destroy(); + __f.__f_ = __f_; + __f_ = (__base*)&__buf_; + } + else + _VSTD::swap(__f_, __f.__f_); +} + +template<class _R, class _A0, class _A1> +_R +function<_R(_A0, _A1)>::operator()(_A0 __a0, _A1 __a1) const +{ +#ifndef _LIBCPP_NO_EXCEPTIONS + if (__f_ == 0) + throw bad_function_call(); +#endif // _LIBCPP_NO_EXCEPTIONS + return (*__f_)(__a0, __a1); +} + +#ifndef _LIBCPP_NO_RTTI + +template<class _R, class _A0, class _A1> +const std::type_info& +function<_R(_A0, _A1)>::target_type() const +{ + if (__f_ == 0) + return typeid(void); + return __f_->target_type(); +} + +template<class _R, class _A0, class _A1> +template <typename _T> +_T* +function<_R(_A0, _A1)>::target() +{ + if (__f_ == 0) + return (_T*)0; + return (_T*)__f_->target(typeid(_T)); +} + +template<class _R, class _A0, class _A1> +template <typename _T> +const _T* +function<_R(_A0, _A1)>::target() const +{ + if (__f_ == 0) + return (const _T*)0; + return (const _T*)__f_->target(typeid(_T)); +} + +#endif // _LIBCPP_NO_RTTI + +template<class _R, class _A0, class _A1, class _A2> +class _LIBCPP_VISIBLE function<_R(_A0, _A1, _A2)> +{ + typedef __function::__base<_R(_A0, _A1, _A2)> __base; + aligned_storage<3*sizeof(void*)>::type __buf_; + __base* __f_; + + template <class _F> + _LIBCPP_INLINE_VISIBILITY + static bool __not_null(const _F&) {return true;} + template <class _R2, class _B0, class _B1, class _B2> + _LIBCPP_INLINE_VISIBILITY + static bool __not_null(_R2 (*__p)(_B0, _B1, _B2)) {return __p;} + template <class _R2, class _C, class _B1, class _B2> + _LIBCPP_INLINE_VISIBILITY + static bool __not_null(_R2 (_C::*__p)(_B1, _B2)) {return __p;} + template <class _R2, class _C, class _B1, class _B2> + _LIBCPP_INLINE_VISIBILITY + static bool __not_null(_R2 (_C::*__p)(_B1, _B2) const) {return __p;} + template <class _R2, class _C, class _B1, class _B2> + _LIBCPP_INLINE_VISIBILITY + static bool __not_null(_R2 (_C::*__p)(_B1, _B2) volatile) {return __p;} + template <class _R2, class _C, class _B1, class _B2> + _LIBCPP_INLINE_VISIBILITY + static bool __not_null(_R2 (_C::*__p)(_B1, _B2) const volatile) {return __p;} + template <class _R2, class _B0, class _B1, class _B2> + _LIBCPP_INLINE_VISIBILITY + static bool __not_null(const function<_R(_B0, _B1, _B2)>& __p) {return __p;} +public: + typedef _R result_type; + + // 20.7.16.2.1, construct/copy/destroy: + _LIBCPP_INLINE_VISIBILITY explicit function() : __f_(0) {} + _LIBCPP_INLINE_VISIBILITY function(nullptr_t) : __f_(0) {} + function(const function&); + template<class _F> + function(_F, + typename enable_if<!is_integral<_F>::value>::type* = 0); + + template<class _Alloc> + _LIBCPP_INLINE_VISIBILITY + function(allocator_arg_t, const _Alloc&) : __f_(0) {} + template<class _Alloc> + _LIBCPP_INLINE_VISIBILITY + function(allocator_arg_t, const _Alloc&, nullptr_t) : __f_(0) {} + template<class _Alloc> + function(allocator_arg_t, const _Alloc&, const function&); + template<class _F, class _Alloc> + function(allocator_arg_t, const _Alloc& __a, _F __f, + typename enable_if<!is_integral<_F>::value>::type* = 0); + + function& operator=(const function&); + function& operator=(nullptr_t); + template<class _F> + typename enable_if + < + !is_integral<_F>::value, + function& + >::type + operator=(_F); + + ~function(); + + // 20.7.16.2.2, function modifiers: + void swap(function&); + template<class _F, class _Alloc> + _LIBCPP_INLINE_VISIBILITY + void assign(_F __f, const _Alloc& __a) + {function(allocator_arg, __a, __f).swap(*this);} + + // 20.7.16.2.3, function capacity: + _LIBCPP_INLINE_VISIBILITY operator bool() const {return __f_;} + +private: + // deleted overloads close possible hole in the type system + template<class _R2, class _B0, class _B1, class _B2> + bool operator==(const function<_R2(_B0, _B1, _B2)>&) const;// = delete; + template<class _R2, class _B0, class _B1, class _B2> + bool operator!=(const function<_R2(_B0, _B1, _B2)>&) const;// = delete; +public: + // 20.7.16.2.4, function invocation: + _R operator()(_A0, _A1, _A2) const; + +#ifndef _LIBCPP_NO_RTTI + // 20.7.16.2.5, function target access: + const std::type_info& target_type() const; + template <typename _T> _T* target(); + template <typename _T> const _T* target() const; +#endif // _LIBCPP_NO_RTTI +}; + +template<class _R, class _A0, class _A1, class _A2> +function<_R(_A0, _A1, _A2)>::function(const function& __f) +{ + if (__f.__f_ == 0) + __f_ = 0; + else if (__f.__f_ == (const __base*)&__f.__buf_) + { + __f_ = (__base*)&__buf_; + __f.__f_->__clone(__f_); + } + else + __f_ = __f.__f_->__clone(); +} + +template<class _R, class _A0, class _A1, class _A2> +template<class _Alloc> +function<_R(_A0, _A1, _A2)>::function(allocator_arg_t, const _Alloc&, + const function& __f) +{ + if (__f.__f_ == 0) + __f_ = 0; + else if (__f.__f_ == (const __base*)&__f.__buf_) + { + __f_ = (__base*)&__buf_; + __f.__f_->__clone(__f_); + } + else + __f_ = __f.__f_->__clone(); +} + +template<class _R, class _A0, class _A1, class _A2> +template <class _F> +function<_R(_A0, _A1, _A2)>::function(_F __f, + typename enable_if<!is_integral<_F>::value>::type*) + : __f_(0) +{ + if (__not_null(__f)) + { + typedef __function::__func<_F, allocator<_F>, _R(_A0, _A1, _A2)> _FF; + if (sizeof(_FF) <= sizeof(__buf_)) + { + __f_ = (__base*)&__buf_; + ::new (__f_) _FF(__f); + } + else + { + typedef allocator<_FF> _A; + _A __a; + typedef __allocator_destructor<_A> _D; + unique_ptr<__base, _D> __hold(__a.allocate(1), _D(__a, 1)); + ::new (__hold.get()) _FF(__f, allocator<_F>(__a)); + __f_ = __hold.release(); + } + } +} + +template<class _R, class _A0, class _A1, class _A2> +template <class _F, class _Alloc> +function<_R(_A0, _A1, _A2)>::function(allocator_arg_t, const _Alloc& __a0, _F __f, + typename enable_if<!is_integral<_F>::value>::type*) + : __f_(0) +{ + typedef allocator_traits<_Alloc> __alloc_traits; + if (__not_null(__f)) + { + typedef __function::__func<_F, _Alloc, _R(_A0, _A1, _A2)> _FF; + if (sizeof(_FF) <= sizeof(__buf_)) + { + __f_ = (__base*)&__buf_; + ::new (__f_) _FF(__f); + } + else + { + typedef typename __alloc_traits::template +#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES + rebind_alloc<_FF> +#else + rebind_alloc<_FF>::other +#endif + _A; + _A __a(__a0); + typedef __allocator_destructor<_A> _D; + unique_ptr<__base, _D> __hold(__a.allocate(1), _D(__a, 1)); + ::new (__hold.get()) _FF(__f, _Alloc(__a)); + __f_ = __hold.release(); + } + } +} + +template<class _R, class _A0, class _A1, class _A2> +function<_R(_A0, _A1, _A2)>& +function<_R(_A0, _A1, _A2)>::operator=(const function& __f) +{ + function(__f).swap(*this); + return *this; +} + +template<class _R, class _A0, class _A1, class _A2> +function<_R(_A0, _A1, _A2)>& +function<_R(_A0, _A1, _A2)>::operator=(nullptr_t) +{ + if (__f_ == (__base*)&__buf_) + __f_->destroy(); + else if (__f_) + __f_->destroy_deallocate(); + __f_ = 0; +} + +template<class _R, class _A0, class _A1, class _A2> +template <class _F> +typename enable_if +< + !is_integral<_F>::value, + function<_R(_A0, _A1, _A2)>& +>::type +function<_R(_A0, _A1, _A2)>::operator=(_F __f) +{ + function(_VSTD::move(__f)).swap(*this); + return *this; +} + +template<class _R, class _A0, class _A1, class _A2> +function<_R(_A0, _A1, _A2)>::~function() +{ + if (__f_ == (__base*)&__buf_) + __f_->destroy(); + else if (__f_) + __f_->destroy_deallocate(); +} + +template<class _R, class _A0, class _A1, class _A2> +void +function<_R(_A0, _A1, _A2)>::swap(function& __f) +{ + if (__f_ == (__base*)&__buf_ && __f.__f_ == (__base*)&__f.__buf_) + { + typename aligned_storage<sizeof(__buf_)>::type __tempbuf; + __base* __t = (__base*)&__tempbuf; + __f_->__clone(__t); + __f_->destroy(); + __f_ = 0; + __f.__f_->__clone((__base*)&__buf_); + __f.__f_->destroy(); + __f.__f_ = 0; + __f_ = (__base*)&__buf_; + __t->__clone((__base*)&__f.__buf_); + __t->destroy(); + __f.__f_ = (__base*)&__f.__buf_; + } + else if (__f_ == (__base*)&__buf_) + { + __f_->__clone((__base*)&__f.__buf_); + __f_->destroy(); + __f_ = __f.__f_; + __f.__f_ = (__base*)&__f.__buf_; + } + else if (__f.__f_ == (__base*)&__f.__buf_) + { + __f.__f_->__clone((__base*)&__buf_); + __f.__f_->destroy(); + __f.__f_ = __f_; + __f_ = (__base*)&__buf_; + } + else + _VSTD::swap(__f_, __f.__f_); +} + +template<class _R, class _A0, class _A1, class _A2> +_R +function<_R(_A0, _A1, _A2)>::operator()(_A0 __a0, _A1 __a1, _A2 __a2) const +{ +#ifndef _LIBCPP_NO_EXCEPTIONS + if (__f_ == 0) + throw bad_function_call(); +#endif // _LIBCPP_NO_EXCEPTIONS + return (*__f_)(__a0, __a1, __a2); +} + +#ifndef _LIBCPP_NO_RTTI + +template<class _R, class _A0, class _A1, class _A2> +const std::type_info& +function<_R(_A0, _A1, _A2)>::target_type() const +{ + if (__f_ == 0) + return typeid(void); + return __f_->target_type(); +} + +template<class _R, class _A0, class _A1, class _A2> +template <typename _T> +_T* +function<_R(_A0, _A1, _A2)>::target() +{ + if (__f_ == 0) + return (_T*)0; + return (_T*)__f_->target(typeid(_T)); +} + +template<class _R, class _A0, class _A1, class _A2> +template <typename _T> +const _T* +function<_R(_A0, _A1, _A2)>::target() const +{ + if (__f_ == 0) + return (const _T*)0; + return (const _T*)__f_->target(typeid(_T)); +} + +#endif // _LIBCPP_NO_RTTI + +template <class _F> +inline _LIBCPP_INLINE_VISIBILITY +bool +operator==(const function<_F>& __f, nullptr_t) {return !__f;} + +template <class _F> +inline _LIBCPP_INLINE_VISIBILITY +bool +operator==(nullptr_t, const function<_F>& __f) {return !__f;} + +template <class _F> +inline _LIBCPP_INLINE_VISIBILITY +bool +operator!=(const function<_F>& __f, nullptr_t) {return (bool)__f;} + +template <class _F> +inline _LIBCPP_INLINE_VISIBILITY +bool +operator!=(nullptr_t, const function<_F>& __f) {return (bool)__f;} + +template <class _F> +inline _LIBCPP_INLINE_VISIBILITY +void +swap(function<_F>& __x, function<_F>& __y) +{return __x.swap(__y);} + +template<class _Tp> struct __is_bind_expression : public false_type {}; +template<class _Tp> struct _LIBCPP_VISIBLE is_bind_expression + : public __is_bind_expression<typename remove_cv<_Tp>::type> {}; + +template<class _Tp> struct __is_placeholder : public integral_constant<int, 0> {}; +template<class _Tp> struct _LIBCPP_VISIBLE is_placeholder + : public __is_placeholder<typename remove_cv<_Tp>::type> {}; + +namespace placeholders +{ + +template <int _N> struct __ph {}; + +extern __ph<1> _1; +extern __ph<2> _2; +extern __ph<3> _3; +extern __ph<4> _4; +extern __ph<5> _5; +extern __ph<6> _6; +extern __ph<7> _7; +extern __ph<8> _8; +extern __ph<9> _9; +extern __ph<10> _10; + +} // placeholders + +template<int _N> +struct __is_placeholder<placeholders::__ph<_N> > + : public integral_constant<int, _N> {}; + +template <class _Tp, class _Uj> +inline _LIBCPP_INLINE_VISIBILITY +_Tp& +__mu(reference_wrapper<_Tp> __t, _Uj&) +{ + return __t.get(); +} +/* +template <bool _IsBindExpr, class _Ti, class ..._Uj> +struct __mu_return1 {}; + +template <class _Ti, class ..._Uj> +struct __mu_return1<true, _Ti, _Uj...> +{ + typedef typename result_of<_Ti(_Uj...)>::type type; +}; + +template <class _Ti, class ..._Uj, size_t ..._Indx> +inline _LIBCPP_INLINE_VISIBILITY +typename __mu_return1<true, _Ti, _Uj...>::type +__mu_expand(_Ti& __ti, tuple<_Uj...>&& __uj, __tuple_indices<_Indx...>) +{ + __ti(_VSTD::forward<typename tuple_element<_Indx, _Uj>::type>(get<_Indx>(__uj))...); +} + +template <class _Ti, class ..._Uj> +inline _LIBCPP_INLINE_VISIBILITY +typename enable_if +< + is_bind_expression<_Ti>::value, + typename __mu_return1<is_bind_expression<_Ti>::value, _Ti, _Uj...>::type +>::type +__mu(_Ti& __ti, tuple<_Uj...>& __uj) +{ + typedef typename __make_tuple_indices<sizeof...(_Uj)>::type __indices; + return __mu_expand(__ti, __uj, __indices()); +} + +template <bool IsPh, class _Ti, class _Uj> +struct __mu_return2 {}; + +template <class _Ti, class _Uj> +struct __mu_return2<true, _Ti, _Uj> +{ + typedef typename tuple_element<is_placeholder<_Ti>::value - 1, _Uj>::type type; +}; + +template <class _Ti, class _Uj> +inline _LIBCPP_INLINE_VISIBILITY +typename enable_if +< + 0 < is_placeholder<_Ti>::value, + typename __mu_return2<0 < is_placeholder<_Ti>::value, _Ti, _Uj>::type +>::type +__mu(_Ti&, _Uj& __uj) +{ + const size_t _Indx = is_placeholder<_Ti>::value - 1; + // compiler bug workaround + typename tuple_element<_Indx, _Uj>::type __t = get<_Indx>(__uj); + return __t; +// return _VSTD::forward<typename tuple_element<_Indx, _Uj>::type>(get<_Indx>(__uj)); +} + +template <class _Ti, class _Uj> +inline _LIBCPP_INLINE_VISIBILITY +typename enable_if +< + !is_bind_expression<_Ti>::value && + is_placeholder<_Ti>::value == 0 && + !__is_reference_wrapper<_Ti>::value, + _Ti& +>::type +__mu(_Ti& __ti, _Uj& __uj) +{ + return __ti; +} + +template <class _Ti, bool IsBindEx, bool IsPh, class _TupleUj> +struct ____mu_return; + +template <class _Ti, class ..._Uj> +struct ____mu_return<_Ti, true, false, tuple<_Uj...> > +{ + typedef typename result_of<_Ti(_Uj...)>::type type; +}; + +template <class _Ti, class _TupleUj> +struct ____mu_return<_Ti, false, true, _TupleUj> +{ + typedef typename tuple_element<is_placeholder<_Ti>::value - 1, + _TupleUj>::type&& type; +}; + +template <class _Ti, class _TupleUj> +struct ____mu_return<_Ti, false, false, _TupleUj> +{ + typedef _Ti& type; +}; + +template <class _Ti, class _TupleUj> +struct __mu_return + : public ____mu_return<_Ti, + is_bind_expression<_Ti>::value, + 0 < is_placeholder<_Ti>::value, + _TupleUj> +{ +}; + +template <class _Ti, class _TupleUj> +struct __mu_return<reference_wrapper<_Ti>, _TupleUj> +{ + typedef _Ti& type; +}; + +template <class _F, class _BoundArgs, class _TupleUj> +struct __bind_return; + +template <class _F, class ..._BoundArgs, class _TupleUj> +struct __bind_return<_F, tuple<_BoundArgs...>, _TupleUj> +{ + typedef typename __ref_return + < + _F&, + typename __mu_return + < + _BoundArgs, + _TupleUj + >::type... + >::type type; +}; + +template <class _F, class ..._BoundArgs, class _TupleUj> +struct __bind_return<_F, const tuple<_BoundArgs...>, _TupleUj> +{ + typedef typename __ref_return + < + _F&, + typename __mu_return + < + const _BoundArgs, + _TupleUj + >::type... + >::type type; +}; + +template <class _F, class _BoundArgs, size_t ..._Indx, class _Args> +inline _LIBCPP_INLINE_VISIBILITY +typename __bind_return<_F, _BoundArgs, _Args>::type +__apply_functor(_F& __f, _BoundArgs& __bound_args, __tuple_indices<_Indx...>, + _Args&& __args) +{ + return __invoke(__f, __mu(get<_Indx>(__bound_args), __args)...); +} + +template<class _F, class ..._BoundArgs> +class __bind +{ + _F __f_; + tuple<_BoundArgs...> __bound_args_; + + typedef typename __make_tuple_indices<sizeof...(_BoundArgs)>::type __indices; +public: + template <class _G, class ..._BA> + explicit __bind(_G&& __f, _BA&& ...__bound_args) + : __f_(_VSTD::forward<_G>(__f)), + __bound_args_(_VSTD::forward<_BA>(__bound_args)...) {} + + template <class ..._Args> + typename __bind_return<_F, tuple<_BoundArgs...>, tuple<_Args&&...> >::type + operator()(_Args&& ...__args) + { + // compiler bug workaround + return __apply_functor(__f_, __bound_args_, __indices(), + tuple<_Args&&...>(__args...)); + } + + template <class ..._Args> + typename __bind_return<_F, tuple<_BoundArgs...>, tuple<_Args&&...> >::type + operator()(_Args&& ...__args) const + { + return __apply_functor(__f_, __bound_args_, __indices(), + tuple<_Args&&...>(__args...)); + } +}; + +template<class _F, class ..._BoundArgs> +struct __is_bind_expression<__bind<_F, _BoundArgs...> > : public true_type {}; + +template<class _R, class _F, class ..._BoundArgs> +class __bind_r + : public __bind<_F, _BoundArgs...> +{ + typedef __bind<_F, _BoundArgs...> base; +public: + typedef _R result_type; + + template <class _G, class ..._BA> + explicit __bind_r(_G&& __f, _BA&& ...__bound_args) + : base(_VSTD::forward<_G>(__f), + _VSTD::forward<_BA>(__bound_args)...) {} + + template <class ..._Args> + result_type + operator()(_Args&& ...__args) + { + return base::operator()(_VSTD::forward<_Args>(__args)...); + } + + template <class ..._Args> + result_type + operator()(_Args&& ...__args) const + { + return base::operator()(_VSTD::forward<_Args>(__args)...); + } +}; + +template<class _R, class _F, class ..._BoundArgs> +struct __is_bind_expression<__bind_r<_R, _F, _BoundArgs...> > : public true_type {}; + +template<class _F, class ..._BoundArgs> +inline _LIBCPP_INLINE_VISIBILITY +__bind<typename decay<_F>::type, typename decay<_BoundArgs>::type...> +bind(_F&& __f, _BoundArgs&&... __bound_args) +{ + typedef __bind<typename decay<_F>::type, typename decay<_BoundArgs>::type...> type; + return type(_VSTD::forward<_F>(__f), _VSTD::forward<_BoundArgs>(__bound_args)...); +} + +template<class _R, class _F, class ..._BoundArgs> +inline _LIBCPP_INLINE_VISIBILITY +__bind_r<_R, typename decay<_F>::type, typename decay<_BoundArgs>::type...> +bind(_F&& __f, _BoundArgs&&... __bound_args) +{ + typedef __bind_r<_R, typename decay<_F>::type, typename decay<_BoundArgs>::type...> type; + return type(_VSTD::forward<_F>(__f), _VSTD::forward<_BoundArgs>(__bound_args)...); +} +*/ + +#endif // _LIBCPP_FUNCTIONAL_03 diff --git a/include/__functional_base b/include/__functional_base new file mode 100644 index 000000000000..8d8e4b5e6e9a --- /dev/null +++ b/include/__functional_base @@ -0,0 +1,430 @@ +// -*- C++ -*- +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#ifndef _LIBCPP_FUNCTIONAL_BASE +#define _LIBCPP_FUNCTIONAL_BASE + +#include <__config> +#include <type_traits> +#include <typeinfo> +#include <exception> + +#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) +#pragma GCC system_header +#endif + +_LIBCPP_BEGIN_NAMESPACE_STD + +template <class _Arg, class _Result> +struct _LIBCPP_VISIBLE unary_function +{ + typedef _Arg argument_type; + typedef _Result result_type; +}; + +template <class _Arg1, class _Arg2, class _Result> +struct _LIBCPP_VISIBLE binary_function +{ + typedef _Arg1 first_argument_type; + typedef _Arg2 second_argument_type; + typedef _Result result_type; +}; + +template <class _Tp> struct _LIBCPP_VISIBLE hash; + +template <class _Tp> +struct __has_result_type +{ +private: + struct __two {char _; char __;}; + template <class _Up> static __two __test(...); + template <class _Up> static char __test(typename _Up::result_type* = 0); +public: + static const bool value = sizeof(__test<_Tp>(0)) == 1; +}; + +#ifdef _LIBCPP_HAS_NO_VARIADICS + +#include <__functional_base_03> + +#else // _LIBCPP_HAS_NO_VARIADICS + +// __weak_result_type + +template <class _Tp> +struct __derives_from_unary_function +{ +private: + struct __two {char _; char __;}; + static __two __test(...); + template <class _A, class _R> + static unary_function<_A, _R> + __test(const volatile unary_function<_A, _R>*); +public: + static const bool value = !is_same<decltype(__test((_Tp*)0)), __two>::value; + typedef decltype(__test((_Tp*)0)) type; +}; + +template <class _Tp> +struct __derives_from_binary_function +{ +private: + struct __two {char _; char __;}; + static __two __test(...); + template <class _A1, class _A2, class _R> + static binary_function<_A1, _A2, _R> + __test(const volatile binary_function<_A1, _A2, _R>*); +public: + static const bool value = !is_same<decltype(__test((_Tp*)0)), __two>::value; + typedef decltype(__test((_Tp*)0)) type; +}; + +template <class _Tp, bool = __derives_from_unary_function<_Tp>::value> +struct __maybe_derive_from_unary_function // bool is true + : public __derives_from_unary_function<_Tp>::type +{ +}; + +template <class _Tp> +struct __maybe_derive_from_unary_function<_Tp, false> +{ +}; + +template <class _Tp, bool = __derives_from_binary_function<_Tp>::value> +struct __maybe_derive_from_binary_function // bool is true + : public __derives_from_binary_function<_Tp>::type +{ +}; + +template <class _Tp> +struct __maybe_derive_from_binary_function<_Tp, false> +{ +}; + +template <class _Tp, bool = __has_result_type<_Tp>::value> +struct __weak_result_type_imp // bool is true + : public __maybe_derive_from_unary_function<_Tp>, + public __maybe_derive_from_binary_function<_Tp> +{ + typedef typename _Tp::result_type result_type; +}; + +template <class _Tp> +struct __weak_result_type_imp<_Tp, false> + : public __maybe_derive_from_unary_function<_Tp>, + public __maybe_derive_from_binary_function<_Tp> +{ +}; + +template <class _Tp> +struct __weak_result_type + : public __weak_result_type_imp<_Tp> +{ +}; + +// 0 argument case + +template <class _R> +struct __weak_result_type<_R ()> +{ + typedef _R result_type; +}; + +template <class _R> +struct __weak_result_type<_R (&)()> +{ + typedef _R result_type; +}; + +template <class _R> +struct __weak_result_type<_R (*)()> +{ + typedef _R result_type; +}; + +// 1 argument case + +template <class _R, class _A1> +struct __weak_result_type<_R (_A1)> + : public unary_function<_A1, _R> +{ +}; + +template <class _R, class _A1> +struct __weak_result_type<_R (&)(_A1)> + : public unary_function<_A1, _R> +{ +}; + +template <class _R, class _A1> +struct __weak_result_type<_R (*)(_A1)> + : public unary_function<_A1, _R> +{ +}; + +template <class _R, class _C> +struct __weak_result_type<_R (_C::*)()> + : public unary_function<_C*, _R> +{ +}; + +template <class _R, class _C> +struct __weak_result_type<_R (_C::*)() const> + : public unary_function<const _C*, _R> +{ +}; + +template <class _R, class _C> +struct __weak_result_type<_R (_C::*)() volatile> + : public unary_function<volatile _C*, _R> +{ +}; + +template <class _R, class _C> +struct __weak_result_type<_R (_C::*)() const volatile> + : public unary_function<const volatile _C*, _R> +{ +}; + +// 2 argument case + +template <class _R, class _A1, class _A2> +struct __weak_result_type<_R (_A1, _A2)> + : public binary_function<_A1, _A2, _R> +{ +}; + +template <class _R, class _A1, class _A2> +struct __weak_result_type<_R (*)(_A1, _A2)> + : public binary_function<_A1, _A2, _R> +{ +}; + +template <class _R, class _A1, class _A2> +struct __weak_result_type<_R (&)(_A1, _A2)> + : public binary_function<_A1, _A2, _R> +{ +}; + +template <class _R, class _C, class _A1> +struct __weak_result_type<_R (_C::*)(_A1)> + : public binary_function<_C*, _A1, _R> +{ +}; + +template <class _R, class _C, class _A1> +struct __weak_result_type<_R (_C::*)(_A1) const> + : public binary_function<const _C*, _A1, _R> +{ +}; + +template <class _R, class _C, class _A1> +struct __weak_result_type<_R (_C::*)(_A1) volatile> + : public binary_function<volatile _C*, _A1, _R> +{ +}; + +template <class _R, class _C, class _A1> +struct __weak_result_type<_R (_C::*)(_A1) const volatile> + : public binary_function<const volatile _C*, _A1, _R> +{ +}; + +// 3 or more arguments + +template <class _R, class _A1, class _A2, class _A3, class ..._A4> +struct __weak_result_type<_R (_A1, _A2, _A3, _A4...)> +{ + typedef _R result_type; +}; + +template <class _R, class _A1, class _A2, class _A3, class ..._A4> +struct __weak_result_type<_R (&)(_A1, _A2, _A3, _A4...)> +{ + typedef _R result_type; +}; + +template <class _R, class _A1, class _A2, class _A3, class ..._A4> +struct __weak_result_type<_R (*)(_A1, _A2, _A3, _A4...)> +{ + typedef _R result_type; +}; + +template <class _R, class _C, class _A1, class _A2, class ..._A3> +struct __weak_result_type<_R (_C::*)(_A1, _A2, _A3...)> +{ + typedef _R result_type; +}; + +template <class _R, class _C, class _A1, class _A2, class ..._A3> +struct __weak_result_type<_R (_C::*)(_A1, _A2, _A3...) const> +{ + typedef _R result_type; +}; + +template <class _R, class _C, class _A1, class _A2, class ..._A3> +struct __weak_result_type<_R (_C::*)(_A1, _A2, _A3...) volatile> +{ + typedef _R result_type; +}; + +template <class _R, class _C, class _A1, class _A2, class ..._A3> +struct __weak_result_type<_R (_C::*)(_A1, _A2, _A3...) const volatile> +{ + typedef _R result_type; +}; + +// __invoke + +// bullets 1 and 2 + +template <class _F, class _A0, class ..._Args> +inline _LIBCPP_INLINE_VISIBILITY +auto +__invoke(_F&& __f, _A0&& __a0, _Args&& ...__args) + -> decltype((_VSTD::forward<_A0>(__a0).*__f)(_VSTD::forward<_Args>(__args)...)) +{ + return (_VSTD::forward<_A0>(__a0).*__f)(_VSTD::forward<_Args>(__args)...); +} + +template <class _F, class _A0, class ..._Args> +inline _LIBCPP_INLINE_VISIBILITY +auto +__invoke(_F&& __f, _A0&& __a0, _Args&& ...__args) + -> decltype(((*_VSTD::forward<_A0>(__a0)).*__f)(_VSTD::forward<_Args>(__args)...)) +{ + return ((*_VSTD::forward<_A0>(__a0)).*__f)(_VSTD::forward<_Args>(__args)...); +} + +// bullets 3 and 4 + +template <class _F, class _A0> +inline _LIBCPP_INLINE_VISIBILITY +auto +__invoke(_F&& __f, _A0&& __a0) + -> decltype(_VSTD::forward<_A0>(__a0).*__f) +{ + return _VSTD::forward<_A0>(__a0).*__f; +} + +template <class _F, class _A0> +inline _LIBCPP_INLINE_VISIBILITY +auto +__invoke(_F&& __f, _A0&& __a0) + -> decltype((*_VSTD::forward<_A0>(__a0)).*__f) +{ + return (*_VSTD::forward<_A0>(__a0)).*__f; +} + +// bullet 5 + +template <class _F, class ..._Args> +inline _LIBCPP_INLINE_VISIBILITY +auto +__invoke(_F&& __f, _Args&& ...__args) + -> decltype(_VSTD::forward<_F>(__f)(_VSTD::forward<_Args>(__args)...)) +{ + return _VSTD::forward<_F>(__f)(_VSTD::forward<_Args>(__args)...); +} + +template <class _Tp, class ..._Args> +struct __invoke_return +{ + typedef decltype(__invoke(_VSTD::declval<_Tp>(), _VSTD::declval<_Args>()...)) type; +}; + +template <class _Tp> +class _LIBCPP_VISIBLE reference_wrapper + : public __weak_result_type<_Tp> +{ +public: + // types + typedef _Tp type; +private: + type* __f_; + +public: + // construct/copy/destroy + _LIBCPP_INLINE_VISIBILITY reference_wrapper(type& __f) _NOEXCEPT : __f_(&__f) {} +#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES + private: reference_wrapper(type&&); public: // = delete; // do not bind to temps +#endif + + // access + _LIBCPP_INLINE_VISIBILITY operator type& () const _NOEXCEPT {return *__f_;} + _LIBCPP_INLINE_VISIBILITY type& get() const _NOEXCEPT {return *__f_;} + + // invoke + template <class... _ArgTypes> + _LIBCPP_INLINE_VISIBILITY + typename __invoke_of<type&, _ArgTypes...>::type + operator() (_ArgTypes&&... __args) const + { + return __invoke(get(), _VSTD::forward<_ArgTypes>(__args)...); + } +}; + +template <class _Tp> struct ____is_reference_wrapper : public false_type {}; +template <class _Tp> struct ____is_reference_wrapper<reference_wrapper<_Tp> > : public true_type {}; +template <class _Tp> struct __is_reference_wrapper + : public ____is_reference_wrapper<typename remove_cv<_Tp>::type> {}; + +template <class _Tp> +inline _LIBCPP_INLINE_VISIBILITY +reference_wrapper<_Tp> +ref(_Tp& __t) _NOEXCEPT +{ + return reference_wrapper<_Tp>(__t); +} + +template <class _Tp> +inline _LIBCPP_INLINE_VISIBILITY +reference_wrapper<_Tp> +ref(reference_wrapper<_Tp> __t) _NOEXCEPT +{ + return ref(__t.get()); +} + +template <class _Tp> +inline _LIBCPP_INLINE_VISIBILITY +reference_wrapper<const _Tp> +cref(const _Tp& __t) _NOEXCEPT +{ + return reference_wrapper<const _Tp>(__t); +} + +template <class _Tp> +inline _LIBCPP_INLINE_VISIBILITY +reference_wrapper<const _Tp> +cref(reference_wrapper<_Tp> __t) _NOEXCEPT +{ + return cref(__t.get()); +} + +#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES +#ifndef _LIBCPP_HAS_NO_DELETED_FUNCTIONS + +template <class _Tp> void ref(const _Tp&& __t) = delete; +template <class _Tp> void cref(const _Tp&& __t) = delete; + +#else // _LIBCPP_HAS_NO_DELETED_FUNCTIONS + +template <class _Tp> void ref(const _Tp&& __t);// = delete; +template <class _Tp> void cref(const _Tp&& __t);// = delete; + +#endif // _LIBCPP_HAS_NO_DELETED_FUNCTIONS + +#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES + +#endif // _LIBCPP_HAS_NO_VARIADICS + +_LIBCPP_END_NAMESPACE_STD + +#endif // _LIBCPP_FUNCTIONAL_BASE diff --git a/include/__functional_base_03 b/include/__functional_base_03 new file mode 100644 index 000000000000..fabda5bc3d4b --- /dev/null +++ b/include/__functional_base_03 @@ -0,0 +1,1087 @@ +// -*- C++ -*- +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#ifndef _LIBCPP_FUNCTIONAL_BASE_03 +#define _LIBCPP_FUNCTIONAL_BASE_03 + +// manual variadic expansion for <functional> + +// __weak_result_type + +template <class _Tp> +struct __derives_from_unary_function +{ +private: + struct __two {char _; char __;}; + static __two __test(...); + template <class _A, class _R> + static unary_function<_A, _R> + __test(const volatile unary_function<_A, _R>*); +public: + static const bool value = !is_same<decltype(__test((_Tp*)0)), __two>::value; + typedef decltype(__test((_Tp*)0)) type; +}; + +template <class _Tp> +struct __derives_from_binary_function +{ +private: + struct __two {char _; char __;}; + static __two __test(...); + template <class _A1, class _A2, class _R> + static binary_function<_A1, _A2, _R> + __test(const volatile binary_function<_A1, _A2, _R>*); +public: + static const bool value = !is_same<decltype(__test((_Tp*)0)), __two>::value; + typedef decltype(__test((_Tp*)0)) type; +}; + +template <class _Tp, bool = __derives_from_unary_function<_Tp>::value> +struct __maybe_derive_from_unary_function // bool is true + : public __derives_from_unary_function<_Tp>::type +{ +}; + +template <class _Tp> +struct __maybe_derive_from_unary_function<_Tp, false> +{ +}; + +template <class _Tp, bool = __derives_from_binary_function<_Tp>::value> +struct __maybe_derive_from_binary_function // bool is true + : public __derives_from_binary_function<_Tp>::type +{ +}; + +template <class _Tp> +struct __maybe_derive_from_binary_function<_Tp, false> +{ +}; + +template <class _Tp, bool = __has_result_type<_Tp>::value> +struct __weak_result_type_imp // bool is true + : public __maybe_derive_from_unary_function<_Tp>, + public __maybe_derive_from_binary_function<_Tp> +{ + typedef typename _Tp::result_type result_type; +}; + +template <class _Tp> +struct __weak_result_type_imp<_Tp, false> + : public __maybe_derive_from_unary_function<_Tp>, + public __maybe_derive_from_binary_function<_Tp> +{ +}; + +template <class _Tp> +struct __weak_result_type + : public __weak_result_type_imp<typename remove_reference<_Tp>::type> +{ +}; + +// 0 argument case + +template <class _R> +struct __weak_result_type<_R ()> +{ + typedef _R result_type; +}; + +template <class _R> +struct __weak_result_type<_R (&)()> +{ + typedef _R result_type; +}; + +template <class _R> +struct __weak_result_type<_R (*)()> +{ + typedef _R result_type; +}; + +// 1 argument case + +template <class _R, class _A1> +struct __weak_result_type<_R (_A1)> + : public unary_function<_A1, _R> +{ +}; + +template <class _R, class _A1> +struct __weak_result_type<_R (&)(_A1)> + : public unary_function<_A1, _R> +{ +}; + +template <class _R, class _A1> +struct __weak_result_type<_R (*)(_A1)> + : public unary_function<_A1, _R> +{ +}; + +template <class _R, class _C> +struct __weak_result_type<_R (_C::*)()> + : public unary_function<_C*, _R> +{ +}; + +template <class _R, class _C> +struct __weak_result_type<_R (_C::*)() const> + : public unary_function<const _C*, _R> +{ +}; + +template <class _R, class _C> +struct __weak_result_type<_R (_C::*)() volatile> + : public unary_function<volatile _C*, _R> +{ +}; + +template <class _R, class _C> +struct __weak_result_type<_R (_C::*)() const volatile> + : public unary_function<const volatile _C*, _R> +{ +}; + +// 2 argument case + +template <class _R, class _A1, class _A2> +struct __weak_result_type<_R (_A1, _A2)> + : public binary_function<_A1, _A2, _R> +{ +}; + +template <class _R, class _A1, class _A2> +struct __weak_result_type<_R (*)(_A1, _A2)> + : public binary_function<_A1, _A2, _R> +{ +}; + +template <class _R, class _A1, class _A2> +struct __weak_result_type<_R (&)(_A1, _A2)> + : public binary_function<_A1, _A2, _R> +{ +}; + +template <class _R, class _C, class _A1> +struct __weak_result_type<_R (_C::*)(_A1)> + : public binary_function<_C*, _A1, _R> +{ +}; + +template <class _R, class _C, class _A1> +struct __weak_result_type<_R (_C::*)(_A1) const> + : public binary_function<const _C*, _A1, _R> +{ +}; + +template <class _R, class _C, class _A1> +struct __weak_result_type<_R (_C::*)(_A1) volatile> + : public binary_function<volatile _C*, _A1, _R> +{ +}; + +template <class _R, class _C, class _A1> +struct __weak_result_type<_R (_C::*)(_A1) const volatile> + : public binary_function<const volatile _C*, _A1, _R> +{ +}; + +// 3 or more arguments + +template <class _R, class _A1, class _A2, class _A3> +struct __weak_result_type<_R (_A1, _A2, _A3)> +{ + typedef _R result_type; +}; + +template <class _R, class _A1, class _A2, class _A3> +struct __weak_result_type<_R (&)(_A1, _A2, _A3)> +{ + typedef _R result_type; +}; + +template <class _R, class _A1, class _A2, class _A3> +struct __weak_result_type<_R (*)(_A1, _A2, _A3)> +{ + typedef _R result_type; +}; + +template <class _R, class _C, class _A1, class _A2> +struct __weak_result_type<_R (_C::*)(_A1, _A2)> +{ + typedef _R result_type; +}; + +template <class _R, class _C, class _A1, class _A2> +struct __weak_result_type<_R (_C::*)(_A1, _A2) const> +{ + typedef _R result_type; +}; + +template <class _R, class _C, class _A1, class _A2> +struct __weak_result_type<_R (_C::*)(_A1, _A2) volatile> +{ + typedef _R result_type; +}; + +// __invoke + +// __ref_return0 +// +// template <class _Tp, bool _HasResultType> +// struct ________ref_return0 // _HasResultType is true +// { +// typedef typename _Tp::result_type type; +// }; +// +// template <class _Tp> +// struct ________ref_return0<_Tp, false> +// { +// typedef void type; +// }; +// +// template <class _Tp, bool _IsClass> +// struct ____ref_return0 // _IsClass is true +// : public ________ref_return0<_Tp, __has_result_type<typename remove_cv<_Tp>::type>::value> +// { +// }; +// +// template <class _Tp, bool _HasResultType> +// struct ______ref_return0 // _HasResultType is true +// { +// typedef typename __callable_type<_Tp>::result_type type; +// }; +// +// template <class _Tp> +// struct ______ref_return0<_Tp, false> // pointer to member data +// { +// typedef void type; +// }; +// +// template <class _Tp> +// struct ____ref_return0<_Tp, false> +// : public ______ref_return0<typename remove_cv<_Tp>::type, +// __has_result_type<__callable_type<typename remove_cv<_Tp>::type> >::value> +// { +// }; +// +// template <class _Tp> +// struct __ref_return0 +// : public ____ref_return0<typename remove_reference<_Tp>::type, +// is_class<typename remove_reference<_Tp>::type>::value> +// { +// }; +// +// __ref_return1 +// +// template <class _Tp, bool _IsClass, class _A0> +// struct ____ref_return1 // _IsClass is true +// { +// typedef typename result_of<_Tp(_A0)>::type type; +// }; +// +// template <class _Tp, bool _HasResultType, class _A0> +// struct ______ref_return1 // _HasResultType is true +// { +// typedef typename __callable_type<_Tp>::result_type type; +// }; +// +// template <class _Tp, class _A0, bool> +// struct __ref_return1_member_data1; +// +// template <class _R, class _C, class _A0> +// struct __ref_return1_member_data1<_R _C::*, _A0, true> +// { +// typedef typename __apply_cv<_A0, _R>::type& type; +// }; +// +// template <class _R, class _C, class _A0> +// struct __ref_return1_member_data1<_R _C::*, _A0, false> +// { +// static _A0 __a; +// typedef typename __apply_cv<decltype(*__a), _R>::type& type; +// }; +// +// template <class _Tp, class _A0> +// struct __ref_return1_member_data; +// +// template <class _R, class _C, class _A0> +// struct __ref_return1_member_data<_R _C::*, _A0> +// : public __ref_return1_member_data1<_R _C::*, _A0, +// is_same<typename remove_cv<_C>::type, +// typename remove_cv<typename remove_reference<_A0>::type>::type>::value> +// { +// }; +// +// template <class _Tp, class _A0> +// struct ______ref_return1<_Tp, false, _A0> // pointer to member data +// : public __ref_return1_member_data<typename remove_cv<_Tp>::type, _A0> +// { +// }; +// +// template <class _Tp, class _A0> +// struct ____ref_return1<_Tp, false, _A0> +// : public ______ref_return1<typename remove_cv<_Tp>::type, +// __has_result_type<__callable_type<typename remove_cv<_Tp>::type> >::value, _A0> +// { +// }; +// +// template <class _Tp, class _A0> +// struct __ref_return1 +// : public ____ref_return1<typename remove_reference<_Tp>::type, +// is_class<typename remove_reference<_Tp>::type>::value, _A0> +// { +// }; +// +// __ref_return2 +// +// template <class _Tp, bool _IsClass, class _A0, class _A1> +// struct ____ref_return2 // _IsClass is true +// { +// typedef typename result_of<_Tp(_A0, _A1)>::type type; +// }; +// +// template <class _Tp, bool _HasResultType, class _A0, class _A1> +// struct ______ref_return2 // _HasResultType is true +// { +// typedef typename __callable_type<_Tp>::result_type type; +// }; +// +// template <class _Tp> +// struct ______ref_return2<_Tp, false, class _A0, class _A1> // pointer to member data +// { +// static_assert(sizeof(_Tp) == 0, "An attempt has been made to `call` a pointer" +// " to member data with too many arguments."); +// }; +// +// template <class _Tp, class _A0, class _A1> +// struct ____ref_return2<_Tp, false, _A0, _A1> +// : public ______ref_return2<typename remove_cv<_Tp>::type, +// __has_result_type<__callable_type<typename remove_cv<_Tp>::type> >::value, _A0, _A1> +// { +// }; +// +// template <class _Tp, class _A0, class _A1> +// struct __ref_return2 +// : public ____ref_return2<typename remove_reference<_Tp>::type, +// is_class<typename remove_reference<_Tp>::type>::value, _A0, _A1> +// { +// }; +// +// __ref_return3 +// +// template <class _Tp, bool _IsClass, class _A0, class _A1, class _A2> +// struct ____ref_return3 // _IsClass is true +// { +// typedef typename result_of<_Tp(_A0, _A1, _A2)>::type type; +// }; +// +// template <class _Tp, bool _HasResultType, class _A0, class _A1, class _A2> +// struct ______ref_return3 // _HasResultType is true +// { +// typedef typename __callable_type<_Tp>::result_type type; +// }; +// +// template <class _Tp> +// struct ______ref_return3<_Tp, false, class _A0, class _A1, class _A2> // pointer to member data +// { +// static_assert(sizeof(_Tp) == 0, "An attempt has been made to `call` a pointer" +// " to member data with too many arguments."); +// }; +// +// template <class _Tp, class _A0, class _A1, class _A2> +// struct ____ref_return3<_Tp, false, _A0, _A1, _A2> +// : public ______ref_return3<typename remove_cv<_Tp>::type, +// __has_result_type<__callable_type<typename remove_cv<_Tp>::type> >::value, _A0, _A1, _A2> +// { +// }; +// +// template <class _Tp, class _A0, class _A1, class _A2> +// struct __ref_return3 +// : public ____ref_return3<typename remove_reference<_Tp>::type, +// is_class<typename remove_reference<_Tp>::type>::value, _A0, _A1, _A2> +// { +// }; + +// first bullet + +template <class _R, class _T, class _T1> +inline _LIBCPP_INLINE_VISIBILITY +typename enable_if +< + is_base_of<_T, typename remove_reference<_T1>::type>::value, + _R +>::type +__invoke(_R (_T::*__f)(), _T1& __t1) +{ + return (__t1.*__f)(); +} + +template <class _R, class _T, class _T1, class _A0> +inline _LIBCPP_INLINE_VISIBILITY +typename enable_if +< + is_base_of<_T, typename remove_reference<_T1>::type>::value, + _R +>::type +__invoke(_R (_T::*__f)(_A0), _T1& __t1, _A0& __a0) +{ + return (__t1.*__f)(__a0); +} + +template <class _R, class _T, class _T1, class _A0, class _A1> +inline _LIBCPP_INLINE_VISIBILITY +typename enable_if +< + is_base_of<_T, typename remove_reference<_T1>::type>::value, + _R +>::type +__invoke(_R (_T::*__f)(_A0, _A1), _T1& __t1, _A0& __a0, _A1& __a1) +{ + return (__t1.*__f)(__a0, __a1); +} + +template <class _R, class _T, class _T1, class _A0, class _A1, class _A2> +inline _LIBCPP_INLINE_VISIBILITY +typename enable_if +< + is_base_of<_T, typename remove_reference<_T1>::type>::value, + _R +>::type +__invoke(_R (_T::*__f)(_A0, _A1, _A2), _T1& __t1, _A0& __a0, _A1& __a1, _A2& __a2) +{ + return (__t1.*__f)(__a0, __a1, __a2); +} + +template <class _R, class _T, class _T1> +inline _LIBCPP_INLINE_VISIBILITY +typename enable_if +< + is_base_of<_T, typename remove_reference<_T1>::type>::value, + _R +>::type +__invoke(_R (_T::*__f)() const, _T1& __t1) +{ + return (__t1.*__f)(); +} + +template <class _R, class _T, class _T1, class _A0> +inline _LIBCPP_INLINE_VISIBILITY +typename enable_if +< + is_base_of<_T, typename remove_reference<_T1>::type>::value, + _R +>::type +__invoke(_R (_T::*__f)(_A0) const, _T1& __t1, _A0& __a0) +{ + return (__t1.*__f)(__a0); +} + +template <class _R, class _T, class _T1, class _A0, class _A1> +inline _LIBCPP_INLINE_VISIBILITY +typename enable_if +< + is_base_of<_T, typename remove_reference<_T1>::type>::value, + _R +>::type +__invoke(_R (_T::*__f)(_A0, _A1) const, _T1& __t1, _A0& __a0, _A1& __a1) +{ + return (__t1.*__f)(__a0, __a1); +} + +template <class _R, class _T, class _T1, class _A0, class _A1, class _A2> +inline _LIBCPP_INLINE_VISIBILITY +typename enable_if +< + is_base_of<_T, typename remove_reference<_T1>::type>::value, + _R +>::type +__invoke(_R (_T::*__f)(_A0, _A1, _A2) const, _T1& __t1, _A0& __a0, _A1& __a1, _A2& __a2) +{ + return (__t1.*__f)(__a0, __a1, __a2); +} + +template <class _R, class _T, class _T1> +inline _LIBCPP_INLINE_VISIBILITY +typename enable_if +< + is_base_of<_T, typename remove_reference<_T1>::type>::value, + _R +>::type +__invoke(_R (_T::*__f)() volatile, _T1& __t1) +{ + return (__t1.*__f)(); +} + +template <class _R, class _T, class _T1, class _A0> +inline _LIBCPP_INLINE_VISIBILITY +typename enable_if +< + is_base_of<_T, typename remove_reference<_T1>::type>::value, + _R +>::type +__invoke(_R (_T::*__f)(_A0) volatile, _T1& __t1, _A0& __a0) +{ + return (__t1.*__f)(__a0); +} + +template <class _R, class _T, class _T1, class _A0, class _A1> +inline _LIBCPP_INLINE_VISIBILITY +typename enable_if +< + is_base_of<_T, typename remove_reference<_T1>::type>::value, + _R +>::type +__invoke(_R (_T::*__f)(_A0, _A1) volatile, _T1& __t1, _A0& __a0, _A1& __a1) +{ + return (__t1.*__f)(__a0, __a1); +} + +template <class _R, class _T, class _T1, class _A0, class _A1, class _A2> +inline _LIBCPP_INLINE_VISIBILITY +typename enable_if +< + is_base_of<_T, typename remove_reference<_T1>::type>::value, + _R +>::type +__invoke(_R (_T::*__f)(_A0, _A1, _A2) volatile, _T1& __t1, _A0& __a0, _A1& __a1, _A2& __a2) +{ + return (__t1.*__f)(__a0, __a1, __a2); +} + +template <class _R, class _T, class _T1> +inline _LIBCPP_INLINE_VISIBILITY +typename enable_if +< + is_base_of<_T, typename remove_reference<_T1>::type>::value, + _R +>::type +__invoke(_R (_T::*__f)() const volatile, _T1& __t1) +{ + return (__t1.*__f)(); +} + +template <class _R, class _T, class _T1, class _A0> +inline _LIBCPP_INLINE_VISIBILITY +typename enable_if +< + is_base_of<_T, typename remove_reference<_T1>::type>::value, + _R +>::type +__invoke(_R (_T::*__f)(_A0) const volatile, _T1& __t1, _A0& __a0) +{ + return (__t1.*__f)(__a0); +} + +template <class _R, class _T, class _T1, class _A0, class _A1> +inline _LIBCPP_INLINE_VISIBILITY +typename enable_if +< + is_base_of<_T, typename remove_reference<_T1>::type>::value, + _R +>::type +__invoke(_R (_T::*__f)(_A0, _A1) const volatile, _T1& __t1, _A0& __a0, _A1& __a1) +{ + return (__t1.*__f)(__a0, __a1); +} + +template <class _R, class _T, class _T1, class _A0, class _A1, class _A2> +inline _LIBCPP_INLINE_VISIBILITY +typename enable_if +< + is_base_of<_T, typename remove_reference<_T1>::type>::value, + _R +>::type +__invoke(_R (_T::*__f)(_A0, _A1, _A2) const volatile, _T1& __t1, _A0& __a0, _A1& __a1, _A2& __a2) +{ + return (__t1.*__f)(__a0, __a1, __a2); +} + +// second bullet + +template <class _R, class _T, class _T1> +inline _LIBCPP_INLINE_VISIBILITY +typename enable_if +< + !is_base_of<_T, typename remove_reference<_T1>::type>::value, + _R +>::type +__invoke(_R (_T::*__f)(), _T1 __t1) +{ + return ((*__t1).*__f)(); +} + +template <class _R, class _T, class _T1, class _A0> +inline _LIBCPP_INLINE_VISIBILITY +typename enable_if +< + !is_base_of<_T, typename remove_reference<_T1>::type>::value, + _R +>::type +__invoke(_R (_T::*__f)(_A0), _T1 __t1, _A0& __a0) +{ + return ((*__t1).*__f)(__a0); +} + +template <class _R, class _T, class _T1, class _A0, class _A1> +inline _LIBCPP_INLINE_VISIBILITY +typename enable_if +< + !is_base_of<_T, typename remove_reference<_T1>::type>::value, + _R +>::type +__invoke(_R (_T::*__f)(_A0, _A1), _T1 __t1, _A0& __a0, _A1& __a1) +{ + return ((*__t1).*__f)(__a0, __a1); +} + +template <class _R, class _T, class _T1, class _A0, class _A1, class _A2> +inline _LIBCPP_INLINE_VISIBILITY +typename enable_if +< + !is_base_of<_T, typename remove_reference<_T1>::type>::value, + _R +>::type +__invoke(_R (_T::*__f)(_A0, _A1, _A2), _T1 __t1, _A0& __a0, _A1& __a1, _A2& __a2) +{ + return ((*__t1).*__f)(__a0, __a1, __a2); +} + +template <class _R, class _T, class _T1> +inline _LIBCPP_INLINE_VISIBILITY +typename enable_if +< + !is_base_of<_T, typename remove_reference<_T1>::type>::value, + _R +>::type +__invoke(_R (_T::*__f)() const, _T1 __t1) +{ + return ((*__t1).*__f)(); +} + +template <class _R, class _T, class _T1, class _A0> +inline _LIBCPP_INLINE_VISIBILITY +typename enable_if +< + !is_base_of<_T, typename remove_reference<_T1>::type>::value, + _R +>::type +__invoke(_R (_T::*__f)(_A0) const, _T1 __t1, _A0& __a0) +{ + return ((*__t1).*__f)(__a0); +} + +template <class _R, class _T, class _T1, class _A0, class _A1> +inline _LIBCPP_INLINE_VISIBILITY +typename enable_if +< + !is_base_of<_T, typename remove_reference<_T1>::type>::value, + _R +>::type +__invoke(_R (_T::*__f)(_A0, _A1) const, _T1 __t1, _A0& __a0, _A1& __a1) +{ + return ((*__t1).*__f)(__a0, __a1); +} + +template <class _R, class _T, class _T1, class _A0, class _A1, class _A2> +inline _LIBCPP_INLINE_VISIBILITY +typename enable_if +< + !is_base_of<_T, typename remove_reference<_T1>::type>::value, + _R +>::type +__invoke(_R (_T::*__f)(_A0, _A1, _A2) const, _T1 __t1, _A0& __a0, _A1& __a1, _A2& __a2) +{ + return ((*__t1).*__f)(__a0, __a1, __a2); +} + +template <class _R, class _T, class _T1> +inline _LIBCPP_INLINE_VISIBILITY +typename enable_if +< + !is_base_of<_T, typename remove_reference<_T1>::type>::value, + _R +>::type +__invoke(_R (_T::*__f)() volatile, _T1 __t1) +{ + return ((*__t1).*__f)(); +} + +template <class _R, class _T, class _T1, class _A0> +inline _LIBCPP_INLINE_VISIBILITY +typename enable_if +< + !is_base_of<_T, typename remove_reference<_T1>::type>::value, + _R +>::type +__invoke(_R (_T::*__f)(_A0) volatile, _T1 __t1, _A0& __a0) +{ + return ((*__t1).*__f)(__a0); +} + +template <class _R, class _T, class _T1, class _A0, class _A1> +inline _LIBCPP_INLINE_VISIBILITY +typename enable_if +< + !is_base_of<_T, typename remove_reference<_T1>::type>::value, + _R +>::type +__invoke(_R (_T::*__f)(_A0, _A1) volatile, _T1 __t1, _A0& __a0, _A1& __a1) +{ + return ((*__t1).*__f)(__a0, __a1); +} + +template <class _R, class _T, class _T1, class _A0, class _A1, class _A2> +inline _LIBCPP_INLINE_VISIBILITY +typename enable_if +< + !is_base_of<_T, typename remove_reference<_T1>::type>::value, + _R +>::type +__invoke(_R (_T::*__f)(_A0, _A1, _A2) volatile, _T1 __t1, _A0& __a0, _A1& __a1, _A2& __a2) +{ + return ((*__t1).*__f)(__a0, __a1, __a2); +} + +template <class _R, class _T, class _T1> +inline _LIBCPP_INLINE_VISIBILITY +typename enable_if +< + !is_base_of<_T, typename remove_reference<_T1>::type>::value, + _R +>::type +__invoke(_R (_T::*__f)() const volatile, _T1 __t1) +{ + return ((*__t1).*__f)(); +} + +template <class _R, class _T, class _T1, class _A0> +inline _LIBCPP_INLINE_VISIBILITY +typename enable_if +< + !is_base_of<_T, typename remove_reference<_T1>::type>::value, + _R +>::type +__invoke(_R (_T::*__f)(_A0) const volatile, _T1 __t1, _A0& __a0) +{ + return ((*__t1).*__f)(__a0); +} + +template <class _R, class _T, class _T1, class _A0, class _A1> +inline _LIBCPP_INLINE_VISIBILITY +typename enable_if +< + !is_base_of<_T, typename remove_reference<_T1>::type>::value, + _R +>::type +__invoke(_R (_T::*__f)(_A0, _A1) const volatile, _T1 __t1, _A0& __a0, _A1& __a1) +{ + return ((*__t1).*__f)(__a0, __a1); +} + +template <class _R, class _T, class _T1, class _A0, class _A1, class _A2> +inline _LIBCPP_INLINE_VISIBILITY +typename enable_if +< + !is_base_of<_T, typename remove_reference<_T1>::type>::value, + _R +>::type +__invoke(_R (_T::*__f)(_A0, _A1, _A2) const volatile, _T1 __t1, _A0& __a0, _A1& __a1, _A2& __a2) +{ + return ((*__t1).*__f)(__a0, __a1, __a2); +} + +// third bullet + +template <class _R, class _T, class _T1> +inline _LIBCPP_INLINE_VISIBILITY +typename enable_if +< + is_base_of<_T, typename remove_reference<_T1>::type>::value, + typename __apply_cv<_T1, _R>::type& +>::type +__invoke(_R _T::* __f, _T1& __t1) +{ + return __t1.*__f; +} + +template <class _R, class _T> +inline _LIBCPP_INLINE_VISIBILITY +void +__invoke(_R _T::*) +{ +} + +// template <class _D, class _R, class _T, class _T1> +// inline _LIBCPP_INLINE_VISIBILITY +// typename enable_if +// < +// is_base_of<_T, typename remove_reference<_T1>::type>::value, +// typename __ref_return1<_R _T::*, _T1>::type +// >::type +// __invoke(_R _T::* __f, _T1& __t1) +// { +// return __t1.*__f; +// } + +// forth bullet + +template <class _T1, class _R, bool> +struct __4th_helper +{ +}; + +template <class _T1, class _R> +struct __4th_helper<_T1, _R, true> +{ + typedef typename __apply_cv<decltype(*_VSTD::declval<_T1>()), _R>::type type; +}; + +template <class _R, class _T, class _T1> +inline _LIBCPP_INLINE_VISIBILITY +typename __4th_helper<_T1, _R, + !is_base_of<_T, + typename remove_reference<_T1>::type + >::value + >::type& +__invoke(_R _T::* __f, _T1& __t1) +{ + return (*__t1).*__f; +} + +// template <class _D, class _R, class _T, class _T1> +// inline _LIBCPP_INLINE_VISIBILITY +// typename enable_if +// < +// !is_base_of<_T, typename remove_reference<_T1>::type>::value, +// typename __ref_return1<_R _T::*, _T1>::type +// >::type +// __invoke(_R _T::* __f, _T1 __t1) +// { +// return (*__t1).*__f; +// } + +// fifth bullet + +template <class _F> +inline _LIBCPP_INLINE_VISIBILITY +decltype(declval<_F>()()) +__invoke(_F __f) +{ + return __f(); +} + +template <class _F, class _A0> +inline _LIBCPP_INLINE_VISIBILITY +decltype(declval<_F>()(declval<_A0&>())) +__invoke(_F __f, _A0& __a0) +{ + return __f(__a0); +} + +template <class _F, class _A0, class _A1> +inline _LIBCPP_INLINE_VISIBILITY +decltype(declval<_F>()(declval<_A0&>(), declval<_A1&>())) +__invoke(_F __f, _A0& __a0, _A1& __a1) +{ + return __f(__a0, __a1); +} + +template <class _F, class _A0, class _A1, class _A2> +inline _LIBCPP_INLINE_VISIBILITY +decltype(declval<_F>()(declval<_A0&>(), declval<_A1&>(), declval<_A2&>())) +__invoke(_F __f, _A0& __a0, _A1& __a1, _A2& __a2) +{ + return __f(__a0, __a1, __a2); +} + +// template <class _R, class _F> +// inline _LIBCPP_INLINE_VISIBILITY +// _R +// __invoke(_F& __f) +// { +// return __f(); +// } +// +// template <class _R, class _F, class _A0> +// inline _LIBCPP_INLINE_VISIBILITY +// typename enable_if +// < +// !is_member_pointer<_F>::value, +// _R +// >::type +// __invoke(_F& __f, _A0& __a0) +// { +// return __f(__a0); +// } +// +// template <class _R, class _F, class _A0, class _A1> +// inline _LIBCPP_INLINE_VISIBILITY +// _R +// __invoke(_F& __f, _A0& __a0, _A1& __a1) +// { +// return __f(__a0, __a1); +// } +// +// template <class _R, class _F, class _A0, class _A1, class _A2> +// inline _LIBCPP_INLINE_VISIBILITY +// _R +// __invoke(_F& __f, _A0& __a0, _A1& __a1, _A2& __a2) +// { +// return __f(__a0, __a1, __a2); +// } + +template <class _Tp> +struct __has_type +{ +private: + struct __two {char _; char __;}; + template <class _Up> static __two __test(...); + template <class _Up> static char __test(typename _Up::type* = 0); +public: + static const bool value = sizeof(__test<_Tp>(0)) == 1; +}; + +template <class _F, bool = __has_result_type<__weak_result_type<_F> >::value> +struct __invoke_return +{ + typedef typename __weak_result_type<_F>::result_type type; +}; + +template <class _F> +struct __invoke_return<_F, false> +{ + typedef decltype(__invoke(_VSTD::declval<_F>())) type; +}; + +template <class _Tp, class _A0> +struct __invoke_return0 +{ + typedef decltype(__invoke(_VSTD::declval<_Tp>(), _VSTD::declval<_A0>())) type; +}; + +template <class _R, class _T, class _A0> +struct __invoke_return0<_R _T::*, _A0> +{ + typedef typename __apply_cv<_A0, _R>::type& type; +}; + +template <class _R, class _T, class _A0> +struct __invoke_return0<_R _T::*, _A0*> +{ + typedef typename __apply_cv<_A0, _R>::type& type; +}; + +template <class _Tp, class _A0, class _A1> +struct __invoke_return1 +{ + typedef decltype(__invoke(_VSTD::declval<_Tp>(), _VSTD::declval<_A0>(), + _VSTD::declval<_A1>())) type; +}; + +template <class _Tp, class _A0, class _A1, class _A2> +struct __invoke_return2 +{ + typedef decltype(__invoke(_VSTD::declval<_Tp>(), _VSTD::declval<_A0>(), + _VSTD::declval<_A1>(), + _VSTD::declval<_A2>())) type; +}; + +template <class _Tp> +class _LIBCPP_VISIBLE reference_wrapper + : public __weak_result_type<_Tp> +{ +public: + // types + typedef _Tp type; +private: + type* __f_; + +public: + // construct/copy/destroy + _LIBCPP_INLINE_VISIBILITY reference_wrapper(type& __f) : __f_(&__f) {} + + // access + _LIBCPP_INLINE_VISIBILITY operator type& () const {return *__f_;} + _LIBCPP_INLINE_VISIBILITY type& get() const {return *__f_;} + + // invoke + + _LIBCPP_INLINE_VISIBILITY + typename __invoke_return<type&>::type + operator() () const + { + return __invoke(get()); + } + + template <class _A0> + _LIBCPP_INLINE_VISIBILITY + typename __invoke_return0<type&, _A0>::type + operator() (_A0& __a0) const + { + return __invoke(get(), __a0); + } + + template <class _A0, class _A1> + _LIBCPP_INLINE_VISIBILITY + typename __invoke_return1<type&, _A0, _A1>::type + operator() (_A0& __a0, _A1& __a1) const + { + return __invoke(get(), __a0, __a1); + } + + template <class _A0, class _A1, class _A2> + _LIBCPP_INLINE_VISIBILITY + typename __invoke_return2<type&, _A0, _A1, _A2>::type + operator() (_A0& __a0, _A1& __a1, _A2& __a2) const + { + return __invoke(get(), __a0, __a1, __a2); + } +}; + +template <class _Tp> struct ____is_reference_wrapper : public false_type {}; +template <class _Tp> struct ____is_reference_wrapper<reference_wrapper<_Tp> > : public true_type {}; +template <class _Tp> struct __is_reference_wrapper + : public ____is_reference_wrapper<typename remove_cv<_Tp>::type> {}; + +template <class _Tp> +inline _LIBCPP_INLINE_VISIBILITY +reference_wrapper<_Tp> +ref(_Tp& __t) +{ + return reference_wrapper<_Tp>(__t); +} + +template <class _Tp> +inline _LIBCPP_INLINE_VISIBILITY +reference_wrapper<_Tp> +ref(reference_wrapper<_Tp> __t) +{ + return ref(__t.get()); +} + +template <class _Tp> +inline _LIBCPP_INLINE_VISIBILITY +reference_wrapper<const _Tp> +cref(const _Tp& __t) +{ + return reference_wrapper<const _Tp>(__t); +} + +template <class _Tp> +inline _LIBCPP_INLINE_VISIBILITY +reference_wrapper<const _Tp> +cref(reference_wrapper<_Tp> __t) +{ + return cref(__t.get()); +} + +#endif // _LIBCPP_FUNCTIONAL_BASE_03 diff --git a/include/__hash_table b/include/__hash_table new file mode 100644 index 000000000000..4399caad658b --- /dev/null +++ b/include/__hash_table @@ -0,0 +1,1918 @@ +// -*- C++ -*- +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#ifndef _LIBCPP__HASH_TABLE +#define _LIBCPP__HASH_TABLE + +#include <__config> +#include <initializer_list> +#include <memory> +#include <iterator> +#include <algorithm> +#include <cmath> + +#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) +#pragma GCC system_header +#endif + +_LIBCPP_BEGIN_NAMESPACE_STD + +_LIBCPP_VISIBLE +size_t __next_prime(size_t __n); + +template <class _NodePtr> +struct __hash_node_base +{ + typedef __hash_node_base __first_node; + // typedef _NodePtr pointer; + + _NodePtr __next_; + + _LIBCPP_INLINE_VISIBILITY __hash_node_base() _NOEXCEPT : __next_(nullptr) {} +}; + +template <class _Tp, class _VoidPtr> +struct __hash_node + : public __hash_node_base + < + typename pointer_traits<_VoidPtr>::template +#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES + rebind<__hash_node<_Tp, _VoidPtr> > +#else + rebind<__hash_node<_Tp, _VoidPtr> >::other +#endif + > +{ + typedef _Tp value_type; + + size_t __hash_; + value_type __value_; +}; + +template <class _Tp, class _Hash, class _Equal, class _Alloc> class __hash_table; +template <class _ConstNodePtr> class __hash_const_iterator; +template <class _HashIterator> class __hash_map_iterator; +template <class _HashIterator> class __hash_map_const_iterator; +template <class _Key, class _Tp, class _Hash, class _Pred, class _Alloc> + class _LIBCPP_VISIBLE unordered_map; + +template <class _NodePtr> +class _LIBCPP_VISIBLE __hash_iterator +{ + typedef _NodePtr __node_pointer; + + __node_pointer __node_; + +public: + typedef forward_iterator_tag iterator_category; + typedef typename pointer_traits<__node_pointer>::element_type::value_type value_type; + typedef typename pointer_traits<__node_pointer>::difference_type difference_type; + typedef value_type& reference; + typedef typename pointer_traits<__node_pointer>::template +#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES + rebind<value_type> +#else + rebind<value_type>::other +#endif + pointer; + + _LIBCPP_INLINE_VISIBILITY __hash_iterator() _NOEXCEPT {} + + _LIBCPP_INLINE_VISIBILITY + reference operator*() const {return __node_->__value_;} + _LIBCPP_INLINE_VISIBILITY + pointer operator->() const {return _VSTD::addressof(__node_->__value_);} + + _LIBCPP_INLINE_VISIBILITY + __hash_iterator& operator++() + { + __node_ = __node_->__next_; + return *this; + } + + _LIBCPP_INLINE_VISIBILITY + __hash_iterator operator++(int) + { + __hash_iterator __t(*this); + ++(*this); + return __t; + } + + friend _LIBCPP_INLINE_VISIBILITY + bool operator==(const __hash_iterator& __x, const __hash_iterator& __y) + {return __x.__node_ == __y.__node_;} + friend _LIBCPP_INLINE_VISIBILITY + bool operator!=(const __hash_iterator& __x, const __hash_iterator& __y) + {return __x.__node_ != __y.__node_;} + +private: + _LIBCPP_INLINE_VISIBILITY + __hash_iterator(__node_pointer __node) _NOEXCEPT + : __node_(__node) + {} + + template <class, class, class, class> friend class __hash_table; + template <class> friend class _LIBCPP_VISIBLE __hash_const_iterator; + template <class> friend class _LIBCPP_VISIBLE __hash_map_iterator; + template <class, class, class, class, class> friend class _LIBCPP_VISIBLE unordered_map; + template <class, class, class, class, class> friend class _LIBCPP_VISIBLE unordered_multimap; +}; + +template <class _ConstNodePtr> +class _LIBCPP_VISIBLE __hash_const_iterator +{ + typedef _ConstNodePtr __node_pointer; + + __node_pointer __node_; + + typedef typename remove_const< + typename pointer_traits<__node_pointer>::element_type + >::type __node; + +public: + typedef forward_iterator_tag iterator_category; + typedef typename __node::value_type value_type; + typedef typename pointer_traits<__node_pointer>::difference_type difference_type; + typedef const value_type& reference; + typedef typename pointer_traits<__node_pointer>::template +#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES + rebind<const value_type> +#else + rebind<const value_type>::other +#endif + pointer; + typedef typename pointer_traits<__node_pointer>::template +#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES + rebind<__node> +#else + rebind<__node>::other +#endif + __non_const_node_pointer; + typedef __hash_iterator<__non_const_node_pointer> __non_const_iterator; + + _LIBCPP_INLINE_VISIBILITY __hash_const_iterator() _NOEXCEPT {} + _LIBCPP_INLINE_VISIBILITY + __hash_const_iterator(const __non_const_iterator& __x) _NOEXCEPT + : __node_(__x.__node_) + {} + + _LIBCPP_INLINE_VISIBILITY + reference operator*() const {return __node_->__value_;} + _LIBCPP_INLINE_VISIBILITY + pointer operator->() const {return _VSTD::addressof(__node_->__value_);} + + _LIBCPP_INLINE_VISIBILITY + __hash_const_iterator& operator++() + { + __node_ = __node_->__next_; + return *this; + } + + _LIBCPP_INLINE_VISIBILITY + __hash_const_iterator operator++(int) + { + __hash_const_iterator __t(*this); + ++(*this); + return __t; + } + + friend _LIBCPP_INLINE_VISIBILITY + bool operator==(const __hash_const_iterator& __x, const __hash_const_iterator& __y) + {return __x.__node_ == __y.__node_;} + friend _LIBCPP_INLINE_VISIBILITY + bool operator!=(const __hash_const_iterator& __x, const __hash_const_iterator& __y) + {return __x.__node_ != __y.__node_;} + +private: + _LIBCPP_INLINE_VISIBILITY + __hash_const_iterator(__node_pointer __node) _NOEXCEPT + : __node_(__node) + {} + + template <class, class, class, class> friend class __hash_table; + template <class> friend class _LIBCPP_VISIBLE __hash_map_const_iterator; + template <class, class, class, class, class> friend class _LIBCPP_VISIBLE unordered_map; + template <class, class, class, class, class> friend class _LIBCPP_VISIBLE unordered_multimap; +}; + +template <class _ConstNodePtr> class _LIBCPP_VISIBLE __hash_const_local_iterator; + +template <class _NodePtr> +class _LIBCPP_VISIBLE __hash_local_iterator +{ + typedef _NodePtr __node_pointer; + + __node_pointer __node_; + size_t __bucket_; + size_t __bucket_count_; + + typedef pointer_traits<__node_pointer> __pointer_traits; +public: + typedef forward_iterator_tag iterator_category; + typedef typename __pointer_traits::element_type::value_type value_type; + typedef typename __pointer_traits::difference_type difference_type; + typedef value_type& reference; + typedef typename __pointer_traits::template +#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES + rebind<value_type> +#else + rebind<value_type>::other +#endif + pointer; + + _LIBCPP_INLINE_VISIBILITY __hash_local_iterator() _NOEXCEPT {} + + _LIBCPP_INLINE_VISIBILITY + reference operator*() const {return __node_->__value_;} + _LIBCPP_INLINE_VISIBILITY + pointer operator->() const {return &__node_->__value_;} + + _LIBCPP_INLINE_VISIBILITY + __hash_local_iterator& operator++() + { + __node_ = __node_->__next_; + if (__node_ != nullptr && __node_->__hash_ % __bucket_count_ != __bucket_) + __node_ = nullptr; + return *this; + } + + _LIBCPP_INLINE_VISIBILITY + __hash_local_iterator operator++(int) + { + __hash_local_iterator __t(*this); + ++(*this); + return __t; + } + + friend _LIBCPP_INLINE_VISIBILITY + bool operator==(const __hash_local_iterator& __x, const __hash_local_iterator& __y) + {return __x.__node_ == __y.__node_;} + friend _LIBCPP_INLINE_VISIBILITY + bool operator!=(const __hash_local_iterator& __x, const __hash_local_iterator& __y) + {return __x.__node_ != __y.__node_;} + +private: + _LIBCPP_INLINE_VISIBILITY + __hash_local_iterator(__node_pointer __node, size_t __bucket, + size_t __bucket_count) _NOEXCEPT + : __node_(__node), + __bucket_(__bucket), + __bucket_count_(__bucket_count) + { + if (__node_ != nullptr) + __node_ = __node_->__next_; + } + + template <class, class, class, class> friend class __hash_table; + template <class> friend class _LIBCPP_VISIBLE __hash_const_local_iterator; + template <class> friend class _LIBCPP_VISIBLE __hash_map_iterator; +}; + +template <class _ConstNodePtr> +class _LIBCPP_VISIBLE __hash_const_local_iterator +{ + typedef _ConstNodePtr __node_pointer; + + __node_pointer __node_; + size_t __bucket_; + size_t __bucket_count_; + + typedef pointer_traits<__node_pointer> __pointer_traits; + typedef typename __pointer_traits::element_type __node; + typedef typename remove_const<__node>::type __non_const_node; + typedef typename pointer_traits<__node_pointer>::template +#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES + rebind<__non_const_node> +#else + rebind<__non_const_node>::other +#endif + __non_const_node_pointer; + typedef __hash_local_iterator<__non_const_node_pointer> + __non_const_iterator; +public: + typedef forward_iterator_tag iterator_category; + typedef typename remove_const< + typename __pointer_traits::element_type::value_type + >::type value_type; + typedef typename __pointer_traits::difference_type difference_type; + typedef const value_type& reference; + typedef typename __pointer_traits::template +#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES + rebind<const value_type> +#else + rebind<const value_type>::other +#endif + pointer; + + _LIBCPP_INLINE_VISIBILITY __hash_const_local_iterator() _NOEXCEPT {} + _LIBCPP_INLINE_VISIBILITY + __hash_const_local_iterator(const __non_const_iterator& __x) _NOEXCEPT + : __node_(__x.__node_), + __bucket_(__x.__bucket_), + __bucket_count_(__x.__bucket_count_) + {} + + _LIBCPP_INLINE_VISIBILITY + reference operator*() const {return __node_->__value_;} + _LIBCPP_INLINE_VISIBILITY + pointer operator->() const {return &__node_->__value_;} + + _LIBCPP_INLINE_VISIBILITY + __hash_const_local_iterator& operator++() + { + __node_ = __node_->__next_; + if (__node_ != nullptr && __node_->__hash_ % __bucket_count_ != __bucket_) + __node_ = nullptr; + return *this; + } + + _LIBCPP_INLINE_VISIBILITY + __hash_const_local_iterator operator++(int) + { + __hash_const_local_iterator __t(*this); + ++(*this); + return __t; + } + + friend _LIBCPP_INLINE_VISIBILITY + bool operator==(const __hash_const_local_iterator& __x, const __hash_const_local_iterator& __y) + {return __x.__node_ == __y.__node_;} + friend _LIBCPP_INLINE_VISIBILITY + bool operator!=(const __hash_const_local_iterator& __x, const __hash_const_local_iterator& __y) + {return __x.__node_ != __y.__node_;} + +private: + _LIBCPP_INLINE_VISIBILITY + __hash_const_local_iterator(__node_pointer __node, size_t __bucket, + size_t __bucket_count) _NOEXCEPT + : __node_(__node), + __bucket_(__bucket), + __bucket_count_(__bucket_count) + { + if (__node_ != nullptr) + __node_ = __node_->__next_; + } + + template <class, class, class, class> friend class __hash_table; + template <class> friend class _LIBCPP_VISIBLE __hash_map_const_iterator; +}; + +template <class _Alloc> +class __bucket_list_deallocator +{ + typedef _Alloc allocator_type; + typedef allocator_traits<allocator_type> __alloc_traits; + typedef typename __alloc_traits::size_type size_type; + + __compressed_pair<size_type, allocator_type> __data_; +public: + typedef typename __alloc_traits::pointer pointer; + + _LIBCPP_INLINE_VISIBILITY + __bucket_list_deallocator() + _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value) + : __data_(0) {} + + _LIBCPP_INLINE_VISIBILITY + __bucket_list_deallocator(const allocator_type& __a, size_type __size) + _NOEXCEPT_(is_nothrow_copy_constructible<allocator_type>::value) + : __data_(__size, __a) {} + +#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES + + _LIBCPP_INLINE_VISIBILITY + __bucket_list_deallocator(__bucket_list_deallocator&& __x) + _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value) + : __data_(_VSTD::move(__x.__data_)) + { + __x.size() = 0; + } + +#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES + + _LIBCPP_INLINE_VISIBILITY + size_type& size() _NOEXCEPT {return __data_.first();} + _LIBCPP_INLINE_VISIBILITY + size_type size() const _NOEXCEPT {return __data_.first();} + + _LIBCPP_INLINE_VISIBILITY + allocator_type& __alloc() _NOEXCEPT {return __data_.second();} + _LIBCPP_INLINE_VISIBILITY + const allocator_type& __alloc() const _NOEXCEPT {return __data_.second();} + + _LIBCPP_INLINE_VISIBILITY + void operator()(pointer __p) _NOEXCEPT + { + __alloc_traits::deallocate(__alloc(), __p, size()); + } +}; + +template <class _Alloc> class __hash_map_node_destructor; + +template <class _Alloc> +class __hash_node_destructor +{ + typedef _Alloc allocator_type; + typedef allocator_traits<allocator_type> __alloc_traits; + typedef typename __alloc_traits::value_type::value_type value_type; +public: + typedef typename __alloc_traits::pointer pointer; +private: + + allocator_type& __na_; + + __hash_node_destructor& operator=(const __hash_node_destructor&); + +public: + bool __value_constructed; + + _LIBCPP_INLINE_VISIBILITY + explicit __hash_node_destructor(allocator_type& __na, + bool __constructed = false) _NOEXCEPT + : __na_(__na), + __value_constructed(__constructed) + {} + + _LIBCPP_INLINE_VISIBILITY + void operator()(pointer __p) _NOEXCEPT + { + if (__value_constructed) + __alloc_traits::destroy(__na_, _VSTD::addressof(__p->__value_)); + if (__p) + __alloc_traits::deallocate(__na_, __p, 1); + } + + template <class> friend class __hash_map_node_destructor; +}; + +template <class _Tp, class _Hash, class _Equal, class _Alloc> +class __hash_table +{ +public: + typedef _Tp value_type; + typedef _Hash hasher; + typedef _Equal key_equal; + typedef _Alloc allocator_type; + +private: + typedef allocator_traits<allocator_type> __alloc_traits; +public: + typedef value_type& reference; + typedef const value_type& const_reference; + typedef typename __alloc_traits::pointer pointer; + typedef typename __alloc_traits::const_pointer const_pointer; + typedef typename __alloc_traits::size_type size_type; + typedef typename __alloc_traits::difference_type difference_type; +public: + // Create __node + typedef __hash_node<value_type, typename __alloc_traits::void_pointer> __node; + typedef typename __node::__first_node __first_node; + typedef typename __alloc_traits::template +#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES + rebind_alloc<__node> +#else + rebind_alloc<__node>::other +#endif + __node_allocator; + typedef allocator_traits<__node_allocator> __node_traits; + typedef typename __node_traits::pointer __node_pointer; + typedef typename __node_traits::const_pointer __node_const_pointer; + +private: + + typedef typename __node_traits::template +#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES + rebind_alloc<__node_pointer> +#else + rebind_alloc<__node_pointer>::other +#endif + __pointer_allocator; + typedef __bucket_list_deallocator<__pointer_allocator> __bucket_list_deleter; + typedef unique_ptr<__node_pointer[], __bucket_list_deleter> __bucket_list; + typedef allocator_traits<__pointer_allocator> __pointer_alloc_traits; + typedef typename __bucket_list_deleter::pointer __node_pointer_pointer; + + // --- Member data begin --- + __bucket_list __bucket_list_; + __compressed_pair<__first_node, __node_allocator> __p1_; + __compressed_pair<size_type, hasher> __p2_; + __compressed_pair<float, key_equal> __p3_; + // --- Member data end --- + + _LIBCPP_INLINE_VISIBILITY + size_type& size() _NOEXCEPT {return __p2_.first();} +public: + _LIBCPP_INLINE_VISIBILITY + size_type size() const _NOEXCEPT {return __p2_.first();} + + _LIBCPP_INLINE_VISIBILITY + hasher& hash_function() _NOEXCEPT {return __p2_.second();} + _LIBCPP_INLINE_VISIBILITY + const hasher& hash_function() const _NOEXCEPT {return __p2_.second();} + + _LIBCPP_INLINE_VISIBILITY + float& max_load_factor() _NOEXCEPT {return __p3_.first();} + _LIBCPP_INLINE_VISIBILITY + float max_load_factor() const _NOEXCEPT {return __p3_.first();} + + _LIBCPP_INLINE_VISIBILITY + key_equal& key_eq() _NOEXCEPT {return __p3_.second();} + _LIBCPP_INLINE_VISIBILITY + const key_equal& key_eq() const _NOEXCEPT {return __p3_.second();} + + _LIBCPP_INLINE_VISIBILITY + __node_allocator& __node_alloc() _NOEXCEPT {return __p1_.second();} + _LIBCPP_INLINE_VISIBILITY + const __node_allocator& __node_alloc() const _NOEXCEPT + {return __p1_.second();} + +public: + typedef __hash_iterator<__node_pointer> iterator; + typedef __hash_const_iterator<__node_const_pointer> const_iterator; + typedef __hash_local_iterator<__node_pointer> local_iterator; + typedef __hash_const_local_iterator<__node_const_pointer> const_local_iterator; + + __hash_table() + _NOEXCEPT_( + is_nothrow_default_constructible<__bucket_list>::value && + is_nothrow_default_constructible<__first_node>::value && + is_nothrow_default_constructible<__node_allocator>::value && + is_nothrow_default_constructible<hasher>::value && + is_nothrow_default_constructible<key_equal>::value); + __hash_table(const hasher& __hf, const key_equal& __eql); + __hash_table(const hasher& __hf, const key_equal& __eql, + const allocator_type& __a); + explicit __hash_table(const allocator_type& __a); + __hash_table(const __hash_table& __u); + __hash_table(const __hash_table& __u, const allocator_type& __a); +#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES + __hash_table(__hash_table&& __u) + _NOEXCEPT_( + is_nothrow_move_constructible<__bucket_list>::value && + is_nothrow_move_constructible<__first_node>::value && + is_nothrow_move_constructible<__node_allocator>::value && + is_nothrow_move_constructible<hasher>::value && + is_nothrow_move_constructible<key_equal>::value); + __hash_table(__hash_table&& __u, const allocator_type& __a); +#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES + ~__hash_table(); + + __hash_table& operator=(const __hash_table& __u); +#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES + __hash_table& operator=(__hash_table&& __u) + _NOEXCEPT_( + __node_traits::propagate_on_container_move_assignment::value && + is_nothrow_move_assignable<__node_allocator>::value && + is_nothrow_move_assignable<hasher>::value && + is_nothrow_move_assignable<key_equal>::value); +#endif + template <class _InputIterator> + void __assign_unique(_InputIterator __first, _InputIterator __last); + template <class _InputIterator> + void __assign_multi(_InputIterator __first, _InputIterator __last); + + _LIBCPP_INLINE_VISIBILITY + size_type max_size() const _NOEXCEPT + { + return allocator_traits<__pointer_allocator>::max_size( + __bucket_list_.get_deleter().__alloc()); + } + + pair<iterator, bool> __node_insert_unique(__node_pointer __nd); + iterator __node_insert_multi(__node_pointer __nd); + iterator __node_insert_multi(const_iterator __p, + __node_pointer __nd); + +#if !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS) + template <class... _Args> + pair<iterator, bool> __emplace_unique(_Args&&... __args); + template <class... _Args> + iterator __emplace_multi(_Args&&... __args); + template <class... _Args> + iterator __emplace_hint_multi(const_iterator __p, _Args&&... __args); +#endif // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS) + + pair<iterator, bool> __insert_unique(const value_type& __x); + +#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES + template <class _P> + pair<iterator, bool> __insert_unique(_P&& __x); +#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES + +#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES + template <class _P> + iterator __insert_multi(_P&& __x); + template <class _P> + iterator __insert_multi(const_iterator __p, _P&& __x); +#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES + iterator __insert_multi(const value_type& __x); + iterator __insert_multi(const_iterator __p, const value_type& __x); +#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES + + void clear() _NOEXCEPT; + void rehash(size_type __n); + _LIBCPP_INLINE_VISIBILITY void reserve(size_type __n) + {rehash(static_cast<size_type>(ceil(__n / max_load_factor())));} + + _LIBCPP_INLINE_VISIBILITY + size_type bucket_count() const _NOEXCEPT + { + return __bucket_list_.get_deleter().size(); + } + + iterator begin() _NOEXCEPT; + iterator end() _NOEXCEPT; + const_iterator begin() const _NOEXCEPT; + const_iterator end() const _NOEXCEPT; + + template <class _Key> + _LIBCPP_INLINE_VISIBILITY + size_type bucket(const _Key& __k) const + {return hash_function()(__k) % bucket_count();} + + template <class _Key> + iterator find(const _Key& __x); + template <class _Key> + const_iterator find(const _Key& __x) const; + + typedef __hash_node_destructor<__node_allocator> _D; + typedef unique_ptr<__node, _D> __node_holder; + + iterator erase(const_iterator __p); + iterator erase(const_iterator __first, const_iterator __last); + template <class _Key> + size_type __erase_unique(const _Key& __k); + template <class _Key> + size_type __erase_multi(const _Key& __k); + __node_holder remove(const_iterator __p) _NOEXCEPT; + + template <class _Key> + size_type __count_unique(const _Key& __k) const; + template <class _Key> + size_type __count_multi(const _Key& __k) const; + + template <class _Key> + pair<iterator, iterator> + __equal_range_unique(const _Key& __k); + template <class _Key> + pair<const_iterator, const_iterator> + __equal_range_unique(const _Key& __k) const; + + template <class _Key> + pair<iterator, iterator> + __equal_range_multi(const _Key& __k); + template <class _Key> + pair<const_iterator, const_iterator> + __equal_range_multi(const _Key& __k) const; + + void swap(__hash_table& __u) + _NOEXCEPT_( + (!allocator_traits<__pointer_allocator>::propagate_on_container_swap::value || + __is_nothrow_swappable<__pointer_allocator>::value) && + (!__node_traits::propagate_on_container_swap::value || + __is_nothrow_swappable<__node_allocator>::value) && + __is_nothrow_swappable<hasher>::value && + __is_nothrow_swappable<key_equal>::value); + + _LIBCPP_INLINE_VISIBILITY + size_type max_bucket_count() const _NOEXCEPT + {return __bucket_list_.get_deleter().__alloc().max_size();} + size_type bucket_size(size_type __n) const; + _LIBCPP_INLINE_VISIBILITY float load_factor() const _NOEXCEPT + { + size_type __bc = bucket_count(); + return __bc != 0 ? (float)size() / __bc : 0.f; + } + _LIBCPP_INLINE_VISIBILITY void max_load_factor(float __mlf) _NOEXCEPT + {max_load_factor() = _VSTD::max(__mlf, load_factor());} + + _LIBCPP_INLINE_VISIBILITY local_iterator begin(size_type __n) + {return local_iterator(__bucket_list_[__n], __n, bucket_count());} + _LIBCPP_INLINE_VISIBILITY local_iterator end(size_type __n) + {return local_iterator(nullptr, __n, bucket_count());} + _LIBCPP_INLINE_VISIBILITY const_local_iterator cbegin(size_type __n) const + {return const_local_iterator(__bucket_list_[__n], __n, bucket_count());} + _LIBCPP_INLINE_VISIBILITY const_local_iterator cend(size_type __n) const + {return const_local_iterator(nullptr, __n, bucket_count());} +private: + void __rehash(size_type __n); + +#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES +#ifndef _LIBCPP_HAS_NO_VARIADICS + template <class ..._Args> + __node_holder __construct_node(_Args&& ...__args); +#endif // _LIBCPP_HAS_NO_VARIADICS + __node_holder __construct_node(value_type&& __v, size_t __hash); +#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES + __node_holder __construct_node(const value_type& __v); +#endif + __node_holder __construct_node(const value_type& __v, size_t __hash); + + _LIBCPP_INLINE_VISIBILITY + void __copy_assign_alloc(const __hash_table& __u) + {__copy_assign_alloc(__u, integral_constant<bool, + __node_traits::propagate_on_container_copy_assignment::value>());} + void __copy_assign_alloc(const __hash_table& __u, true_type); + _LIBCPP_INLINE_VISIBILITY + void __copy_assign_alloc(const __hash_table& __u, false_type) {} + + void __move_assign(__hash_table& __u, false_type); + void __move_assign(__hash_table& __u, true_type) + _NOEXCEPT_( + is_nothrow_move_assignable<__node_allocator>::value && + is_nothrow_move_assignable<hasher>::value && + is_nothrow_move_assignable<key_equal>::value); + _LIBCPP_INLINE_VISIBILITY + void __move_assign_alloc(__hash_table& __u) + _NOEXCEPT_( + !__node_traits::propagate_on_container_move_assignment::value || + (is_nothrow_move_assignable<__pointer_allocator>::value && + is_nothrow_move_assignable<__node_allocator>::value)) + {__move_assign_alloc(__u, integral_constant<bool, + __node_traits::propagate_on_container_move_assignment::value>());} + _LIBCPP_INLINE_VISIBILITY + void __move_assign_alloc(__hash_table& __u, true_type) + _NOEXCEPT_( + is_nothrow_move_assignable<__pointer_allocator>::value && + is_nothrow_move_assignable<__node_allocator>::value) + { + __bucket_list_.get_deleter().__alloc() = + _VSTD::move(__u.__bucket_list_.get_deleter().__alloc()); + __node_alloc() = _VSTD::move(__u.__node_alloc()); + } + _LIBCPP_INLINE_VISIBILITY + void __move_assign_alloc(__hash_table&, false_type) _NOEXCEPT {} + + template <class _A> + _LIBCPP_INLINE_VISIBILITY + static + void + __swap_alloc(_A& __x, _A& __y) + _NOEXCEPT_( + !allocator_traits<_A>::propagate_on_container_swap::value || + __is_nothrow_swappable<_A>::value) + { + __swap_alloc(__x, __y, + integral_constant<bool, + allocator_traits<_A>::propagate_on_container_swap::value + >()); + } + + template <class _A> + _LIBCPP_INLINE_VISIBILITY + static + void + __swap_alloc(_A& __x, _A& __y, true_type) + _NOEXCEPT_(__is_nothrow_swappable<_A>::value) + { + using _VSTD::swap; + swap(__x, __y); + } + + template <class _A> + _LIBCPP_INLINE_VISIBILITY + static + void + __swap_alloc(_A& __x, _A& __y, false_type) _NOEXCEPT {} + + void __deallocate(__node_pointer __np) _NOEXCEPT; + __node_pointer __detach() _NOEXCEPT; +}; + +template <class _Tp, class _Hash, class _Equal, class _Alloc> +inline _LIBCPP_INLINE_VISIBILITY +__hash_table<_Tp, _Hash, _Equal, _Alloc>::__hash_table() + _NOEXCEPT_( + is_nothrow_default_constructible<__bucket_list>::value && + is_nothrow_default_constructible<__first_node>::value && + is_nothrow_default_constructible<hasher>::value && + is_nothrow_default_constructible<key_equal>::value) + : __p2_(0), + __p3_(1.0f) +{ +} + +template <class _Tp, class _Hash, class _Equal, class _Alloc> +inline _LIBCPP_INLINE_VISIBILITY +__hash_table<_Tp, _Hash, _Equal, _Alloc>::__hash_table(const hasher& __hf, + const key_equal& __eql) + : __bucket_list_(nullptr, __bucket_list_deleter()), + __p1_(), + __p2_(0, __hf), + __p3_(1.0f, __eql) +{ +} + +template <class _Tp, class _Hash, class _Equal, class _Alloc> +__hash_table<_Tp, _Hash, _Equal, _Alloc>::__hash_table(const hasher& __hf, + const key_equal& __eql, + const allocator_type& __a) + : __bucket_list_(nullptr, __bucket_list_deleter(__pointer_allocator(__a), 0)), + __p1_(__node_allocator(__a)), + __p2_(0, __hf), + __p3_(1.0f, __eql) +{ +} + +template <class _Tp, class _Hash, class _Equal, class _Alloc> +__hash_table<_Tp, _Hash, _Equal, _Alloc>::__hash_table(const allocator_type& __a) + : __bucket_list_(nullptr, __bucket_list_deleter(__pointer_allocator(__a), 0)), + __p1_(__node_allocator(__a)), + __p2_(0), + __p3_(1.0f) +{ +} + +template <class _Tp, class _Hash, class _Equal, class _Alloc> +__hash_table<_Tp, _Hash, _Equal, _Alloc>::__hash_table(const __hash_table& __u) + : __bucket_list_(nullptr, + __bucket_list_deleter(allocator_traits<__pointer_allocator>:: + select_on_container_copy_construction( + __u.__bucket_list_.get_deleter().__alloc()), 0)), + __p1_(allocator_traits<__node_allocator>:: + select_on_container_copy_construction(__u.__node_alloc())), + __p2_(0, __u.hash_function()), + __p3_(__u.__p3_) +{ +} + +template <class _Tp, class _Hash, class _Equal, class _Alloc> +__hash_table<_Tp, _Hash, _Equal, _Alloc>::__hash_table(const __hash_table& __u, + const allocator_type& __a) + : __bucket_list_(nullptr, __bucket_list_deleter(__pointer_allocator(__a), 0)), + __p1_(__node_allocator(__a)), + __p2_(0, __u.hash_function()), + __p3_(__u.__p3_) +{ +} + +#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES + +template <class _Tp, class _Hash, class _Equal, class _Alloc> +__hash_table<_Tp, _Hash, _Equal, _Alloc>::__hash_table(__hash_table&& __u) + _NOEXCEPT_( + is_nothrow_move_constructible<__bucket_list>::value && + is_nothrow_move_constructible<__first_node>::value && + is_nothrow_move_constructible<hasher>::value && + is_nothrow_move_constructible<key_equal>::value) + : __bucket_list_(_VSTD::move(__u.__bucket_list_)), + __p1_(_VSTD::move(__u.__p1_)), + __p2_(_VSTD::move(__u.__p2_)), + __p3_(_VSTD::move(__u.__p3_)) +{ + if (size() > 0) + { + __bucket_list_[__p1_.first().__next_->__hash_ % bucket_count()] = + static_cast<__node_pointer>(_VSTD::addressof(__p1_.first())); + __u.__p1_.first().__next_ = nullptr; + __u.size() = 0; + } +} + +template <class _Tp, class _Hash, class _Equal, class _Alloc> +__hash_table<_Tp, _Hash, _Equal, _Alloc>::__hash_table(__hash_table&& __u, + const allocator_type& __a) + : __bucket_list_(nullptr, __bucket_list_deleter(__pointer_allocator(__a), 0)), + __p1_(__node_allocator(__a)), + __p2_(0, _VSTD::move(__u.hash_function())), + __p3_(_VSTD::move(__u.__p3_)) +{ + if (__a == allocator_type(__u.__node_alloc())) + { + __bucket_list_.reset(__u.__bucket_list_.release()); + __bucket_list_.get_deleter().size() = __u.__bucket_list_.get_deleter().size(); + __u.__bucket_list_.get_deleter().size() = 0; + if (__u.size() > 0) + { + __p1_.first().__next_ = __u.__p1_.first().__next_; + __u.__p1_.first().__next_ = nullptr; + __bucket_list_[__p1_.first().__next_->__hash_ % bucket_count()] = + static_cast<__node_pointer>(_VSTD::addressof(__p1_.first())); + size() = __u.size(); + __u.size() = 0; + } + } +} + +#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES + +template <class _Tp, class _Hash, class _Equal, class _Alloc> +__hash_table<_Tp, _Hash, _Equal, _Alloc>::~__hash_table() +{ + __deallocate(__p1_.first().__next_); +} + +template <class _Tp, class _Hash, class _Equal, class _Alloc> +void +__hash_table<_Tp, _Hash, _Equal, _Alloc>::__copy_assign_alloc( + const __hash_table& __u, true_type) +{ + if (__node_alloc() != __u.__node_alloc()) + { + clear(); + __bucket_list_.reset(); + __bucket_list_.get_deleter().size() = 0; + } + __bucket_list_.get_deleter().__alloc() = __u.__bucket_list_.get_deleter().__alloc(); + __node_alloc() = __u.__node_alloc(); +} + +template <class _Tp, class _Hash, class _Equal, class _Alloc> +__hash_table<_Tp, _Hash, _Equal, _Alloc>& +__hash_table<_Tp, _Hash, _Equal, _Alloc>::operator=(const __hash_table& __u) +{ + if (this != &__u) + { + __copy_assign_alloc(__u); + hash_function() = __u.hash_function(); + key_eq() = __u.key_eq(); + max_load_factor() = __u.max_load_factor(); + __assign_multi(__u.begin(), __u.end()); + } + return *this; +} + +template <class _Tp, class _Hash, class _Equal, class _Alloc> +void +__hash_table<_Tp, _Hash, _Equal, _Alloc>::__deallocate(__node_pointer __np) + _NOEXCEPT +{ + __node_allocator& __na = __node_alloc(); + while (__np != nullptr) + { + __node_pointer __next = __np->__next_; + __node_traits::destroy(__na, _VSTD::addressof(__np->__value_)); + __node_traits::deallocate(__na, __np, 1); + __np = __next; + } +} + +template <class _Tp, class _Hash, class _Equal, class _Alloc> +typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_pointer +__hash_table<_Tp, _Hash, _Equal, _Alloc>::__detach() _NOEXCEPT +{ + size_type __bc = bucket_count(); + for (size_type __i = 0; __i < __bc; ++__i) + __bucket_list_[__i] = nullptr; + size() = 0; + __node_pointer __cache = __p1_.first().__next_; + __p1_.first().__next_ = nullptr; + return __cache; +} + +#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES + +template <class _Tp, class _Hash, class _Equal, class _Alloc> +void +__hash_table<_Tp, _Hash, _Equal, _Alloc>::__move_assign( + __hash_table& __u, true_type) + _NOEXCEPT_( + is_nothrow_move_assignable<__node_allocator>::value && + is_nothrow_move_assignable<hasher>::value && + is_nothrow_move_assignable<key_equal>::value) +{ + clear(); + __bucket_list_.reset(__u.__bucket_list_.release()); + __bucket_list_.get_deleter().size() = __u.__bucket_list_.get_deleter().size(); + __u.__bucket_list_.get_deleter().size() = 0; + __move_assign_alloc(__u); + size() = __u.size(); + hash_function() = _VSTD::move(__u.hash_function()); + max_load_factor() = __u.max_load_factor(); + key_eq() = _VSTD::move(__u.key_eq()); + __p1_.first().__next_ = __u.__p1_.first().__next_; + if (size() > 0) + { + __bucket_list_[__p1_.first().__next_->__hash_ % bucket_count()] = + static_cast<__node_pointer>(_VSTD::addressof(__p1_.first())); + __u.__p1_.first().__next_ = nullptr; + __u.size() = 0; + } +} + +template <class _Tp, class _Hash, class _Equal, class _Alloc> +void +__hash_table<_Tp, _Hash, _Equal, _Alloc>::__move_assign( + __hash_table& __u, false_type) +{ + if (__node_alloc() == __u.__node_alloc()) + __move_assign(__u, true_type()); + else + { + hash_function() = _VSTD::move(__u.hash_function()); + key_eq() = _VSTD::move(__u.key_eq()); + max_load_factor() = __u.max_load_factor(); + if (bucket_count() != 0) + { + __node_pointer __cache = __detach(); +#ifndef _LIBCPP_NO_EXCEPTIONS + try + { +#endif // _LIBCPP_NO_EXCEPTIONS + const_iterator __i = __u.begin(); + while (__cache != nullptr && __u.size() != 0) + { + __cache->__value_ = _VSTD::move(__u.remove(__i++)->__value_); + __node_pointer __next = __cache->__next_; + __node_insert_multi(__cache); + __cache = __next; + } +#ifndef _LIBCPP_NO_EXCEPTIONS + } + catch (...) + { + __deallocate(__cache); + throw; + } +#endif // _LIBCPP_NO_EXCEPTIONS + __deallocate(__cache); + } + const_iterator __i = __u.begin(); + while (__u.size() != 0) + { + __node_holder __h = + __construct_node(_VSTD::move(__u.remove(__i++)->__value_)); + __node_insert_multi(__h.get()); + __h.release(); + } + } +} + +template <class _Tp, class _Hash, class _Equal, class _Alloc> +inline _LIBCPP_INLINE_VISIBILITY +__hash_table<_Tp, _Hash, _Equal, _Alloc>& +__hash_table<_Tp, _Hash, _Equal, _Alloc>::operator=(__hash_table&& __u) + _NOEXCEPT_( + __node_traits::propagate_on_container_move_assignment::value && + is_nothrow_move_assignable<__node_allocator>::value && + is_nothrow_move_assignable<hasher>::value && + is_nothrow_move_assignable<key_equal>::value) +{ + __move_assign(__u, integral_constant<bool, + __node_traits::propagate_on_container_move_assignment::value>()); + return *this; +} + +#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES + +template <class _Tp, class _Hash, class _Equal, class _Alloc> +template <class _InputIterator> +void +__hash_table<_Tp, _Hash, _Equal, _Alloc>::__assign_unique(_InputIterator __first, + _InputIterator __last) +{ + if (bucket_count() != 0) + { + __node_pointer __cache = __detach(); +#ifndef _LIBCPP_NO_EXCEPTIONS + try + { +#endif // _LIBCPP_NO_EXCEPTIONS + for (; __cache != nullptr && __first != __last; ++__first) + { + __cache->__value_ = *__first; + __node_pointer __next = __cache->__next_; + __node_insert_unique(__cache); + __cache = __next; + } +#ifndef _LIBCPP_NO_EXCEPTIONS + } + catch (...) + { + __deallocate(__cache); + throw; + } +#endif // _LIBCPP_NO_EXCEPTIONS + __deallocate(__cache); + } + for (; __first != __last; ++__first) + __insert_unique(*__first); +} + +template <class _Tp, class _Hash, class _Equal, class _Alloc> +template <class _InputIterator> +void +__hash_table<_Tp, _Hash, _Equal, _Alloc>::__assign_multi(_InputIterator __first, + _InputIterator __last) +{ + if (bucket_count() != 0) + { + __node_pointer __cache = __detach(); +#ifndef _LIBCPP_NO_EXCEPTIONS + try + { +#endif // _LIBCPP_NO_EXCEPTIONS + for (; __cache != nullptr && __first != __last; ++__first) + { + __cache->__value_ = *__first; + __node_pointer __next = __cache->__next_; + __node_insert_multi(__cache); + __cache = __next; + } +#ifndef _LIBCPP_NO_EXCEPTIONS + } + catch (...) + { + __deallocate(__cache); + throw; + } +#endif // _LIBCPP_NO_EXCEPTIONS + __deallocate(__cache); + } + for (; __first != __last; ++__first) + __insert_multi(*__first); +} + +template <class _Tp, class _Hash, class _Equal, class _Alloc> +inline _LIBCPP_INLINE_VISIBILITY +typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator +__hash_table<_Tp, _Hash, _Equal, _Alloc>::begin() _NOEXCEPT +{ + return iterator(__p1_.first().__next_); +} + +template <class _Tp, class _Hash, class _Equal, class _Alloc> +inline _LIBCPP_INLINE_VISIBILITY +typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator +__hash_table<_Tp, _Hash, _Equal, _Alloc>::end() _NOEXCEPT +{ + return iterator(nullptr); +} + +template <class _Tp, class _Hash, class _Equal, class _Alloc> +inline _LIBCPP_INLINE_VISIBILITY +typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::const_iterator +__hash_table<_Tp, _Hash, _Equal, _Alloc>::begin() const _NOEXCEPT +{ + return const_iterator(__p1_.first().__next_); +} + +template <class _Tp, class _Hash, class _Equal, class _Alloc> +inline _LIBCPP_INLINE_VISIBILITY +typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::const_iterator +__hash_table<_Tp, _Hash, _Equal, _Alloc>::end() const _NOEXCEPT +{ + return const_iterator(nullptr); +} + +template <class _Tp, class _Hash, class _Equal, class _Alloc> +void +__hash_table<_Tp, _Hash, _Equal, _Alloc>::clear() _NOEXCEPT +{ + if (size() > 0) + { + __deallocate(__p1_.first().__next_); + __p1_.first().__next_ = nullptr; + size_type __bc = bucket_count(); + for (size_type __i = 0; __i < __bc; ++__i) + __bucket_list_[__i] = nullptr; + size() = 0; + } +} + +template <class _Tp, class _Hash, class _Equal, class _Alloc> +pair<typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator, bool> +__hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_insert_unique(__node_pointer __nd) +{ + __nd->__hash_ = hash_function()(__nd->__value_); + size_type __bc = bucket_count(); + bool __inserted = false; + __node_pointer __ndptr; + size_t __chash; + if (__bc != 0) + { + __chash = __nd->__hash_ % __bc; + __ndptr = __bucket_list_[__chash]; + if (__ndptr != nullptr) + { + for (__ndptr = __ndptr->__next_; __ndptr != nullptr && + __ndptr->__hash_ % __bc == __chash; + __ndptr = __ndptr->__next_) + { + if (key_eq()(__ndptr->__value_, __nd->__value_)) + goto __done; + } + } + } + { + if (size()+1 > __bc * max_load_factor() || __bc == 0) + { + rehash(_VSTD::max<size_type>(2 * __bc + 1, + size_type(ceil(float(size() + 1) / max_load_factor())))); + __bc = bucket_count(); + __chash = __nd->__hash_ % __bc; + } + // insert_after __bucket_list_[__chash], or __first_node if bucket is null + __node_pointer __pn = __bucket_list_[__chash]; + if (__pn == nullptr) + { + __pn = static_cast<__node_pointer>(_VSTD::addressof(__p1_.first())); + __nd->__next_ = __pn->__next_; + __pn->__next_ = __nd; + // fix up __bucket_list_ + __bucket_list_[__chash] = __pn; + if (__nd->__next_ != nullptr) + __bucket_list_[__nd->__next_->__hash_ % __bc] = __nd; + } + else + { + __nd->__next_ = __pn->__next_; + __pn->__next_ = __nd; + } + __ndptr = __nd; + // increment size + ++size(); + __inserted = true; + } +__done: + return pair<iterator, bool>(iterator(__ndptr), __inserted); +} + +template <class _Tp, class _Hash, class _Equal, class _Alloc> +typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator +__hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_insert_multi(__node_pointer __cp) +{ + __cp->__hash_ = hash_function()(__cp->__value_); + size_type __bc = bucket_count(); + if (size()+1 > __bc * max_load_factor() || __bc == 0) + { + rehash(_VSTD::max<size_type>(2 * __bc + 1, + size_type(ceil(float(size() + 1) / max_load_factor())))); + __bc = bucket_count(); + } + size_t __chash = __cp->__hash_ % __bc; + __node_pointer __pn = __bucket_list_[__chash]; + if (__pn == nullptr) + { + __pn = static_cast<__node_pointer>(_VSTD::addressof(__p1_.first())); + __cp->__next_ = __pn->__next_; + __pn->__next_ = __cp; + // fix up __bucket_list_ + __bucket_list_[__chash] = __pn; + if (__cp->__next_ != nullptr) + __bucket_list_[__cp->__next_->__hash_ % __bc] = __cp; + } + else + { + for (bool __found = false; __pn->__next_ != nullptr && + __pn->__next_->__hash_ % __bc == __chash; + __pn = __pn->__next_) + { + // __found key_eq() action + // false false loop + // true true loop + // false true set __found to true + // true false break + if (__found != (__pn->__next_->__hash_ == __cp->__hash_ && + key_eq()(__pn->__next_->__value_, __cp->__value_))) + { + if (!__found) + __found = true; + else + break; + } + } + __cp->__next_ = __pn->__next_; + __pn->__next_ = __cp; + if (__cp->__next_ != nullptr) + { + size_t __nhash = __cp->__next_->__hash_ % __bc; + if (__nhash != __chash) + __bucket_list_[__nhash] = __cp; + } + } + ++size(); + return iterator(__cp); +} + +template <class _Tp, class _Hash, class _Equal, class _Alloc> +typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator +__hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_insert_multi( + const_iterator __p, __node_pointer __cp) +{ + if (__p != end() && key_eq()(*__p, __cp->__value_)) + { + __node_pointer __np = const_cast<__node_pointer>(__p.__node_); + __cp->__hash_ = __np->__hash_; + size_type __bc = bucket_count(); + if (size()+1 > __bc * max_load_factor() || __bc == 0) + { + rehash(_VSTD::max<size_type>(2 * __bc + 1, + size_type(ceil(float(size() + 1) / max_load_factor())))); + __bc = bucket_count(); + } + size_t __chash = __cp->__hash_ % __bc; + __node_pointer __pp = __bucket_list_[__chash]; + while (__pp->__next_ != __np) + __pp = __pp->__next_; + __cp->__next_ = __np; + __pp->__next_ = __cp; + ++size(); + return iterator(__cp); + } + return __node_insert_multi(__cp); +} + +template <class _Tp, class _Hash, class _Equal, class _Alloc> +pair<typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator, bool> +__hash_table<_Tp, _Hash, _Equal, _Alloc>::__insert_unique(const value_type& __x) +{ + size_t __hash = hash_function()(__x); + size_type __bc = bucket_count(); + bool __inserted = false; + __node_pointer __nd; + size_t __chash; + if (__bc != 0) + { + __chash = __hash % __bc; + __nd = __bucket_list_[__chash]; + if (__nd != nullptr) + { + for (__nd = __nd->__next_; __nd != nullptr && + __nd->__hash_ % __bc == __chash; + __nd = __nd->__next_) + { + if (key_eq()(__nd->__value_, __x)) + goto __done; + } + } + } + { + __node_holder __h = __construct_node(__x, __hash); + if (size()+1 > __bc * max_load_factor() || __bc == 0) + { + rehash(_VSTD::max<size_type>(2 * __bc + 1, + size_type(ceil(float(size() + 1) / max_load_factor())))); + __bc = bucket_count(); + __chash = __hash % __bc; + } + // insert_after __bucket_list_[__chash], or __first_node if bucket is null + __node_pointer __pn = __bucket_list_[__chash]; + if (__pn == nullptr) + { + __pn = static_cast<__node_pointer>(_VSTD::addressof(__p1_.first())); + __h->__next_ = __pn->__next_; + __pn->__next_ = __h.get(); + // fix up __bucket_list_ + __bucket_list_[__chash] = __pn; + if (__h->__next_ != nullptr) + __bucket_list_[__h->__next_->__hash_ % __bc] = __h.get(); + } + else + { + __h->__next_ = __pn->__next_; + __pn->__next_ = __h.get(); + } + __nd = __h.release(); + // increment size + ++size(); + __inserted = true; + } +__done: + return pair<iterator, bool>(iterator(__nd), __inserted); +} + +#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES +#ifndef _LIBCPP_HAS_NO_VARIADICS + +template <class _Tp, class _Hash, class _Equal, class _Alloc> +template <class... _Args> +pair<typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator, bool> +__hash_table<_Tp, _Hash, _Equal, _Alloc>::__emplace_unique(_Args&&... __args) +{ + __node_holder __h = __construct_node(_VSTD::forward<_Args>(__args)...); + pair<iterator, bool> __r = __node_insert_unique(__h.get()); + if (__r.second) + __h.release(); + return __r; +} + +template <class _Tp, class _Hash, class _Equal, class _Alloc> +template <class... _Args> +typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator +__hash_table<_Tp, _Hash, _Equal, _Alloc>::__emplace_multi(_Args&&... __args) +{ + __node_holder __h = __construct_node(_VSTD::forward<_Args>(__args)...); + iterator __r = __node_insert_multi(__h.get()); + __h.release(); + return __r; +} + +template <class _Tp, class _Hash, class _Equal, class _Alloc> +template <class... _Args> +typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator +__hash_table<_Tp, _Hash, _Equal, _Alloc>::__emplace_hint_multi( + const_iterator __p, _Args&&... __args) +{ + __node_holder __h = __construct_node(_VSTD::forward<_Args>(__args)...); + iterator __r = __node_insert_multi(__p, __h.get()); + __h.release(); + return __r; +} + +#endif // _LIBCPP_HAS_NO_VARIADICS + +template <class _Tp, class _Hash, class _Equal, class _Alloc> +template <class _P> +pair<typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator, bool> +__hash_table<_Tp, _Hash, _Equal, _Alloc>::__insert_unique(_P&& __x) +{ + __node_holder __h = __construct_node(_VSTD::forward<_P>(__x)); + pair<iterator, bool> __r = __node_insert_unique(__h.get()); + if (__r.second) + __h.release(); + return __r; +} + +#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES + +#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES + +template <class _Tp, class _Hash, class _Equal, class _Alloc> +template <class _P> +typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator +__hash_table<_Tp, _Hash, _Equal, _Alloc>::__insert_multi(_P&& __x) +{ + __node_holder __h = __construct_node(_VSTD::forward<_P>(__x)); + iterator __r = __node_insert_multi(__h.get()); + __h.release(); + return __r; +} + +template <class _Tp, class _Hash, class _Equal, class _Alloc> +template <class _P> +typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator +__hash_table<_Tp, _Hash, _Equal, _Alloc>::__insert_multi(const_iterator __p, + _P&& __x) +{ + __node_holder __h = __construct_node(_VSTD::forward<_P>(__x)); + iterator __r = __node_insert_multi(__p, __h.get()); + __h.release(); + return __r; +} + +#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES + +template <class _Tp, class _Hash, class _Equal, class _Alloc> +typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator +__hash_table<_Tp, _Hash, _Equal, _Alloc>::__insert_multi(const value_type& __x) +{ + __node_holder __h = __construct_node(__x); + iterator __r = __node_insert_multi(__h.get()); + __h.release(); + return __r; +} + +template <class _Tp, class _Hash, class _Equal, class _Alloc> +typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator +__hash_table<_Tp, _Hash, _Equal, _Alloc>::__insert_multi(const_iterator __p, + const value_type& __x) +{ + __node_holder __h = __construct_node(__x); + iterator __r = __node_insert_multi(__p, __h.get()); + __h.release(); + return __r; +} + +#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES + +template <class _Tp, class _Hash, class _Equal, class _Alloc> +void +__hash_table<_Tp, _Hash, _Equal, _Alloc>::rehash(size_type __n) +{ + __n = __next_prime(_VSTD::max<size_type>(__n, size() > 0)); + size_type __bc = bucket_count(); + if (__n > __bc) + __rehash(__n); + else + { + __n = _VSTD::max<size_type> + ( + __n, + __next_prime(size_t(ceil(float(size()) / max_load_factor()))) + ); + if (__n < __bc) + __rehash(__n); + } +} + +template <class _Tp, class _Hash, class _Equal, class _Alloc> +void +__hash_table<_Tp, _Hash, _Equal, _Alloc>::__rehash(size_type __nbc) +{ + __pointer_allocator& __npa = __bucket_list_.get_deleter().__alloc(); + __bucket_list_.reset(__nbc > 0 ? + __pointer_alloc_traits::allocate(__npa, __nbc) : nullptr); + __bucket_list_.get_deleter().size() = __nbc; + if (__nbc > 0) + { + for (size_type __i = 0; __i < __nbc; ++__i) + __bucket_list_[__i] = nullptr; + __node_pointer __pp(static_cast<__node_pointer>(_VSTD::addressof(__p1_.first()))); + __node_pointer __cp = __pp->__next_; + if (__cp != nullptr) + { + size_type __chash = __cp->__hash_ % __nbc; + __bucket_list_[__chash] = __pp; + size_type __phash = __chash; + for (__pp = __cp, __cp = __cp->__next_; __cp != nullptr; + __cp = __pp->__next_) + { + __chash = __cp->__hash_ % __nbc; + if (__chash == __phash) + __pp = __cp; + else + { + if (__bucket_list_[__chash] == nullptr) + { + __bucket_list_[__chash] = __pp; + __pp = __cp; + __phash = __chash; + } + else + { + __node_pointer __np = __cp; + for (; __np->__next_ != nullptr && + key_eq()(__cp->__value_, __np->__next_->__value_); + __np = __np->__next_) + ; + __pp->__next_ = __np->__next_; + __np->__next_ = __bucket_list_[__chash]->__next_; + __bucket_list_[__chash]->__next_ = __cp; + + } + } + } + } + } +} + +template <class _Tp, class _Hash, class _Equal, class _Alloc> +template <class _Key> +typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator +__hash_table<_Tp, _Hash, _Equal, _Alloc>::find(const _Key& __k) +{ + size_t __hash = hash_function()(__k); + size_type __bc = bucket_count(); + if (__bc != 0) + { + size_t __chash = __hash % __bc; + __node_pointer __nd = __bucket_list_[__chash]; + if (__nd != nullptr) + { + for (__nd = __nd->__next_; __nd != nullptr && + __nd->__hash_ % __bc == __chash; + __nd = __nd->__next_) + { + if (key_eq()(__nd->__value_, __k)) + return iterator(__nd); + } + } + } + return end(); +} + +template <class _Tp, class _Hash, class _Equal, class _Alloc> +template <class _Key> +typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::const_iterator +__hash_table<_Tp, _Hash, _Equal, _Alloc>::find(const _Key& __k) const +{ + size_t __hash = hash_function()(__k); + size_type __bc = bucket_count(); + if (__bc != 0) + { + size_t __chash = __hash % __bc; + __node_const_pointer __nd = __bucket_list_[__chash]; + if (__nd != nullptr) + { + for (__nd = __nd->__next_; __nd != nullptr && + __nd->__hash_ % __bc == __chash; + __nd = __nd->__next_) + { + if (key_eq()(__nd->__value_, __k)) + return const_iterator(__nd); + } + } + + } + return end(); +} + +#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES +#ifndef _LIBCPP_HAS_NO_VARIADICS + +template <class _Tp, class _Hash, class _Equal, class _Alloc> +template <class ..._Args> +typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_holder +__hash_table<_Tp, _Hash, _Equal, _Alloc>::__construct_node(_Args&& ...__args) +{ + __node_allocator& __na = __node_alloc(); + __node_holder __h(__node_traits::allocate(__na, 1), _D(__na)); + __node_traits::construct(__na, _VSTD::addressof(__h->__value_), _VSTD::forward<_Args>(__args)...); + __h.get_deleter().__value_constructed = true; + __h->__hash_ = hash_function()(__h->__value_); + __h->__next_ = nullptr; + return __h; +} + +#endif // _LIBCPP_HAS_NO_VARIADICS + +template <class _Tp, class _Hash, class _Equal, class _Alloc> +typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_holder +__hash_table<_Tp, _Hash, _Equal, _Alloc>::__construct_node(value_type&& __v, + size_t __hash) +{ + __node_allocator& __na = __node_alloc(); + __node_holder __h(__node_traits::allocate(__na, 1), _D(__na)); + __node_traits::construct(__na, _VSTD::addressof(__h->__value_), _VSTD::move(__v)); + __h.get_deleter().__value_constructed = true; + __h->__hash_ = __hash; + __h->__next_ = nullptr; + return _VSTD::move(__h); +} + +#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES + +template <class _Tp, class _Hash, class _Equal, class _Alloc> +typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_holder +__hash_table<_Tp, _Hash, _Equal, _Alloc>::__construct_node(const value_type& __v) +{ + __node_allocator& __na = __node_alloc(); + __node_holder __h(__node_traits::allocate(__na, 1), _D(__na)); + __node_traits::construct(__na, _VSTD::addressof(__h->__value_), __v); + __h.get_deleter().__value_constructed = true; + __h->__hash_ = hash_function()(__h->__value_); + __h->__next_ = nullptr; + return _VSTD::move(__h); +} + +#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES + +template <class _Tp, class _Hash, class _Equal, class _Alloc> +typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_holder +__hash_table<_Tp, _Hash, _Equal, _Alloc>::__construct_node(const value_type& __v, + size_t __hash) +{ + __node_allocator& __na = __node_alloc(); + __node_holder __h(__node_traits::allocate(__na, 1), _D(__na)); + __node_traits::construct(__na, _VSTD::addressof(__h->__value_), __v); + __h.get_deleter().__value_constructed = true; + __h->__hash_ = __hash; + __h->__next_ = nullptr; + return _VSTD::move(__h); +} + +template <class _Tp, class _Hash, class _Equal, class _Alloc> +typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator +__hash_table<_Tp, _Hash, _Equal, _Alloc>::erase(const_iterator __p) +{ + __node_pointer __np = const_cast<__node_pointer>(__p.__node_); + iterator __r(__np); + ++__r; + remove(__p); + return __r; +} + +template <class _Tp, class _Hash, class _Equal, class _Alloc> +typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator +__hash_table<_Tp, _Hash, _Equal, _Alloc>::erase(const_iterator __first, + const_iterator __last) +{ + for (const_iterator __p = __first; __first != __last; __p = __first) + { + ++__first; + erase(__p); + } + __node_pointer __np = const_cast<__node_pointer>(__last.__node_); + return iterator (__np); +} + +template <class _Tp, class _Hash, class _Equal, class _Alloc> +template <class _Key> +typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::size_type +__hash_table<_Tp, _Hash, _Equal, _Alloc>::__erase_unique(const _Key& __k) +{ + iterator __i = find(__k); + if (__i == end()) + return 0; + erase(__i); + return 1; +} + +template <class _Tp, class _Hash, class _Equal, class _Alloc> +template <class _Key> +typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::size_type +__hash_table<_Tp, _Hash, _Equal, _Alloc>::__erase_multi(const _Key& __k) +{ + size_type __r = 0; + iterator __i = find(__k); + if (__i != end()) + { + iterator __e = end(); + do + { + erase(__i++); + ++__r; + } while (__i != __e && key_eq()(*__i, __k)); + } + return __r; +} + +template <class _Tp, class _Hash, class _Equal, class _Alloc> +typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_holder +__hash_table<_Tp, _Hash, _Equal, _Alloc>::remove(const_iterator __p) _NOEXCEPT +{ + // current node + __node_pointer __cn = const_cast<__node_pointer>(__p.__node_); + size_type __bc = bucket_count(); + size_t __chash = __cn->__hash_ % __bc; + // find previous node + __node_pointer __pn = __bucket_list_[__chash]; + for (; __pn->__next_ != __cn; __pn = __pn->__next_) + ; + // Fix up __bucket_list_ + // if __pn is not in same bucket (before begin is not in same bucket) && + // if __cn->__next_ is not in same bucket (nullptr is not in same bucket) + if (__pn == _VSTD::addressof(__p1_.first()) || __pn->__hash_ % __bc != __chash) + { + if (__cn->__next_ == nullptr || __cn->__next_->__hash_ % __bc != __chash) + __bucket_list_[__chash] = nullptr; + } + // if __cn->__next_ is not in same bucket (nullptr is in same bucket) + if (__cn->__next_ != nullptr) + { + size_t __nhash = __cn->__next_->__hash_ % __bc; + if (__nhash != __chash) + __bucket_list_[__nhash] = __pn; + } + // remove __cn + __pn->__next_ = __cn->__next_; + __cn->__next_ = nullptr; + --size(); + return __node_holder(__cn, _D(__node_alloc(), true)); +} + +template <class _Tp, class _Hash, class _Equal, class _Alloc> +template <class _Key> +inline _LIBCPP_INLINE_VISIBILITY +typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::size_type +__hash_table<_Tp, _Hash, _Equal, _Alloc>::__count_unique(const _Key& __k) const +{ + return static_cast<size_type>(find(__k) != end()); +} + +template <class _Tp, class _Hash, class _Equal, class _Alloc> +template <class _Key> +typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::size_type +__hash_table<_Tp, _Hash, _Equal, _Alloc>::__count_multi(const _Key& __k) const +{ + size_type __r = 0; + const_iterator __i = find(__k); + if (__i != end()) + { + const_iterator __e = end(); + do + { + ++__i; + ++__r; + } while (__i != __e && key_eq()(*__i, __k)); + } + return __r; +} + +template <class _Tp, class _Hash, class _Equal, class _Alloc> +template <class _Key> +pair<typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator, + typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator> +__hash_table<_Tp, _Hash, _Equal, _Alloc>::__equal_range_unique( + const _Key& __k) +{ + iterator __i = find(__k); + iterator __j = __i; + if (__i != end()) + ++__j; + return pair<iterator, iterator>(__i, __j); +} + +template <class _Tp, class _Hash, class _Equal, class _Alloc> +template <class _Key> +pair<typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::const_iterator, + typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::const_iterator> +__hash_table<_Tp, _Hash, _Equal, _Alloc>::__equal_range_unique( + const _Key& __k) const +{ + const_iterator __i = find(__k); + const_iterator __j = __i; + if (__i != end()) + ++__j; + return pair<const_iterator, const_iterator>(__i, __j); +} + +template <class _Tp, class _Hash, class _Equal, class _Alloc> +template <class _Key> +pair<typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator, + typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator> +__hash_table<_Tp, _Hash, _Equal, _Alloc>::__equal_range_multi( + const _Key& __k) +{ + iterator __i = find(__k); + iterator __j = __i; + if (__i != end()) + { + iterator __e = end(); + do + { + ++__j; + } while (__j != __e && key_eq()(*__j, __k)); + } + return pair<iterator, iterator>(__i, __j); +} + +template <class _Tp, class _Hash, class _Equal, class _Alloc> +template <class _Key> +pair<typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::const_iterator, + typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::const_iterator> +__hash_table<_Tp, _Hash, _Equal, _Alloc>::__equal_range_multi( + const _Key& __k) const +{ + const_iterator __i = find(__k); + const_iterator __j = __i; + if (__i != end()) + { + const_iterator __e = end(); + do + { + ++__j; + } while (__j != __e && key_eq()(*__j, __k)); + } + return pair<const_iterator, const_iterator>(__i, __j); +} + +template <class _Tp, class _Hash, class _Equal, class _Alloc> +void +__hash_table<_Tp, _Hash, _Equal, _Alloc>::swap(__hash_table& __u) + _NOEXCEPT_( + (!allocator_traits<__pointer_allocator>::propagate_on_container_swap::value || + __is_nothrow_swappable<__pointer_allocator>::value) && + (!__node_traits::propagate_on_container_swap::value || + __is_nothrow_swappable<__node_allocator>::value) && + __is_nothrow_swappable<hasher>::value && + __is_nothrow_swappable<key_equal>::value) +{ + { + __node_pointer_pointer __npp = __bucket_list_.release(); + __bucket_list_.reset(__u.__bucket_list_.release()); + __u.__bucket_list_.reset(__npp); + } + _VSTD::swap(__bucket_list_.get_deleter().size(), __u.__bucket_list_.get_deleter().size()); + __swap_alloc(__bucket_list_.get_deleter().__alloc(), + __u.__bucket_list_.get_deleter().__alloc()); + __swap_alloc(__node_alloc(), __u.__node_alloc()); + _VSTD::swap(__p1_.first().__next_, __u.__p1_.first().__next_); + __p2_.swap(__u.__p2_); + __p3_.swap(__u.__p3_); + if (size() > 0) + __bucket_list_[__p1_.first().__next_->__hash_ % bucket_count()] = + static_cast<__node_pointer>(_VSTD::addressof(__p1_.first())); + if (__u.size() > 0) + __u.__bucket_list_[__u.__p1_.first().__next_->__hash_ % __u.bucket_count()] = + static_cast<__node_pointer>(_VSTD::addressof(__u.__p1_.first())); +} + +template <class _Tp, class _Hash, class _Equal, class _Alloc> +typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::size_type +__hash_table<_Tp, _Hash, _Equal, _Alloc>::bucket_size(size_type __n) const +{ + __node_const_pointer __np = __bucket_list_[__n]; + size_type __bc = bucket_count(); + size_type __r = 0; + if (__np != nullptr) + { + for (__np = __np->__next_; __np != nullptr && + __np->__hash_ % __bc == __n; + __np = __np->__next_, ++__r) + ; + } + return __r; +} + +template <class _Tp, class _Hash, class _Equal, class _Alloc> +inline _LIBCPP_INLINE_VISIBILITY +void +swap(__hash_table<_Tp, _Hash, _Equal, _Alloc>& __x, + __hash_table<_Tp, _Hash, _Equal, _Alloc>& __y) + _NOEXCEPT_(_NOEXCEPT_(__x.swap(__y))) +{ + __x.swap(__y); +} + +_LIBCPP_END_NAMESPACE_STD + +#endif // _LIBCPP__HASH_TABLE diff --git a/include/__locale b/include/__locale new file mode 100644 index 000000000000..28cb3ef65292 --- /dev/null +++ b/include/__locale @@ -0,0 +1,1414 @@ +// -*- C++ -*- +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#ifndef _LIBCPP___LOCALE +#define _LIBCPP___LOCALE + +#include <__config> +#include <string> +#include <memory> +#include <utility> +#include <mutex> +#include <cstdint> +#include <cctype> +#include <locale.h> +#if _WIN32 +# include <support/win32/locale_win32.h> +#elif (__GLIBC__ || __APPLE__ || __FreeBSD__) +# include <xlocale.h> +#endif // _WIN32 || __GLIBC__ || __APPLE__ || __FreeBSD_ + +#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) +#pragma GCC system_header +#endif + +_LIBCPP_BEGIN_NAMESPACE_STD + +class locale; + +template <class _Facet> bool has_facet(const locale&) _NOEXCEPT; +template <class _Facet> const _Facet& use_facet(const locale&); + +class _LIBCPP_VISIBLE locale +{ +public: + // types: + class facet; + class id; + + typedef int category; + static const category // values assigned here are for exposition only + none = 0, + collate = LC_COLLATE_MASK, + ctype = LC_CTYPE_MASK, + monetary = LC_MONETARY_MASK, + numeric = LC_NUMERIC_MASK, + time = LC_TIME_MASK, + messages = LC_MESSAGES_MASK, + all = collate | ctype | monetary | numeric | time | messages; + + // construct/copy/destroy: + locale() _NOEXCEPT; + locale(const locale&) _NOEXCEPT; + explicit locale(const char*); + explicit locale(const string&); + locale(const locale&, const char*, category); + locale(const locale&, const string&, category); + template <class _Facet> + _LIBCPP_INLINE_VISIBILITY locale(const locale&, _Facet*); + locale(const locale&, const locale&, category); + + ~locale(); + + const locale& operator=(const locale&) _NOEXCEPT; + + template <class _Facet> locale combine(const locale&) const; + + // locale operations: + string name() const; + bool operator==(const locale&) const; + bool operator!=(const locale& __y) const {return !(*this == __y);} + template <class _CharT, class _Traits, class _Allocator> + bool operator()(const basic_string<_CharT, _Traits, _Allocator>&, + const basic_string<_CharT, _Traits, _Allocator>&) const; + + // global locale objects: + static locale global(const locale&); + static const locale& classic(); + +private: + class __imp; + __imp* __locale_; + + void __install_ctor(const locale&, facet*, long); + static locale& __global(); + bool has_facet(id&) const; + const facet* use_facet(id&) const; + + template <class _Facet> friend bool has_facet(const locale&) _NOEXCEPT; + template <class _Facet> friend const _Facet& use_facet(const locale&); +}; + +class _LIBCPP_VISIBLE locale::facet + : public __shared_count +{ +protected: + _LIBCPP_INLINE_VISIBILITY + explicit facet(size_t __refs = 0) + : __shared_count(static_cast<long>(__refs)-1) {} + + virtual ~facet(); + +// facet(const facet&) = delete; // effectively done in __shared_count +// void operator=(const facet&) = delete; +private: + virtual void __on_zero_shared() _NOEXCEPT; +}; + +class _LIBCPP_VISIBLE locale::id +{ + once_flag __flag_; + int32_t __id_; + + static int32_t __next_id; +public: + _LIBCPP_INLINE_VISIBILITY id() {} +private: + void __init(); + void operator=(const id&); // = delete; + id(const id&); // = delete; +public: // only needed for tests + long __get(); + + friend class locale; + friend class locale::__imp; +}; + +template <class _Facet> +inline _LIBCPP_INLINE_VISIBILITY +locale::locale(const locale& __other, _Facet* __f) +{ + __install_ctor(__other, __f, __f ? __f->id.__get() : 0); +} + +template <class _Facet> +locale +locale::combine(const locale& __other) const +{ +#ifndef _LIBCPP_NO_EXCEPTIONS + if (!_VSTD::has_facet<_Facet>(__other)) + throw runtime_error("locale::combine: locale missing facet"); +#endif // _LIBCPP_NO_EXCEPTIONS + return locale(*this, &const_cast<_Facet&>(_VSTD::use_facet<_Facet>(__other))); +} + +template <class _Facet> +inline _LIBCPP_INLINE_VISIBILITY +bool +has_facet(const locale& __l) _NOEXCEPT +{ + return __l.has_facet(_Facet::id); +} + +template <class _Facet> +inline _LIBCPP_INLINE_VISIBILITY +const _Facet& +use_facet(const locale& __l) +{ + return static_cast<const _Facet&>(*__l.use_facet(_Facet::id)); +} + +// template <class _CharT> class collate; + +template <class _CharT> +class _LIBCPP_VISIBLE collate + : public locale::facet +{ +public: + typedef _CharT char_type; + typedef basic_string<char_type> string_type; + + _LIBCPP_INLINE_VISIBILITY + explicit collate(size_t __refs = 0) + : locale::facet(__refs) {} + + _LIBCPP_INLINE_VISIBILITY + int compare(const char_type* __lo1, const char_type* __hi1, + const char_type* __lo2, const char_type* __hi2) const + { + return do_compare(__lo1, __hi1, __lo2, __hi2); + } + + _LIBCPP_INLINE_VISIBILITY + string_type transform(const char_type* __lo, const char_type* __hi) const + { + return do_transform(__lo, __hi); + } + + _LIBCPP_INLINE_VISIBILITY + long hash(const char_type* __lo, const char_type* __hi) const + { + return do_hash(__lo, __hi); + } + + static locale::id id; + +protected: + ~collate(); + virtual int do_compare(const char_type* __lo1, const char_type* __hi1, + const char_type* __lo2, const char_type* __hi2) const; + virtual string_type do_transform(const char_type* __lo, const char_type* __hi) const + {return string_type(__lo, __hi);} + virtual long do_hash(const char_type* __lo, const char_type* __hi) const; +}; + +template <class _CharT> locale::id collate<_CharT>::id; + +template <class _CharT> +collate<_CharT>::~collate() +{ +} + +template <class _CharT> +int +collate<_CharT>::do_compare(const char_type* __lo1, const char_type* __hi1, + const char_type* __lo2, const char_type* __hi2) const +{ + for (; __lo2 != __hi2; ++__lo1, ++__lo2) + { + if (__lo1 == __hi1 || *__lo1 < *__lo2) + return -1; + if (*__lo2 < *__lo1) + return 1; + } + return __lo1 != __hi1; +} + +template <class _CharT> +long +collate<_CharT>::do_hash(const char_type* __lo, const char_type* __hi) const +{ + size_t __h = 0; + const size_t __sr = __CHAR_BIT__ * sizeof(size_t) - 8; + const size_t __mask = size_t(0xF) << (__sr + 4); + for(const char_type* __p = __lo; __p != __hi; ++__p) + { + __h = (__h << 4) + *__p; + size_t __g = __h & __mask; + __h ^= __g | (__g >> __sr); + } + return static_cast<long>(__h); +} + +extern template class _LIBCPP_VISIBLE collate<char>; +extern template class _LIBCPP_VISIBLE collate<wchar_t>; + +// template <class CharT> class collate_byname; + +template <class _CharT> class _LIBCPP_VISIBLE collate_byname; + +template <> +class _LIBCPP_VISIBLE collate_byname<char> + : public collate<char> +{ + locale_t __l; +public: + typedef char char_type; + typedef basic_string<char_type> string_type; + + explicit collate_byname(const char* __n, size_t __refs = 0); + explicit collate_byname(const string& __n, size_t __refs = 0); + +protected: + ~collate_byname(); + virtual int do_compare(const char_type* __lo1, const char_type* __hi1, + const char_type* __lo2, const char_type* __hi2) const; + virtual string_type do_transform(const char_type* __lo, const char_type* __hi) const; +}; + +template <> +class _LIBCPP_VISIBLE collate_byname<wchar_t> + : public collate<wchar_t> +{ + locale_t __l; +public: + typedef wchar_t char_type; + typedef basic_string<char_type> string_type; + + explicit collate_byname(const char* __n, size_t __refs = 0); + explicit collate_byname(const string& __n, size_t __refs = 0); + +protected: + ~collate_byname(); + + virtual int do_compare(const char_type* __lo1, const char_type* __hi1, + const char_type* __lo2, const char_type* __hi2) const; + virtual string_type do_transform(const char_type* __lo, const char_type* __hi) const; +}; + +template <class _CharT, class _Traits, class _Allocator> +bool +locale::operator()(const basic_string<_CharT, _Traits, _Allocator>& __x, + const basic_string<_CharT, _Traits, _Allocator>& __y) const +{ + return _VSTD::use_facet<_VSTD::collate<_CharT> >(*this).compare( + __x.data(), __x.data() + __x.size(), + __y.data(), __y.data() + __y.size()) < 0; +} + +// template <class charT> class ctype + +class _LIBCPP_VISIBLE ctype_base +{ +public: +#if __GLIBC__ + typedef unsigned short mask; + static const mask space = _ISspace; + static const mask print = _ISprint; + static const mask cntrl = _IScntrl; + static const mask upper = _ISupper; + static const mask lower = _ISlower; + static const mask alpha = _ISalpha; + static const mask digit = _ISdigit; + static const mask punct = _ISpunct; + static const mask xdigit = _ISxdigit; + static const mask blank = _ISblank; +#elif _WIN32 + typedef unsigned short mask; + static const mask space = _SPACE; + static const mask print = _BLANK|_PUNCT|_ALPHA|_DIGIT; + static const mask cntrl = _CONTROL; + static const mask upper = _UPPER; + static const mask lower = _LOWER; + static const mask alpha = _ALPHA; + static const mask digit = _DIGIT; + static const mask punct = _PUNCT; + static const mask xdigit = _HEX; + static const mask blank = _BLANK; +#elif (__APPLE__ || __FreeBSD__) +#if __APPLE__ + typedef __uint32_t mask; +#elif __FreeBSD__ + typedef unsigned long mask; +#endif + static const mask space = _CTYPE_S; + static const mask print = _CTYPE_R; + static const mask cntrl = _CTYPE_C; + static const mask upper = _CTYPE_U; + static const mask lower = _CTYPE_L; + static const mask alpha = _CTYPE_A; + static const mask digit = _CTYPE_D; + static const mask punct = _CTYPE_P; + static const mask xdigit = _CTYPE_X; + static const mask blank = _CTYPE_B; +#else // __GLIBC__ || _WIN32 || __APPLE__ || __FreeBSD__ + typedef unsigned long mask; + static const mask space = 1<<0; + static const mask print = 1<<1; + static const mask cntrl = 1<<2; + static const mask upper = 1<<3; + static const mask lower = 1<<4; + static const mask alpha = 1<<5; + static const mask digit = 1<<6; + static const mask punct = 1<<7; + static const mask xdigit = 1<<8; + static const mask blank = 1<<9; +#endif // __GLIBC__ || _WIN32 || __APPLE__ || __FreeBSD__ + static const mask alnum = alpha | digit; + static const mask graph = alnum | punct; + + _LIBCPP_ALWAYS_INLINE ctype_base() {} +}; + +template <class _CharT> class _LIBCPP_VISIBLE ctype; + +template <> +class _LIBCPP_VISIBLE ctype<wchar_t> + : public locale::facet, + public ctype_base +{ +public: + typedef wchar_t char_type; + + _LIBCPP_ALWAYS_INLINE + explicit ctype(size_t __refs = 0) + : locale::facet(__refs) {} + + _LIBCPP_ALWAYS_INLINE + bool is(mask __m, char_type __c) const + { + return do_is(__m, __c); + } + + _LIBCPP_ALWAYS_INLINE + const char_type* is(const char_type* __low, const char_type* __high, mask* __vec) const + { + return do_is(__low, __high, __vec); + } + + _LIBCPP_ALWAYS_INLINE + const char_type* scan_is(mask __m, const char_type* __low, const char_type* __high) const + { + return do_scan_is(__m, __low, __high); + } + + _LIBCPP_ALWAYS_INLINE + const char_type* scan_not(mask __m, const char_type* __low, const char_type* __high) const + { + return do_scan_not(__m, __low, __high); + } + + _LIBCPP_ALWAYS_INLINE + char_type toupper(char_type __c) const + { + return do_toupper(__c); + } + + _LIBCPP_ALWAYS_INLINE + const char_type* toupper(char_type* __low, const char_type* __high) const + { + return do_toupper(__low, __high); + } + + _LIBCPP_ALWAYS_INLINE + char_type tolower(char_type __c) const + { + return do_tolower(__c); + } + + _LIBCPP_ALWAYS_INLINE + const char_type* tolower(char_type* __low, const char_type* __high) const + { + return do_tolower(__low, __high); + } + + _LIBCPP_ALWAYS_INLINE + char_type widen(char __c) const + { + return do_widen(__c); + } + + _LIBCPP_ALWAYS_INLINE + const char* widen(const char* __low, const char* __high, char_type* __to) const + { + return do_widen(__low, __high, __to); + } + + _LIBCPP_ALWAYS_INLINE + char narrow(char_type __c, char __dfault) const + { + return do_narrow(__c, __dfault); + } + + _LIBCPP_ALWAYS_INLINE + const char_type* narrow(const char_type* __low, const char_type* __high, char __dfault, char* __to) const + { + return do_narrow(__low, __high, __dfault, __to); + } + + static locale::id id; + +protected: + ~ctype(); + virtual bool do_is(mask __m, char_type __c) const; + virtual const char_type* do_is(const char_type* __low, const char_type* __high, mask* __vec) const; + virtual const char_type* do_scan_is(mask __m, const char_type* __low, const char_type* __high) const; + virtual const char_type* do_scan_not(mask __m, const char_type* __low, const char_type* __high) const; + virtual char_type do_toupper(char_type) const; + virtual const char_type* do_toupper(char_type* __low, const char_type* __high) const; + virtual char_type do_tolower(char_type) const; + virtual const char_type* do_tolower(char_type* __low, const char_type* __high) const; + virtual char_type do_widen(char) const; + virtual const char* do_widen(const char* __low, const char* __high, char_type* __dest) const; + virtual char do_narrow(char_type, char __dfault) const; + virtual const char_type* do_narrow(const char_type* __low, const char_type* __high, char __dfault, char* __dest) const; +}; + +template <> +class _LIBCPP_VISIBLE ctype<char> + : public locale::facet, public ctype_base +{ + const mask* __tab_; + bool __del_; +public: + typedef char char_type; + + explicit ctype(const mask* __tab = 0, bool __del = false, size_t __refs = 0); + + _LIBCPP_ALWAYS_INLINE + bool is(mask __m, char_type __c) const + { + return isascii(__c) ? __tab_[__c] & __m : false; + } + + _LIBCPP_ALWAYS_INLINE + const char_type* is(const char_type* __low, const char_type* __high, mask* __vec) const + { + for (; __low != __high; ++__low, ++__vec) + *__vec = isascii(*__low) ? __tab_[*__low] : 0; + return __low; + } + + _LIBCPP_ALWAYS_INLINE + const char_type* scan_is (mask __m, const char_type* __low, const char_type* __high) const + { + for (; __low != __high; ++__low) + if (isascii(*__low) && (__tab_[*__low] & __m)) + break; + return __low; + } + + _LIBCPP_ALWAYS_INLINE + const char_type* scan_not(mask __m, const char_type* __low, const char_type* __high) const + { + for (; __low != __high; ++__low) + if (!(isascii(*__low) && (__tab_[*__low] & __m))) + break; + return __low; + } + + _LIBCPP_ALWAYS_INLINE + char_type toupper(char_type __c) const + { + return do_toupper(__c); + } + + _LIBCPP_ALWAYS_INLINE + const char_type* toupper(char_type* __low, const char_type* __high) const + { + return do_toupper(__low, __high); + } + + _LIBCPP_ALWAYS_INLINE + char_type tolower(char_type __c) const + { + return do_tolower(__c); + } + + _LIBCPP_ALWAYS_INLINE + const char_type* tolower(char_type* __low, const char_type* __high) const + { + return do_tolower(__low, __high); + } + + _LIBCPP_ALWAYS_INLINE + char_type widen(char __c) const + { + return do_widen(__c); + } + + _LIBCPP_ALWAYS_INLINE + const char* widen(const char* __low, const char* __high, char_type* __to) const + { + return do_widen(__low, __high, __to); + } + + _LIBCPP_ALWAYS_INLINE + char narrow(char_type __c, char __dfault) const + { + return do_narrow(__c, __dfault); + } + + _LIBCPP_ALWAYS_INLINE + const char* narrow(const char_type* __low, const char_type* __high, char __dfault, char* __to) const + { + return do_narrow(__low, __high, __dfault, __to); + } + + static locale::id id; + +#ifdef _CACHED_RUNES + static const size_t table_size = _CACHED_RUNES; +#else + static const size_t table_size = 256; // FIXME: Don't hardcode this. +#endif + _LIBCPP_ALWAYS_INLINE const mask* table() const _NOEXCEPT {return __tab_;} + static const mask* classic_table() _NOEXCEPT; +#if defined(__GLIBC__) + static const int* __classic_upper_table() _NOEXCEPT; + static const int* __classic_lower_table() _NOEXCEPT; +#endif + +protected: + ~ctype(); + virtual char_type do_toupper(char_type __c) const; + virtual const char_type* do_toupper(char_type* __low, const char_type* __high) const; + virtual char_type do_tolower(char_type __c) const; + virtual const char_type* do_tolower(char_type* __low, const char_type* __high) const; + virtual char_type do_widen(char __c) const; + virtual const char* do_widen(const char* __low, const char* __high, char_type* __to) const; + virtual char do_narrow(char_type __c, char __dfault) const; + virtual const char* do_narrow(const char_type* __low, const char_type* __high, char __dfault, char* __to) const; +}; + +// template <class CharT> class ctype_byname; + +template <class _CharT> class _LIBCPP_VISIBLE ctype_byname; + +template <> +class _LIBCPP_VISIBLE ctype_byname<char> + : public ctype<char> +{ + locale_t __l; + +public: + explicit ctype_byname(const char*, size_t = 0); + explicit ctype_byname(const string&, size_t = 0); + +protected: + ~ctype_byname(); + virtual char_type do_toupper(char_type) const; + virtual const char_type* do_toupper(char_type* __low, const char_type* __high) const; + virtual char_type do_tolower(char_type) const; + virtual const char_type* do_tolower(char_type* __low, const char_type* __high) const; +}; + +template <> +class _LIBCPP_VISIBLE ctype_byname<wchar_t> + : public ctype<wchar_t> +{ + locale_t __l; + +public: + explicit ctype_byname(const char*, size_t = 0); + explicit ctype_byname(const string&, size_t = 0); + +protected: + ~ctype_byname(); + virtual bool do_is(mask __m, char_type __c) const; + virtual const char_type* do_is(const char_type* __low, const char_type* __high, mask* __vec) const; + virtual const char_type* do_scan_is(mask __m, const char_type* __low, const char_type* __high) const; + virtual const char_type* do_scan_not(mask __m, const char_type* __low, const char_type* __high) const; + virtual char_type do_toupper(char_type) const; + virtual const char_type* do_toupper(char_type* __low, const char_type* __high) const; + virtual char_type do_tolower(char_type) const; + virtual const char_type* do_tolower(char_type* __low, const char_type* __high) const; + virtual char_type do_widen(char) const; + virtual const char* do_widen(const char* __low, const char* __high, char_type* __dest) const; + virtual char do_narrow(char_type, char __dfault) const; + virtual const char_type* do_narrow(const char_type* __low, const char_type* __high, char __dfault, char* __dest) const; +}; + +template <class _CharT> +inline _LIBCPP_INLINE_VISIBILITY +bool +isspace(_CharT __c, const locale& __loc) +{ + return use_facet<ctype<_CharT> >(__loc).is(ctype_base::space, __c); +} + +template <class _CharT> +inline _LIBCPP_INLINE_VISIBILITY +bool +isprint(_CharT __c, const locale& __loc) +{ + return use_facet<ctype<_CharT> >(__loc).is(ctype_base::print, __c); +} + +template <class _CharT> +inline _LIBCPP_INLINE_VISIBILITY +bool +iscntrl(_CharT __c, const locale& __loc) +{ + return use_facet<ctype<_CharT> >(__loc).is(ctype_base::cntrl, __c); +} + +template <class _CharT> +inline _LIBCPP_INLINE_VISIBILITY +bool +isupper(_CharT __c, const locale& __loc) +{ + return use_facet<ctype<_CharT> >(__loc).is(ctype_base::upper, __c); +} + +template <class _CharT> +inline _LIBCPP_INLINE_VISIBILITY +bool +islower(_CharT __c, const locale& __loc) +{ + return use_facet<ctype<_CharT> >(__loc).is(ctype_base::lower, __c); +} + +template <class _CharT> +inline _LIBCPP_INLINE_VISIBILITY +bool +isalpha(_CharT __c, const locale& __loc) +{ + return use_facet<ctype<_CharT> >(__loc).is(ctype_base::alpha, __c); +} + +template <class _CharT> +inline _LIBCPP_INLINE_VISIBILITY +bool +isdigit(_CharT __c, const locale& __loc) +{ + return use_facet<ctype<_CharT> >(__loc).is(ctype_base::digit, __c); +} + +template <class _CharT> +inline _LIBCPP_INLINE_VISIBILITY +bool +ispunct(_CharT __c, const locale& __loc) +{ + return use_facet<ctype<_CharT> >(__loc).is(ctype_base::punct, __c); +} + +template <class _CharT> +inline _LIBCPP_INLINE_VISIBILITY +bool +isxdigit(_CharT __c, const locale& __loc) +{ + return use_facet<ctype<_CharT> >(__loc).is(ctype_base::xdigit, __c); +} + +template <class _CharT> +inline _LIBCPP_INLINE_VISIBILITY +bool +isalnum(_CharT __c, const locale& __loc) +{ + return use_facet<ctype<_CharT> >(__loc).is(ctype_base::alnum, __c); +} + +template <class _CharT> +inline _LIBCPP_INLINE_VISIBILITY +bool +isgraph(_CharT __c, const locale& __loc) +{ + return use_facet<ctype<_CharT> >(__loc).is(ctype_base::graph, __c); +} + +template <class _CharT> +inline _LIBCPP_INLINE_VISIBILITY +_CharT +toupper(_CharT __c, const locale& __loc) +{ + return use_facet<ctype<_CharT> >(__loc).toupper(__c); +} + +template <class _CharT> +inline _LIBCPP_INLINE_VISIBILITY +_CharT +tolower(_CharT __c, const locale& __loc) +{ + return use_facet<ctype<_CharT> >(__loc).tolower(__c); +} + +// codecvt_base + +class _LIBCPP_VISIBLE codecvt_base +{ +public: + _LIBCPP_ALWAYS_INLINE codecvt_base() {} + enum result {ok, partial, error, noconv}; +}; + +// template <class internT, class externT, class stateT> class codecvt; + +template <class _InternT, class _ExternT, class _StateT> class _LIBCPP_VISIBLE codecvt; + +// template <> class codecvt<char, char, mbstate_t> + +template <> +class _LIBCPP_VISIBLE codecvt<char, char, mbstate_t> + : public locale::facet, + public codecvt_base +{ +public: + typedef char intern_type; + typedef char extern_type; + typedef mbstate_t state_type; + + _LIBCPP_ALWAYS_INLINE + explicit codecvt(size_t __refs = 0) + : locale::facet(__refs) {} + + _LIBCPP_ALWAYS_INLINE + result out(state_type& __st, + const intern_type* __frm, const intern_type* __frm_end, const intern_type*& __frm_nxt, + extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const + { + return do_out(__st, __frm, __frm_end, __frm_nxt, __to, __to_end, __to_nxt); + } + + _LIBCPP_ALWAYS_INLINE + result unshift(state_type& __st, + extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const + { + return do_unshift(__st, __to, __to_end, __to_nxt); + } + + _LIBCPP_ALWAYS_INLINE + result in(state_type& __st, + const extern_type* __frm, const extern_type* __frm_end, const extern_type*& __frm_nxt, + intern_type* __to, intern_type* __to_end, intern_type*& __to_nxt) const + { + return do_in(__st, __frm, __frm_end, __frm_nxt, __to, __to_end, __to_nxt); + } + + _LIBCPP_ALWAYS_INLINE + int encoding() const _NOEXCEPT + { + return do_encoding(); + } + + _LIBCPP_ALWAYS_INLINE + bool always_noconv() const _NOEXCEPT + { + return do_always_noconv(); + } + + _LIBCPP_ALWAYS_INLINE + int length(state_type& __st, const extern_type* __frm, const extern_type* __end, size_t __mx) const + { + return do_length(__st, __frm, __end, __mx); + } + + _LIBCPP_ALWAYS_INLINE + int max_length() const _NOEXCEPT + { + return do_max_length(); + } + + static locale::id id; + +protected: + _LIBCPP_ALWAYS_INLINE + explicit codecvt(const char*, size_t __refs = 0) + : locale::facet(__refs) {} + + ~codecvt(); + + virtual result do_out(state_type& __st, + const intern_type* __frm, const intern_type* __frm_end, const intern_type*& __frm_nxt, + extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const; + virtual result do_in(state_type& __st, + const extern_type* __frm, const extern_type* __frm_end, const extern_type*& __frm_nxt, + intern_type* __to, intern_type* __to_end, intern_type*& __to_nxt) const; + virtual result do_unshift(state_type& __st, + extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const; + virtual int do_encoding() const _NOEXCEPT; + virtual bool do_always_noconv() const _NOEXCEPT; + virtual int do_length(state_type& __st, const extern_type* __frm, const extern_type* __end, size_t __mx) const; + virtual int do_max_length() const _NOEXCEPT; +}; + +// template <> class codecvt<wchar_t, char, mbstate_t> + +template <> +class _LIBCPP_VISIBLE codecvt<wchar_t, char, mbstate_t> + : public locale::facet, + public codecvt_base +{ + locale_t __l; +public: + typedef wchar_t intern_type; + typedef char extern_type; + typedef mbstate_t state_type; + + explicit codecvt(size_t __refs = 0); + + _LIBCPP_ALWAYS_INLINE + result out(state_type& __st, + const intern_type* __frm, const intern_type* __frm_end, const intern_type*& __frm_nxt, + extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const + { + return do_out(__st, __frm, __frm_end, __frm_nxt, __to, __to_end, __to_nxt); + } + + _LIBCPP_ALWAYS_INLINE + result unshift(state_type& __st, + extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const + { + return do_unshift(__st, __to, __to_end, __to_nxt); + } + + _LIBCPP_ALWAYS_INLINE + result in(state_type& __st, + const extern_type* __frm, const extern_type* __frm_end, const extern_type*& __frm_nxt, + intern_type* __to, intern_type* __to_end, intern_type*& __to_nxt) const + { + return do_in(__st, __frm, __frm_end, __frm_nxt, __to, __to_end, __to_nxt); + } + + _LIBCPP_ALWAYS_INLINE + int encoding() const _NOEXCEPT + { + return do_encoding(); + } + + _LIBCPP_ALWAYS_INLINE + bool always_noconv() const _NOEXCEPT + { + return do_always_noconv(); + } + + _LIBCPP_ALWAYS_INLINE + int length(state_type& __st, const extern_type* __frm, const extern_type* __end, size_t __mx) const + { + return do_length(__st, __frm, __end, __mx); + } + + _LIBCPP_ALWAYS_INLINE + int max_length() const _NOEXCEPT + { + return do_max_length(); + } + + static locale::id id; + +protected: + explicit codecvt(const char*, size_t __refs = 0); + + ~codecvt(); + + virtual result do_out(state_type& __st, + const intern_type* __frm, const intern_type* __frm_end, const intern_type*& __frm_nxt, + extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const; + virtual result do_in(state_type& __st, + const extern_type* __frm, const extern_type* __frm_end, const extern_type*& __frm_nxt, + intern_type* __to, intern_type* __to_end, intern_type*& __to_nxt) const; + virtual result do_unshift(state_type& __st, + extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const; + virtual int do_encoding() const _NOEXCEPT; + virtual bool do_always_noconv() const _NOEXCEPT; + virtual int do_length(state_type&, const extern_type* __frm, const extern_type* __end, size_t __mx) const; + virtual int do_max_length() const _NOEXCEPT; +}; + +// template <> class codecvt<char16_t, char, mbstate_t> + +template <> +class _LIBCPP_VISIBLE codecvt<char16_t, char, mbstate_t> + : public locale::facet, + public codecvt_base +{ +public: + typedef char16_t intern_type; + typedef char extern_type; + typedef mbstate_t state_type; + + _LIBCPP_ALWAYS_INLINE + explicit codecvt(size_t __refs = 0) + : locale::facet(__refs) {} + + _LIBCPP_ALWAYS_INLINE + result out(state_type& __st, + const intern_type* __frm, const intern_type* __frm_end, const intern_type*& __frm_nxt, + extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const + { + return do_out(__st, __frm, __frm_end, __frm_nxt, __to, __to_end, __to_nxt); + } + + _LIBCPP_ALWAYS_INLINE + result unshift(state_type& __st, + extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const + { + return do_unshift(__st, __to, __to_end, __to_nxt); + } + + _LIBCPP_ALWAYS_INLINE + result in(state_type& __st, + const extern_type* __frm, const extern_type* __frm_end, const extern_type*& __frm_nxt, + intern_type* __to, intern_type* __to_end, intern_type*& __to_nxt) const + { + return do_in(__st, __frm, __frm_end, __frm_nxt, __to, __to_end, __to_nxt); + } + + _LIBCPP_ALWAYS_INLINE + int encoding() const _NOEXCEPT + { + return do_encoding(); + } + + _LIBCPP_ALWAYS_INLINE + bool always_noconv() const _NOEXCEPT + { + return do_always_noconv(); + } + + _LIBCPP_ALWAYS_INLINE + int length(state_type& __st, const extern_type* __frm, const extern_type* __end, size_t __mx) const + { + return do_length(__st, __frm, __end, __mx); + } + + _LIBCPP_ALWAYS_INLINE + int max_length() const _NOEXCEPT + { + return do_max_length(); + } + + static locale::id id; + +protected: + _LIBCPP_ALWAYS_INLINE + explicit codecvt(const char*, size_t __refs = 0) + : locale::facet(__refs) {} + + ~codecvt(); + + virtual result do_out(state_type& __st, + const intern_type* __frm, const intern_type* __frm_end, const intern_type*& __frm_nxt, + extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const; + virtual result do_in(state_type& __st, + const extern_type* __frm, const extern_type* __frm_end, const extern_type*& __frm_nxt, + intern_type* __to, intern_type* __to_end, intern_type*& __to_nxt) const; + virtual result do_unshift(state_type& __st, + extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const; + virtual int do_encoding() const _NOEXCEPT; + virtual bool do_always_noconv() const _NOEXCEPT; + virtual int do_length(state_type&, const extern_type* __frm, const extern_type* __end, size_t __mx) const; + virtual int do_max_length() const _NOEXCEPT; +}; + +// template <> class codecvt<char32_t, char, mbstate_t> + +template <> +class _LIBCPP_VISIBLE codecvt<char32_t, char, mbstate_t> + : public locale::facet, + public codecvt_base +{ +public: + typedef char32_t intern_type; + typedef char extern_type; + typedef mbstate_t state_type; + + _LIBCPP_ALWAYS_INLINE + explicit codecvt(size_t __refs = 0) + : locale::facet(__refs) {} + + _LIBCPP_ALWAYS_INLINE + result out(state_type& __st, + const intern_type* __frm, const intern_type* __frm_end, const intern_type*& __frm_nxt, + extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const + { + return do_out(__st, __frm, __frm_end, __frm_nxt, __to, __to_end, __to_nxt); + } + + _LIBCPP_ALWAYS_INLINE + result unshift(state_type& __st, + extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const + { + return do_unshift(__st, __to, __to_end, __to_nxt); + } + + _LIBCPP_ALWAYS_INLINE + result in(state_type& __st, + const extern_type* __frm, const extern_type* __frm_end, const extern_type*& __frm_nxt, + intern_type* __to, intern_type* __to_end, intern_type*& __to_nxt) const + { + return do_in(__st, __frm, __frm_end, __frm_nxt, __to, __to_end, __to_nxt); + } + + _LIBCPP_ALWAYS_INLINE + int encoding() const _NOEXCEPT + { + return do_encoding(); + } + + _LIBCPP_ALWAYS_INLINE + bool always_noconv() const _NOEXCEPT + { + return do_always_noconv(); + } + + _LIBCPP_ALWAYS_INLINE + int length(state_type& __st, const extern_type* __frm, const extern_type* __end, size_t __mx) const + { + return do_length(__st, __frm, __end, __mx); + } + + _LIBCPP_ALWAYS_INLINE + int max_length() const _NOEXCEPT + { + return do_max_length(); + } + + static locale::id id; + +protected: + _LIBCPP_ALWAYS_INLINE + explicit codecvt(const char*, size_t __refs = 0) + : locale::facet(__refs) {} + + ~codecvt(); + + virtual result do_out(state_type& __st, + const intern_type* __frm, const intern_type* __frm_end, const intern_type*& __frm_nxt, + extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const; + virtual result do_in(state_type& __st, + const extern_type* __frm, const extern_type* __frm_end, const extern_type*& __frm_nxt, + intern_type* __to, intern_type* __to_end, intern_type*& __to_nxt) const; + virtual result do_unshift(state_type& __st, + extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const; + virtual int do_encoding() const _NOEXCEPT; + virtual bool do_always_noconv() const _NOEXCEPT; + virtual int do_length(state_type&, const extern_type* __frm, const extern_type* __end, size_t __mx) const; + virtual int do_max_length() const _NOEXCEPT; +}; + +// template <class _InternT, class _ExternT, class _StateT> class codecvt_byname + +template <class _InternT, class _ExternT, class _StateT> +class _LIBCPP_VISIBLE codecvt_byname + : public codecvt<_InternT, _ExternT, _StateT> +{ +public: + _LIBCPP_ALWAYS_INLINE + explicit codecvt_byname(const char* __nm, size_t __refs = 0) + : codecvt<_InternT, _ExternT, _StateT>(__nm, __refs) {} + _LIBCPP_ALWAYS_INLINE + explicit codecvt_byname(const string& __nm, size_t __refs = 0) + : codecvt<_InternT, _ExternT, _StateT>(__nm.c_str(), __refs) {} +protected: + ~codecvt_byname(); +}; + +template <class _InternT, class _ExternT, class _StateT> +codecvt_byname<_InternT, _ExternT, _StateT>::~codecvt_byname() +{ +} + +extern template class codecvt_byname<char, char, mbstate_t>; +extern template class codecvt_byname<wchar_t, char, mbstate_t>; +extern template class codecvt_byname<char16_t, char, mbstate_t>; +extern template class codecvt_byname<char32_t, char, mbstate_t>; + +_LIBCPP_VISIBLE void __throw_runtime_error(const char*); + +template <size_t _N> +struct __narrow_to_utf8 +{ + template <class _OutputIterator, class _CharT> + _OutputIterator + operator()(_OutputIterator __s, const _CharT* __wb, const _CharT* __we) const; +}; + +template <> +struct __narrow_to_utf8<8> +{ + template <class _OutputIterator, class _CharT> + _LIBCPP_ALWAYS_INLINE + _OutputIterator + operator()(_OutputIterator __s, const _CharT* __wb, const _CharT* __we) const + { + for (; __wb < __we; ++__wb, ++__s) + *__s = *__wb; + return __s; + } +}; + +template <> +struct __narrow_to_utf8<16> + : public codecvt<char16_t, char, mbstate_t> +{ + _LIBCPP_ALWAYS_INLINE + __narrow_to_utf8() : codecvt<char16_t, char, mbstate_t>(1) {} + + ~__narrow_to_utf8(); + + template <class _OutputIterator, class _CharT> + _LIBCPP_ALWAYS_INLINE + _OutputIterator + operator()(_OutputIterator __s, const _CharT* __wb, const _CharT* __we) const + { + result __r = ok; + mbstate_t __mb; + while (__wb < __we && __r != error) + { + const int __sz = 32; + char __buf[__sz]; + char* __bn; + const char16_t* __wn = (const char16_t*)__wb; + __r = do_out(__mb, (const char16_t*)__wb, (const char16_t*)__we, __wn, + __buf, __buf+__sz, __bn); + if (__r == codecvt_base::error || __wn == (const char16_t*)__wb) + __throw_runtime_error("locale not supported"); + for (const char* __p = __buf; __p < __bn; ++__p, ++__s) + *__s = *__p; + __wb = (const _CharT*)__wn; + } + return __s; + } +}; + +template <> +struct __narrow_to_utf8<32> + : public codecvt<char32_t, char, mbstate_t> +{ + _LIBCPP_ALWAYS_INLINE + __narrow_to_utf8() : codecvt<char32_t, char, mbstate_t>(1) {} + + ~__narrow_to_utf8(); + + template <class _OutputIterator, class _CharT> + _LIBCPP_ALWAYS_INLINE + _OutputIterator + operator()(_OutputIterator __s, const _CharT* __wb, const _CharT* __we) const + { + result __r = ok; + mbstate_t __mb; + while (__wb < __we && __r != error) + { + const int __sz = 32; + char __buf[__sz]; + char* __bn; + const char32_t* __wn = (const char32_t*)__wb; + __r = do_out(__mb, (const char32_t*)__wb, (const char32_t*)__we, __wn, + __buf, __buf+__sz, __bn); + if (__r == codecvt_base::error || __wn == (const char32_t*)__wb) + __throw_runtime_error("locale not supported"); + for (const char* __p = __buf; __p < __bn; ++__p, ++__s) + *__s = *__p; + __wb = (const _CharT*)__wn; + } + return __s; + } +}; + +template <size_t _N> +struct __widen_from_utf8 +{ + template <class _OutputIterator> + _OutputIterator + operator()(_OutputIterator __s, const char* __nb, const char* __ne) const; +}; + +template <> +struct __widen_from_utf8<8> +{ + template <class _OutputIterator> + _LIBCPP_ALWAYS_INLINE + _OutputIterator + operator()(_OutputIterator __s, const char* __nb, const char* __ne) const + { + for (; __nb < __ne; ++__nb, ++__s) + *__s = *__nb; + return __s; + } +}; + +template <> +struct __widen_from_utf8<16> + : public codecvt<char16_t, char, mbstate_t> +{ + _LIBCPP_ALWAYS_INLINE + __widen_from_utf8() : codecvt<char16_t, char, mbstate_t>(1) {} + + ~__widen_from_utf8(); + + template <class _OutputIterator> + _LIBCPP_ALWAYS_INLINE + _OutputIterator + operator()(_OutputIterator __s, const char* __nb, const char* __ne) const + { + result __r = ok; + mbstate_t __mb; + while (__nb < __ne && __r != error) + { + const int __sz = 32; + char16_t __buf[__sz]; + char16_t* __bn; + const char* __nn = __nb; + __r = do_in(__mb, __nb, __ne - __nb > __sz ? __nb+__sz : __ne, __nn, + __buf, __buf+__sz, __bn); + if (__r == codecvt_base::error || __nn == __nb) + __throw_runtime_error("locale not supported"); + for (const char16_t* __p = __buf; __p < __bn; ++__p, ++__s) + *__s = (wchar_t)*__p; + __nb = __nn; + } + return __s; + } +}; + +template <> +struct __widen_from_utf8<32> + : public codecvt<char32_t, char, mbstate_t> +{ + _LIBCPP_ALWAYS_INLINE + __widen_from_utf8() : codecvt<char32_t, char, mbstate_t>(1) {} + + ~__widen_from_utf8(); + + template <class _OutputIterator> + _LIBCPP_ALWAYS_INLINE + _OutputIterator + operator()(_OutputIterator __s, const char* __nb, const char* __ne) const + { + result __r = ok; + mbstate_t __mb; + while (__nb < __ne && __r != error) + { + const int __sz = 32; + char32_t __buf[__sz]; + char32_t* __bn; + const char* __nn = __nb; + __r = do_in(__mb, __nb, __ne - __nb > __sz ? __nb+__sz : __ne, __nn, + __buf, __buf+__sz, __bn); + if (__r == codecvt_base::error || __nn == __nb) + __throw_runtime_error("locale not supported"); + for (const char32_t* __p = __buf; __p < __bn; ++__p, ++__s) + *__s = (wchar_t)*__p; + __nb = __nn; + } + return __s; + } +}; + +// template <class charT> class numpunct + +template <class _CharT> class _LIBCPP_VISIBLE numpunct; + +template <> +class _LIBCPP_VISIBLE numpunct<char> + : public locale::facet +{ +public: + typedef char char_type; + typedef basic_string<char_type> string_type; + + explicit numpunct(size_t __refs = 0); + + _LIBCPP_ALWAYS_INLINE char_type decimal_point() const {return do_decimal_point();} + _LIBCPP_ALWAYS_INLINE char_type thousands_sep() const {return do_thousands_sep();} + _LIBCPP_ALWAYS_INLINE string grouping() const {return do_grouping();} + _LIBCPP_ALWAYS_INLINE string_type truename() const {return do_truename();} + _LIBCPP_ALWAYS_INLINE string_type falsename() const {return do_falsename();} + + static locale::id id; + +protected: + ~numpunct(); + virtual char_type do_decimal_point() const; + virtual char_type do_thousands_sep() const; + virtual string do_grouping() const; + virtual string_type do_truename() const; + virtual string_type do_falsename() const; + + char_type __decimal_point_; + char_type __thousands_sep_; + string __grouping_; +}; + +template <> +class _LIBCPP_VISIBLE numpunct<wchar_t> + : public locale::facet +{ +public: + typedef wchar_t char_type; + typedef basic_string<char_type> string_type; + + explicit numpunct(size_t __refs = 0); + + _LIBCPP_ALWAYS_INLINE char_type decimal_point() const {return do_decimal_point();} + _LIBCPP_ALWAYS_INLINE char_type thousands_sep() const {return do_thousands_sep();} + _LIBCPP_ALWAYS_INLINE string grouping() const {return do_grouping();} + _LIBCPP_ALWAYS_INLINE string_type truename() const {return do_truename();} + _LIBCPP_ALWAYS_INLINE string_type falsename() const {return do_falsename();} + + static locale::id id; + +protected: + ~numpunct(); + virtual char_type do_decimal_point() const; + virtual char_type do_thousands_sep() const; + virtual string do_grouping() const; + virtual string_type do_truename() const; + virtual string_type do_falsename() const; + + char_type __decimal_point_; + char_type __thousands_sep_; + string __grouping_; +}; + +// template <class charT> class numpunct_byname + +template <class charT> class _LIBCPP_VISIBLE numpunct_byname; + +template <> +class _LIBCPP_VISIBLE numpunct_byname<char> +: public numpunct<char> +{ +public: + typedef char char_type; + typedef basic_string<char_type> string_type; + + explicit numpunct_byname(const char* __nm, size_t __refs = 0); + explicit numpunct_byname(const string& __nm, size_t __refs = 0); + +protected: + ~numpunct_byname(); + +private: + void __init(const char*); +}; + +template <> +class _LIBCPP_VISIBLE numpunct_byname<wchar_t> +: public numpunct<wchar_t> +{ +public: + typedef wchar_t char_type; + typedef basic_string<char_type> string_type; + + explicit numpunct_byname(const char* __nm, size_t __refs = 0); + explicit numpunct_byname(const string& __nm, size_t __refs = 0); + +protected: + ~numpunct_byname(); + +private: + void __init(const char*); +}; + +_LIBCPP_END_NAMESPACE_STD + +#endif // _LIBCPP___LOCALE diff --git a/include/__mutex_base b/include/__mutex_base new file mode 100644 index 000000000000..55687659a03a --- /dev/null +++ b/include/__mutex_base @@ -0,0 +1,439 @@ +// -*- C++ -*- +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#ifndef _LIBCPP___MUTEX_BASE +#define _LIBCPP___MUTEX_BASE + +#include <__config> +#include <chrono> +#include <system_error> +#include <pthread.h> + +#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) +#pragma GCC system_header +#endif + +#ifdef _LIBCPP_SHARED_LOCK + +namespace ting { +template <class _Mutex> class shared_lock; +template <class _Mutex> class upgrade_lock; +} + +#endif // _LIBCPP_SHARED_LOCK + + +_LIBCPP_BEGIN_NAMESPACE_STD + +class _LIBCPP_VISIBLE mutex +{ + pthread_mutex_t __m_; + +public: + _LIBCPP_INLINE_VISIBILITY + mutex() {__m_ = (pthread_mutex_t)PTHREAD_MUTEX_INITIALIZER;} + ~mutex(); + +private: + mutex(const mutex&);// = delete; + mutex& operator=(const mutex&);// = delete; + +public: + void lock(); + bool try_lock(); + void unlock(); + + typedef pthread_mutex_t* native_handle_type; + _LIBCPP_INLINE_VISIBILITY native_handle_type native_handle() {return &__m_;} +}; + +struct _LIBCPP_VISIBLE defer_lock_t {}; +struct _LIBCPP_VISIBLE try_to_lock_t {}; +struct _LIBCPP_VISIBLE adopt_lock_t {}; + +//constexpr +extern const +defer_lock_t defer_lock; + +//constexpr +extern const +try_to_lock_t try_to_lock; + +//constexpr +extern const +adopt_lock_t adopt_lock; + +template <class _Mutex> +class _LIBCPP_VISIBLE lock_guard +{ +public: + typedef _Mutex mutex_type; + +private: + mutex_type& __m_; +public: + + _LIBCPP_INLINE_VISIBILITY + explicit lock_guard(mutex_type& __m) + : __m_(__m) {__m_.lock();} + _LIBCPP_INLINE_VISIBILITY + lock_guard(mutex_type& __m, adopt_lock_t) + : __m_(__m) {} + _LIBCPP_INLINE_VISIBILITY + ~lock_guard() {__m_.unlock();} + +private: + lock_guard(lock_guard const&);// = delete; + lock_guard& operator=(lock_guard const&);// = delete; +}; + +template <class _Mutex> +class _LIBCPP_VISIBLE unique_lock +{ +public: + typedef _Mutex mutex_type; + +private: + mutex_type* __m_; + bool __owns_; + +public: + _LIBCPP_INLINE_VISIBILITY + unique_lock() : __m_(nullptr), __owns_(false) {} + _LIBCPP_INLINE_VISIBILITY + explicit unique_lock(mutex_type& __m) + : __m_(&__m), __owns_(true) {__m_->lock();} + _LIBCPP_INLINE_VISIBILITY + unique_lock(mutex_type& __m, defer_lock_t) + : __m_(&__m), __owns_(false) {} + _LIBCPP_INLINE_VISIBILITY + unique_lock(mutex_type& __m, try_to_lock_t) + : __m_(&__m), __owns_(__m.try_lock()) {} + _LIBCPP_INLINE_VISIBILITY + unique_lock(mutex_type& __m, adopt_lock_t) + : __m_(&__m), __owns_(true) {} + template <class _Clock, class _Duration> + _LIBCPP_INLINE_VISIBILITY + unique_lock(mutex_type& __m, const chrono::time_point<_Clock, _Duration>& __t) + : __m_(&__m), __owns_(__m.try_lock_until(__t)) {} + template <class _Rep, class _Period> + _LIBCPP_INLINE_VISIBILITY + unique_lock(mutex_type& __m, const chrono::duration<_Rep, _Period>& __d) + : __m_(&__m), __owns_(__m.try_lock_for(__d)) {} + _LIBCPP_INLINE_VISIBILITY + ~unique_lock() + { + if (__owns_) + __m_->unlock(); + } + +private: + unique_lock(unique_lock const&); // = delete; + unique_lock& operator=(unique_lock const&); // = delete; + +public: +#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES + _LIBCPP_INLINE_VISIBILITY + unique_lock(unique_lock&& __u) + : __m_(__u.__m_), __owns_(__u.__owns_) + {__u.__m_ = nullptr; __u.__owns_ = false;} + _LIBCPP_INLINE_VISIBILITY + unique_lock& operator=(unique_lock&& __u) + { + if (__owns_) + __m_->unlock(); + __m_ = __u.__m_; + __owns_ = __u.__owns_; + __u.__m_ = nullptr; + __u.__owns_ = false; + return *this; + } + +#ifdef _LIBCPP_SHARED_LOCK + + unique_lock(ting::shared_lock<mutex_type>&&, try_to_lock_t); + template <class _Clock, class _Duration> + unique_lock(ting::shared_lock<mutex_type>&&, + const chrono::time_point<_Clock, _Duration>&); + template <class _Rep, class _Period> + unique_lock(ting::shared_lock<mutex_type>&&, + const chrono::duration<_Rep, _Period>&); + + explicit unique_lock(ting::upgrade_lock<mutex_type>&&); + unique_lock(ting::upgrade_lock<mutex_type>&&, try_to_lock_t); + template <class _Clock, class _Duration> + unique_lock(ting::upgrade_lock<mutex_type>&&, + const chrono::time_point<_Clock, _Duration>&); + template <class _Rep, class _Period> + unique_lock(ting::upgrade_lock<mutex_type>&&, + const chrono::duration<_Rep, _Period>&); + +#endif // _LIBCPP_SHARED_LOCK + +#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES + + void lock(); + bool try_lock(); + + template <class _Rep, class _Period> + bool try_lock_for(const chrono::duration<_Rep, _Period>& __d); + template <class _Clock, class _Duration> + bool try_lock_until(const chrono::time_point<_Clock, _Duration>& __t); + + void unlock(); + + _LIBCPP_INLINE_VISIBILITY + void swap(unique_lock& __u) + { + _VSTD::swap(__m_, __u.__m_); + _VSTD::swap(__owns_, __u.__owns_); + } + _LIBCPP_INLINE_VISIBILITY + mutex_type* release() + { + mutex_type* __m = __m_; + __m_ = nullptr; + __owns_ = false; + return __m; + } + + _LIBCPP_INLINE_VISIBILITY + bool owns_lock() const {return __owns_;} + _LIBCPP_INLINE_VISIBILITY +// explicit + operator bool () const {return __owns_;} + _LIBCPP_INLINE_VISIBILITY + mutex_type* mutex() const {return __m_;} +}; + +template <class _Mutex> +void +unique_lock<_Mutex>::lock() +{ + if (__m_ == nullptr) + __throw_system_error(EPERM, "unique_lock::lock: references null mutex"); + if (__owns_) + __throw_system_error(EDEADLK, "unique_lock::lock: already locked"); + __m_->lock(); + __owns_ = true; +} + +template <class _Mutex> +bool +unique_lock<_Mutex>::try_lock() +{ + if (__m_ == nullptr) + __throw_system_error(EPERM, "unique_lock::try_lock: references null mutex"); + if (__owns_) + __throw_system_error(EDEADLK, "unique_lock::try_lock: already locked"); + __owns_ = __m_->try_lock(); + return __owns_; +} + +template <class _Mutex> +template <class _Rep, class _Period> +bool +unique_lock<_Mutex>::try_lock_for(const chrono::duration<_Rep, _Period>& __d) +{ + if (__m_ == nullptr) + __throw_system_error(EPERM, "unique_lock::try_lock_for: references null mutex"); + if (__owns_) + __throw_system_error(EDEADLK, "unique_lock::try_lock_for: already locked"); + __owns_ = __m_->try_lock_for(__d); + return __owns_; +} + +template <class _Mutex> +template <class _Clock, class _Duration> +bool +unique_lock<_Mutex>::try_lock_until(const chrono::time_point<_Clock, _Duration>& __t) +{ + if (__m_ == nullptr) + __throw_system_error(EPERM, "unique_lock::try_lock_until: references null mutex"); + if (__owns_) + __throw_system_error(EDEADLK, "unique_lock::try_lock_until: already locked"); + __owns_ = __m_->try_lock_until(__t); + return __owns_; +} + +template <class _Mutex> +void +unique_lock<_Mutex>::unlock() +{ + if (!__owns_) + __throw_system_error(EPERM, "unique_lock::unlock: not locked"); + __m_->unlock(); + __owns_ = false; +} + +template <class _Mutex> +inline _LIBCPP_INLINE_VISIBILITY +void +swap(unique_lock<_Mutex>& __x, unique_lock<_Mutex>& __y) {__x.swap(__y);} + +struct _LIBCPP_VISIBLE cv_status +{ + enum _ { + no_timeout, + timeout + }; + + _ __v_; + + _LIBCPP_INLINE_VISIBILITY cv_status(_ __v) : __v_(__v) {} + _LIBCPP_INLINE_VISIBILITY operator int() const {return __v_;} + +}; + +class _LIBCPP_VISIBLE condition_variable +{ + pthread_cond_t __cv_; +public: + _LIBCPP_INLINE_VISIBILITY + condition_variable() {__cv_ = (pthread_cond_t)PTHREAD_COND_INITIALIZER;} + ~condition_variable(); + +private: + condition_variable(const condition_variable&); // = delete; + condition_variable& operator=(const condition_variable&); // = delete; + +public: + void notify_one(); + void notify_all(); + + void wait(unique_lock<mutex>& __lk); + template <class _Predicate> + void wait(unique_lock<mutex>& __lk, _Predicate __pred); + + template <class _Duration> + cv_status + wait_until(unique_lock<mutex>& __lk, + const chrono::time_point<chrono::system_clock, _Duration>& __t); + + template <class _Clock, class _Duration> + cv_status + wait_until(unique_lock<mutex>& __lk, + const chrono::time_point<_Clock, _Duration>& __t); + + template <class _Clock, class _Duration, class _Predicate> + bool + wait_until(unique_lock<mutex>& __lk, + const chrono::time_point<_Clock, _Duration>& __t, + _Predicate __pred); + + template <class _Rep, class _Period> + cv_status + wait_for(unique_lock<mutex>& __lk, + const chrono::duration<_Rep, _Period>& __d); + + template <class _Rep, class _Period, class _Predicate> + bool + wait_for(unique_lock<mutex>& __lk, + const chrono::duration<_Rep, _Period>& __d, + _Predicate __pred); + + typedef pthread_cond_t* native_handle_type; + _LIBCPP_INLINE_VISIBILITY native_handle_type native_handle() {return &__cv_;} + +private: + void __do_timed_wait(unique_lock<mutex>& __lk, + chrono::time_point<chrono::system_clock, chrono::nanoseconds>); +}; + +template <class _To, class _Rep, class _Period> +inline _LIBCPP_INLINE_VISIBILITY +typename enable_if +< + chrono::__is_duration<_To>::value, + _To +>::type +__ceil(chrono::duration<_Rep, _Period> __d) +{ + using namespace chrono; + _To __r = duration_cast<_To>(__d); + if (__r < __d) + ++__r; + return __r; +} + +template <class _Predicate> +void +condition_variable::wait(unique_lock<mutex>& __lk, _Predicate __pred) +{ + while (!__pred()) + wait(__lk); +} + +template <class _Duration> +cv_status +condition_variable::wait_until(unique_lock<mutex>& __lk, + const chrono::time_point<chrono::system_clock, _Duration>& __t) +{ + using namespace chrono; + typedef time_point<system_clock, nanoseconds> __nano_sys_tmpt; + __do_timed_wait(__lk, + __nano_sys_tmpt(__ceil<nanoseconds>(__t.time_since_epoch()))); + return system_clock::now() < __t ? cv_status::no_timeout : + cv_status::timeout; +} + +template <class _Clock, class _Duration> +cv_status +condition_variable::wait_until(unique_lock<mutex>& __lk, + const chrono::time_point<_Clock, _Duration>& __t) +{ + using namespace chrono; + system_clock::time_point __s_now = system_clock::now(); + typename _Clock::time_point __c_now = _Clock::now(); + __do_timed_wait(__lk, __s_now + __ceil<nanoseconds>(__t - __c_now)); + return _Clock::now() < __t ? cv_status::no_timeout : cv_status::timeout; +} + +template <class _Clock, class _Duration, class _Predicate> +bool +condition_variable::wait_until(unique_lock<mutex>& __lk, + const chrono::time_point<_Clock, _Duration>& __t, + _Predicate __pred) +{ + while (!__pred()) + { + if (wait_until(__lk, __t) == cv_status::timeout) + return __pred(); + } + return true; +} + +template <class _Rep, class _Period> +cv_status +condition_variable::wait_for(unique_lock<mutex>& __lk, + const chrono::duration<_Rep, _Period>& __d) +{ + using namespace chrono; + system_clock::time_point __s_now = system_clock::now(); + steady_clock::time_point __c_now = steady_clock::now(); + __do_timed_wait(__lk, __s_now + __ceil<nanoseconds>(__d)); + return steady_clock::now() - __c_now < __d ? cv_status::no_timeout : + cv_status::timeout; +} + +template <class _Rep, class _Period, class _Predicate> +inline _LIBCPP_INLINE_VISIBILITY +bool +condition_variable::wait_for(unique_lock<mutex>& __lk, + const chrono::duration<_Rep, _Period>& __d, + _Predicate __pred) +{ + return wait_until(__lk, chrono::steady_clock::now() + __d, + _VSTD::move(__pred)); +} + +_LIBCPP_END_NAMESPACE_STD + +#endif // _LIBCPP___MUTEX_BASE diff --git a/include/__split_buffer b/include/__split_buffer new file mode 100644 index 000000000000..33ce42da31a4 --- /dev/null +++ b/include/__split_buffer @@ -0,0 +1,650 @@ +// -*- C++ -*- +#ifndef _LIBCPP_SPLIT_BUFFER +#define _LIBCPP_SPLIT_BUFFER + +#include <__config> +#include <type_traits> +#include <algorithm> + +#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) +#pragma GCC system_header +#endif + +_LIBCPP_BEGIN_NAMESPACE_STD + +template <bool> +class __split_buffer_common +{ +protected: + void __throw_length_error() const; + void __throw_out_of_range() const; +}; + +template <class _Tp, class _Allocator = allocator<_Tp> > +struct __split_buffer + : private __split_buffer_common<true> +{ +private: + __split_buffer(const __split_buffer&); + __split_buffer& operator=(const __split_buffer&); +public: + typedef _Tp value_type; + typedef _Allocator allocator_type; + typedef typename remove_reference<allocator_type>::type __alloc_rr; + typedef allocator_traits<__alloc_rr> __alloc_traits; + typedef value_type& reference; + typedef const value_type& const_reference; + typedef typename __alloc_traits::size_type size_type; + typedef typename __alloc_traits::difference_type difference_type; + typedef typename __alloc_traits::pointer pointer; + typedef typename __alloc_traits::const_pointer const_pointer; + typedef pointer iterator; + typedef const_pointer const_iterator; + + pointer __first_; + pointer __begin_; + pointer __end_; + __compressed_pair<pointer, allocator_type> __end_cap_; + + typedef typename add_lvalue_reference<allocator_type>::type __alloc_ref; + typedef typename add_lvalue_reference<allocator_type>::type __alloc_const_ref; + + _LIBCPP_INLINE_VISIBILITY __alloc_rr& __alloc() _NOEXCEPT {return __end_cap_.second();} + _LIBCPP_INLINE_VISIBILITY const __alloc_rr& __alloc() const _NOEXCEPT {return __end_cap_.second();} + _LIBCPP_INLINE_VISIBILITY pointer& __end_cap() _NOEXCEPT {return __end_cap_.first();} + _LIBCPP_INLINE_VISIBILITY const pointer& __end_cap() const _NOEXCEPT {return __end_cap_.first();} + + __split_buffer() + _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value); + explicit __split_buffer(__alloc_rr& __a); + explicit __split_buffer(const __alloc_rr& __a); + __split_buffer(size_type __cap, size_type __start, __alloc_rr& __a); + ~__split_buffer(); + +#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES + __split_buffer(__split_buffer&& __c) + _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value); + __split_buffer(__split_buffer&& __c, const __alloc_rr& __a); + __split_buffer& operator=(__split_buffer&& __c) + _NOEXCEPT_((__alloc_traits::propagate_on_container_move_assignment::value && + is_nothrow_move_assignable<allocator_type>::value) || + !__alloc_traits::propagate_on_container_move_assignment::value); +#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES + + _LIBCPP_INLINE_VISIBILITY iterator begin() _NOEXCEPT {return __begin_;} + _LIBCPP_INLINE_VISIBILITY const_iterator begin() const _NOEXCEPT {return __begin_;} + _LIBCPP_INLINE_VISIBILITY iterator end() _NOEXCEPT {return __end_;} + _LIBCPP_INLINE_VISIBILITY const_iterator end() const _NOEXCEPT {return __end_;} + + _LIBCPP_INLINE_VISIBILITY + void clear() _NOEXCEPT + {__destruct_at_end(__begin_);} + _LIBCPP_INLINE_VISIBILITY size_type size() const {return static_cast<size_type>(__end_ - __begin_);} + _LIBCPP_INLINE_VISIBILITY bool empty() const {return __end_ == __begin_;} + _LIBCPP_INLINE_VISIBILITY size_type capacity() const {return static_cast<size_type>(__end_cap() - __first_);} + _LIBCPP_INLINE_VISIBILITY size_type __front_spare() const {return static_cast<size_type>(__begin_ - __first_);} + _LIBCPP_INLINE_VISIBILITY size_type __back_spare() const {return static_cast<size_type>(__end_cap() - __end_);} + + _LIBCPP_INLINE_VISIBILITY reference front() {return *__begin_;} + _LIBCPP_INLINE_VISIBILITY const_reference front() const {return *__begin_;} + _LIBCPP_INLINE_VISIBILITY reference back() {return *(__end_ - 1);} + _LIBCPP_INLINE_VISIBILITY const_reference back() const {return *(__end_ - 1);} + + void reserve(size_type __n); + void shrink_to_fit() _NOEXCEPT; + void push_front(const_reference __x); + void push_back(const_reference __x); +#if !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) + void push_front(value_type&& __x); + void push_back(value_type&& __x); +#if !defined(_LIBCPP_HAS_NO_VARIADICS) + template <class... _Args> + void emplace_back(_Args&&... __args); +#endif // !defined(_LIBCPP_HAS_NO_VARIADICS) +#endif // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) + + _LIBCPP_INLINE_VISIBILITY void pop_front() {__destruct_at_begin(__begin_+1);} + _LIBCPP_INLINE_VISIBILITY void pop_back() {__destruct_at_end(__end_-1);} + + void __construct_at_end(size_type __n); + void __construct_at_end(size_type __n, const_reference __x); + template <class _InputIter> + typename enable_if + < + __is_input_iterator<_InputIter>::value && + !__is_forward_iterator<_InputIter>::value, + void + >::type + __construct_at_end(_InputIter __first, _InputIter __last); + template <class _ForwardIterator> + typename enable_if + < + __is_forward_iterator<_ForwardIterator>::value, + void + >::type + __construct_at_end(_ForwardIterator __first, _ForwardIterator __last); + + _LIBCPP_INLINE_VISIBILITY void __destruct_at_begin(pointer __new_begin) + {__destruct_at_begin(__new_begin, is_trivially_destructible<value_type>());} + void __destruct_at_begin(pointer __new_begin, false_type); + void __destruct_at_begin(pointer __new_begin, true_type); + + _LIBCPP_INLINE_VISIBILITY + void __destruct_at_end(pointer __new_last) _NOEXCEPT + {__destruct_at_end(__new_last, is_trivially_destructible<value_type>());} + void __destruct_at_end(pointer __new_last, false_type) _NOEXCEPT; + void __destruct_at_end(pointer __new_last, true_type) _NOEXCEPT; + + void swap(__split_buffer& __x) + _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value|| + __is_nothrow_swappable<__alloc_rr>::value); + + bool __invariants() const; + +private: + _LIBCPP_INLINE_VISIBILITY + void __move_assign_alloc(__split_buffer& __c, true_type) + _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value) + { + __alloc() = _VSTD::move(__c.__alloc()); + } + + _LIBCPP_INLINE_VISIBILITY + void __move_assign_alloc(__split_buffer& __c, false_type) _NOEXCEPT + {} + + _LIBCPP_INLINE_VISIBILITY + static void __swap_alloc(__alloc_rr& __x, __alloc_rr& __y) + _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value|| + __is_nothrow_swappable<__alloc_rr>::value) + {__swap_alloc(__x, __y, integral_constant<bool, + __alloc_traits::propagate_on_container_swap::value>());} + + _LIBCPP_INLINE_VISIBILITY + static void __swap_alloc(__alloc_rr& __x, __alloc_rr& __y, true_type) + _NOEXCEPT_(__is_nothrow_swappable<__alloc_rr>::value) + { + using _VSTD::swap; + swap(__x, __y); + } + + _LIBCPP_INLINE_VISIBILITY + static void __swap_alloc(__alloc_rr& __x, __alloc_rr& __y, false_type) _NOEXCEPT + {} +}; + +template <class _Tp, class _Allocator> +bool +__split_buffer<_Tp, _Allocator>::__invariants() const +{ + if (__first_ == nullptr) + { + if (__begin_ != nullptr) + return false; + if (__end_ != nullptr) + return false; + if (__end_cap() != nullptr) + return false; + } + else + { + if (__begin_ < __first_) + return false; + if (__end_ < __begin_) + return false; + if (__end_cap() < __end_) + return false; + } + return true; +} + +// Default constructs __n objects starting at __end_ +// throws if construction throws +// Precondition: __n > 0 +// Precondition: size() + __n <= capacity() +// Postcondition: size() == size() + __n +template <class _Tp, class _Allocator> +void +__split_buffer<_Tp, _Allocator>::__construct_at_end(size_type __n) +{ + __alloc_rr& __a = this->__alloc(); + do + { + __alloc_traits::construct(__a, _VSTD::__to_raw_pointer(this->__end_)); + ++this->__end_; + --__n; + } while (__n > 0); +} + +// Copy constructs __n objects starting at __end_ from __x +// throws if construction throws +// Precondition: __n > 0 +// Precondition: size() + __n <= capacity() +// Postcondition: size() == old size() + __n +// Postcondition: [i] == __x for all i in [size() - __n, __n) +template <class _Tp, class _Allocator> +void +__split_buffer<_Tp, _Allocator>::__construct_at_end(size_type __n, const_reference __x) +{ + __alloc_rr& __a = this->__alloc(); + do + { + __alloc_traits::construct(__a, _VSTD::__to_raw_pointer(this->__end_), __x); + ++this->__end_; + --__n; + } while (__n > 0); +} + +template <class _Tp, class _Allocator> +template <class _InputIter> +typename enable_if +< + __is_input_iterator<_InputIter>::value && + !__is_forward_iterator<_InputIter>::value, + void +>::type +__split_buffer<_Tp, _Allocator>::__construct_at_end(_InputIter __first, _InputIter __last) +{ + __alloc_rr& __a = this->__alloc(); + for (; __first != __last; ++__first) + { + if (__end_ == __end_cap()) + { + size_type __old_cap = __end_cap() - __first_; + size_type __new_cap = _VSTD::max<size_type>(2 * __old_cap, 8); + __split_buffer __buf(__new_cap, 0, __a); + for (pointer __p = __begin_; __p != __end_; ++__p, ++__buf.__end_) + __alloc_traits::construct(__buf.__alloc(), + _VSTD::__to_raw_pointer(__buf.__end_), _VSTD::move(*__p)); + swap(__buf); + } + __alloc_traits::construct(__a, _VSTD::__to_raw_pointer(this->__end_), *__first); + ++this->__end_; + } +} + +template <class _Tp, class _Allocator> +template <class _ForwardIterator> +typename enable_if +< + __is_forward_iterator<_ForwardIterator>::value, + void +>::type +__split_buffer<_Tp, _Allocator>::__construct_at_end(_ForwardIterator __first, _ForwardIterator __last) +{ + __alloc_rr& __a = this->__alloc(); + for (; __first != __last; ++__first) + { + __alloc_traits::construct(__a, _VSTD::__to_raw_pointer(this->__end_), *__first); + ++this->__end_; + } +} + +template <class _Tp, class _Allocator> +_LIBCPP_INLINE_VISIBILITY inline +void +__split_buffer<_Tp, _Allocator>::__destruct_at_begin(pointer __new_begin, false_type) +{ + while (__begin_ < __new_begin) + __alloc_traits::destroy(__alloc(), __begin_++); +} + +template <class _Tp, class _Allocator> +_LIBCPP_INLINE_VISIBILITY inline +void +__split_buffer<_Tp, _Allocator>::__destruct_at_begin(pointer __new_begin, true_type) +{ + __begin_ = __new_begin; +} + +template <class _Tp, class _Allocator> +_LIBCPP_INLINE_VISIBILITY inline +void +__split_buffer<_Tp, _Allocator>::__destruct_at_end(pointer __new_last, false_type) _NOEXCEPT +{ + while (__new_last < __end_) + __alloc_traits::destroy(__alloc(), --__end_); +} + +template <class _Tp, class _Allocator> +_LIBCPP_INLINE_VISIBILITY inline +void +__split_buffer<_Tp, _Allocator>::__destruct_at_end(pointer __new_last, true_type) _NOEXCEPT +{ + __end_ = __new_last; +} + +template <class _Tp, class _Allocator> +__split_buffer<_Tp, _Allocator>::__split_buffer(size_type __cap, size_type __start, __alloc_rr& __a) + : __end_cap_(0, __a) +{ + __first_ = __cap != 0 ? __alloc_traits::allocate(__alloc(), __cap) : nullptr; + __begin_ = __end_ = __first_ + __start; + __end_cap() = __first_ + __cap; +} + +template <class _Tp, class _Allocator> +_LIBCPP_INLINE_VISIBILITY inline +__split_buffer<_Tp, _Allocator>::__split_buffer() + _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value) + : __first_(0), __begin_(0), __end_(0), __end_cap_(0) +{ +} + +template <class _Tp, class _Allocator> +_LIBCPP_INLINE_VISIBILITY inline +__split_buffer<_Tp, _Allocator>::__split_buffer(__alloc_rr& __a) + : __first_(0), __begin_(0), __end_(0), __end_cap_(0, __a) +{ +} + +template <class _Tp, class _Allocator> +_LIBCPP_INLINE_VISIBILITY inline +__split_buffer<_Tp, _Allocator>::__split_buffer(const __alloc_rr& __a) + : __first_(0), __begin_(0), __end_(0), __end_cap_(0, __a) +{ +} + +template <class _Tp, class _Allocator> +__split_buffer<_Tp, _Allocator>::~__split_buffer() +{ + clear(); + if (__first_) + __alloc_traits::deallocate(__alloc(), __first_, capacity()); +} + +#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES + +template <class _Tp, class _Allocator> +__split_buffer<_Tp, _Allocator>::__split_buffer(__split_buffer&& __c) + _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value) + : __first_(_VSTD::move(__c.__first_)), + __begin_(_VSTD::move(__c.__begin_)), + __end_(_VSTD::move(__c.__end_)), + __end_cap_(_VSTD::move(__c.__end_cap_)) +{ + __c.__first_ = nullptr; + __c.__begin_ = nullptr; + __c.__end_ = nullptr; + __c.__end_cap() = nullptr; +} + +template <class _Tp, class _Allocator> +__split_buffer<_Tp, _Allocator>::__split_buffer(__split_buffer&& __c, const __alloc_rr& __a) + : __end_cap_(__a) +{ + if (__a == __c.__alloc()) + { + __first_ = __c.__first_; + __begin_ = __c.__begin_; + __end_ = __c.__end_; + __end_cap() = __c.__end_cap(); + __c.__first_ = nullptr; + __c.__begin_ = nullptr; + __c.__end_ = nullptr; + __c.__end_cap() = nullptr; + } + else + { + size_type __cap = __c.size(); + __first_ = __alloc_traits::allocate(__alloc(), __cap); + __begin_ = __end_ = __first_; + __end_cap() = __first_ + __cap; + typedef move_iterator<iterator> _I; + __construct_at_end(_I(__c.begin()), _I(__c.end())); + } +} + +template <class _Tp, class _Allocator> +__split_buffer<_Tp, _Allocator>& +__split_buffer<_Tp, _Allocator>::operator=(__split_buffer&& __c) + _NOEXCEPT_((__alloc_traits::propagate_on_container_move_assignment::value && + is_nothrow_move_assignable<allocator_type>::value) || + !__alloc_traits::propagate_on_container_move_assignment::value) +{ + clear(); + shrink_to_fit(); + __first_ = __c.__first_; + __begin_ = __c.__begin_; + __end_ = __c.__end_; + __end_cap() = __c.__end_cap(); + __move_assign_alloc(__c, + integral_constant<bool, + __alloc_traits::propagate_on_container_move_assignment::value>()); + __c.__first_ = __c.__begin_ = __c.__end_ = __c.__end_cap() = nullptr; + return *this; +} + +#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES + +template <class _Tp, class _Allocator> +void +__split_buffer<_Tp, _Allocator>::swap(__split_buffer& __x) + _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value|| + __is_nothrow_swappable<__alloc_rr>::value) +{ + _VSTD::swap(__first_, __x.__first_); + _VSTD::swap(__begin_, __x.__begin_); + _VSTD::swap(__end_, __x.__end_); + _VSTD::swap(__end_cap(), __x.__end_cap()); + __swap_alloc(__alloc(), __x.__alloc()); +} + +template <class _Tp, class _Allocator> +void +__split_buffer<_Tp, _Allocator>::reserve(size_type __n) +{ + if (__n < capacity()) + { + __split_buffer<value_type, __alloc_rr&> __t(__n, 0, __alloc()); + __t.__construct_at_end(move_iterator<pointer>(__begin_), + move_iterator<pointer>(__end_)); + _VSTD::swap(__first_, __t.__first_); + _VSTD::swap(__begin_, __t.__begin_); + _VSTD::swap(__end_, __t.__end_); + _VSTD::swap(__end_cap(), __t.__end_cap()); + } +} + +template <class _Tp, class _Allocator> +void +__split_buffer<_Tp, _Allocator>::shrink_to_fit() _NOEXCEPT +{ + if (capacity() > size()) + { +#ifndef _LIBCPP_NO_EXCEPTIONS + try + { +#endif // _LIBCPP_NO_EXCEPTIONS + __split_buffer<value_type, __alloc_rr&> __t(size(), 0, __alloc()); + __t.__construct_at_end(move_iterator<pointer>(__begin_), + move_iterator<pointer>(__end_)); + __t.__end_ = __t.__begin_ + (__end_ - __begin_); + _VSTD::swap(__first_, __t.__first_); + _VSTD::swap(__begin_, __t.__begin_); + _VSTD::swap(__end_, __t.__end_); + _VSTD::swap(__end_cap(), __t.__end_cap()); +#ifndef _LIBCPP_NO_EXCEPTIONS + } + catch (...) + { + } +#endif // _LIBCPP_NO_EXCEPTIONS + } +} + +template <class _Tp, class _Allocator> +void +__split_buffer<_Tp, _Allocator>::push_front(const_reference __x) +{ + if (__begin_ == __first_) + { + if (__end_ < __end_cap()) + { + difference_type __d = __end_cap() - __end_; + __d = (__d + 1) / 2; + __begin_ = _VSTD::move_backward(__begin_, __end_, __end_ + __d); + __end_ += __d; + } + else + { + size_type __c = max<size_type>(2 * (__end_cap() - __first_), 1); + __split_buffer<value_type, __alloc_rr&> __t(__c, (__c + 3) / 4, __alloc()); + __t.__construct_at_end(move_iterator<pointer>(__begin_), + move_iterator<pointer>(__end_)); + _VSTD::swap(__first_, __t.__first_); + _VSTD::swap(__begin_, __t.__begin_); + _VSTD::swap(__end_, __t.__end_); + _VSTD::swap(__end_cap(), __t.__end_cap()); + } + } + __alloc_traits::construct(__alloc(), _VSTD::__to_raw_pointer(__begin_-1), __x); + --__begin_; +} + +#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES + +template <class _Tp, class _Allocator> +void +__split_buffer<_Tp, _Allocator>::push_front(value_type&& __x) +{ + if (__begin_ == __first_) + { + if (__end_ < __end_cap()) + { + difference_type __d = __end_cap() - __end_; + __d = (__d + 1) / 2; + __begin_ = _VSTD::move_backward(__begin_, __end_, __end_ + __d); + __end_ += __d; + } + else + { + size_type __c = max<size_type>(2 * (__end_cap() - __first_), 1); + __split_buffer<value_type, __alloc_rr&> __t(__c, (__c + 3) / 4, __alloc()); + __t.__construct_at_end(move_iterator<pointer>(__begin_), + move_iterator<pointer>(__end_)); + _VSTD::swap(__first_, __t.__first_); + _VSTD::swap(__begin_, __t.__begin_); + _VSTD::swap(__end_, __t.__end_); + _VSTD::swap(__end_cap(), __t.__end_cap()); + } + } + __alloc_traits::construct(__alloc(), _VSTD::__to_raw_pointer(__begin_-1), + _VSTD::move(__x)); + --__begin_; +} + +#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES + +template <class _Tp, class _Allocator> +_LIBCPP_INLINE_VISIBILITY inline +void +__split_buffer<_Tp, _Allocator>::push_back(const_reference __x) +{ + if (__end_ == __end_cap()) + { + if (__begin_ > __first_) + { + difference_type __d = __begin_ - __first_; + __d = (__d + 1) / 2; + __end_ = _VSTD::move(__begin_, __end_, __begin_ - __d); + __begin_ -= __d; + } + else + { + size_type __c = max<size_type>(2 * (__end_cap() - __first_), 1); + __split_buffer<value_type, __alloc_rr&> __t(__c, __c / 4, __alloc()); + __t.__construct_at_end(move_iterator<pointer>(__begin_), + move_iterator<pointer>(__end_)); + _VSTD::swap(__first_, __t.__first_); + _VSTD::swap(__begin_, __t.__begin_); + _VSTD::swap(__end_, __t.__end_); + _VSTD::swap(__end_cap(), __t.__end_cap()); + } + } + __alloc_traits::construct(__alloc(), _VSTD::__to_raw_pointer(__end_), __x); + ++__end_; +} + +#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES + +template <class _Tp, class _Allocator> +void +__split_buffer<_Tp, _Allocator>::push_back(value_type&& __x) +{ + if (__end_ == __end_cap()) + { + if (__begin_ > __first_) + { + difference_type __d = __begin_ - __first_; + __d = (__d + 1) / 2; + __end_ = _VSTD::move(__begin_, __end_, __begin_ - __d); + __begin_ -= __d; + } + else + { + size_type __c = max<size_type>(2 * (__end_cap() - __first_), 1); + __split_buffer<value_type, __alloc_rr&> __t(__c, __c / 4, __alloc()); + __t.__construct_at_end(move_iterator<pointer>(__begin_), + move_iterator<pointer>(__end_)); + _VSTD::swap(__first_, __t.__first_); + _VSTD::swap(__begin_, __t.__begin_); + _VSTD::swap(__end_, __t.__end_); + _VSTD::swap(__end_cap(), __t.__end_cap()); + } + } + __alloc_traits::construct(__alloc(), _VSTD::__to_raw_pointer(__end_), + _VSTD::move(__x)); + ++__end_; +} + +#ifndef _LIBCPP_HAS_NO_VARIADICS + +template <class _Tp, class _Allocator> +template <class... _Args> +void +__split_buffer<_Tp, _Allocator>::emplace_back(_Args&&... __args) +{ + if (__end_ == __end_cap()) + { + if (__begin_ > __first_) + { + difference_type __d = __begin_ - __first_; + __d = (__d + 1) / 2; + __end_ = _VSTD::move(__begin_, __end_, __begin_ - __d); + __begin_ -= __d; + } + else + { + size_type __c = max<size_type>(2 * (__end_cap() - __first_), 1); + __split_buffer<value_type, __alloc_rr&> __t(__c, __c / 4, __alloc()); + __t.__construct_at_end(move_iterator<pointer>(__begin_), + move_iterator<pointer>(__end_)); + _VSTD::swap(__first_, __t.__first_); + _VSTD::swap(__begin_, __t.__begin_); + _VSTD::swap(__end_, __t.__end_); + _VSTD::swap(__end_cap(), __t.__end_cap()); + } + } + __alloc_traits::construct(__alloc(), _VSTD::__to_raw_pointer(__end_), + _VSTD::forward<_Args>(__args)...); + ++__end_; +} + +#endif // _LIBCPP_HAS_NO_VARIADICS + +#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES + +template <class _Tp, class _Allocator> +_LIBCPP_INLINE_VISIBILITY inline +void +swap(__split_buffer<_Tp, _Allocator>& __x, __split_buffer<_Tp, _Allocator>& __y) + _NOEXCEPT_(_NOEXCEPT_(__x.swap(__y))) +{ + __x.swap(__y); +} + + +_LIBCPP_END_NAMESPACE_STD + +#endif // _LIBCPP_SPLIT_BUFFER diff --git a/include/__sso_allocator b/include/__sso_allocator new file mode 100644 index 000000000000..16354d83cc5b --- /dev/null +++ b/include/__sso_allocator @@ -0,0 +1,77 @@ +// -*- C++ -*- +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#ifndef _LIBCPP___SSO_ALLOCATOR +#define _LIBCPP___SSO_ALLOCATOR + +#include <__config> +#include <type_traits> +#include <new> + +#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) +#pragma GCC system_header +#endif + +_LIBCPP_BEGIN_NAMESPACE_STD + +template <class _Tp, size_t _N> class _LIBCPP_HIDDEN __sso_allocator; + +template <size_t _N> +class _LIBCPP_HIDDEN __sso_allocator<void, _N> +{ +public: + typedef const void* const_pointer; + typedef void value_type; +}; + +template <class _Tp, size_t _N> +class _LIBCPP_HIDDEN __sso_allocator +{ + typename aligned_storage<sizeof(_Tp) * _N>::type buf_; + bool __allocated_; +public: + typedef size_t size_type; + typedef _Tp* pointer; + typedef _Tp value_type; + + _LIBCPP_INLINE_VISIBILITY __sso_allocator() throw() : __allocated_(false) {} + _LIBCPP_INLINE_VISIBILITY __sso_allocator(const __sso_allocator&) throw() : __allocated_(false) {} + template <class _Up> _LIBCPP_INLINE_VISIBILITY __sso_allocator(const __sso_allocator<_Up, _N>&) throw() + : __allocated_(false) {} +private: + __sso_allocator& operator=(const __sso_allocator&); +public: + _LIBCPP_INLINE_VISIBILITY pointer allocate(size_type __n, typename __sso_allocator<void, _N>::const_pointer = 0) + { + if (!__allocated_ && __n <= _N) + { + __allocated_ = true; + return (pointer)&buf_; + } + return static_cast<pointer>(::operator new(__n * sizeof(_Tp))); + } + _LIBCPP_INLINE_VISIBILITY void deallocate(pointer __p, size_type) + { + if (__p == (pointer)&buf_) + __allocated_ = false; + else + ::operator delete(__p); + } + _LIBCPP_INLINE_VISIBILITY size_type max_size() const throw() {return size_type(~0) / sizeof(_Tp);} + + _LIBCPP_INLINE_VISIBILITY + bool operator==(__sso_allocator& __a) const {return &buf_ == &__a.buf_;} + _LIBCPP_INLINE_VISIBILITY + bool operator!=(__sso_allocator& __a) const {return &buf_ != &__a.buf_;} +}; + +_LIBCPP_END_NAMESPACE_STD + +#endif // _LIBCPP___SSO_ALLOCATOR diff --git a/include/__std_stream b/include/__std_stream new file mode 100644 index 000000000000..6df6556da767 --- /dev/null +++ b/include/__std_stream @@ -0,0 +1,315 @@ +// -*- C++ -*- +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#ifndef _LIBCPP___STD_STREAM +#define _LIBCPP___STD_STREAM + +#include <__config> +#include <ostream> +#include <istream> +#include <__locale> +#include <cstdio> + +#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) +#pragma GCC system_header +#endif + +_LIBCPP_BEGIN_NAMESPACE_STD + +static const unsigned __limit = 8; + +// __stdinbuf + +template <class _CharT> +class _LIBCPP_HIDDEN __stdinbuf + : public basic_streambuf<_CharT, char_traits<_CharT> > +{ +public: + typedef _CharT char_type; + typedef char_traits<char_type> traits_type; + typedef typename traits_type::int_type int_type; + typedef typename traits_type::pos_type pos_type; + typedef typename traits_type::off_type off_type; + typedef typename traits_type::state_type state_type; + + explicit __stdinbuf(FILE* __fp); + +protected: + virtual int_type underflow(); + virtual int_type uflow(); + virtual int_type pbackfail(int_type __c = traits_type::eof()); + virtual void imbue(const locale& __loc); + +private: + + FILE* __file_; + const codecvt<char_type, char, state_type>* __cv_; + state_type __st_; + int __encoding_; + bool __always_noconv_; + + __stdinbuf(const __stdinbuf&); + __stdinbuf& operator=(const __stdinbuf&); + + int_type __getchar(bool __consume); +}; + +template <class _CharT> +__stdinbuf<_CharT>::__stdinbuf(FILE* __fp) + : __file_(__fp), + __st_() +{ + imbue(this->getloc()); +} + +template <class _CharT> +void +__stdinbuf<_CharT>::imbue(const locale& __loc) +{ + __cv_ = &use_facet<codecvt<char_type, char, state_type> >(__loc); + __encoding_ = __cv_->encoding(); + __always_noconv_ = __cv_->always_noconv(); + if (__encoding_ > __limit) + __throw_runtime_error("unsupported locale for standard input"); +} + +template <class _CharT> +typename __stdinbuf<_CharT>::int_type +__stdinbuf<_CharT>::underflow() +{ + return __getchar(false); +} + +template <class _CharT> +typename __stdinbuf<_CharT>::int_type +__stdinbuf<_CharT>::uflow() +{ + return __getchar(true); +} + +template <class _CharT> +typename __stdinbuf<_CharT>::int_type +__stdinbuf<_CharT>::__getchar(bool __consume) +{ + char __extbuf[__limit]; + int __nread = _VSTD::max(1, __encoding_); + for (int __i = 0; __i < __nread; ++__i) + { + char __c = getc(__file_); + if (__c == EOF) + return traits_type::eof(); + __extbuf[__i] = static_cast<char>(__c); + } + char_type __1buf; + if (__always_noconv_) + __1buf = static_cast<char_type>(__extbuf[0]); + else + { + const char* __enxt; + char_type* __inxt; + codecvt_base::result __r; + do + { + state_type __sv_st = __st_; + __r = __cv_->in(__st_, __extbuf, __extbuf + __nread, __enxt, + &__1buf, &__1buf + 1, __inxt); + switch (__r) + { + case _VSTD::codecvt_base::ok: + break; + case codecvt_base::partial: + __st_ = __sv_st; + if (__nread == sizeof(__extbuf)) + return traits_type::eof(); + { + char __c = getc(__file_); + if (__c == EOF) + return traits_type::eof(); + __extbuf[__nread] = static_cast<char>(__c); + } + ++__nread; + break; + case codecvt_base::error: + return traits_type::eof(); + case _VSTD::codecvt_base::noconv: + __1buf = static_cast<char_type>(__extbuf[0]); + break; + } + } while (__r == _VSTD::codecvt_base::partial); + } + if (!__consume) + { + for (int __i = __nread; __i > 0;) + { + if (ungetc(__extbuf[--__i], __file_) == EOF) + return traits_type::eof(); + } + } + return traits_type::to_int_type(__1buf); +} + +template <class _CharT> +typename __stdinbuf<_CharT>::int_type +__stdinbuf<_CharT>::pbackfail(int_type __c) +{ + if (traits_type::eq_int_type(__c, traits_type::eof())) + return __c; + char __extbuf[__limit]; + char* __enxt; + const char_type __ci = traits_type::to_char_type(__c); + const char_type* __inxt; + switch (__cv_->out(__st_, &__ci, &__ci + 1, __inxt, + __extbuf, __extbuf + sizeof(__extbuf), __enxt)) + { + case _VSTD::codecvt_base::ok: + break; + case _VSTD::codecvt_base::noconv: + __extbuf[0] = static_cast<char>(__c); + __enxt = __extbuf + 1; + break; + case codecvt_base::partial: + case codecvt_base::error: + return traits_type::eof(); + } + while (__enxt > __extbuf) + if (ungetc(*--__enxt, __file_) == EOF) + return traits_type::eof(); + return traits_type::not_eof(__c); +} + +// __stdoutbuf + +template <class _CharT> +class _LIBCPP_HIDDEN __stdoutbuf + : public basic_streambuf<_CharT, char_traits<_CharT> > +{ +public: + typedef _CharT char_type; + typedef char_traits<char_type> traits_type; + typedef typename traits_type::int_type int_type; + typedef typename traits_type::pos_type pos_type; + typedef typename traits_type::off_type off_type; + typedef typename traits_type::state_type state_type; + + explicit __stdoutbuf(FILE* __fp); + +protected: + virtual int_type overflow (int_type __c = traits_type::eof()); + virtual int sync(); + virtual void imbue(const locale& __loc); + +private: + FILE* __file_; + const codecvt<char_type, char, state_type>* __cv_; + state_type __st_; + bool __always_noconv_; + + __stdoutbuf(const __stdoutbuf&); + __stdoutbuf& operator=(const __stdoutbuf&); +}; + +template <class _CharT> +__stdoutbuf<_CharT>::__stdoutbuf(FILE* __fp) + : __file_(__fp), + __cv_(&use_facet<codecvt<char_type, char, state_type> >(this->getloc())), + __st_(), + __always_noconv_(__cv_->always_noconv()) +{ +} + +template <class _CharT> +typename __stdoutbuf<_CharT>::int_type +__stdoutbuf<_CharT>::overflow(int_type __c) +{ + char __extbuf[__limit]; + char_type __1buf; + if (!traits_type::eq_int_type(__c, traits_type::eof())) + { + this->setp(&__1buf, &__1buf+1); + *this->pptr() = traits_type::to_char_type(__c); + this->pbump(1); + if (__always_noconv_) + { + if (fwrite(this->pbase(), sizeof(char_type), 1, __file_) != 1) + return traits_type::eof(); + } + else + { + char* __extbe = __extbuf; + codecvt_base::result __r; + do + { + const char_type* __e; + __r = __cv_->out(__st_, this->pbase(), this->pptr(), __e, + __extbuf, + __extbuf + sizeof(__extbuf), + __extbe); + if (__e == this->pbase()) + return traits_type::eof(); + if (__r == codecvt_base::noconv) + { + if (fwrite(this->pbase(), 1, 1, __file_) != 1) + return traits_type::eof(); + } + else if (__r == codecvt_base::ok || __r == codecvt_base::partial) + { + size_t __nmemb = static_cast<size_t>(__extbe - __extbuf); + if (fwrite(__extbuf, 1, __nmemb, __file_) != __nmemb) + return traits_type::eof(); + if (__r == codecvt_base::partial) + { + this->setp((char_type*)__e, this->pptr()); + this->pbump(this->epptr() - this->pbase()); + } + } + else + return traits_type::eof(); + } while (__r == codecvt_base::partial); + } + this->setp(0, 0); + } + return traits_type::not_eof(__c); +} + +template <class _CharT> +int +__stdoutbuf<_CharT>::sync() +{ + char __extbuf[__limit]; + codecvt_base::result __r; + do + { + char* __extbe; + __r = __cv_->unshift(__st_, __extbuf, + __extbuf + sizeof(__extbuf), + __extbe); + size_t __nmemb = static_cast<size_t>(__extbe - __extbuf); + if (fwrite(__extbuf, 1, __nmemb, __file_) != __nmemb) + return -1; + } while (__r == codecvt_base::partial); + if (__r == codecvt_base::error) + return -1; + if (fflush(__file_)) + return -1; + return 0; +} + +template <class _CharT> +void +__stdoutbuf<_CharT>::imbue(const locale& __loc) +{ + sync(); + __cv_ = &use_facet<codecvt<char_type, char, state_type> >(__loc); + __always_noconv_ = __cv_->always_noconv(); +} + +_LIBCPP_END_NAMESPACE_STD + +#endif // _LIBCPP___STD_STREAM diff --git a/include/__tree b/include/__tree new file mode 100644 index 000000000000..ad5d2f4b8eaa --- /dev/null +++ b/include/__tree @@ -0,0 +1,2293 @@ +// -*- C++ -*- +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#ifndef _LIBCPP___TREE +#define _LIBCPP___TREE + +#include <__config> +#include <iterator> +#include <memory> +#include <stdexcept> +#include <algorithm> + +#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) +#pragma GCC system_header +#endif + +_LIBCPP_BEGIN_NAMESPACE_STD + +template <class _Tp, class _Compare, class _Allocator> class __tree; +template <class _Tp, class _NodePtr, class _DiffType> + class _LIBCPP_VISIBLE __tree_iterator; +template <class _Tp, class _ConstNodePtr, class _DiffType> + class _LIBCPP_VISIBLE __tree_const_iterator; +template <class _Key, class _Tp, class _Compare, class _Allocator> + class _LIBCPP_VISIBLE map; +template <class _Key, class _Tp, class _Compare, class _Allocator> + class _LIBCPP_VISIBLE multimap; +template <class _Key, class _Compare, class _Allocator> + class _LIBCPP_VISIBLE set; +template <class _Key, class _Compare, class _Allocator> + class _LIBCPP_VISIBLE multiset; + +/* + +_NodePtr algorithms + +The algorithms taking _NodePtr are red black tree algorithms. Those +algorithms taking a parameter named __root should assume that __root +points to a proper red black tree (unless otherwise specified). + +Each algorithm herein assumes that __root->__parent_ points to a non-null +structure which has a member __left_ which points back to __root. No other +member is read or written to at __root->__parent_. + +__root->__parent_ will be referred to below (in comments only) as end_node. +end_node->__left_ is an externably accessible lvalue for __root, and can be +changed by node insertion and removal (without explicit reference to end_node). + +All nodes (with the exception of end_node), even the node referred to as +__root, have a non-null __parent_ field. + +*/ + +// Returns: true if __x is a left child of its parent, else false +// Precondition: __x != nullptr. +template <class _NodePtr> +inline _LIBCPP_INLINE_VISIBILITY +bool +__tree_is_left_child(_NodePtr __x) _NOEXCEPT +{ + return __x == __x->__parent_->__left_; +} + +// Determintes if the subtree rooted at __x is a proper red black subtree. If +// __x is a proper subtree, returns the black height (null counts as 1). If +// __x is an improper subtree, returns 0. +template <class _NodePtr> +unsigned +__tree_sub_invariant(_NodePtr __x) +{ + if (__x == nullptr) + return 1; + // parent consistency checked by caller + // check __x->__left_ consistency + if (__x->__left_ != nullptr && __x->__left_->__parent_ != __x) + return 0; + // check __x->__right_ consistency + if (__x->__right_ != nullptr && __x->__right_->__parent_ != __x) + return 0; + // check __x->__left_ != __x->__right_ unless both are nullptr + if (__x->__left_ == __x->__right_ && __x->__left_ != nullptr) + return 0; + // If this is red, neither child can be red + if (!__x->__is_black_) + { + if (__x->__left_ && !__x->__left_->__is_black_) + return 0; + if (__x->__right_ && !__x->__right_->__is_black_) + return 0; + } + unsigned __h = __tree_sub_invariant(__x->__left_); + if (__h == 0) + return 0; // invalid left subtree + if (__h != __tree_sub_invariant(__x->__right_)) + return 0; // invalid or different height right subtree + return __h + __x->__is_black_; // return black height of this node +} + +// Determintes if the red black tree rooted at __root is a proper red black tree. +// __root == nullptr is a proper tree. Returns true is __root is a proper +// red black tree, else returns false. +template <class _NodePtr> +bool +__tree_invariant(_NodePtr __root) +{ + if (__root == nullptr) + return true; + // check __x->__parent_ consistency + if (__root->__parent_ == nullptr) + return false; + if (!__tree_is_left_child(__root)) + return false; + // root must be black + if (!__root->__is_black_) + return false; + // do normal node checks + return __tree_sub_invariant(__root) != 0; +} + +// Returns: pointer to the left-most node under __x. +// Precondition: __x != nullptr. +template <class _NodePtr> +inline _LIBCPP_INLINE_VISIBILITY +_NodePtr +__tree_min(_NodePtr __x) _NOEXCEPT +{ + while (__x->__left_ != nullptr) + __x = __x->__left_; + return __x; +} + +// Returns: pointer to the right-most node under __x. +// Precondition: __x != nullptr. +template <class _NodePtr> +inline _LIBCPP_INLINE_VISIBILITY +_NodePtr +__tree_max(_NodePtr __x) _NOEXCEPT +{ + while (__x->__right_ != nullptr) + __x = __x->__right_; + return __x; +} + +// Returns: pointer to the next in-order node after __x. +// Precondition: __x != nullptr. +template <class _NodePtr> +_NodePtr +__tree_next(_NodePtr __x) _NOEXCEPT +{ + if (__x->__right_ != nullptr) + return __tree_min(__x->__right_); + while (!__tree_is_left_child(__x)) + __x = __x->__parent_; + return __x->__parent_; +} + +// Returns: pointer to the previous in-order node before __x. +// Precondition: __x != nullptr. +template <class _NodePtr> +_NodePtr +__tree_prev(_NodePtr __x) _NOEXCEPT +{ + if (__x->__left_ != nullptr) + return __tree_max(__x->__left_); + while (__tree_is_left_child(__x)) + __x = __x->__parent_; + return __x->__parent_; +} + +// Returns: pointer to a node which has no children +// Precondition: __x != nullptr. +template <class _NodePtr> +_NodePtr +__tree_leaf(_NodePtr __x) _NOEXCEPT +{ + while (true) + { + if (__x->__left_ != nullptr) + { + __x = __x->__left_; + continue; + } + if (__x->__right_ != nullptr) + { + __x = __x->__right_; + continue; + } + break; + } + return __x; +} + +// Effects: Makes __x->__right_ the subtree root with __x as its left child +// while preserving in-order order. +// Precondition: __x->__right_ != nullptr +template <class _NodePtr> +void +__tree_left_rotate(_NodePtr __x) _NOEXCEPT +{ + _NodePtr __y = __x->__right_; + __x->__right_ = __y->__left_; + if (__x->__right_ != nullptr) + __x->__right_->__parent_ = __x; + __y->__parent_ = __x->__parent_; + if (__tree_is_left_child(__x)) + __x->__parent_->__left_ = __y; + else + __x->__parent_->__right_ = __y; + __y->__left_ = __x; + __x->__parent_ = __y; +} + +// Effects: Makes __x->__left_ the subtree root with __x as its right child +// while preserving in-order order. +// Precondition: __x->__left_ != nullptr +template <class _NodePtr> +void +__tree_right_rotate(_NodePtr __x) _NOEXCEPT +{ + _NodePtr __y = __x->__left_; + __x->__left_ = __y->__right_; + if (__x->__left_ != nullptr) + __x->__left_->__parent_ = __x; + __y->__parent_ = __x->__parent_; + if (__tree_is_left_child(__x)) + __x->__parent_->__left_ = __y; + else + __x->__parent_->__right_ = __y; + __y->__right_ = __x; + __x->__parent_ = __y; +} + +// Effects: Rebalances __root after attaching __x to a leaf. +// Precondition: __root != nulptr && __x != nullptr. +// __x has no children. +// __x == __root or == a direct or indirect child of __root. +// If __x were to be unlinked from __root (setting __root to +// nullptr if __root == __x), __tree_invariant(__root) == true. +// Postcondition: __tree_invariant(end_node->__left_) == true. end_node->__left_ +// may be different than the value passed in as __root. +template <class _NodePtr> +void +__tree_balance_after_insert(_NodePtr __root, _NodePtr __x) _NOEXCEPT +{ + __x->__is_black_ = __x == __root; + while (__x != __root && !__x->__parent_->__is_black_) + { + // __x->__parent_ != __root because __x->__parent_->__is_black == false + if (__tree_is_left_child(__x->__parent_)) + { + _NodePtr __y = __x->__parent_->__parent_->__right_; + if (__y != nullptr && !__y->__is_black_) + { + __x = __x->__parent_; + __x->__is_black_ = true; + __x = __x->__parent_; + __x->__is_black_ = __x == __root; + __y->__is_black_ = true; + } + else + { + if (!__tree_is_left_child(__x)) + { + __x = __x->__parent_; + __tree_left_rotate(__x); + } + __x = __x->__parent_; + __x->__is_black_ = true; + __x = __x->__parent_; + __x->__is_black_ = false; + __tree_right_rotate(__x); + break; + } + } + else + { + _NodePtr __y = __x->__parent_->__parent_->__left_; + if (__y != nullptr && !__y->__is_black_) + { + __x = __x->__parent_; + __x->__is_black_ = true; + __x = __x->__parent_; + __x->__is_black_ = __x == __root; + __y->__is_black_ = true; + } + else + { + if (__tree_is_left_child(__x)) + { + __x = __x->__parent_; + __tree_right_rotate(__x); + } + __x = __x->__parent_; + __x->__is_black_ = true; + __x = __x->__parent_; + __x->__is_black_ = false; + __tree_left_rotate(__x); + break; + } + } + } +} + +// Precondition: __root != nullptr && __z != nullptr. +// __tree_invariant(__root) == true. +// __z == __root or == a direct or indirect child of __root. +// Effects: unlinks __z from the tree rooted at __root, rebalancing as needed. +// Postcondition: __tree_invariant(end_node->__left_) == true && end_node->__left_ +// nor any of its children refer to __z. end_node->__left_ +// may be different than the value passed in as __root. +template <class _NodePtr> +void +__tree_remove(_NodePtr __root, _NodePtr __z) _NOEXCEPT +{ + // __z will be removed from the tree. Client still needs to destruct/deallocate it + // __y is either __z, or if __z has two children, __tree_next(__z). + // __y will have at most one child. + // __y will be the initial hole in the tree (make the hole at a leaf) + _NodePtr __y = (__z->__left_ == nullptr || __z->__right_ == nullptr) ? + __z : __tree_next(__z); + // __x is __y's possibly null single child + _NodePtr __x = __y->__left_ != nullptr ? __y->__left_ : __y->__right_; + // __w is __x's possibly null uncle (will become __x's sibling) + _NodePtr __w = nullptr; + // link __x to __y's parent, and find __w + if (__x != nullptr) + __x->__parent_ = __y->__parent_; + if (__tree_is_left_child(__y)) + { + __y->__parent_->__left_ = __x; + if (__y != __root) + __w = __y->__parent_->__right_; + else + __root = __x; // __w == nullptr + } + else + { + __y->__parent_->__right_ = __x; + // __y can't be root if it is a right child + __w = __y->__parent_->__left_; + } + bool __removed_black = __y->__is_black_; + // If we didn't remove __z, do so now by splicing in __y for __z, + // but copy __z's color. This does not impact __x or __w. + if (__y != __z) + { + // __z->__left_ != nulptr but __z->__right_ might == __x == nullptr + __y->__parent_ = __z->__parent_; + if (__tree_is_left_child(__z)) + __y->__parent_->__left_ = __y; + else + __y->__parent_->__right_ = __y; + __y->__left_ = __z->__left_; + __y->__left_->__parent_ = __y; + __y->__right_ = __z->__right_; + if (__y->__right_ != nullptr) + __y->__right_->__parent_ = __y; + __y->__is_black_ = __z->__is_black_; + if (__root == __z) + __root = __y; + } + // There is no need to rebalance if we removed a red, or if we removed + // the last node. + if (__removed_black && __root != nullptr) + { + // Rebalance: + // __x has an implicit black color (transferred from the removed __y) + // associated with it, no matter what its color is. + // If __x is __root (in which case it can't be null), it is supposed + // to be black anyway, and if it is doubly black, then the double + // can just be ignored. + // If __x is red (in which case it can't be null), then it can absorb + // the implicit black just by setting its color to black. + // Since __y was black and only had one child (which __x points to), __x + // is either red with no children, else null, otherwise __y would have + // different black heights under left and right pointers. + // if (__x == __root || __x != nullptr && !__x->__is_black_) + if (__x != nullptr) + __x->__is_black_ = true; + else + { + // Else __x isn't root, and is "doubly black", even though it may + // be null. __w can not be null here, else the parent would + // see a black height >= 2 on the __x side and a black height + // of 1 on the __w side (__w must be a non-null black or a red + // with a non-null black child). + while (true) + { + if (!__tree_is_left_child(__w)) // if x is left child + { + if (!__w->__is_black_) + { + __w->__is_black_ = true; + __w->__parent_->__is_black_ = false; + __tree_left_rotate(__w->__parent_); + // __x is still valid + // reset __root only if necessary + if (__root == __w->__left_) + __root = __w; + // reset sibling, and it still can't be null + __w = __w->__left_->__right_; + } + // __w->__is_black_ is now true, __w may have null children + if ((__w->__left_ == nullptr || __w->__left_->__is_black_) && + (__w->__right_ == nullptr || __w->__right_->__is_black_)) + { + __w->__is_black_ = false; + __x = __w->__parent_; + // __x can no longer be null + if (__x == __root || !__x->__is_black_) + { + __x->__is_black_ = true; + break; + } + // reset sibling, and it still can't be null + __w = __tree_is_left_child(__x) ? + __x->__parent_->__right_ : + __x->__parent_->__left_; + // continue; + } + else // __w has a red child + { + if (__w->__right_ == nullptr || __w->__right_->__is_black_) + { + // __w left child is non-null and red + __w->__left_->__is_black_ = true; + __w->__is_black_ = false; + __tree_right_rotate(__w); + // __w is known not to be root, so root hasn't changed + // reset sibling, and it still can't be null + __w = __w->__parent_; + } + // __w has a right red child, left child may be null + __w->__is_black_ = __w->__parent_->__is_black_; + __w->__parent_->__is_black_ = true; + __w->__right_->__is_black_ = true; + __tree_left_rotate(__w->__parent_); + break; + } + } + else + { + if (!__w->__is_black_) + { + __w->__is_black_ = true; + __w->__parent_->__is_black_ = false; + __tree_right_rotate(__w->__parent_); + // __x is still valid + // reset __root only if necessary + if (__root == __w->__right_) + __root = __w; + // reset sibling, and it still can't be null + __w = __w->__right_->__left_; + } + // __w->__is_black_ is now true, __w may have null children + if ((__w->__left_ == nullptr || __w->__left_->__is_black_) && + (__w->__right_ == nullptr || __w->__right_->__is_black_)) + { + __w->__is_black_ = false; + __x = __w->__parent_; + // __x can no longer be null + if (!__x->__is_black_ || __x == __root) + { + __x->__is_black_ = true; + break; + } + // reset sibling, and it still can't be null + __w = __tree_is_left_child(__x) ? + __x->__parent_->__right_ : + __x->__parent_->__left_; + // continue; + } + else // __w has a red child + { + if (__w->__left_ == nullptr || __w->__left_->__is_black_) + { + // __w right child is non-null and red + __w->__right_->__is_black_ = true; + __w->__is_black_ = false; + __tree_left_rotate(__w); + // __w is known not to be root, so root hasn't changed + // reset sibling, and it still can't be null + __w = __w->__parent_; + } + // __w has a left red child, right child may be null + __w->__is_black_ = __w->__parent_->__is_black_; + __w->__parent_->__is_black_ = true; + __w->__left_->__is_black_ = true; + __tree_right_rotate(__w->__parent_); + break; + } + } + } + } + } +} + +template <class _Allocator> class __map_node_destructor; + +template <class _Allocator> +class __tree_node_destructor +{ + typedef _Allocator allocator_type; + typedef allocator_traits<allocator_type> __alloc_traits; + typedef typename __alloc_traits::value_type::value_type value_type; +public: + typedef typename __alloc_traits::pointer pointer; +private: + + allocator_type& __na_; + + __tree_node_destructor& operator=(const __tree_node_destructor&); + +public: + bool __value_constructed; + + _LIBCPP_INLINE_VISIBILITY + explicit __tree_node_destructor(allocator_type& __na) _NOEXCEPT + : __na_(__na), + __value_constructed(false) + {} + + _LIBCPP_INLINE_VISIBILITY + void operator()(pointer __p) _NOEXCEPT + { + if (__value_constructed) + __alloc_traits::destroy(__na_, _VSTD::addressof(__p->__value_)); + if (__p) + __alloc_traits::deallocate(__na_, __p, 1); + } + + template <class> friend class __map_node_destructor; +}; + +// node + +template <class _Pointer> +class __tree_end_node +{ +public: + typedef _Pointer pointer; + pointer __left_; + + _LIBCPP_INLINE_VISIBILITY + __tree_end_node() _NOEXCEPT : __left_() {} +}; + +template <class _VoidPtr> +class __tree_node_base + : public __tree_end_node + < + typename pointer_traits<_VoidPtr>::template +#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES + rebind<__tree_node_base<_VoidPtr> > +#else + rebind<__tree_node_base<_VoidPtr> >::other +#endif + > +{ + __tree_node_base(const __tree_node_base&); + __tree_node_base& operator=(const __tree_node_base&); +public: + typedef typename pointer_traits<_VoidPtr>::template +#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES + rebind<__tree_node_base> +#else + rebind<__tree_node_base>::other +#endif + pointer; + typedef typename pointer_traits<_VoidPtr>::template +#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES + rebind<const __tree_node_base> +#else + rebind<const __tree_node_base>::other +#endif + const_pointer; + typedef __tree_end_node<pointer> base; + + pointer __right_; + pointer __parent_; + bool __is_black_; + + _LIBCPP_INLINE_VISIBILITY + __tree_node_base() _NOEXCEPT + : __right_(), __parent_(), __is_black_(false) {} +}; + +template <class _Tp, class _VoidPtr> +class __tree_node + : public __tree_node_base<_VoidPtr> +{ +public: + typedef __tree_node_base<_VoidPtr> base; + typedef _Tp value_type; + + value_type __value_; + +#if !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS) + template <class ..._Args> + _LIBCPP_INLINE_VISIBILITY + explicit __tree_node(_Args&& ...__args) + : __value_(_VSTD::forward<_Args>(__args)...) {} +#else // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS) + _LIBCPP_INLINE_VISIBILITY + explicit __tree_node(const value_type& __v) + : __value_(__v) {} +#endif // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS) +}; + +template <class _TreeIterator> class __map_iterator; +template <class _TreeIterator> class __map_const_iterator; + +template <class _Tp, class _NodePtr, class _DiffType> +class _LIBCPP_VISIBLE __tree_iterator +{ + typedef _NodePtr __node_pointer; + typedef typename pointer_traits<__node_pointer>::element_type __node; + typedef typename __node::base __node_base; + typedef typename __node_base::pointer __node_base_pointer; + + __node_pointer __ptr_; + + typedef pointer_traits<__node_pointer> __pointer_traits; +public: + typedef bidirectional_iterator_tag iterator_category; + typedef _Tp value_type; + typedef _DiffType difference_type; + typedef value_type& reference; + typedef typename pointer_traits<__node_pointer>::template +#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES + rebind<value_type> +#else + rebind<value_type>::other +#endif + pointer; + + _LIBCPP_INLINE_VISIBILITY __tree_iterator() _NOEXCEPT {} + + _LIBCPP_INLINE_VISIBILITY reference operator*() const {return __ptr_->__value_;} + _LIBCPP_INLINE_VISIBILITY pointer operator->() const {return &__ptr_->__value_;} + + _LIBCPP_INLINE_VISIBILITY + __tree_iterator& operator++() + {__ptr_ = static_cast<__node_pointer>(__tree_next(static_cast<__node_base_pointer>(__ptr_))); + return *this;} + _LIBCPP_INLINE_VISIBILITY + __tree_iterator operator++(int) + {__tree_iterator __t(*this); ++(*this); return __t;} + + _LIBCPP_INLINE_VISIBILITY + __tree_iterator& operator--() + {__ptr_ = static_cast<__node_pointer>(__tree_prev(static_cast<__node_base_pointer>(__ptr_))); + return *this;} + _LIBCPP_INLINE_VISIBILITY + __tree_iterator operator--(int) + {__tree_iterator __t(*this); --(*this); return __t;} + + friend _LIBCPP_INLINE_VISIBILITY + bool operator==(const __tree_iterator& __x, const __tree_iterator& __y) + {return __x.__ptr_ == __y.__ptr_;} + friend _LIBCPP_INLINE_VISIBILITY + bool operator!=(const __tree_iterator& __x, const __tree_iterator& __y) + {return !(__x == __y);} + +private: + _LIBCPP_INLINE_VISIBILITY + explicit __tree_iterator(__node_pointer __p) _NOEXCEPT : __ptr_(__p) {} + template <class, class, class> friend class __tree; + template <class, class, class> friend class _LIBCPP_VISIBLE __tree_const_iterator; + template <class> friend class _LIBCPP_VISIBLE __map_iterator; + template <class, class, class, class> friend class _LIBCPP_VISIBLE map; + template <class, class, class, class> friend class _LIBCPP_VISIBLE multimap; + template <class, class, class> friend class _LIBCPP_VISIBLE set; + template <class, class, class> friend class _LIBCPP_VISIBLE multiset; +}; + +template <class _Tp, class _ConstNodePtr, class _DiffType> +class _LIBCPP_VISIBLE __tree_const_iterator +{ + typedef _ConstNodePtr __node_pointer; + typedef typename pointer_traits<__node_pointer>::element_type __node; + typedef const typename __node::base __node_base; + typedef typename pointer_traits<__node_pointer>::template +#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES + rebind<__node_base> +#else + rebind<__node_base>::other +#endif + __node_base_pointer; + + __node_pointer __ptr_; + + typedef pointer_traits<__node_pointer> __pointer_traits; +public: + typedef bidirectional_iterator_tag iterator_category; + typedef _Tp value_type; + typedef _DiffType difference_type; + typedef const value_type& reference; + typedef typename pointer_traits<__node_pointer>::template +#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES + rebind<const value_type> +#else + rebind<const value_type>::other +#endif + pointer; + + _LIBCPP_INLINE_VISIBILITY __tree_const_iterator() {} +private: + typedef typename remove_const<__node>::type __non_const_node; + typedef typename pointer_traits<__node_pointer>::template +#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES + rebind<__non_const_node> +#else + rebind<__non_const_node>::other +#endif + __non_const_node_pointer; + typedef __tree_iterator<value_type, __non_const_node_pointer, difference_type> + __non_const_iterator; +public: + _LIBCPP_INLINE_VISIBILITY + __tree_const_iterator(__non_const_iterator __p) _NOEXCEPT + : __ptr_(__p.__ptr_) {} + + _LIBCPP_INLINE_VISIBILITY reference operator*() const {return __ptr_->__value_;} + _LIBCPP_INLINE_VISIBILITY pointer operator->() const {return &__ptr_->__value_;} + + _LIBCPP_INLINE_VISIBILITY + __tree_const_iterator& operator++() + {__ptr_ = static_cast<__node_pointer>(__tree_next(static_cast<__node_base_pointer>(__ptr_))); + return *this;} + _LIBCPP_INLINE_VISIBILITY + __tree_const_iterator operator++(int) + {__tree_const_iterator __t(*this); ++(*this); return __t;} + + _LIBCPP_INLINE_VISIBILITY + __tree_const_iterator& operator--() + {__ptr_ = static_cast<__node_pointer>(__tree_prev(static_cast<__node_base_pointer>(__ptr_))); + return *this;} + _LIBCPP_INLINE_VISIBILITY + __tree_const_iterator operator--(int) + {__tree_const_iterator __t(*this); --(*this); return __t;} + + friend _LIBCPP_INLINE_VISIBILITY + bool operator==(const __tree_const_iterator& __x, const __tree_const_iterator& __y) + {return __x.__ptr_ == __y.__ptr_;} + friend _LIBCPP_INLINE_VISIBILITY + bool operator!=(const __tree_const_iterator& __x, const __tree_const_iterator& __y) + {return !(__x == __y);} + +private: + _LIBCPP_INLINE_VISIBILITY + explicit __tree_const_iterator(__node_pointer __p) _NOEXCEPT + : __ptr_(__p) {} + template <class, class, class> friend class __tree; + template <class, class, class, class> friend class _LIBCPP_VISIBLE map; + template <class, class, class, class> friend class _LIBCPP_VISIBLE multimap; + template <class, class, class> friend class _LIBCPP_VISIBLE set; + template <class, class, class> friend class _LIBCPP_VISIBLE multiset; + template <class> friend class _LIBCPP_VISIBLE __map_const_iterator; +}; + +template <class _Tp, class _Compare, class _Allocator> +class __tree +{ +public: + typedef _Tp value_type; + typedef _Compare value_compare; + typedef _Allocator allocator_type; + typedef allocator_traits<allocator_type> __alloc_traits; + typedef typename __alloc_traits::pointer pointer; + typedef typename __alloc_traits::const_pointer const_pointer; + typedef typename __alloc_traits::size_type size_type; + typedef typename __alloc_traits::difference_type difference_type; + + typedef __tree_node<value_type, typename __alloc_traits::void_pointer> __node; + typedef __tree_node_base<typename __alloc_traits::void_pointer> __node_base; + typedef typename __alloc_traits::template +#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES + rebind_alloc<__node> +#else + rebind_alloc<__node>::other +#endif + __node_allocator; + typedef allocator_traits<__node_allocator> __node_traits; + typedef typename __node_traits::pointer __node_pointer; + typedef typename __node_traits::const_pointer __node_const_pointer; + typedef typename __node_base::pointer __node_base_pointer; + typedef typename __node_base::const_pointer __node_base_const_pointer; +private: + typedef typename __node_base::base __end_node_t; + typedef typename pointer_traits<__node_pointer>::template +#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES + rebind<__end_node_t> +#else + rebind<__end_node_t>::other +#endif + __end_node_ptr; + typedef typename pointer_traits<__node_pointer>::template +#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES + rebind<const __end_node_t> +#else + rebind<const __end_node_t>::other +#endif + __end_node_const_ptr; + + __node_pointer __begin_node_; + __compressed_pair<__end_node_t, __node_allocator> __pair1_; + __compressed_pair<size_type, value_compare> __pair3_; + +public: + _LIBCPP_INLINE_VISIBILITY + __node_pointer __end_node() _NOEXCEPT + { + return static_cast<__node_pointer> + ( + pointer_traits<__end_node_ptr>::pointer_to(__pair1_.first()) + ); + } + _LIBCPP_INLINE_VISIBILITY + __node_const_pointer __end_node() const _NOEXCEPT + { + return static_cast<__node_const_pointer> + ( + pointer_traits<__end_node_const_ptr>::pointer_to(__pair1_.first()) + ); + } + _LIBCPP_INLINE_VISIBILITY + __node_allocator& __node_alloc() _NOEXCEPT {return __pair1_.second();} +private: + _LIBCPP_INLINE_VISIBILITY + const __node_allocator& __node_alloc() const _NOEXCEPT + {return __pair1_.second();} + _LIBCPP_INLINE_VISIBILITY + __node_pointer& __begin_node() _NOEXCEPT {return __begin_node_;} + _LIBCPP_INLINE_VISIBILITY + const __node_pointer& __begin_node() const _NOEXCEPT {return __begin_node_;} +public: + _LIBCPP_INLINE_VISIBILITY + allocator_type __alloc() const _NOEXCEPT + {return allocator_type(__node_alloc());} +private: + _LIBCPP_INLINE_VISIBILITY + size_type& size() _NOEXCEPT {return __pair3_.first();} +public: + _LIBCPP_INLINE_VISIBILITY + const size_type& size() const _NOEXCEPT {return __pair3_.first();} + _LIBCPP_INLINE_VISIBILITY + value_compare& value_comp() _NOEXCEPT {return __pair3_.second();} + _LIBCPP_INLINE_VISIBILITY + const value_compare& value_comp() const _NOEXCEPT + {return __pair3_.second();} +public: + _LIBCPP_INLINE_VISIBILITY + __node_pointer __root() _NOEXCEPT + {return static_cast<__node_pointer> (__end_node()->__left_);} + _LIBCPP_INLINE_VISIBILITY + __node_const_pointer __root() const _NOEXCEPT + {return static_cast<__node_const_pointer>(__end_node()->__left_);} + + typedef __tree_iterator<value_type, __node_pointer, difference_type> iterator; + typedef __tree_const_iterator<value_type, __node_const_pointer, difference_type> const_iterator; + + explicit __tree(const value_compare& __comp) + _NOEXCEPT_( + is_nothrow_default_constructible<__node_allocator>::value && + is_nothrow_copy_constructible<value_compare>::value); + explicit __tree(const allocator_type& __a); + __tree(const value_compare& __comp, const allocator_type& __a); + __tree(const __tree& __t); + __tree& operator=(const __tree& __t); + template <class _InputIterator> + void __assign_unique(_InputIterator __first, _InputIterator __last); + template <class _InputIterator> + void __assign_multi(_InputIterator __first, _InputIterator __last); +#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES + __tree(__tree&& __t) + _NOEXCEPT_( + is_nothrow_move_constructible<__node_allocator>::value && + is_nothrow_move_constructible<value_compare>::value); + __tree(__tree&& __t, const allocator_type& __a); + __tree& operator=(__tree&& __t) + _NOEXCEPT_( + __node_traits::propagate_on_container_move_assignment::value && + is_nothrow_move_assignable<value_compare>::value && + is_nothrow_move_assignable<__node_allocator>::value); +#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES + + ~__tree(); + + _LIBCPP_INLINE_VISIBILITY + iterator begin() _NOEXCEPT {return iterator(__begin_node());} + _LIBCPP_INLINE_VISIBILITY + const_iterator begin() const _NOEXCEPT {return const_iterator(__begin_node());} + _LIBCPP_INLINE_VISIBILITY + iterator end() _NOEXCEPT {return iterator(__end_node());} + _LIBCPP_INLINE_VISIBILITY + const_iterator end() const _NOEXCEPT {return const_iterator(__end_node());} + + _LIBCPP_INLINE_VISIBILITY + size_type max_size() const _NOEXCEPT + {return __node_traits::max_size(__node_alloc());} + + void clear() _NOEXCEPT; + + void swap(__tree& __t) + _NOEXCEPT_( + __is_nothrow_swappable<value_compare>::value && + (!__node_traits::propagate_on_container_swap::value || + __is_nothrow_swappable<__node_allocator>::value)); + +#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES +#ifndef _LIBCPP_HAS_NO_VARIADICS + template <class... _Args> + pair<iterator, bool> + __emplace_unique(_Args&&... __args); + template <class... _Args> + iterator + __emplace_multi(_Args&&... __args); + + template <class... _Args> + iterator + __emplace_hint_unique(const_iterator __p, _Args&&... __args); + template <class... _Args> + iterator + __emplace_hint_multi(const_iterator __p, _Args&&... __args); +#endif // _LIBCPP_HAS_NO_VARIADICS + + template <class _V> + pair<iterator, bool> __insert_unique(_V&& __v); + template <class _V> + iterator __insert_unique(const_iterator __p, _V&& __v); + template <class _V> + iterator __insert_multi(_V&& __v); + template <class _V> + iterator __insert_multi(const_iterator __p, _V&& __v); +#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES + + pair<iterator, bool> __insert_unique(const value_type& __v); + iterator __insert_unique(const_iterator __p, const value_type& __v); + iterator __insert_multi(const value_type& __v); + iterator __insert_multi(const_iterator __p, const value_type& __v); + + pair<iterator, bool> __node_insert_unique(__node_pointer __nd); + iterator __node_insert_unique(const_iterator __p, + __node_pointer __nd); + + iterator __node_insert_multi(__node_pointer __nd); + iterator __node_insert_multi(const_iterator __p, __node_pointer __nd); + + iterator erase(const_iterator __p); + iterator erase(const_iterator __f, const_iterator __l); + template <class _Key> + size_type __erase_unique(const _Key& __k); + template <class _Key> + size_type __erase_multi(const _Key& __k); + + void __insert_node_at(__node_base_pointer __parent, + __node_base_pointer& __child, + __node_base_pointer __new_node); + + template <class _Key> + iterator find(const _Key& __v); + template <class _Key> + const_iterator find(const _Key& __v) const; + + template <class _Key> + size_type __count_unique(const _Key& __k) const; + template <class _Key> + size_type __count_multi(const _Key& __k) const; + + template <class _Key> + _LIBCPP_INLINE_VISIBILITY + iterator lower_bound(const _Key& __v) + {return __lower_bound(__v, __root(), __end_node());} + template <class _Key> + iterator __lower_bound(const _Key& __v, + __node_pointer __root, + __node_pointer __result); + template <class _Key> + _LIBCPP_INLINE_VISIBILITY + const_iterator lower_bound(const _Key& __v) const + {return __lower_bound(__v, __root(), __end_node());} + template <class _Key> + const_iterator __lower_bound(const _Key& __v, + __node_const_pointer __root, + __node_const_pointer __result) const; + template <class _Key> + _LIBCPP_INLINE_VISIBILITY + iterator upper_bound(const _Key& __v) + {return __upper_bound(__v, __root(), __end_node());} + template <class _Key> + iterator __upper_bound(const _Key& __v, + __node_pointer __root, + __node_pointer __result); + template <class _Key> + _LIBCPP_INLINE_VISIBILITY + const_iterator upper_bound(const _Key& __v) const + {return __upper_bound(__v, __root(), __end_node());} + template <class _Key> + const_iterator __upper_bound(const _Key& __v, + __node_const_pointer __root, + __node_const_pointer __result) const; + template <class _Key> + pair<iterator, iterator> + __equal_range_unique(const _Key& __k); + template <class _Key> + pair<const_iterator, const_iterator> + __equal_range_unique(const _Key& __k) const; + + template <class _Key> + pair<iterator, iterator> + __equal_range_multi(const _Key& __k); + template <class _Key> + pair<const_iterator, const_iterator> + __equal_range_multi(const _Key& __k) const; + + typedef __tree_node_destructor<__node_allocator> _D; + typedef unique_ptr<__node, _D> __node_holder; + + __node_holder remove(const_iterator __p) _NOEXCEPT; +private: + typename __node_base::pointer& + __find_leaf_low(typename __node_base::pointer& __parent, const value_type& __v); + typename __node_base::pointer& + __find_leaf_high(typename __node_base::pointer& __parent, const value_type& __v); + typename __node_base::pointer& + __find_leaf(const_iterator __hint, + typename __node_base::pointer& __parent, const value_type& __v); + template <class _Key> + typename __node_base::pointer& + __find_equal(typename __node_base::pointer& __parent, const _Key& __v); + template <class _Key> + typename __node_base::pointer& + __find_equal(const_iterator __hint, typename __node_base::pointer& __parent, + const _Key& __v); + +#if !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS) + template <class ..._Args> + __node_holder __construct_node(_Args&& ...__args); +#else // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS) + __node_holder __construct_node(const value_type& __v); +#endif + + void destroy(__node_pointer __nd) _NOEXCEPT; + + _LIBCPP_INLINE_VISIBILITY + void __copy_assign_alloc(const __tree& __t) + {__copy_assign_alloc(__t, integral_constant<bool, + __node_traits::propagate_on_container_copy_assignment::value>());} + + _LIBCPP_INLINE_VISIBILITY + void __copy_assign_alloc(const __tree& __t, true_type) + {__node_alloc() = __t.__node_alloc();} + _LIBCPP_INLINE_VISIBILITY + void __copy_assign_alloc(const __tree& __t, false_type) {} + + void __move_assign(__tree& __t, false_type); + void __move_assign(__tree& __t, true_type) + _NOEXCEPT_(is_nothrow_move_assignable<value_compare>::value && + is_nothrow_move_assignable<__node_allocator>::value); + + _LIBCPP_INLINE_VISIBILITY + void __move_assign_alloc(__tree& __t) + _NOEXCEPT_( + !__node_traits::propagate_on_container_move_assignment::value || + is_nothrow_move_assignable<__node_allocator>::value) + {__move_assign_alloc(__t, integral_constant<bool, + __node_traits::propagate_on_container_move_assignment::value>());} + + _LIBCPP_INLINE_VISIBILITY + void __move_assign_alloc(__tree& __t, true_type) + _NOEXCEPT_(is_nothrow_move_assignable<__node_allocator>::value) + {__node_alloc() = _VSTD::move(__t.__node_alloc());} + _LIBCPP_INLINE_VISIBILITY + void __move_assign_alloc(__tree& __t, false_type) _NOEXCEPT {} + + _LIBCPP_INLINE_VISIBILITY + static void __swap_alloc(__node_allocator& __x, __node_allocator& __y) + _NOEXCEPT_( + !__node_traits::propagate_on_container_swap::value || + __is_nothrow_swappable<__node_allocator>::value) + {__swap_alloc(__x, __y, integral_constant<bool, + __node_traits::propagate_on_container_swap::value>());} + _LIBCPP_INLINE_VISIBILITY + static void __swap_alloc(__node_allocator& __x, __node_allocator& __y, true_type) + _NOEXCEPT_(__is_nothrow_swappable<__node_allocator>::value) + { + using _VSTD::swap; + swap(__x, __y); + } + _LIBCPP_INLINE_VISIBILITY + static void __swap_alloc(__node_allocator& __x, __node_allocator& __y, false_type) + _NOEXCEPT + {} + + __node_pointer __detach(); + static __node_pointer __detach(__node_pointer); +}; + +template <class _Tp, class _Compare, class _Allocator> +__tree<_Tp, _Compare, _Allocator>::__tree(const value_compare& __comp) + _NOEXCEPT_( + is_nothrow_default_constructible<__node_allocator>::value && + is_nothrow_copy_constructible<value_compare>::value) + : __pair3_(0, __comp) +{ + __begin_node() = __end_node(); +} + +template <class _Tp, class _Compare, class _Allocator> +__tree<_Tp, _Compare, _Allocator>::__tree(const allocator_type& __a) + : __pair1_(__node_allocator(__a)), + __begin_node_(__node_pointer()), + __pair3_(0) +{ + __begin_node() = __end_node(); +} + +template <class _Tp, class _Compare, class _Allocator> +__tree<_Tp, _Compare, _Allocator>::__tree(const value_compare& __comp, + const allocator_type& __a) + : __pair1_(__node_allocator(__a)), + __begin_node_(__node_pointer()), + __pair3_(0, __comp) +{ + __begin_node() = __end_node(); +} + +// Precondition: size() != 0 +template <class _Tp, class _Compare, class _Allocator> +typename __tree<_Tp, _Compare, _Allocator>::__node_pointer +__tree<_Tp, _Compare, _Allocator>::__detach() +{ + __node_pointer __cache = __begin_node(); + __begin_node() = __end_node(); + __end_node()->__left_->__parent_ = nullptr; + __end_node()->__left_ = nullptr; + size() = 0; + // __cache->__left_ == nullptr + if (__cache->__right_ != nullptr) + __cache = static_cast<__node_pointer>(__cache->__right_); + // __cache->__left_ == nullptr + // __cache->__right_ == nullptr + return __cache; +} + +// Precondition: __cache != nullptr +// __cache->left_ == nullptr +// __cache->right_ == nullptr +// This is no longer a red-black tree +template <class _Tp, class _Compare, class _Allocator> +typename __tree<_Tp, _Compare, _Allocator>::__node_pointer +__tree<_Tp, _Compare, _Allocator>::__detach(__node_pointer __cache) +{ + if (__cache->__parent_ == nullptr) + return nullptr; + if (__tree_is_left_child(__cache)) + { + __cache->__parent_->__left_ = nullptr; + __cache = static_cast<__node_pointer>(__cache->__parent_); + if (__cache->__right_ == nullptr) + return __cache; + return static_cast<__node_pointer>(__tree_leaf(__cache->__right_)); + } + // __cache is right child + __cache->__parent_->__right_ = nullptr; + __cache = static_cast<__node_pointer>(__cache->__parent_); + if (__cache->__left_ == nullptr) + return __cache; + return static_cast<__node_pointer>(__tree_leaf(__cache->__left_)); +} + +template <class _Tp, class _Compare, class _Allocator> +__tree<_Tp, _Compare, _Allocator>& +__tree<_Tp, _Compare, _Allocator>::operator=(const __tree& __t) +{ + if (this != &__t) + { + value_comp() = __t.value_comp(); + __copy_assign_alloc(__t); + __assign_multi(__t.begin(), __t.end()); + } + return *this; +} + +template <class _Tp, class _Compare, class _Allocator> +template <class _InputIterator> +void +__tree<_Tp, _Compare, _Allocator>::__assign_unique(_InputIterator __first, _InputIterator __last) +{ + if (size() != 0) + { + __node_pointer __cache = __detach(); +#ifndef _LIBCPP_NO_EXCEPTIONS + try + { +#endif // _LIBCPP_NO_EXCEPTIONS + for (; __cache != nullptr && __first != __last; ++__first) + { + __cache->__value_ = *__first; + __node_pointer __next = __detach(__cache); + __node_insert_unique(__cache); + __cache = __next; + } +#ifndef _LIBCPP_NO_EXCEPTIONS + } + catch (...) + { + while (__cache->__parent_ != nullptr) + __cache = static_cast<__node_pointer>(__cache->__parent_); + destroy(__cache); + throw; + } +#endif // _LIBCPP_NO_EXCEPTIONS + if (__cache != nullptr) + { + while (__cache->__parent_ != nullptr) + __cache = static_cast<__node_pointer>(__cache->__parent_); + destroy(__cache); + } + } + for (; __first != __last; ++__first) + __insert_unique(*__first); +} + +template <class _Tp, class _Compare, class _Allocator> +template <class _InputIterator> +void +__tree<_Tp, _Compare, _Allocator>::__assign_multi(_InputIterator __first, _InputIterator __last) +{ + if (size() != 0) + { + __node_pointer __cache = __detach(); +#ifndef _LIBCPP_NO_EXCEPTIONS + try + { +#endif // _LIBCPP_NO_EXCEPTIONS + for (; __cache != nullptr && __first != __last; ++__first) + { + __cache->__value_ = *__first; + __node_pointer __next = __detach(__cache); + __node_insert_multi(__cache); + __cache = __next; + } +#ifndef _LIBCPP_NO_EXCEPTIONS + } + catch (...) + { + while (__cache->__parent_ != nullptr) + __cache = static_cast<__node_pointer>(__cache->__parent_); + destroy(__cache); + throw; + } +#endif // _LIBCPP_NO_EXCEPTIONS + if (__cache != nullptr) + { + while (__cache->__parent_ != nullptr) + __cache = static_cast<__node_pointer>(__cache->__parent_); + destroy(__cache); + } + } + for (; __first != __last; ++__first) + __insert_multi(*__first); +} + +template <class _Tp, class _Compare, class _Allocator> +__tree<_Tp, _Compare, _Allocator>::__tree(const __tree& __t) + : __begin_node_(__node_pointer()), + __pair1_(__node_traits::select_on_container_copy_construction(__t.__node_alloc())), + __pair3_(0, __t.value_comp()) +{ + __begin_node() = __end_node(); +} + +#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES + +template <class _Tp, class _Compare, class _Allocator> +__tree<_Tp, _Compare, _Allocator>::__tree(__tree&& __t) + _NOEXCEPT_( + is_nothrow_move_constructible<__node_allocator>::value && + is_nothrow_move_constructible<value_compare>::value) + : __begin_node_(_VSTD::move(__t.__begin_node_)), + __pair1_(_VSTD::move(__t.__pair1_)), + __pair3_(_VSTD::move(__t.__pair3_)) +{ + if (size() == 0) + __begin_node() = __end_node(); + else + { + __end_node()->__left_->__parent_ = __end_node(); + __t.__begin_node() = __t.__end_node(); + __t.__end_node()->__left_ = nullptr; + __t.size() = 0; + } +} + +template <class _Tp, class _Compare, class _Allocator> +__tree<_Tp, _Compare, _Allocator>::__tree(__tree&& __t, const allocator_type& __a) + : __pair1_(__node_allocator(__a)), + __pair3_(0, _VSTD::move(__t.value_comp())) +{ + if (__a == __t.__alloc()) + { + if (__t.size() == 0) + __begin_node() = __end_node(); + else + { + __begin_node() = __t.__begin_node(); + __end_node()->__left_ = __t.__end_node()->__left_; + __end_node()->__left_->__parent_ = __end_node(); + size() = __t.size(); + __t.__begin_node() = __t.__end_node(); + __t.__end_node()->__left_ = nullptr; + __t.size() = 0; + } + } + else + { + __begin_node() = __end_node(); + } +} + +template <class _Tp, class _Compare, class _Allocator> +void +__tree<_Tp, _Compare, _Allocator>::__move_assign(__tree& __t, true_type) + _NOEXCEPT_(is_nothrow_move_assignable<value_compare>::value && + is_nothrow_move_assignable<__node_allocator>::value) +{ + destroy(static_cast<__node_pointer>(__end_node()->__left_)); + __begin_node_ = __t.__begin_node_; + __pair1_.first() = __t.__pair1_.first(); + __move_assign_alloc(__t); + __pair3_ = _VSTD::move(__t.__pair3_); + if (size() == 0) + __begin_node() = __end_node(); + else + { + __end_node()->__left_->__parent_ = __end_node(); + __t.__begin_node() = __t.__end_node(); + __t.__end_node()->__left_ = nullptr; + __t.size() = 0; + } +} + +template <class _Tp, class _Compare, class _Allocator> +void +__tree<_Tp, _Compare, _Allocator>::__move_assign(__tree& __t, false_type) +{ + if (__node_alloc() == __t.__node_alloc()) + __move_assign(__t, true_type()); + else + { + value_comp() = _VSTD::move(__t.value_comp()); + const_iterator __e = end(); + if (size() != 0) + { + __node_pointer __cache = __detach(); +#ifndef _LIBCPP_NO_EXCEPTIONS + try + { +#endif // _LIBCPP_NO_EXCEPTIONS + while (__cache != nullptr && __t.size() != 0) + { + __cache->__value_ = _VSTD::move(__t.remove(__t.begin())->__value_); + __node_pointer __next = __detach(__cache); + __node_insert_multi(__cache); + __cache = __next; + } +#ifndef _LIBCPP_NO_EXCEPTIONS + } + catch (...) + { + while (__cache->__parent_ != nullptr) + __cache = static_cast<__node_pointer>(__cache->__parent_); + destroy(__cache); + throw; + } +#endif // _LIBCPP_NO_EXCEPTIONS + if (__cache != nullptr) + { + while (__cache->__parent_ != nullptr) + __cache = static_cast<__node_pointer>(__cache->__parent_); + destroy(__cache); + } + } + while (__t.size() != 0) + __insert_multi(__e, _VSTD::move(__t.remove(__t.begin())->__value_)); + } +} + +template <class _Tp, class _Compare, class _Allocator> +__tree<_Tp, _Compare, _Allocator>& +__tree<_Tp, _Compare, _Allocator>::operator=(__tree&& __t) + _NOEXCEPT_( + __node_traits::propagate_on_container_move_assignment::value && + is_nothrow_move_assignable<value_compare>::value && + is_nothrow_move_assignable<__node_allocator>::value) + +{ + __move_assign(__t, integral_constant<bool, + __node_traits::propagate_on_container_move_assignment::value>()); + return *this; +} + +#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES + +template <class _Tp, class _Compare, class _Allocator> +__tree<_Tp, _Compare, _Allocator>::~__tree() +{ + destroy(__root()); +} + +template <class _Tp, class _Compare, class _Allocator> +void +__tree<_Tp, _Compare, _Allocator>::destroy(__node_pointer __nd) _NOEXCEPT +{ + if (__nd != nullptr) + { + destroy(static_cast<__node_pointer>(__nd->__left_)); + destroy(static_cast<__node_pointer>(__nd->__right_)); + __node_allocator& __na = __node_alloc(); + __node_traits::destroy(__na, _VSTD::addressof(__nd->__value_)); + __node_traits::deallocate(__na, __nd, 1); + } +} + +template <class _Tp, class _Compare, class _Allocator> +void +__tree<_Tp, _Compare, _Allocator>::swap(__tree& __t) + _NOEXCEPT_( + __is_nothrow_swappable<value_compare>::value && + (!__node_traits::propagate_on_container_swap::value || + __is_nothrow_swappable<__node_allocator>::value)) +{ + using _VSTD::swap; + swap(__begin_node_, __t.__begin_node_); + swap(__pair1_.first(), __t.__pair1_.first()); + __swap_alloc(__node_alloc(), __t.__node_alloc()); + __pair3_.swap(__t.__pair3_); + if (size() == 0) + __begin_node() = __end_node(); + else + __end_node()->__left_->__parent_ = __end_node(); + if (__t.size() == 0) + __t.__begin_node() = __t.__end_node(); + else + __t.__end_node()->__left_->__parent_ = __t.__end_node(); +} + +template <class _Tp, class _Compare, class _Allocator> +void +__tree<_Tp, _Compare, _Allocator>::clear() _NOEXCEPT +{ + destroy(__root()); + size() = 0; + __begin_node() = __end_node(); + __end_node()->__left_ = nullptr; +} + +// Find lower_bound place to insert +// Set __parent to parent of null leaf +// Return reference to null leaf +template <class _Tp, class _Compare, class _Allocator> +typename __tree<_Tp, _Compare, _Allocator>::__node_base::pointer& +__tree<_Tp, _Compare, _Allocator>::__find_leaf_low(typename __node_base::pointer& __parent, + const value_type& __v) +{ + __node_pointer __nd = __root(); + if (__nd != nullptr) + { + while (true) + { + if (value_comp()(__nd->__value_, __v)) + { + if (__nd->__right_ != nullptr) + __nd = static_cast<__node_pointer>(__nd->__right_); + else + { + __parent = __nd; + return __parent->__right_; + } + } + else + { + if (__nd->__left_ != nullptr) + __nd = static_cast<__node_pointer>(__nd->__left_); + else + { + __parent = __nd; + return __parent->__left_; + } + } + } + } + __parent = __end_node(); + return __parent->__left_; +} + +// Find upper_bound place to insert +// Set __parent to parent of null leaf +// Return reference to null leaf +template <class _Tp, class _Compare, class _Allocator> +typename __tree<_Tp, _Compare, _Allocator>::__node_base::pointer& +__tree<_Tp, _Compare, _Allocator>::__find_leaf_high(typename __node_base::pointer& __parent, + const value_type& __v) +{ + __node_pointer __nd = __root(); + if (__nd != nullptr) + { + while (true) + { + if (value_comp()(__v, __nd->__value_)) + { + if (__nd->__left_ != nullptr) + __nd = static_cast<__node_pointer>(__nd->__left_); + else + { + __parent = __nd; + return __parent->__left_; + } + } + else + { + if (__nd->__right_ != nullptr) + __nd = static_cast<__node_pointer>(__nd->__right_); + else + { + __parent = __nd; + return __parent->__right_; + } + } + } + } + __parent = __end_node(); + return __parent->__left_; +} + +// Find leaf place to insert closest to __hint +// First check prior to __hint. +// Next check after __hint. +// Next do O(log N) search. +// Set __parent to parent of null leaf +// Return reference to null leaf +template <class _Tp, class _Compare, class _Allocator> +typename __tree<_Tp, _Compare, _Allocator>::__node_base::pointer& +__tree<_Tp, _Compare, _Allocator>::__find_leaf(const_iterator __hint, + typename __node_base::pointer& __parent, + const value_type& __v) +{ + if (__hint == end() || !value_comp()(*__hint, __v)) // check before + { + // __v <= *__hint + const_iterator __prior = __hint; + if (__prior == begin() || !value_comp()(__v, *--__prior)) + { + // *prev(__hint) <= __v <= *__hint + if (__hint.__ptr_->__left_ == nullptr) + { + __parent = const_cast<__node_pointer&>(__hint.__ptr_); + return __parent->__left_; + } + else + { + __parent = const_cast<__node_pointer&>(__prior.__ptr_); + return __parent->__right_; + } + } + // __v < *prev(__hint) + return __find_leaf_high(__parent, __v); + } + // else __v > *__hint + return __find_leaf_low(__parent, __v); +} + +// Find place to insert if __v doesn't exist +// Set __parent to parent of null leaf +// Return reference to null leaf +// If __v exists, set parent to node of __v and return reference to node of __v +template <class _Tp, class _Compare, class _Allocator> +template <class _Key> +typename __tree<_Tp, _Compare, _Allocator>::__node_base::pointer& +__tree<_Tp, _Compare, _Allocator>::__find_equal(typename __node_base::pointer& __parent, + const _Key& __v) +{ + __node_pointer __nd = __root(); + if (__nd != nullptr) + { + while (true) + { + if (value_comp()(__v, __nd->__value_)) + { + if (__nd->__left_ != nullptr) + __nd = static_cast<__node_pointer>(__nd->__left_); + else + { + __parent = __nd; + return __parent->__left_; + } + } + else if (value_comp()(__nd->__value_, __v)) + { + if (__nd->__right_ != nullptr) + __nd = static_cast<__node_pointer>(__nd->__right_); + else + { + __parent = __nd; + return __parent->__right_; + } + } + else + { + __parent = __nd; + return __parent; + } + } + } + __parent = __end_node(); + return __parent->__left_; +} + +// Find place to insert if __v doesn't exist +// First check prior to __hint. +// Next check after __hint. +// Next do O(log N) search. +// Set __parent to parent of null leaf +// Return reference to null leaf +// If __v exists, set parent to node of __v and return reference to node of __v +template <class _Tp, class _Compare, class _Allocator> +template <class _Key> +typename __tree<_Tp, _Compare, _Allocator>::__node_base::pointer& +__tree<_Tp, _Compare, _Allocator>::__find_equal(const_iterator __hint, + typename __node_base::pointer& __parent, + const _Key& __v) +{ + if (__hint == end() || value_comp()(__v, *__hint)) // check before + { + // __v < *__hint + const_iterator __prior = __hint; + if (__prior == begin() || value_comp()(*--__prior, __v)) + { + // *prev(__hint) < __v < *__hint + if (__hint.__ptr_->__left_ == nullptr) + { + __parent = const_cast<__node_pointer&>(__hint.__ptr_); + return __parent->__left_; + } + else + { + __parent = const_cast<__node_pointer&>(__prior.__ptr_); + return __parent->__right_; + } + } + // __v <= *prev(__hint) + return __find_equal(__parent, __v); + } + else if (value_comp()(*__hint, __v)) // check after + { + // *__hint < __v + const_iterator __next = _VSTD::next(__hint); + if (__next == end() || value_comp()(__v, *__next)) + { + // *__hint < __v < *_VSTD::next(__hint) + if (__hint.__ptr_->__right_ == nullptr) + { + __parent = const_cast<__node_pointer&>(__hint.__ptr_); + return __parent->__right_; + } + else + { + __parent = const_cast<__node_pointer&>(__next.__ptr_); + return __parent->__left_; + } + } + // *next(__hint) <= __v + return __find_equal(__parent, __v); + } + // else __v == *__hint + __parent = const_cast<__node_pointer&>(__hint.__ptr_); + return __parent; +} + +template <class _Tp, class _Compare, class _Allocator> +void +__tree<_Tp, _Compare, _Allocator>::__insert_node_at(__node_base_pointer __parent, + __node_base_pointer& __child, + __node_base_pointer __new_node) +{ + __new_node->__left_ = nullptr; + __new_node->__right_ = nullptr; + __new_node->__parent_ = __parent; + __child = __new_node; + if (__begin_node()->__left_ != nullptr) + __begin_node() = static_cast<__node_pointer>(__begin_node()->__left_); + __tree_balance_after_insert(__end_node()->__left_, __child); + ++size(); +} + +#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES +#ifndef _LIBCPP_HAS_NO_VARIADICS + +template <class _Tp, class _Compare, class _Allocator> +template <class ..._Args> +typename __tree<_Tp, _Compare, _Allocator>::__node_holder +__tree<_Tp, _Compare, _Allocator>::__construct_node(_Args&& ...__args) +{ + __node_allocator& __na = __node_alloc(); + __node_holder __h(__node_traits::allocate(__na, 1), _D(__na)); + __node_traits::construct(__na, _VSTD::addressof(__h->__value_), _VSTD::forward<_Args>(__args)...); + __h.get_deleter().__value_constructed = true; + return __h; +} + +template <class _Tp, class _Compare, class _Allocator> +template <class... _Args> +pair<typename __tree<_Tp, _Compare, _Allocator>::iterator, bool> +__tree<_Tp, _Compare, _Allocator>::__emplace_unique(_Args&&... __args) +{ + __node_holder __h = __construct_node(_VSTD::forward<_Args>(__args)...); + __node_base_pointer __parent; + __node_base_pointer& __child = __find_equal(__parent, __h->__value_); + __node_pointer __r = static_cast<__node_pointer>(__child); + bool __inserted = false; + if (__child == nullptr) + { + __insert_node_at(__parent, __child, __h.get()); + __r = __h.release(); + __inserted = true; + } + return pair<iterator, bool>(iterator(__r), __inserted); +} + +template <class _Tp, class _Compare, class _Allocator> +template <class... _Args> +typename __tree<_Tp, _Compare, _Allocator>::iterator +__tree<_Tp, _Compare, _Allocator>::__emplace_hint_unique(const_iterator __p, _Args&&... __args) +{ + __node_holder __h = __construct_node(_VSTD::forward<_Args>(__args)...); + __node_base_pointer __parent; + __node_base_pointer& __child = __find_equal(__p, __parent, __h->__value_); + __node_pointer __r = static_cast<__node_pointer>(__child); + if (__child == nullptr) + { + __insert_node_at(__parent, __child, __h.get()); + __r = __h.release(); + } + return iterator(__r); +} + +template <class _Tp, class _Compare, class _Allocator> +template <class... _Args> +typename __tree<_Tp, _Compare, _Allocator>::iterator +__tree<_Tp, _Compare, _Allocator>::__emplace_multi(_Args&&... __args) +{ + __node_holder __h = __construct_node(_VSTD::forward<_Args>(__args)...); + __node_base_pointer __parent; + __node_base_pointer& __child = __find_leaf_high(__parent, __h->__value_); + __insert_node_at(__parent, __child, __h.get()); + return iterator(static_cast<__node_pointer>(__h.release())); +} + +template <class _Tp, class _Compare, class _Allocator> +template <class... _Args> +typename __tree<_Tp, _Compare, _Allocator>::iterator +__tree<_Tp, _Compare, _Allocator>::__emplace_hint_multi(const_iterator __p, + _Args&&... __args) +{ + __node_holder __h = __construct_node(_VSTD::forward<_Args>(__args)...); + __node_base_pointer __parent; + __node_base_pointer& __child = __find_leaf(__p, __parent, __h->__value_); + __insert_node_at(__parent, __child, __h.get()); + return iterator(static_cast<__node_pointer>(__h.release())); +} + +#endif // _LIBCPP_HAS_NO_VARIADICS + +template <class _Tp, class _Compare, class _Allocator> +template <class _V> +pair<typename __tree<_Tp, _Compare, _Allocator>::iterator, bool> +__tree<_Tp, _Compare, _Allocator>::__insert_unique(_V&& __v) +{ + __node_holder __h = __construct_node(_VSTD::forward<_V>(__v)); + pair<iterator, bool> __r = __node_insert_unique(__h.get()); + if (__r.second) + __h.release(); + return __r; +} + +template <class _Tp, class _Compare, class _Allocator> +template <class _V> +typename __tree<_Tp, _Compare, _Allocator>::iterator +__tree<_Tp, _Compare, _Allocator>::__insert_unique(const_iterator __p, _V&& __v) +{ + __node_holder __h = __construct_node(_VSTD::forward<_V>(__v)); + iterator __r = __node_insert_unique(__p, __h.get()); + if (__r.__ptr_ == __h.get()) + __h.release(); + return __r; +} + +template <class _Tp, class _Compare, class _Allocator> +template <class _V> +typename __tree<_Tp, _Compare, _Allocator>::iterator +__tree<_Tp, _Compare, _Allocator>::__insert_multi(_V&& __v) +{ + __node_holder __h = __construct_node(_VSTD::forward<_V>(__v)); + __node_base_pointer __parent; + __node_base_pointer& __child = __find_leaf_high(__parent, __h->__value_); + __insert_node_at(__parent, __child, __h.get()); + return iterator(__h.release()); +} + +template <class _Tp, class _Compare, class _Allocator> +template <class _V> +typename __tree<_Tp, _Compare, _Allocator>::iterator +__tree<_Tp, _Compare, _Allocator>::__insert_multi(const_iterator __p, _V&& __v) +{ + __node_holder __h = __construct_node(_VSTD::forward<_V>(__v)); + __node_base_pointer __parent; + __node_base_pointer& __child = __find_leaf(__p, __parent, __h->__value_); + __insert_node_at(__parent, __child, __h.get()); + return iterator(__h.release()); +} + +#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES + +template <class _Tp, class _Compare, class _Allocator> +typename __tree<_Tp, _Compare, _Allocator>::__node_holder +__tree<_Tp, _Compare, _Allocator>::__construct_node(const value_type& __v) +{ + __node_allocator& __na = __node_alloc(); + __node_holder __h(__node_traits::allocate(__na, 1), _D(__na)); + __node_traits::construct(__na, _VSTD::addressof(__h->__value_), __v); + __h.get_deleter().__value_constructed = true; + return _VSTD::move(__h); +} + +#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES + +template <class _Tp, class _Compare, class _Allocator> +pair<typename __tree<_Tp, _Compare, _Allocator>::iterator, bool> +__tree<_Tp, _Compare, _Allocator>::__insert_unique(const value_type& __v) +{ + __node_base_pointer __parent; + __node_base_pointer& __child = __find_equal(__parent, __v); + __node_pointer __r = static_cast<__node_pointer>(__child); + bool __inserted = false; + if (__child == nullptr) + { + __node_holder __h = __construct_node(__v); + __insert_node_at(__parent, __child, __h.get()); + __r = __h.release(); + __inserted = true; + } + return pair<iterator, bool>(iterator(__r), __inserted); +} + +template <class _Tp, class _Compare, class _Allocator> +typename __tree<_Tp, _Compare, _Allocator>::iterator +__tree<_Tp, _Compare, _Allocator>::__insert_unique(const_iterator __p, const value_type& __v) +{ + __node_base_pointer __parent; + __node_base_pointer& __child = __find_equal(__p, __parent, __v); + __node_pointer __r = static_cast<__node_pointer>(__child); + if (__child == nullptr) + { + __node_holder __h = __construct_node(__v); + __insert_node_at(__parent, __child, __h.get()); + __r = __h.release(); + } + return iterator(__r); +} + +template <class _Tp, class _Compare, class _Allocator> +typename __tree<_Tp, _Compare, _Allocator>::iterator +__tree<_Tp, _Compare, _Allocator>::__insert_multi(const value_type& __v) +{ + __node_base_pointer __parent; + __node_base_pointer& __child = __find_leaf_high(__parent, __v); + __node_holder __h = __construct_node(__v); + __insert_node_at(__parent, __child, __h.get()); + return iterator(__h.release()); +} + +template <class _Tp, class _Compare, class _Allocator> +typename __tree<_Tp, _Compare, _Allocator>::iterator +__tree<_Tp, _Compare, _Allocator>::__insert_multi(const_iterator __p, const value_type& __v) +{ + __node_base_pointer __parent; + __node_base_pointer& __child = __find_leaf(__p, __parent, __v); + __node_holder __h = __construct_node(__v); + __insert_node_at(__parent, __child, __h.get()); + return iterator(__h.release()); +} + +template <class _Tp, class _Compare, class _Allocator> +pair<typename __tree<_Tp, _Compare, _Allocator>::iterator, bool> +__tree<_Tp, _Compare, _Allocator>::__node_insert_unique(__node_pointer __nd) +{ + __node_base_pointer __parent; + __node_base_pointer& __child = __find_equal(__parent, __nd->__value_); + __node_pointer __r = static_cast<__node_pointer>(__child); + bool __inserted = false; + if (__child == nullptr) + { + __insert_node_at(__parent, __child, __nd); + __r = __nd; + __inserted = true; + } + return pair<iterator, bool>(iterator(__r), __inserted); +} + +template <class _Tp, class _Compare, class _Allocator> +typename __tree<_Tp, _Compare, _Allocator>::iterator +__tree<_Tp, _Compare, _Allocator>::__node_insert_unique(const_iterator __p, + __node_pointer __nd) +{ + __node_base_pointer __parent; + __node_base_pointer& __child = __find_equal(__p, __parent, __nd->__value_); + __node_pointer __r = static_cast<__node_pointer>(__child); + if (__child == nullptr) + { + __insert_node_at(__parent, __child, __nd); + __r = __nd; + } + return iterator(__r); +} + +template <class _Tp, class _Compare, class _Allocator> +typename __tree<_Tp, _Compare, _Allocator>::iterator +__tree<_Tp, _Compare, _Allocator>::__node_insert_multi(__node_pointer __nd) +{ + __node_base_pointer __parent; + __node_base_pointer& __child = __find_leaf_high(__parent, __nd->__value_); + __insert_node_at(__parent, __child, __nd); + return iterator(__nd); +} + +template <class _Tp, class _Compare, class _Allocator> +typename __tree<_Tp, _Compare, _Allocator>::iterator +__tree<_Tp, _Compare, _Allocator>::__node_insert_multi(const_iterator __p, + __node_pointer __nd) +{ + __node_base_pointer __parent; + __node_base_pointer& __child = __find_leaf(__p, __parent, __nd->__value_); + __insert_node_at(__parent, __child, __nd); + return iterator(__nd); +} + +template <class _Tp, class _Compare, class _Allocator> +typename __tree<_Tp, _Compare, _Allocator>::iterator +__tree<_Tp, _Compare, _Allocator>::erase(const_iterator __p) +{ + __node_pointer __np = const_cast<__node_pointer>(__p.__ptr_); + iterator __r(__np); + ++__r; + if (__begin_node() == __np) + __begin_node() = __r.__ptr_; + --size(); + __node_allocator& __na = __node_alloc(); + __node_traits::destroy(__na, const_cast<value_type*>(_VSTD::addressof(*__p))); + __tree_remove(__end_node()->__left_, + static_cast<__node_base_pointer>(__np)); + __node_traits::deallocate(__na, __np, 1); + return __r; +} + +template <class _Tp, class _Compare, class _Allocator> +typename __tree<_Tp, _Compare, _Allocator>::iterator +__tree<_Tp, _Compare, _Allocator>::erase(const_iterator __f, const_iterator __l) +{ + while (__f != __l) + __f = erase(__f); + return iterator(const_cast<__node_pointer>(__l.__ptr_)); +} + +template <class _Tp, class _Compare, class _Allocator> +template <class _Key> +typename __tree<_Tp, _Compare, _Allocator>::size_type +__tree<_Tp, _Compare, _Allocator>::__erase_unique(const _Key& __k) +{ + iterator __i = find(__k); + if (__i == end()) + return 0; + erase(__i); + return 1; +} + +template <class _Tp, class _Compare, class _Allocator> +template <class _Key> +typename __tree<_Tp, _Compare, _Allocator>::size_type +__tree<_Tp, _Compare, _Allocator>::__erase_multi(const _Key& __k) +{ + pair<iterator, iterator> __p = __equal_range_multi(__k); + size_type __r = 0; + for (; __p.first != __p.second; ++__r) + __p.first = erase(__p.first); + return __r; +} + +template <class _Tp, class _Compare, class _Allocator> +template <class _Key> +typename __tree<_Tp, _Compare, _Allocator>::iterator +__tree<_Tp, _Compare, _Allocator>::find(const _Key& __v) +{ + iterator __p = __lower_bound(__v, __root(), __end_node()); + if (__p != end() && !value_comp()(__v, *__p)) + return __p; + return end(); +} + +template <class _Tp, class _Compare, class _Allocator> +template <class _Key> +typename __tree<_Tp, _Compare, _Allocator>::const_iterator +__tree<_Tp, _Compare, _Allocator>::find(const _Key& __v) const +{ + const_iterator __p = __lower_bound(__v, __root(), __end_node()); + if (__p != end() && !value_comp()(__v, *__p)) + return __p; + return end(); +} + +template <class _Tp, class _Compare, class _Allocator> +template <class _Key> +typename __tree<_Tp, _Compare, _Allocator>::size_type +__tree<_Tp, _Compare, _Allocator>::__count_unique(const _Key& __k) const +{ + __node_const_pointer __result = __end_node(); + __node_const_pointer __rt = __root(); + while (__rt != nullptr) + { + if (value_comp()(__k, __rt->__value_)) + { + __result = __rt; + __rt = static_cast<__node_const_pointer>(__rt->__left_); + } + else if (value_comp()(__rt->__value_, __k)) + __rt = static_cast<__node_const_pointer>(__rt->__right_); + else + return 1; + } + return 0; +} + +template <class _Tp, class _Compare, class _Allocator> +template <class _Key> +typename __tree<_Tp, _Compare, _Allocator>::size_type +__tree<_Tp, _Compare, _Allocator>::__count_multi(const _Key& __k) const +{ + typedef pair<const_iterator, const_iterator> _P; + __node_const_pointer __result = __end_node(); + __node_const_pointer __rt = __root(); + while (__rt != nullptr) + { + if (value_comp()(__k, __rt->__value_)) + { + __result = __rt; + __rt = static_cast<__node_const_pointer>(__rt->__left_); + } + else if (value_comp()(__rt->__value_, __k)) + __rt = static_cast<__node_const_pointer>(__rt->__right_); + else + return _VSTD::distance( + __lower_bound(__k, static_cast<__node_const_pointer>(__rt->__left_), __rt), + __upper_bound(__k, static_cast<__node_const_pointer>(__rt->__right_), __result) + ); + } + return 0; +} + +template <class _Tp, class _Compare, class _Allocator> +template <class _Key> +typename __tree<_Tp, _Compare, _Allocator>::iterator +__tree<_Tp, _Compare, _Allocator>::__lower_bound(const _Key& __v, + __node_pointer __root, + __node_pointer __result) +{ + while (__root != nullptr) + { + if (!value_comp()(__root->__value_, __v)) + { + __result = __root; + __root = static_cast<__node_pointer>(__root->__left_); + } + else + __root = static_cast<__node_pointer>(__root->__right_); + } + return iterator(__result); +} + +template <class _Tp, class _Compare, class _Allocator> +template <class _Key> +typename __tree<_Tp, _Compare, _Allocator>::const_iterator +__tree<_Tp, _Compare, _Allocator>::__lower_bound(const _Key& __v, + __node_const_pointer __root, + __node_const_pointer __result) const +{ + while (__root != nullptr) + { + if (!value_comp()(__root->__value_, __v)) + { + __result = __root; + __root = static_cast<__node_const_pointer>(__root->__left_); + } + else + __root = static_cast<__node_const_pointer>(__root->__right_); + } + return const_iterator(__result); +} + +template <class _Tp, class _Compare, class _Allocator> +template <class _Key> +typename __tree<_Tp, _Compare, _Allocator>::iterator +__tree<_Tp, _Compare, _Allocator>::__upper_bound(const _Key& __v, + __node_pointer __root, + __node_pointer __result) +{ + while (__root != nullptr) + { + if (value_comp()(__v, __root->__value_)) + { + __result = __root; + __root = static_cast<__node_pointer>(__root->__left_); + } + else + __root = static_cast<__node_pointer>(__root->__right_); + } + return iterator(__result); +} + +template <class _Tp, class _Compare, class _Allocator> +template <class _Key> +typename __tree<_Tp, _Compare, _Allocator>::const_iterator +__tree<_Tp, _Compare, _Allocator>::__upper_bound(const _Key& __v, + __node_const_pointer __root, + __node_const_pointer __result) const +{ + while (__root != nullptr) + { + if (value_comp()(__v, __root->__value_)) + { + __result = __root; + __root = static_cast<__node_const_pointer>(__root->__left_); + } + else + __root = static_cast<__node_const_pointer>(__root->__right_); + } + return const_iterator(__result); +} + +template <class _Tp, class _Compare, class _Allocator> +template <class _Key> +pair<typename __tree<_Tp, _Compare, _Allocator>::iterator, + typename __tree<_Tp, _Compare, _Allocator>::iterator> +__tree<_Tp, _Compare, _Allocator>::__equal_range_unique(const _Key& __k) +{ + typedef pair<iterator, iterator> _P; + __node_pointer __result = __end_node(); + __node_pointer __rt = __root(); + while (__rt != nullptr) + { + if (value_comp()(__k, __rt->__value_)) + { + __result = __rt; + __rt = static_cast<__node_pointer>(__rt->__left_); + } + else if (value_comp()(__rt->__value_, __k)) + __rt = static_cast<__node_pointer>(__rt->__right_); + else + return _P(iterator(__rt), + iterator( + __rt->__right_ != nullptr ? + static_cast<__node_pointer>(__tree_min(__rt->__right_)) + : __result)); + } + return _P(iterator(__result), iterator(__result)); +} + +template <class _Tp, class _Compare, class _Allocator> +template <class _Key> +pair<typename __tree<_Tp, _Compare, _Allocator>::const_iterator, + typename __tree<_Tp, _Compare, _Allocator>::const_iterator> +__tree<_Tp, _Compare, _Allocator>::__equal_range_unique(const _Key& __k) const +{ + typedef pair<const_iterator, const_iterator> _P; + __node_const_pointer __result = __end_node(); + __node_const_pointer __rt = __root(); + while (__rt != nullptr) + { + if (value_comp()(__k, __rt->__value_)) + { + __result = __rt; + __rt = static_cast<__node_const_pointer>(__rt->__left_); + } + else if (value_comp()(__rt->__value_, __k)) + __rt = static_cast<__node_const_pointer>(__rt->__right_); + else + return _P(const_iterator(__rt), + const_iterator( + __rt->__right_ != nullptr ? + static_cast<__node_const_pointer>(__tree_min(__rt->__right_)) + : __result)); + } + return _P(const_iterator(__result), const_iterator(__result)); +} + +template <class _Tp, class _Compare, class _Allocator> +template <class _Key> +pair<typename __tree<_Tp, _Compare, _Allocator>::iterator, + typename __tree<_Tp, _Compare, _Allocator>::iterator> +__tree<_Tp, _Compare, _Allocator>::__equal_range_multi(const _Key& __k) +{ + typedef pair<iterator, iterator> _P; + __node_pointer __result = __end_node(); + __node_pointer __rt = __root(); + while (__rt != nullptr) + { + if (value_comp()(__k, __rt->__value_)) + { + __result = __rt; + __rt = static_cast<__node_pointer>(__rt->__left_); + } + else if (value_comp()(__rt->__value_, __k)) + __rt = static_cast<__node_pointer>(__rt->__right_); + else + return _P(__lower_bound(__k, static_cast<__node_pointer>(__rt->__left_), __rt), + __upper_bound(__k, static_cast<__node_pointer>(__rt->__right_), __result)); + } + return _P(iterator(__result), iterator(__result)); +} + +template <class _Tp, class _Compare, class _Allocator> +template <class _Key> +pair<typename __tree<_Tp, _Compare, _Allocator>::const_iterator, + typename __tree<_Tp, _Compare, _Allocator>::const_iterator> +__tree<_Tp, _Compare, _Allocator>::__equal_range_multi(const _Key& __k) const +{ + typedef pair<const_iterator, const_iterator> _P; + __node_const_pointer __result = __end_node(); + __node_const_pointer __rt = __root(); + while (__rt != nullptr) + { + if (value_comp()(__k, __rt->__value_)) + { + __result = __rt; + __rt = static_cast<__node_const_pointer>(__rt->__left_); + } + else if (value_comp()(__rt->__value_, __k)) + __rt = static_cast<__node_const_pointer>(__rt->__right_); + else + return _P(__lower_bound(__k, static_cast<__node_const_pointer>(__rt->__left_), __rt), + __upper_bound(__k, static_cast<__node_const_pointer>(__rt->__right_), __result)); + } + return _P(const_iterator(__result), const_iterator(__result)); +} + +template <class _Tp, class _Compare, class _Allocator> +typename __tree<_Tp, _Compare, _Allocator>::__node_holder +__tree<_Tp, _Compare, _Allocator>::remove(const_iterator __p) _NOEXCEPT +{ + __node_pointer __np = const_cast<__node_pointer>(__p.__ptr_); + if (__begin_node() == __np) + { + if (__np->__right_ != nullptr) + __begin_node() = static_cast<__node_pointer>(__np->__right_); + else + __begin_node() = static_cast<__node_pointer>(__np->__parent_); + } + --size(); + __tree_remove(__end_node()->__left_, + static_cast<__node_base_pointer>(__np)); + return __node_holder(__np, _D(__node_alloc())); +} + +template <class _Tp, class _Compare, class _Allocator> +inline _LIBCPP_INLINE_VISIBILITY +void +swap(__tree<_Tp, _Compare, _Allocator>& __x, + __tree<_Tp, _Compare, _Allocator>& __y) + _NOEXCEPT_(_NOEXCEPT_(__x.swap(__y))) +{ + __x.swap(__y); +} + +_LIBCPP_END_NAMESPACE_STD + +#endif // _LIBCPP___TREE diff --git a/include/__tuple b/include/__tuple new file mode 100644 index 000000000000..15193b4d1074 --- /dev/null +++ b/include/__tuple @@ -0,0 +1,269 @@ +// -*- C++ -*- +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#ifndef _LIBCPP___TUPLE +#define _LIBCPP___TUPLE + +#include <__config> +#include <cstddef> +#include <type_traits> + +#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) +#pragma GCC system_header +#endif + +#ifdef _LIBCPP_HAS_NO_VARIADICS + +#include <__tuple_03> + +#else // _LIBCPP_HAS_NO_VARIADICS + +_LIBCPP_BEGIN_NAMESPACE_STD + +template <class _Tp> class _LIBCPP_VISIBLE tuple_size; + +template <class _Tp> +class _LIBCPP_VISIBLE tuple_size<const _Tp> + : public tuple_size<_Tp> {}; + +template <class _Tp> +class _LIBCPP_VISIBLE tuple_size<volatile _Tp> + : public tuple_size<_Tp> {}; + +template <class _Tp> +class _LIBCPP_VISIBLE tuple_size<const volatile _Tp> + : public tuple_size<_Tp> {}; + +template <size_t _Ip, class _Tp> class _LIBCPP_VISIBLE tuple_element; + +template <size_t _Ip, class _Tp> +class _LIBCPP_VISIBLE tuple_element<_Ip, const _Tp> +{ +public: + typedef typename add_const<typename tuple_element<_Ip, _Tp>::type>::type type; +}; + +template <size_t _Ip, class _Tp> +class _LIBCPP_VISIBLE tuple_element<_Ip, volatile _Tp> +{ +public: + typedef typename add_volatile<typename tuple_element<_Ip, _Tp>::type>::type type; +}; + +template <size_t _Ip, class _Tp> +class _LIBCPP_VISIBLE tuple_element<_Ip, const volatile _Tp> +{ +public: + typedef typename add_cv<typename tuple_element<_Ip, _Tp>::type>::type type; +}; + +template <class ..._Tp> class _LIBCPP_VISIBLE tuple; +template <class _T1, class _T2> class _LIBCPP_VISIBLE pair; +template <class _Tp, size_t _Size> struct _LIBCPP_VISIBLE array; + +template <class _Tp> struct __tuple_like : false_type {}; + +template <class _Tp> struct __tuple_like<const _Tp> : public __tuple_like<_Tp> {}; +template <class _Tp> struct __tuple_like<volatile _Tp> : public __tuple_like<_Tp> {}; +template <class _Tp> struct __tuple_like<const volatile _Tp> : public __tuple_like<_Tp> {}; + +template <class... _Tp> struct __tuple_like<tuple<_Tp...> > : true_type {}; +template <class _T1, class _T2> struct __tuple_like<pair<_T1, _T2> > : true_type {}; +template <class _Tp, size_t _Size> struct __tuple_like<array<_Tp, _Size> > : true_type {}; + +template <size_t _Ip, class ..._Tp> +typename tuple_element<_Ip, tuple<_Tp...> >::type& +get(tuple<_Tp...>&) _NOEXCEPT; + +template <size_t _Ip, class ..._Tp> +const typename tuple_element<_Ip, tuple<_Tp...> >::type& +get(const tuple<_Tp...>&) _NOEXCEPT; + +template <size_t _Ip, class ..._Tp> +typename tuple_element<_Ip, tuple<_Tp...> >::type&& +get(tuple<_Tp...>&&) _NOEXCEPT; + +template <size_t _Ip, class _T1, class _T2> +typename tuple_element<_Ip, pair<_T1, _T2> >::type& +get(pair<_T1, _T2>&) _NOEXCEPT; + +template <size_t _Ip, class _T1, class _T2> +const typename tuple_element<_Ip, pair<_T1, _T2> >::type& +get(const pair<_T1, _T2>&) _NOEXCEPT; + +template <size_t _Ip, class _T1, class _T2> +typename tuple_element<_Ip, pair<_T1, _T2> >::type&& +get(pair<_T1, _T2>&&) _NOEXCEPT; + +template <size_t _Ip, class _Tp, size_t _Size> +_Tp& +get(array<_Tp, _Size>&) _NOEXCEPT; + +template <size_t _Ip, class _Tp, size_t _Size> +const _Tp& +get(const array<_Tp, _Size>&) _NOEXCEPT; + +template <size_t _Ip, class _Tp, size_t _Size> +_Tp&& +get(array<_Tp, _Size>&&) _NOEXCEPT; + +// __make_tuple_indices + +template <size_t...> struct __tuple_indices {}; + +template <size_t _Sp, class _IntTuple, size_t _Ep> +struct __make_indices_imp; + +template <size_t _Sp, size_t ..._Indices, size_t _Ep> +struct __make_indices_imp<_Sp, __tuple_indices<_Indices...>, _Ep> +{ + typedef typename __make_indices_imp<_Sp+1, __tuple_indices<_Indices..., _Sp>, _Ep>::type type; +}; + +template <size_t _Ep, size_t ..._Indices> +struct __make_indices_imp<_Ep, __tuple_indices<_Indices...>, _Ep> +{ + typedef __tuple_indices<_Indices...> type; +}; + +template <size_t _Ep, size_t _Sp = 0> +struct __make_tuple_indices +{ + static_assert(_Sp <= _Ep, "__make_tuple_indices input error"); + typedef typename __make_indices_imp<_Sp, __tuple_indices<>, _Ep>::type type; +}; + +// __tuple_types + +template <class ..._Tp> struct __tuple_types {}; + +template <size_t _Ip> +class _LIBCPP_VISIBLE tuple_element<_Ip, __tuple_types<> > +{ +public: + static_assert(_Ip == 0, "tuple_element index out of range"); + static_assert(_Ip != 0, "tuple_element index out of range"); +}; + +template <class _Hp, class ..._Tp> +class _LIBCPP_VISIBLE tuple_element<0, __tuple_types<_Hp, _Tp...> > +{ +public: + typedef _Hp type; +}; + +template <size_t _Ip, class _Hp, class ..._Tp> +class _LIBCPP_VISIBLE tuple_element<_Ip, __tuple_types<_Hp, _Tp...> > +{ +public: + typedef typename tuple_element<_Ip-1, __tuple_types<_Tp...> >::type type; +}; + +template <class ..._Tp> +class _LIBCPP_VISIBLE tuple_size<__tuple_types<_Tp...> > + : public integral_constant<size_t, sizeof...(_Tp)> +{ +}; + +template <class... _Tp> struct __tuple_like<__tuple_types<_Tp...> > : true_type {}; + +// __make_tuple_types + +// __make_tuple_types<_Tuple<_Types...>, _Ep, _Sp>::type is a +// __tuple_types<_Types...> using only those _Types in the range [_Sp, _Ep). +// _Sp defaults to 0 and _Ep defaults to tuple_size<_Tuple>. If _Tuple is a +// lvalue_reference type, then __tuple_types<_Types&...> is the result. + +template <class _TupleTypes, class _Tp, size_t _Sp, size_t _Ep> +struct __make_tuple_types_imp; + +template <class ..._Types, class _Tp, size_t _Sp, size_t _Ep> +struct __make_tuple_types_imp<__tuple_types<_Types...>, _Tp, _Sp, _Ep> +{ + typedef typename remove_reference<_Tp>::type _Tpr; + typedef typename __make_tuple_types_imp<__tuple_types<_Types..., + typename conditional<is_lvalue_reference<_Tp>::value, + typename tuple_element<_Sp, _Tpr>::type&, + typename tuple_element<_Sp, _Tpr>::type>::type>, + _Tp, _Sp+1, _Ep>::type type; +}; + +template <class ..._Types, class _Tp, size_t _Ep> +struct __make_tuple_types_imp<__tuple_types<_Types...>, _Tp, _Ep, _Ep> +{ + typedef __tuple_types<_Types...> type; +}; + +template <class _Tp, size_t _Ep = tuple_size<typename remove_reference<_Tp>::type>::value, size_t _Sp = 0> +struct __make_tuple_types +{ + static_assert(_Sp <= _Ep, "__make_tuple_types input error"); + typedef typename __make_tuple_types_imp<__tuple_types<>, _Tp, _Sp, _Ep>::type type; +}; + +// __tuple_convertible + +template <bool, class _Tp, class _Up> +struct __tuple_convertible_imp : public false_type {}; + +template <class _Tp0, class ..._Tp, class _Up0, class ..._Up> +struct __tuple_convertible_imp<true, __tuple_types<_Tp0, _Tp...>, __tuple_types<_Up0, _Up...> > + : public integral_constant<bool, + is_constructible<_Up0, _Tp0>::value && + __tuple_convertible_imp<true, __tuple_types<_Tp...>, __tuple_types<_Up...> >::value> {}; + +template <> +struct __tuple_convertible_imp<true, __tuple_types<>, __tuple_types<> > + : public true_type {}; + +template <class _Tp, class _Up, bool = __tuple_like<typename remove_reference<_Tp>::type>::value, + bool = __tuple_like<_Up>::value> +struct __tuple_convertible + : public false_type {}; + +template <class _Tp, class _Up> +struct __tuple_convertible<_Tp, _Up, true, true> + : public __tuple_convertible_imp<tuple_size<typename remove_reference<_Tp>::type>::value == + tuple_size<_Up>::value, + typename __make_tuple_types<_Tp>::type, typename __make_tuple_types<_Up>::type> +{}; + +// __tuple_assignable + +template <bool, class _Tp, class _Up> +struct __tuple_assignable_imp : public false_type {}; + +template <class _Tp0, class ..._Tp, class _Up0, class ..._Up> +struct __tuple_assignable_imp<true, __tuple_types<_Tp0, _Tp...>, __tuple_types<_Up0, _Up...> > + : public integral_constant<bool, + is_assignable<_Up0&, _Tp0>::value && + __tuple_assignable_imp<true, __tuple_types<_Tp...>, __tuple_types<_Up...> >::value> {}; + +template <> +struct __tuple_assignable_imp<true, __tuple_types<>, __tuple_types<> > + : public true_type {}; + +template <class _Tp, class _Up, bool = __tuple_like<typename remove_reference<_Tp>::type>::value, + bool = __tuple_like<_Up>::value> +struct __tuple_assignable + : public false_type {}; + +template <class _Tp, class _Up> +struct __tuple_assignable<_Tp, _Up, true, true> + : public __tuple_assignable_imp<tuple_size<typename remove_reference<_Tp>::type>::value == + tuple_size<_Up>::value, + typename __make_tuple_types<_Tp>::type, typename __make_tuple_types<_Up>::type> +{}; + +_LIBCPP_END_NAMESPACE_STD + +#endif // _LIBCPP_HAS_NO_VARIADICS + +#endif // _LIBCPP___TUPLE diff --git a/include/__tuple_03 b/include/__tuple_03 new file mode 100644 index 000000000000..a28ac0800ae9 --- /dev/null +++ b/include/__tuple_03 @@ -0,0 +1,27 @@ +// -*- C++ -*- +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#ifndef _LIBCPP___TUPLE_03 +#define _LIBCPP___TUPLE_03 + +#include <__config> + +#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) +#pragma GCC system_header +#endif + +_LIBCPP_BEGIN_NAMESPACE_STD + +template <class _Tp> class _LIBCPP_VISIBLE tuple_size; +template <size_t _Ip, class _Tp> class _LIBCPP_VISIBLE tuple_element; + +_LIBCPP_END_NAMESPACE_STD + +#endif // _LIBCPP___TUPLE_03 diff --git a/include/algorithm b/include/algorithm new file mode 100644 index 000000000000..a89b9dd65eab --- /dev/null +++ b/include/algorithm @@ -0,0 +1,5361 @@ +// -*- C++ -*- +//===-------------------------- algorithm ---------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#ifndef _LIBCPP_ALGORITHM +#define _LIBCPP_ALGORITHM + +/* + algorithm synopsis + +#include <initializer_list> + +namespace std +{ + +template <class InputIterator, class Predicate> + bool + all_of(InputIterator first, InputIterator last, Predicate pred); + +template <class InputIterator, class Predicate> + bool + any_of(InputIterator first, InputIterator last, Predicate pred); + +template <class InputIterator, class Predicate> + bool + none_of(InputIterator first, InputIterator last, Predicate pred); + +template <class InputIterator, class Function> + Function + for_each(InputIterator first, InputIterator last, Function f); + +template <class InputIterator, class T> + InputIterator + find(InputIterator first, InputIterator last, const T& value); + +template <class InputIterator, class Predicate> + InputIterator + find_if(InputIterator first, InputIterator last, Predicate pred); + +template<class InputIterator, class Predicate> + InputIterator + find_if_not(InputIterator first, InputIterator last, Predicate pred); + +template <class ForwardIterator1, class ForwardIterator2> + ForwardIterator1 + find_end(ForwardIterator1 first1, ForwardIterator1 last1, + ForwardIterator2 first2, ForwardIterator2 last2); + +template <class ForwardIterator1, class ForwardIterator2, class BinaryPredicate> + ForwardIterator1 + find_end(ForwardIterator1 first1, ForwardIterator1 last1, + ForwardIterator2 first2, ForwardIterator2 last2, BinaryPredicate pred); + +template <class ForwardIterator1, class ForwardIterator2> + ForwardIterator1 + find_first_of(ForwardIterator1 first1, ForwardIterator1 last1, + ForwardIterator2 first2, ForwardIterator2 last2); + +template <class ForwardIterator1, class ForwardIterator2, class BinaryPredicate> + ForwardIterator1 + find_first_of(ForwardIterator1 first1, ForwardIterator1 last1, + ForwardIterator2 first2, ForwardIterator2 last2, BinaryPredicate pred); + +template <class ForwardIterator> + ForwardIterator + adjacent_find(ForwardIterator first, ForwardIterator last); + +template <class ForwardIterator, class BinaryPredicate> + ForwardIterator + adjacent_find(ForwardIterator first, ForwardIterator last, BinaryPredicate pred); + +template <class InputIterator, class T> + typename iterator_traits<InputIterator>::difference_type + count(InputIterator first, InputIterator last, const T& value); + +template <class InputIterator, class Predicate> + typename iterator_traits<InputIterator>::difference_type + count_if(InputIterator first, InputIterator last, Predicate pred); + +template <class InputIterator1, class InputIterator2> + pair<InputIterator1, InputIterator2> + mismatch(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2); + +template <class InputIterator1, class InputIterator2, class BinaryPredicate> + pair<InputIterator1, InputIterator2> + mismatch(InputIterator1 first1, InputIterator1 last1, + InputIterator2 first2, BinaryPredicate pred); + +template <class InputIterator1, class InputIterator2> + bool + equal(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2); + +template <class InputIterator1, class InputIterator2, class BinaryPredicate> + bool + equal(InputIterator1 first1, InputIterator1 last1, + InputIterator2 first2, BinaryPredicate pred); + +template<class ForwardIterator1, class ForwardIterator2> + bool + is_permutation(ForwardIterator1 first1, ForwardIterator1 last1, + ForwardIterator2 first2); + +template<class ForwardIterator1, class ForwardIterator2, class BinaryPredicate> + bool + is_permutation(ForwardIterator1 first1, ForwardIterator1 last1, + ForwardIterator2 first2, BinaryPredicate pred); + +template <class ForwardIterator1, class ForwardIterator2> + ForwardIterator1 + search(ForwardIterator1 first1, ForwardIterator1 last1, + ForwardIterator2 first2, ForwardIterator2 last2); + +template <class ForwardIterator1, class ForwardIterator2, class BinaryPredicate> + ForwardIterator1 + search(ForwardIterator1 first1, ForwardIterator1 last1, + ForwardIterator2 first2, ForwardIterator2 last2, BinaryPredicate pred); + +template <class ForwardIterator, class Size, class T> + ForwardIterator + search_n(ForwardIterator first, ForwardIterator last, Size count, const T& value); + +template <class ForwardIterator, class Size, class T, class BinaryPredicate> + ForwardIterator + search_n(ForwardIterator first, ForwardIterator last, + Size count, const T& value, BinaryPredicate pred); + +template <class InputIterator, class OutputIterator> + OutputIterator + copy(InputIterator first, InputIterator last, OutputIterator result); + +template<class InputIterator, class OutputIterator, class Predicate> + OutputIterator + copy_if(InputIterator first, InputIterator last, + OutputIterator result, Predicate pred); + +template<class InputIterator, class Size, class OutputIterator> + OutputIterator + copy_n(InputIterator first, Size n, OutputIterator result); + +template <class BidirectionalIterator1, class BidirectionalIterator2> + BidirectionalIterator2 + copy_backward(BidirectionalIterator1 first, BidirectionalIterator1 last, + BidirectionalIterator2 result); + +template <class ForwardIterator1, class ForwardIterator2> + ForwardIterator2 + swap_ranges(ForwardIterator1 first1, ForwardIterator1 last1, ForwardIterator2 first2); + +template <class ForwardIterator1, class ForwardIterator2> + void + iter_swap(ForwardIterator1 a, ForwardIterator2 b); + +template <class InputIterator, class OutputIterator, class UnaryOperation> + OutputIterator + transform(InputIterator first, InputIterator last, OutputIterator result, UnaryOperation op); + +template <class InputIterator1, class InputIterator2, class OutputIterator, class BinaryOperation> + OutputIterator + transform(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, + OutputIterator result, BinaryOperation binary_op); + +template <class ForwardIterator, class T> + void + replace(ForwardIterator first, ForwardIterator last, const T& old_value, const T& new_value); + +template <class ForwardIterator, class Predicate, class T> + void + replace_if(ForwardIterator first, ForwardIterator last, Predicate pred, const T& new_value); + +template <class InputIterator, class OutputIterator, class T> + OutputIterator + replace_copy(InputIterator first, InputIterator last, OutputIterator result, + const T& old_value, const T& new_value); + +template <class InputIterator, class OutputIterator, class Predicate, class T> + OutputIterator + replace_copy_if(InputIterator first, InputIterator last, OutputIterator result, Predicate pred, const T& new_value); + +template <class ForwardIterator, class T> + void + fill(ForwardIterator first, ForwardIterator last, const T& value); + +template <class OutputIterator, class Size, class T> + OutputIterator + fill_n(OutputIterator first, Size n, const T& value); + +template <class ForwardIterator, class Generator> + void + generate(ForwardIterator first, ForwardIterator last, Generator gen); + +template <class OutputIterator, class Size, class Generator> + OutputIterator + generate_n(OutputIterator first, Size n, Generator gen); + +template <class ForwardIterator, class T> + ForwardIterator + remove(ForwardIterator first, ForwardIterator last, const T& value); + +template <class ForwardIterator, class Predicate> + ForwardIterator + remove_if(ForwardIterator first, ForwardIterator last, Predicate pred); + +template <class InputIterator, class OutputIterator, class T> + OutputIterator + remove_copy(InputIterator first, InputIterator last, OutputIterator result, const T& value); + +template <class InputIterator, class OutputIterator, class Predicate> + OutputIterator + remove_copy_if(InputIterator first, InputIterator last, OutputIterator result, Predicate pred); + +template <class ForwardIterator> + ForwardIterator + unique(ForwardIterator first, ForwardIterator last); + +template <class ForwardIterator, class BinaryPredicate> + ForwardIterator + unique(ForwardIterator first, ForwardIterator last, BinaryPredicate pred); + +template <class InputIterator, class OutputIterator> + OutputIterator + unique_copy(InputIterator first, InputIterator last, OutputIterator result); + +template <class InputIterator, class OutputIterator, class BinaryPredicate> + OutputIterator + unique_copy(InputIterator first, InputIterator last, OutputIterator result, BinaryPredicate pred); + +template <class BidirectionalIterator> + void + reverse(BidirectionalIterator first, BidirectionalIterator last); + +template <class BidirectionalIterator, class OutputIterator> + OutputIterator + reverse_copy(BidirectionalIterator first, BidirectionalIterator last, OutputIterator result); + +template <class ForwardIterator> + ForwardIterator + rotate(ForwardIterator first, ForwardIterator middle, ForwardIterator last); + +template <class ForwardIterator, class OutputIterator> + OutputIterator + rotate_copy(ForwardIterator first, ForwardIterator middle, ForwardIterator last, OutputIterator result); + +template <class RandomAccessIterator> + void + random_shuffle(RandomAccessIterator first, RandomAccessIterator last); + +template <class RandomAccessIterator, class RandomNumberGenerator> + void + random_shuffle(RandomAccessIterator first, RandomAccessIterator last, RandomNumberGenerator& rand); + +template<class RandomAccessIterator, class UniformRandomNumberGenerator> + void shuffle(RandomAccessIterator first, RandomAccessIterator last, + UniformRandomNumberGenerator&& g); + +template <class InputIterator, class Predicate> + bool + is_partitioned(InputIterator first, InputIterator last, Predicate pred); + +template <class ForwardIterator, class Predicate> + ForwardIterator + partition(ForwardIterator first, ForwardIterator last, Predicate pred); + +template <class InputIterator, class OutputIterator1, + class OutputIterator2, class Predicate> + pair<OutputIterator1, OutputIterator2> + partition_copy(InputIterator first, InputIterator last, + OutputIterator1 out_true, OutputIterator2 out_false, + Predicate pred); + +template <class ForwardIterator, class Predicate> + ForwardIterator + stable_partition(ForwardIterator first, ForwardIterator last, Predicate pred); + +template<class ForwardIterator, class Predicate> + ForwardIterator + partition_point(ForwardIterator first, ForwardIterator last, Predicate pred); + +template <class ForwardIterator> + bool + is_sorted(ForwardIterator first, ForwardIterator last); + +template <class ForwardIterator, class Compare> + bool + is_sorted(ForwardIterator first, ForwardIterator last, Compare comp); + +template<class ForwardIterator> + ForwardIterator + is_sorted_until(ForwardIterator first, ForwardIterator last); + +template <class ForwardIterator, class Compare> + ForwardIterator + is_sorted_until(ForwardIterator first, ForwardIterator last, Compare comp); + +template <class RandomAccessIterator> + void + sort(RandomAccessIterator first, RandomAccessIterator last); + +template <class RandomAccessIterator, class Compare> + void + sort(RandomAccessIterator first, RandomAccessIterator last, Compare comp); + +template <class RandomAccessIterator> + void + stable_sort(RandomAccessIterator first, RandomAccessIterator last); + +template <class RandomAccessIterator, class Compare> + void + stable_sort(RandomAccessIterator first, RandomAccessIterator last, Compare comp); + +template <class RandomAccessIterator> + void + partial_sort(RandomAccessIterator first, RandomAccessIterator middle, RandomAccessIterator last); + +template <class RandomAccessIterator, class Compare> + void + partial_sort(RandomAccessIterator first, RandomAccessIterator middle, RandomAccessIterator last, Compare comp); + +template <class InputIterator, class RandomAccessIterator> + RandomAccessIterator + partial_sort_copy(InputIterator first, InputIterator last, + RandomAccessIterator result_first, RandomAccessIterator result_last); + +template <class InputIterator, class RandomAccessIterator, class Compare> + RandomAccessIterator + partial_sort_copy(InputIterator first, InputIterator last, + RandomAccessIterator result_first, RandomAccessIterator result_last, Compare comp); + +template <class RandomAccessIterator> + void + nth_element(RandomAccessIterator first, RandomAccessIterator nth, RandomAccessIterator last); + +template <class RandomAccessIterator, class Compare> + void + nth_element(RandomAccessIterator first, RandomAccessIterator nth, RandomAccessIterator last, Compare comp); + +template <class ForwardIterator, class T> + ForwardIterator + lower_bound(ForwardIterator first, ForwardIterator last, const T& value); + +template <class ForwardIterator, class T, class Compare> + ForwardIterator + lower_bound(ForwardIterator first, ForwardIterator last, const T& value, Compare comp); + +template <class ForwardIterator, class T> + ForwardIterator + upper_bound(ForwardIterator first, ForwardIterator last, const T& value); + +template <class ForwardIterator, class T, class Compare> + ForwardIterator + upper_bound(ForwardIterator first, ForwardIterator last, const T& value, Compare comp); + +template <class ForwardIterator, class T> + pair<ForwardIterator, ForwardIterator> + equal_range(ForwardIterator first, ForwardIterator last, const T& value); + +template <class ForwardIterator, class T, class Compare> + pair<ForwardIterator, ForwardIterator> + equal_range(ForwardIterator first, ForwardIterator last, const T& value, Compare comp); + +template <class ForwardIterator, class T> + bool + binary_search(ForwardIterator first, ForwardIterator last, const T& value); + +template <class ForwardIterator, class T, class Compare> + bool + binary_search(ForwardIterator first, ForwardIterator last, const T& value, Compare comp); + +template <class InputIterator1, class InputIterator2, class OutputIterator> + OutputIterator + merge(InputIterator1 first1, InputIterator1 last1, + InputIterator2 first2, InputIterator2 last2, OutputIterator result); + +template <class InputIterator1, class InputIterator2, class OutputIterator, class Compare> + OutputIterator + merge(InputIterator1 first1, InputIterator1 last1, + InputIterator2 first2, InputIterator2 last2, OutputIterator result, Compare comp); + +template <class BidirectionalIterator> + void + inplace_merge(BidirectionalIterator first, BidirectionalIterator middle, BidirectionalIterator last); + +template <class BidirectionalIterator, class Compare> + void + inplace_merge(BidirectionalIterator first, BidirectionalIterator middle, BidirectionalIterator last, Compare comp); + +template <class InputIterator1, class InputIterator2> + bool + includes(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2); + +template <class InputIterator1, class InputIterator2, class Compare> + bool + includes(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, Compare comp); + +template <class InputIterator1, class InputIterator2, class OutputIterator> + OutputIterator + set_union(InputIterator1 first1, InputIterator1 last1, + InputIterator2 first2, InputIterator2 last2, OutputIterator result); + +template <class InputIterator1, class InputIterator2, class OutputIterator, class Compare> + OutputIterator + set_union(InputIterator1 first1, InputIterator1 last1, + InputIterator2 first2, InputIterator2 last2, OutputIterator result, Compare comp); + +template <class InputIterator1, class InputIterator2, class OutputIterator> + OutputIterator + set_intersection(InputIterator1 first1, InputIterator1 last1, + InputIterator2 first2, InputIterator2 last2, OutputIterator result); + +template <class InputIterator1, class InputIterator2, class OutputIterator, class Compare> + OutputIterator + set_intersection(InputIterator1 first1, InputIterator1 last1, + InputIterator2 first2, InputIterator2 last2, OutputIterator result, Compare comp); + +template <class InputIterator1, class InputIterator2, class OutputIterator> + OutputIterator + set_difference(InputIterator1 first1, InputIterator1 last1, + InputIterator2 first2, InputIterator2 last2, OutputIterator result); + +template <class InputIterator1, class InputIterator2, class OutputIterator, class Compare> + OutputIterator + set_difference(InputIterator1 first1, InputIterator1 last1, + InputIterator2 first2, InputIterator2 last2, OutputIterator result, Compare comp); + +template <class InputIterator1, class InputIterator2, class OutputIterator> + OutputIterator + set_symmetric_difference(InputIterator1 first1, InputIterator1 last1, + InputIterator2 first2, InputIterator2 last2, OutputIterator result); + +template <class InputIterator1, class InputIterator2, class OutputIterator, class Compare> + OutputIterator + set_symmetric_difference(InputIterator1 first1, InputIterator1 last1, + InputIterator2 first2, InputIterator2 last2, OutputIterator result, Compare comp); + +template <class RandomAccessIterator> + void + push_heap(RandomAccessIterator first, RandomAccessIterator last); + +template <class RandomAccessIterator, class Compare> + void + push_heap(RandomAccessIterator first, RandomAccessIterator last, Compare comp); + +template <class RandomAccessIterator> + void + pop_heap(RandomAccessIterator first, RandomAccessIterator last); + +template <class RandomAccessIterator, class Compare> + void + pop_heap(RandomAccessIterator first, RandomAccessIterator last, Compare comp); + +template <class RandomAccessIterator> + void + make_heap(RandomAccessIterator first, RandomAccessIterator last); + +template <class RandomAccessIterator, class Compare> + void + make_heap(RandomAccessIterator first, RandomAccessIterator last, Compare comp); + +template <class RandomAccessIterator> + void + sort_heap(RandomAccessIterator first, RandomAccessIterator last); + +template <class RandomAccessIterator, class Compare> + void + sort_heap(RandomAccessIterator first, RandomAccessIterator last, Compare comp); + +template <class RandomAccessIterator> + bool + is_heap(RandomAccessIterator first, RandomAccessiterator last); + +template <class RandomAccessIterator, class Compare> + bool + is_heap(RandomAccessIterator first, RandomAccessiterator last, Compare comp); + +template <class RandomAccessIterator> + RandomAccessIterator + is_heap_until(RandomAccessIterator first, RandomAccessiterator last); + +template <class RandomAccessIterator, class Compare> + RandomAccessIterator + is_heap_until(RandomAccessIterator first, RandomAccessiterator last, Compare comp); + +template <class ForwardIterator> + ForwardIterator + min_element(ForwardIterator first, ForwardIterator last); + +template <class ForwardIterator, class Compare> + ForwardIterator + min_element(ForwardIterator first, ForwardIterator last, Compare comp); + +template <class T> + const T& + min(const T& a, const T& b); + +template <class T, class Compare> + const T& + min(const T& a, const T& b, Compare comp); + +template<class T> + T + min(initializer_list<T> t); + +template<class T, class Compare> + T + min(initializer_list<T> t, Compare comp); + +template <class ForwardIterator> + ForwardIterator + max_element(ForwardIterator first, ForwardIterator last); + +template <class ForwardIterator, class Compare> + ForwardIterator + max_element(ForwardIterator first, ForwardIterator last, Compare comp); + +template <class T> + const T& + max(const T& a, const T& b); + +template <class T, class Compare> + const T& + max(const T& a, const T& b, Compare comp); + +template<class T> + T + max(initializer_list<T> t); + +template<class T, class Compare> + T + max(initializer_list<T> t, Compare comp); + +template<class ForwardIterator> + pair<ForwardIterator, ForwardIterator> + minmax_element(ForwardIterator first, ForwardIterator last); + +template<class ForwardIterator, class Compare> + pair<ForwardIterator, ForwardIterator> + minmax_element(ForwardIterator first, ForwardIterator last, Compare comp); + +template<class T> + pair<const T&, const T&> + minmax(const T& a, const T& b); + +template<class T, class Compare> + pair<const T&, const T&> + minmax(const T& a, const T& b, Compare comp); + +template<class T> + pair<T, T> + minmax(initializer_list<T> t); + +template<class T, class Compare> + pair<T, T> + minmax(initializer_list<T> t, Compare comp); + +template <class InputIterator1, class InputIterator2> + bool + lexicographical_compare(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2); + +template <class InputIterator1, class InputIterator2, class Compare> + bool + lexicographical_compare(InputIterator1 first1, InputIterator1 last1, + InputIterator2 first2, InputIterator2 last2, Compare comp); + +template <class BidirectionalIterator> + bool + next_permutation(BidirectionalIterator first, BidirectionalIterator last); + +template <class BidirectionalIterator, class Compare> + bool + next_permutation(BidirectionalIterator first, BidirectionalIterator last, Compare comp); + +template <class BidirectionalIterator> + bool + prev_permutation(BidirectionalIterator first, BidirectionalIterator last); + +template <class BidirectionalIterator, class Compare> + bool + prev_permutation(BidirectionalIterator first, BidirectionalIterator last, Compare comp); + +} // std + +*/ + +#include <__config> +#include <initializer_list> +#include <type_traits> +#include <cstring> +#include <utility> +#include <memory> +#include <iterator> +#include <cstdlib> + +#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) +#pragma GCC system_header +#endif + +_LIBCPP_BEGIN_NAMESPACE_STD + +template <class _T1, class _T2 = _T1> +struct __equal_to +{ + _LIBCPP_INLINE_VISIBILITY bool operator()(const _T1& __x, const _T1& __y) const {return __x == __y;} + _LIBCPP_INLINE_VISIBILITY bool operator()(const _T1& __x, const _T2& __y) const {return __x == __y;} + _LIBCPP_INLINE_VISIBILITY bool operator()(const _T2& __x, const _T1& __y) const {return __x == __y;} + _LIBCPP_INLINE_VISIBILITY bool operator()(const _T2& __x, const _T2& __y) const {return __x == __y;} +}; + +template <class _T1> +struct __equal_to<_T1, _T1> +{ + _LIBCPP_INLINE_VISIBILITY bool operator()(const _T1& __x, const _T1& __y) const {return __x == __y;} +}; + +template <class _T1> +struct __equal_to<const _T1, _T1> +{ + _LIBCPP_INLINE_VISIBILITY bool operator()(const _T1& __x, const _T1& __y) const {return __x == __y;} +}; + +template <class _T1> +struct __equal_to<_T1, const _T1> +{ + _LIBCPP_INLINE_VISIBILITY bool operator()(const _T1& __x, const _T1& __y) const {return __x == __y;} +}; + +template <class _T1, class _T2 = _T1> +struct __less +{ + _LIBCPP_INLINE_VISIBILITY bool operator()(const _T1& __x, const _T1& __y) const {return __x < __y;} + _LIBCPP_INLINE_VISIBILITY bool operator()(const _T1& __x, const _T2& __y) const {return __x < __y;} + _LIBCPP_INLINE_VISIBILITY bool operator()(const _T2& __x, const _T1& __y) const {return __x < __y;} + _LIBCPP_INLINE_VISIBILITY bool operator()(const _T2& __x, const _T2& __y) const {return __x < __y;} +}; + +template <class _T1> +struct __less<_T1, _T1> +{ + _LIBCPP_INLINE_VISIBILITY bool operator()(const _T1& __x, const _T1& __y) const {return __x < __y;} +}; + +template <class _T1> +struct __less<const _T1, _T1> +{ + _LIBCPP_INLINE_VISIBILITY bool operator()(const _T1& __x, const _T1& __y) const {return __x < __y;} +}; + +template <class _T1> +struct __less<_T1, const _T1> +{ + _LIBCPP_INLINE_VISIBILITY bool operator()(const _T1& __x, const _T1& __y) const {return __x < __y;} +}; + +template <class _Predicate> +class __negate +{ +private: + _Predicate __p_; +public: + _LIBCPP_INLINE_VISIBILITY __negate() {} + + _LIBCPP_INLINE_VISIBILITY + explicit __negate(_Predicate __p) : __p_(__p) {} + + template <class _T1> + _LIBCPP_INLINE_VISIBILITY + bool operator()(const _T1& __x) {return !__p_(__x);} + + template <class _T1, class _T2> + _LIBCPP_INLINE_VISIBILITY + bool operator()(const _T1& __x, const _T2& __y) {return !__p_(__x, __y);} +}; + +#ifdef _LIBCPP_DEBUG2 + +template <class _Compare> +struct __debug_less +{ + _Compare __comp_; + __debug_less(_Compare& __c) : __comp_(__c) {} + template <class _Tp, class _Up> + bool operator()(const _Tp& __x, const _Up& __y) + { + bool __r = __comp_(__x, __y); + if (__r) + _LIBCPP_ASSERT(!__comp_(__y, __x), "Comparator does not induce a strict weak ordering"); + return __r; + } +}; + +#endif // _LIBCPP_DEBUG2 + +// Precondition: __x != 0 +inline _LIBCPP_INLINE_VISIBILITY unsigned __ctz(unsigned __x) {return __builtin_ctz (__x);} +inline _LIBCPP_INLINE_VISIBILITY unsigned long __ctz(unsigned long __x) {return __builtin_ctzl (__x);} +inline _LIBCPP_INLINE_VISIBILITY unsigned long long __ctz(unsigned long long __x) {return __builtin_ctzll(__x);} + +// Precondition: __x != 0 +inline _LIBCPP_INLINE_VISIBILITY unsigned __clz(unsigned __x) {return __builtin_clz (__x);} +inline _LIBCPP_INLINE_VISIBILITY unsigned long __clz(unsigned long __x) {return __builtin_clzl (__x);} +inline _LIBCPP_INLINE_VISIBILITY unsigned long long __clz(unsigned long long __x) {return __builtin_clzll(__x);} + +inline _LIBCPP_INLINE_VISIBILITY int __pop_count(unsigned __x) {return __builtin_popcount (__x);} +inline _LIBCPP_INLINE_VISIBILITY int __pop_count(unsigned long __x) {return __builtin_popcountl (__x);} +inline _LIBCPP_INLINE_VISIBILITY int __pop_count(unsigned long long __x) {return __builtin_popcountll(__x);} + +// all_of + +template <class _InputIterator, class _Predicate> +inline _LIBCPP_INLINE_VISIBILITY +bool +all_of(_InputIterator __first, _InputIterator __last, _Predicate __pred) +{ + for (; __first != __last; ++__first) + if (!__pred(*__first)) + return false; + return true; +} + +// any_of + +template <class _InputIterator, class _Predicate> +inline _LIBCPP_INLINE_VISIBILITY +bool +any_of(_InputIterator __first, _InputIterator __last, _Predicate __pred) +{ + for (; __first != __last; ++__first) + if (__pred(*__first)) + return true; + return false; +} + +// none_of + +template <class _InputIterator, class _Predicate> +inline _LIBCPP_INLINE_VISIBILITY +bool +none_of(_InputIterator __first, _InputIterator __last, _Predicate __pred) +{ + for (; __first != __last; ++__first) + if (__pred(*__first)) + return false; + return true; +} + +// for_each + +template <class _InputIterator, class _Function> +inline _LIBCPP_INLINE_VISIBILITY +_Function +for_each(_InputIterator __first, _InputIterator __last, _Function __f) +{ + for (; __first != __last; ++__first) + __f(*__first); + return _VSTD::move(__f); +} + +// find + +template <class _InputIterator, class _Tp> +inline _LIBCPP_INLINE_VISIBILITY +_InputIterator +find(_InputIterator __first, _InputIterator __last, const _Tp& __value_) +{ + for (; __first != __last; ++__first) + if (*__first == __value_) + break; + return __first; +} + +// find_if + +template <class _InputIterator, class _Predicate> +inline _LIBCPP_INLINE_VISIBILITY +_InputIterator +find_if(_InputIterator __first, _InputIterator __last, _Predicate __pred) +{ + for (; __first != __last; ++__first) + if (__pred(*__first)) + break; + return __first; +} + +// find_if_not + +template<class _InputIterator, class _Predicate> +inline _LIBCPP_INLINE_VISIBILITY +_InputIterator +find_if_not(_InputIterator __first, _InputIterator __last, _Predicate __pred) +{ + for (; __first != __last; ++__first) + if (!__pred(*__first)) + break; + return __first; +} + +// find_end + +template <class _BinaryPredicate, class _ForwardIterator1, class _ForwardIterator2> +_ForwardIterator1 +__find_end(_ForwardIterator1 __first1, _ForwardIterator1 __last1, + _ForwardIterator2 __first2, _ForwardIterator2 __last2, _BinaryPredicate __pred, + forward_iterator_tag, forward_iterator_tag) +{ + // modeled after search algorithm + _ForwardIterator1 __r = __last1; // __last1 is the "default" answer + if (__first2 == __last2) + return __r; + while (true) + { + while (true) + { + if (__first1 == __last1) // if source exhausted return last correct answer + return __r; // (or __last1 if never found) + if (__pred(*__first1, *__first2)) + break; + ++__first1; + } + // *__first1 matches *__first2, now match elements after here + _ForwardIterator1 __m1 = __first1; + _ForwardIterator2 __m2 = __first2; + while (true) + { + if (++__m2 == __last2) + { // Pattern exhaused, record answer and search for another one + __r = __first1; + ++__first1; + break; + } + if (++__m1 == __last1) // Source exhausted, return last answer + return __r; + if (!__pred(*__m1, *__m2)) // mismatch, restart with a new __first + { + ++__first1; + break; + } // else there is a match, check next elements + } + } +} + +template <class _BinaryPredicate, class _BidirectionalIterator1, class _BidirectionalIterator2> +_BidirectionalIterator1 +__find_end(_BidirectionalIterator1 __first1, _BidirectionalIterator1 __last1, + _BidirectionalIterator2 __first2, _BidirectionalIterator2 __last2, _BinaryPredicate __pred, + bidirectional_iterator_tag, bidirectional_iterator_tag) +{ + // modeled after search algorithm (in reverse) + if (__first2 == __last2) + return __last1; // Everything matches an empty sequence + _BidirectionalIterator1 __l1 = __last1; + _BidirectionalIterator2 __l2 = __last2; + --__l2; + while (true) + { + // Find last element in sequence 1 that matchs *(__last2-1), with a mininum of loop checks + while (true) + { + if (__first1 == __l1) // return __last1 if no element matches *__first2 + return __last1; + if (__pred(*--__l1, *__l2)) + break; + } + // *__l1 matches *__l2, now match elements before here + _BidirectionalIterator1 __m1 = __l1; + _BidirectionalIterator2 __m2 = __l2; + while (true) + { + if (__m2 == __first2) // If pattern exhausted, __m1 is the answer (works for 1 element pattern) + return __m1; + if (__m1 == __first1) // Otherwise if source exhaused, pattern not found + return __last1; + if (!__pred(*--__m1, *--__m2)) // if there is a mismatch, restart with a new __l1 + { + break; + } // else there is a match, check next elements + } + } +} + +template <class _BinaryPredicate, class _RandomAccessIterator1, class _RandomAccessIterator2> +_RandomAccessIterator1 +__find_end(_RandomAccessIterator1 __first1, _RandomAccessIterator1 __last1, + _RandomAccessIterator2 __first2, _RandomAccessIterator2 __last2, _BinaryPredicate __pred, + random_access_iterator_tag, random_access_iterator_tag) +{ + // Take advantage of knowing source and pattern lengths. Stop short when source is smaller than pattern + typename iterator_traits<_RandomAccessIterator2>::difference_type __len2 = __last2 - __first2; + if (__len2 == 0) + return __last1; + typename iterator_traits<_RandomAccessIterator1>::difference_type __len1 = __last1 - __first1; + if (__len1 < __len2) + return __last1; + const _RandomAccessIterator1 __s = __first1 + (__len2 - 1); // End of pattern match can't go before here + _RandomAccessIterator1 __l1 = __last1; + _RandomAccessIterator2 __l2 = __last2; + --__l2; + while (true) + { + while (true) + { + if (__s == __l1) + return __last1; + if (__pred(*--__l1, *__l2)) + break; + } + _RandomAccessIterator1 __m1 = __l1; + _RandomAccessIterator2 __m2 = __l2; + while (true) + { + if (__m2 == __first2) + return __m1; + // no need to check range on __m1 because __s guarantees we have enough source + if (!__pred(*--__m1, *--__m2)) + { + break; + } + } + } +} + +template <class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate> +inline _LIBCPP_INLINE_VISIBILITY +_ForwardIterator1 +find_end(_ForwardIterator1 __first1, _ForwardIterator1 __last1, + _ForwardIterator2 __first2, _ForwardIterator2 __last2, _BinaryPredicate __pred) +{ + return _VSTD::__find_end<typename add_lvalue_reference<_BinaryPredicate>::type> + (__first1, __last1, __first2, __last2, __pred, + typename iterator_traits<_ForwardIterator1>::iterator_category(), + typename iterator_traits<_ForwardIterator2>::iterator_category()); +} + +template <class _ForwardIterator1, class _ForwardIterator2> +inline _LIBCPP_INLINE_VISIBILITY +_ForwardIterator1 +find_end(_ForwardIterator1 __first1, _ForwardIterator1 __last1, + _ForwardIterator2 __first2, _ForwardIterator2 __last2) +{ + typedef typename iterator_traits<_ForwardIterator1>::value_type __v1; + typedef typename iterator_traits<_ForwardIterator2>::value_type __v2; + return _VSTD::find_end(__first1, __last1, __first2, __last2, __equal_to<__v1, __v2>()); +} + +// find_first_of + +template <class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate> +_ForwardIterator1 +find_first_of(_ForwardIterator1 __first1, _ForwardIterator1 __last1, + _ForwardIterator2 __first2, _ForwardIterator2 __last2, _BinaryPredicate __pred) +{ + for (; __first1 != __last1; ++__first1) + for (_ForwardIterator2 __j = __first2; __j != __last2; ++__j) + if (__pred(*__first1, *__j)) + return __first1; + return __last1; +} + +template <class _ForwardIterator1, class _ForwardIterator2> +inline _LIBCPP_INLINE_VISIBILITY +_ForwardIterator1 +find_first_of(_ForwardIterator1 __first1, _ForwardIterator1 __last1, + _ForwardIterator2 __first2, _ForwardIterator2 __last2) +{ + typedef typename iterator_traits<_ForwardIterator1>::value_type __v1; + typedef typename iterator_traits<_ForwardIterator2>::value_type __v2; + return _VSTD::find_first_of(__first1, __last1, __first2, __last2, __equal_to<__v1, __v2>()); +} + +// adjacent_find + +template <class _ForwardIterator, class _BinaryPredicate> +inline _LIBCPP_INLINE_VISIBILITY +_ForwardIterator +adjacent_find(_ForwardIterator __first, _ForwardIterator __last, _BinaryPredicate __pred) +{ + if (__first != __last) + { + _ForwardIterator __i = __first; + while (++__i != __last) + { + if (__pred(*__first, *__i)) + return __first; + __first = __i; + } + } + return __last; +} + +template <class _ForwardIterator> +inline _LIBCPP_INLINE_VISIBILITY +_ForwardIterator +adjacent_find(_ForwardIterator __first, _ForwardIterator __last) +{ + typedef typename iterator_traits<_ForwardIterator>::value_type __v; + return _VSTD::adjacent_find(__first, __last, __equal_to<__v>()); +} + +// count + +template <class _InputIterator, class _Tp> +inline _LIBCPP_INLINE_VISIBILITY +typename iterator_traits<_InputIterator>::difference_type +count(_InputIterator __first, _InputIterator __last, const _Tp& __value_) +{ + typename iterator_traits<_InputIterator>::difference_type __r(0); + for (; __first != __last; ++__first) + if (*__first == __value_) + ++__r; + return __r; +} + +// count_if + +template <class _InputIterator, class _Predicate> +inline _LIBCPP_INLINE_VISIBILITY +typename iterator_traits<_InputIterator>::difference_type +count_if(_InputIterator __first, _InputIterator __last, _Predicate __pred) +{ + typename iterator_traits<_InputIterator>::difference_type __r(0); + for (; __first != __last; ++__first) + if (__pred(*__first)) + ++__r; + return __r; +} + +// mismatch + +template <class _InputIterator1, class _InputIterator2, class _BinaryPredicate> +inline _LIBCPP_INLINE_VISIBILITY +pair<_InputIterator1, _InputIterator2> +mismatch(_InputIterator1 __first1, _InputIterator1 __last1, + _InputIterator2 __first2, _BinaryPredicate __pred) +{ + for (; __first1 != __last1; ++__first1, ++__first2) + if (!__pred(*__first1, *__first2)) + break; + return pair<_InputIterator1, _InputIterator2>(__first1, __first2); +} + +template <class _InputIterator1, class _InputIterator2> +inline _LIBCPP_INLINE_VISIBILITY +pair<_InputIterator1, _InputIterator2> +mismatch(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2) +{ + typedef typename iterator_traits<_InputIterator1>::value_type __v1; + typedef typename iterator_traits<_InputIterator2>::value_type __v2; + return _VSTD::mismatch(__first1, __last1, __first2, __equal_to<__v1, __v2>()); +} + +// equal + +template <class _InputIterator1, class _InputIterator2, class _BinaryPredicate> +inline _LIBCPP_INLINE_VISIBILITY +bool +equal(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, _BinaryPredicate __pred) +{ + for (; __first1 != __last1; ++__first1, ++__first2) + if (!__pred(*__first1, *__first2)) + return false; + return true; +} + +template <class _InputIterator1, class _InputIterator2> +inline _LIBCPP_INLINE_VISIBILITY +bool +equal(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2) +{ + typedef typename iterator_traits<_InputIterator1>::value_type __v1; + typedef typename iterator_traits<_InputIterator2>::value_type __v2; + return _VSTD::equal(__first1, __last1, __first2, __equal_to<__v1, __v2>()); +} + +// is_permutation + +template<class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate> +bool +is_permutation(_ForwardIterator1 __first1, _ForwardIterator1 __last1, + _ForwardIterator2 __first2, _BinaryPredicate __pred) +{ + // shorten sequences as much as possible by lopping of any equal parts + for (; __first1 != __last1; ++__first1, ++__first2) + if (!__pred(*__first1, *__first2)) + goto __not_done; + return true; +__not_done: + // __first1 != __last1 && *__first1 != *__first2 + typedef typename iterator_traits<_ForwardIterator1>::difference_type _D1; + _D1 __l1 = _VSTD::distance(__first1, __last1); + if (__l1 == _D1(1)) + return false; + _ForwardIterator2 __last2 = _VSTD::next(__first2, __l1); + // For each element in [f1, l1) see if there are the same number of + // equal elements in [f2, l2) + for (_ForwardIterator1 __i = __first1; __i != __last1; ++__i) + { + // Have we already counted the number of *__i in [f1, l1)? + for (_ForwardIterator1 __j = __first1; __j != __i; ++__j) + if (__pred(*__j, *__i)) + goto __next_iter; + { + // Count number of *__i in [f2, l2) + _D1 __c2 = 0; + for (_ForwardIterator2 __j = __first2; __j != __last2; ++__j) + if (__pred(*__i, *__j)) + ++__c2; + if (__c2 == 0) + return false; + // Count number of *__i in [__i, l1) (we can start with 1) + _D1 __c1 = 1; + for (_ForwardIterator1 __j = _VSTD::next(__i); __j != __last1; ++__j) + if (__pred(*__i, *__j)) + ++__c1; + if (__c1 != __c2) + return false; + } +__next_iter:; + } + return true; +} + +template<class _ForwardIterator1, class _ForwardIterator2> +inline _LIBCPP_INLINE_VISIBILITY +bool +is_permutation(_ForwardIterator1 __first1, _ForwardIterator1 __last1, + _ForwardIterator2 __first2) +{ + typedef typename iterator_traits<_ForwardIterator1>::value_type __v1; + typedef typename iterator_traits<_ForwardIterator2>::value_type __v2; + return _VSTD::is_permutation(__first1, __last1, __first2, __equal_to<__v1, __v2>()); +} + +// search + +template <class _BinaryPredicate, class _ForwardIterator1, class _ForwardIterator2> +_ForwardIterator1 +__search(_ForwardIterator1 __first1, _ForwardIterator1 __last1, + _ForwardIterator2 __first2, _ForwardIterator2 __last2, _BinaryPredicate __pred, + forward_iterator_tag, forward_iterator_tag) +{ + if (__first2 == __last2) + return __first1; // Everything matches an empty sequence + while (true) + { + // Find first element in sequence 1 that matchs *__first2, with a mininum of loop checks + while (true) + { + if (__first1 == __last1) // return __last1 if no element matches *__first2 + return __last1; + if (__pred(*__first1, *__first2)) + break; + ++__first1; + } + // *__first1 matches *__first2, now match elements after here + _ForwardIterator1 __m1 = __first1; + _ForwardIterator2 __m2 = __first2; + while (true) + { + if (++__m2 == __last2) // If pattern exhausted, __first1 is the answer (works for 1 element pattern) + return __first1; + if (++__m1 == __last1) // Otherwise if source exhaused, pattern not found + return __last1; + if (!__pred(*__m1, *__m2)) // if there is a mismatch, restart with a new __first1 + { + ++__first1; + break; + } // else there is a match, check next elements + } + } +} + +template <class _BinaryPredicate, class _RandomAccessIterator1, class _RandomAccessIterator2> +_RandomAccessIterator1 +__search(_RandomAccessIterator1 __first1, _RandomAccessIterator1 __last1, + _RandomAccessIterator2 __first2, _RandomAccessIterator2 __last2, _BinaryPredicate __pred, + random_access_iterator_tag, random_access_iterator_tag) +{ + typedef typename std::iterator_traits<_RandomAccessIterator1>::difference_type _D1; + typedef typename std::iterator_traits<_RandomAccessIterator2>::difference_type _D2; + // Take advantage of knowing source and pattern lengths. Stop short when source is smaller than pattern + _D2 __len2 = __last2 - __first2; + if (__len2 == 0) + return __first1; + _D1 __len1 = __last1 - __first1; + if (__len1 < __len2) + return __last1; + const _RandomAccessIterator1 __s = __last1 - (__len2 - 1); // Start of pattern match can't go beyond here + while (true) + { +#if !_LIBCPP_UNROLL_LOOPS + while (true) + { + if (__first1 == __s) + return __last1; + if (__pred(*__first1, *__first2)) + break; + ++__first1; + } +#else // !_LIBCPP_UNROLL_LOOPS + for (_D1 __loop_unroll = (__s - __first1) / 4; __loop_unroll > 0; --__loop_unroll) + { + if (__pred(*__first1, *__first2)) + goto __phase2; + if (__pred(*++__first1, *__first2)) + goto __phase2; + if (__pred(*++__first1, *__first2)) + goto __phase2; + if (__pred(*++__first1, *__first2)) + goto __phase2; + ++__first1; + } + switch (__s - __first1) + { + case 3: + if (__pred(*__first1, *__first2)) + break; + ++__first1; + case 2: + if (__pred(*__first1, *__first2)) + break; + ++__first1; + case 1: + if (__pred(*__first1, *__first2)) + break; + case 0: + return __last1; + } + __phase2: +#endif // !_LIBCPP_UNROLL_LOOPS + _RandomAccessIterator1 __m1 = __first1; + _RandomAccessIterator2 __m2 = __first2; +#if !_LIBCPP_UNROLL_LOOPS + while (true) + { + if (++__m2 == __last2) + return __first1; + ++__m1; // no need to check range on __m1 because __s guarantees we have enough source + if (!__pred(*__m1, *__m2)) + { + ++__first1; + break; + } + } +#else // !_LIBCPP_UNROLL_LOOPS + ++__m2; + ++__m1; + for (_D2 __loop_unroll = (__last2 - __m2) / 4; __loop_unroll > 0; --__loop_unroll) + { + if (!__pred(*__m1, *__m2)) + goto __continue; + if (!__pred(*++__m1, *++__m2)) + goto __continue; + if (!__pred(*++__m1, *++__m2)) + goto __continue; + if (!__pred(*++__m1, *++__m2)) + goto __continue; + ++__m1; + ++__m2; + } + switch (__last2 - __m2) + { + case 3: + if (!__pred(*__m1, *__m2)) + break; + ++__m1; + ++__m2; + case 2: + if (!__pred(*__m1, *__m2)) + break; + ++__m1; + ++__m2; + case 1: + if (!__pred(*__m1, *__m2)) + break; + case 0: + return __first1; + } + __continue: + ++__first1; +#endif // !_LIBCPP_UNROLL_LOOPS + } +} + +template <class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate> +inline _LIBCPP_INLINE_VISIBILITY +_ForwardIterator1 +search(_ForwardIterator1 __first1, _ForwardIterator1 __last1, + _ForwardIterator2 __first2, _ForwardIterator2 __last2, _BinaryPredicate __pred) +{ + return _VSTD::__search<typename add_lvalue_reference<_BinaryPredicate>::type> + (__first1, __last1, __first2, __last2, __pred, + typename std::iterator_traits<_ForwardIterator1>::iterator_category(), + typename std::iterator_traits<_ForwardIterator2>::iterator_category()); +} + +template <class _ForwardIterator1, class _ForwardIterator2> +inline _LIBCPP_INLINE_VISIBILITY +_ForwardIterator1 +search(_ForwardIterator1 __first1, _ForwardIterator1 __last1, + _ForwardIterator2 __first2, _ForwardIterator2 __last2) +{ + typedef typename std::iterator_traits<_ForwardIterator1>::value_type __v1; + typedef typename std::iterator_traits<_ForwardIterator2>::value_type __v2; + return _VSTD::search(__first1, __last1, __first2, __last2, __equal_to<__v1, __v2>()); +} + +// search_n + +template <class _BinaryPredicate, class _ForwardIterator, class _Size, class _Tp> +_ForwardIterator +__search_n(_ForwardIterator __first, _ForwardIterator __last, + _Size __count, const _Tp& __value_, _BinaryPredicate __pred, forward_iterator_tag) +{ + if (__count <= 0) + return __first; + while (true) + { + // Find first element in sequence that matchs __value_, with a mininum of loop checks + while (true) + { + if (__first == __last) // return __last if no element matches __value_ + return __last; + if (__pred(*__first, __value_)) + break; + ++__first; + } + // *__first matches __value_, now match elements after here + _ForwardIterator __m = __first; + _Size __c(0); + while (true) + { + if (++__c == __count) // If pattern exhausted, __first is the answer (works for 1 element pattern) + return __first; + if (++__m == __last) // Otherwise if source exhaused, pattern not found + return __last; + if (!__pred(*__m, __value_)) // if there is a mismatch, restart with a new __first + { + __first = __m; + ++__first; + break; + } // else there is a match, check next elements + } + } +} + +template <class _BinaryPredicate, class _RandomAccessIterator, class _Size, class _Tp> +_RandomAccessIterator +__search_n(_RandomAccessIterator __first, _RandomAccessIterator __last, + _Size __count, const _Tp& __value_, _BinaryPredicate __pred, random_access_iterator_tag) +{ + if (__count <= 0) + return __first; + _Size __len = static_cast<_Size>(__last - __first); + if (__len < __count) + return __last; + const _RandomAccessIterator __s = __last - (__count - 1); // Start of pattern match can't go beyond here + while (true) + { + // Find first element in sequence that matchs __value_, with a mininum of loop checks + while (true) + { + if (__first == __s) // return __last if no element matches __value_ + return __last; + if (__pred(*__first, __value_)) + break; + ++__first; + } + // *__first matches __value_, now match elements after here + _RandomAccessIterator __m = __first; + _Size __c(0); + while (true) + { + if (++__c == __count) // If pattern exhausted, __first is the answer (works for 1 element pattern) + return __first; + ++__m; // no need to check range on __m because __s guarantees we have enough source + if (!__pred(*__m, __value_)) // if there is a mismatch, restart with a new __first + { + __first = __m; + ++__first; + break; + } // else there is a match, check next elements + } + } +} + +template <class _ForwardIterator, class _Size, class _Tp, class _BinaryPredicate> +inline _LIBCPP_INLINE_VISIBILITY +_ForwardIterator +search_n(_ForwardIterator __first, _ForwardIterator __last, + _Size __count, const _Tp& __value_, _BinaryPredicate __pred) +{ + return _VSTD::__search_n<typename add_lvalue_reference<_BinaryPredicate>::type> + (__first, __last, __count, __value_, __pred, typename iterator_traits<_ForwardIterator>::iterator_category()); +} + +template <class _ForwardIterator, class _Size, class _Tp> +inline _LIBCPP_INLINE_VISIBILITY +_ForwardIterator +search_n(_ForwardIterator __first, _ForwardIterator __last, _Size __count, const _Tp& __value_) +{ + typedef typename iterator_traits<_ForwardIterator>::value_type __v; + return _VSTD::search_n(__first, __last, __count, __value_, __equal_to<__v, _Tp>()); +} + +// copy + +template <class _Iter> +struct __libcpp_is_trivial_iterator +{ + static const bool value = is_pointer<_Iter>::value; +}; + +template <class _Iter> +struct __libcpp_is_trivial_iterator<move_iterator<_Iter> > +{ + static const bool value = is_pointer<_Iter>::value; +}; + +template <class _Iter> +struct __libcpp_is_trivial_iterator<__wrap_iter<_Iter> > +{ + static const bool value = is_pointer<_Iter>::value; +}; + +template <class _Iter> +inline _LIBCPP_INLINE_VISIBILITY +_Iter +__unwrap_iter(_Iter __i) +{ + return __i; +} + +template <class _Tp> +inline _LIBCPP_INLINE_VISIBILITY +typename enable_if +< + is_trivially_copy_assignable<_Tp>::value, + _Tp* +>::type +__unwrap_iter(move_iterator<_Tp*> __i) +{ + return __i.base(); +} + +template <class _Tp> +inline _LIBCPP_INLINE_VISIBILITY +typename enable_if +< + is_trivially_copy_assignable<_Tp>::value, + _Tp* +>::type +__unwrap_iter(__wrap_iter<_Tp*> __i) +{ + return __i.base(); +} + +template <class _InputIterator, class _OutputIterator> +inline _LIBCPP_INLINE_VISIBILITY +_OutputIterator +__copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result) +{ + for (; __first != __last; ++__first, ++__result) + *__result = *__first; + return __result; +} + +template <class _Tp, class _Up> +inline _LIBCPP_INLINE_VISIBILITY +typename enable_if +< + is_same<typename remove_const<_Tp>::type, _Up>::value && + is_trivially_copy_assignable<_Up>::value, + _Up* +>::type +__copy(_Tp* __first, _Tp* __last, _Up* __result) +{ + const size_t __n = static_cast<size_t>(__last - __first); + _VSTD::memmove(__result, __first, __n * sizeof(_Up)); + return __result + __n; +} + +template <class _InputIterator, class _OutputIterator> +inline _LIBCPP_INLINE_VISIBILITY +_OutputIterator +copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result) +{ + return _VSTD::__copy(__unwrap_iter(__first), __unwrap_iter(__last), __unwrap_iter(__result)); +} + +// copy_backward + +template <class _InputIterator, class _OutputIterator> +inline _LIBCPP_INLINE_VISIBILITY +_OutputIterator +__copy_backward(_InputIterator __first, _InputIterator __last, _OutputIterator __result) +{ + while (__first != __last) + *--__result = *--__last; + return __result; +} + +template <class _Tp, class _Up> +inline _LIBCPP_INLINE_VISIBILITY +typename enable_if +< + is_same<typename remove_const<_Tp>::type, _Up>::value && + is_trivially_copy_assignable<_Up>::value, + _Up* +>::type +__copy_backward(_Tp* __first, _Tp* __last, _Up* __result) +{ + const size_t __n = static_cast<size_t>(__last - __first); + __result -= __n; + _VSTD::memmove(__result, __first, __n * sizeof(_Up)); + return __result; +} + +template <class _BidirectionalIterator1, class _BidirectionalIterator2> +inline _LIBCPP_INLINE_VISIBILITY +_BidirectionalIterator2 +copy_backward(_BidirectionalIterator1 __first, _BidirectionalIterator1 __last, + _BidirectionalIterator2 __result) +{ + return _VSTD::__copy_backward(__unwrap_iter(__first), __unwrap_iter(__last), __unwrap_iter(__result)); +} + +// copy_if + +template<class _InputIterator, class _OutputIterator, class _Predicate> +inline _LIBCPP_INLINE_VISIBILITY +_OutputIterator +copy_if(_InputIterator __first, _InputIterator __last, + _OutputIterator __result, _Predicate __pred) +{ + for (; __first != __last; ++__first) + { + if (__pred(*__first)) + { + *__result = *__first; + ++__result; + } + } + return __result; +} + +// copy_n + +template<class _InputIterator, class _Size, class _OutputIterator> +inline _LIBCPP_INLINE_VISIBILITY +typename enable_if +< + __is_input_iterator<_InputIterator>::value && + !__is_random_access_iterator<_InputIterator>::value, + _OutputIterator +>::type +copy_n(_InputIterator __first, _Size __n, _OutputIterator __result) +{ + if (__n > 0) + { + *__result = *__first; + ++__result; + for (--__n; __n > 0; --__n) + { + ++__first; + *__result = *__first; + ++__result; + } + } + return __result; +} + +template<class _InputIterator, class _Size, class _OutputIterator> +inline _LIBCPP_INLINE_VISIBILITY +typename enable_if +< + __is_random_access_iterator<_InputIterator>::value, + _OutputIterator +>::type +copy_n(_InputIterator __first, _Size __n, _OutputIterator __result) +{ + return _VSTD::copy(__first, __first + __n, __result); +} + +// move + +template <class _InputIterator, class _OutputIterator> +inline _LIBCPP_INLINE_VISIBILITY +_OutputIterator +__move(_InputIterator __first, _InputIterator __last, _OutputIterator __result) +{ + for (; __first != __last; ++__first, ++__result) + *__result = _VSTD::move(*__first); + return __result; +} + +template <class _Tp, class _Up> +inline _LIBCPP_INLINE_VISIBILITY +typename enable_if +< + is_same<typename remove_const<_Tp>::type, _Up>::value && + is_trivially_copy_assignable<_Up>::value, + _Up* +>::type +__move(_Tp* __first, _Tp* __last, _Up* __result) +{ + const size_t __n = static_cast<size_t>(__last - __first); + _VSTD::memmove(__result, __first, __n * sizeof(_Up)); + return __result + __n; +} + +template <class _InputIterator, class _OutputIterator> +inline _LIBCPP_INLINE_VISIBILITY +_OutputIterator +move(_InputIterator __first, _InputIterator __last, _OutputIterator __result) +{ + return _VSTD::__move(__unwrap_iter(__first), __unwrap_iter(__last), __unwrap_iter(__result)); +} + +// move_backward + +template <class _InputIterator, class _OutputIterator> +inline _LIBCPP_INLINE_VISIBILITY +_OutputIterator +__move_backward(_InputIterator __first, _InputIterator __last, _OutputIterator __result) +{ + while (__first != __last) + *--__result = _VSTD::move(*--__last); + return __result; +} + +template <class _Tp, class _Up> +inline _LIBCPP_INLINE_VISIBILITY +typename enable_if +< + is_same<typename remove_const<_Tp>::type, _Up>::value && + is_trivially_copy_assignable<_Up>::value, + _Up* +>::type +__move_backward(_Tp* __first, _Tp* __last, _Up* __result) +{ + const size_t __n = static_cast<size_t>(__last - __first); + __result -= __n; + _VSTD::memmove(__result, __first, __n * sizeof(_Up)); + return __result; +} + +template <class _BidirectionalIterator1, class _BidirectionalIterator2> +inline _LIBCPP_INLINE_VISIBILITY +_BidirectionalIterator2 +move_backward(_BidirectionalIterator1 __first, _BidirectionalIterator1 __last, + _BidirectionalIterator2 __result) +{ + return _VSTD::__move_backward(__unwrap_iter(__first), __unwrap_iter(__last), __unwrap_iter(__result)); +} + +// iter_swap + +// moved to <type_traits> for better swap / noexcept support + +// transform + +template <class _InputIterator, class _OutputIterator, class _UnaryOperation> +inline _LIBCPP_INLINE_VISIBILITY +_OutputIterator +transform(_InputIterator __first, _InputIterator __last, _OutputIterator __result, _UnaryOperation __op) +{ + for (; __first != __last; ++__first, ++__result) + *__result = __op(*__first); + return __result; +} + +template <class _InputIterator1, class _InputIterator2, class _OutputIterator, class _BinaryOperation> +inline _LIBCPP_INLINE_VISIBILITY +_OutputIterator +transform(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, + _OutputIterator __result, _BinaryOperation __binary_op) +{ + for (; __first1 != __last1; ++__first1, ++__first2, ++__result) + *__result = __binary_op(*__first1, *__first2); + return __result; +} + +// replace + +template <class _ForwardIterator, class _Tp> +inline _LIBCPP_INLINE_VISIBILITY +void +replace(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __old_value, const _Tp& __new_value) +{ + for (; __first != __last; ++__first) + if (*__first == __old_value) + *__first = __new_value; +} + +// replace_if + +template <class _ForwardIterator, class _Predicate, class _Tp> +inline _LIBCPP_INLINE_VISIBILITY +void +replace_if(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred, const _Tp& __new_value) +{ + for (; __first != __last; ++__first) + if (__pred(*__first)) + *__first = __new_value; +} + +// replace_copy + +template <class _InputIterator, class _OutputIterator, class _Tp> +inline _LIBCPP_INLINE_VISIBILITY +_OutputIterator +replace_copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result, + const _Tp& __old_value, const _Tp& __new_value) +{ + for (; __first != __last; ++__first, ++__result) + if (*__first == __old_value) + *__result = __new_value; + else + *__result = *__first; + return __result; +} + +// replace_copy_if + +template <class _InputIterator, class _OutputIterator, class _Predicate, class _Tp> +inline _LIBCPP_INLINE_VISIBILITY +_OutputIterator +replace_copy_if(_InputIterator __first, _InputIterator __last, _OutputIterator __result, + _Predicate __pred, const _Tp& __new_value) +{ + for (; __first != __last; ++__first, ++__result) + if (__pred(*__first)) + *__result = __new_value; + else + *__result = *__first; + return __result; +} + +// fill_n + +template <class _OutputIterator, class _Size, class _Tp> +inline _LIBCPP_INLINE_VISIBILITY +_OutputIterator +__fill_n(_OutputIterator __first, _Size __n, const _Tp& __value_, false_type) +{ + for (; __n > 0; ++__first, --__n) + *__first = __value_; + return __first; +} + +template <class _OutputIterator, class _Size, class _Tp> +inline _LIBCPP_INLINE_VISIBILITY +_OutputIterator +__fill_n(_OutputIterator __first, _Size __n, const _Tp& __value_, true_type) +{ + if (__n > 0) + _VSTD::memset(__first, (unsigned char)__value_, (size_t)(__n)); + return __first + __n; +} + +template <class _OutputIterator, class _Size, class _Tp> +inline _LIBCPP_INLINE_VISIBILITY +_OutputIterator +fill_n(_OutputIterator __first, _Size __n, const _Tp& __value_) +{ + return _VSTD::__fill_n(__first, __n, __value_, integral_constant<bool, + is_pointer<_OutputIterator>::value && + is_trivially_copy_assignable<_Tp>::value && + sizeof(_Tp) == 1>()); +} + +// fill + +template <class _ForwardIterator, class _Tp> +inline _LIBCPP_INLINE_VISIBILITY +void +__fill(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, forward_iterator_tag) +{ + for (; __first != __last; ++__first) + *__first = __value_; +} + +template <class _RandomAccessIterator, class _Tp> +inline _LIBCPP_INLINE_VISIBILITY +void +__fill(_RandomAccessIterator __first, _RandomAccessIterator __last, const _Tp& __value_, random_access_iterator_tag) +{ + _VSTD::fill_n(__first, __last - __first, __value_); +} + +template <class _ForwardIterator, class _Tp> +inline _LIBCPP_INLINE_VISIBILITY +void +fill(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_) +{ + _VSTD::__fill(__first, __last, __value_, typename iterator_traits<_ForwardIterator>::iterator_category()); +} + +// generate + +template <class _ForwardIterator, class _Generator> +inline _LIBCPP_INLINE_VISIBILITY +void +generate(_ForwardIterator __first, _ForwardIterator __last, _Generator __gen) +{ + for (; __first != __last; ++__first) + *__first = __gen(); +} + +// generate_n + +template <class _OutputIterator, class _Size, class _Generator> +inline _LIBCPP_INLINE_VISIBILITY +_OutputIterator +generate_n(_OutputIterator __first, _Size __n, _Generator __gen) +{ + for (; __n > 0; ++__first, --__n) + *__first = __gen(); + return __first; +} + +// remove + +template <class _ForwardIterator, class _Tp> +_ForwardIterator +remove(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_) +{ + __first = _VSTD::find(__first, __last, __value_); + if (__first != __last) + { + _ForwardIterator __i = __first; + while (++__i != __last) + { + if (!(*__i == __value_)) + { + *__first = _VSTD::move(*__i); + ++__first; + } + } + } + return __first; +} + +// remove_if + +template <class _ForwardIterator, class _Predicate> +_ForwardIterator +remove_if(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred) +{ + __first = _VSTD::find_if<_ForwardIterator, typename add_lvalue_reference<_Predicate>::type> + (__first, __last, __pred); + if (__first != __last) + { + _ForwardIterator __i = __first; + while (++__i != __last) + { + if (!__pred(*__i)) + { + *__first = _VSTD::move(*__i); + ++__first; + } + } + } + return __first; +} + +// remove_copy + +template <class _InputIterator, class _OutputIterator, class _Tp> +inline _LIBCPP_INLINE_VISIBILITY +_OutputIterator +remove_copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result, const _Tp& __value_) +{ + for (; __first != __last; ++__first) + { + if (!(*__first == __value_)) + { + *__result = *__first; + ++__result; + } + } + return __result; +} + +// remove_copy_if + +template <class _InputIterator, class _OutputIterator, class _Predicate> +inline _LIBCPP_INLINE_VISIBILITY +_OutputIterator +remove_copy_if(_InputIterator __first, _InputIterator __last, _OutputIterator __result, _Predicate __pred) +{ + for (; __first != __last; ++__first) + { + if (!__pred(*__first)) + { + *__result = *__first; + ++__result; + } + } + return __result; +} + +// unique + +template <class _ForwardIterator, class _BinaryPredicate> +_ForwardIterator +unique(_ForwardIterator __first, _ForwardIterator __last, _BinaryPredicate __pred) +{ + __first = _VSTD::adjacent_find<_ForwardIterator, typename add_lvalue_reference<_BinaryPredicate>::type> + (__first, __last, __pred); + if (__first != __last) + { + // ... a a ? ... + // f i + _ForwardIterator __i = __first; + for (++__i; ++__i != __last;) + if (!__pred(*__first, *__i)) + *++__first = _VSTD::move(*__i); + ++__first; + } + return __first; +} + +template <class _ForwardIterator> +inline _LIBCPP_INLINE_VISIBILITY +_ForwardIterator +unique(_ForwardIterator __first, _ForwardIterator __last) +{ + typedef typename iterator_traits<_ForwardIterator>::value_type __v; + return _VSTD::unique(__first, __last, __equal_to<__v>()); +} + +// unique_copy + +template <class _BinaryPredicate, class _InputIterator, class _OutputIterator> +_OutputIterator +__unique_copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result, _BinaryPredicate __pred, + input_iterator_tag, output_iterator_tag) +{ + if (__first != __last) + { + typename iterator_traits<_InputIterator>::value_type __t(*__first); + *__result = __t; + ++__result; + while (++__first != __last) + { + if (!__pred(__t, *__first)) + { + __t = *__first; + *__result = __t; + ++__result; + } + } + } + return __result; +} + +template <class _BinaryPredicate, class _ForwardIterator, class _OutputIterator> +_OutputIterator +__unique_copy(_ForwardIterator __first, _ForwardIterator __last, _OutputIterator __result, _BinaryPredicate __pred, + forward_iterator_tag, output_iterator_tag) +{ + if (__first != __last) + { + _ForwardIterator __i = __first; + *__result = *__i; + ++__result; + while (++__first != __last) + { + if (!__pred(*__i, *__first)) + { + *__result = *__first; + ++__result; + __i = __first; + } + } + } + return __result; +} + +template <class _BinaryPredicate, class _InputIterator, class _ForwardIterator> +_ForwardIterator +__unique_copy(_InputIterator __first, _InputIterator __last, _ForwardIterator __result, _BinaryPredicate __pred, + input_iterator_tag, forward_iterator_tag) +{ + if (__first != __last) + { + *__result = *__first; + while (++__first != __last) + if (!__pred(*__result, *__first)) + *++__result = *__first; + ++__result; + } + return __result; +} + +template <class _InputIterator, class _OutputIterator, class _BinaryPredicate> +inline _LIBCPP_INLINE_VISIBILITY +_OutputIterator +unique_copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result, _BinaryPredicate __pred) +{ + return _VSTD::__unique_copy<typename add_lvalue_reference<_BinaryPredicate>::type> + (__first, __last, __result, __pred, + typename iterator_traits<_InputIterator>::iterator_category(), + typename iterator_traits<_OutputIterator>::iterator_category()); +} + +template <class _InputIterator, class _OutputIterator> +inline _LIBCPP_INLINE_VISIBILITY +_OutputIterator +unique_copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result) +{ + typedef typename iterator_traits<_InputIterator>::value_type __v; + return _VSTD::unique_copy(__first, __last, __result, __equal_to<__v>()); +} + +// reverse + +template <class _BidirectionalIterator> +inline _LIBCPP_INLINE_VISIBILITY +void +__reverse(_BidirectionalIterator __first, _BidirectionalIterator __last, bidirectional_iterator_tag) +{ + while (__first != __last) + { + if (__first == --__last) + break; + swap(*__first, *__last); + ++__first; + } +} + +template <class _RandomAccessIterator> +inline _LIBCPP_INLINE_VISIBILITY +void +__reverse(_RandomAccessIterator __first, _RandomAccessIterator __last, random_access_iterator_tag) +{ + if (__first != __last) + for (; __first < --__last; ++__first) + swap(*__first, *__last); +} + +template <class _BidirectionalIterator> +inline _LIBCPP_INLINE_VISIBILITY +void +reverse(_BidirectionalIterator __first, _BidirectionalIterator __last) +{ + _VSTD::__reverse(__first, __last, typename iterator_traits<_BidirectionalIterator>::iterator_category()); +} + +// reverse_copy + +template <class _BidirectionalIterator, class _OutputIterator> +inline _LIBCPP_INLINE_VISIBILITY +_OutputIterator +reverse_copy(_BidirectionalIterator __first, _BidirectionalIterator __last, _OutputIterator __result) +{ + for (; __first != __last; ++__result) + *__result = *--__last; + return __result; +} + +// rotate + +template <class _ForwardIterator> +_ForwardIterator +__rotate(_ForwardIterator __first, _ForwardIterator __middle, _ForwardIterator __last, false_type) +{ + if (__first == __middle) + return __last; + if (__middle == __last) + return __first; + _ForwardIterator __i = __middle; + while (true) + { + swap(*__first, *__i); + ++__first; + if (++__i == __last) + break; + if (__first == __middle) + __middle = __i; + } + _ForwardIterator __r = __first; + if (__first != __middle) + { + __i = __middle; + while (true) + { + swap(*__first, *__i); + ++__first; + if (++__i == __last) + { + if (__first == __middle) + break; + __i = __middle; + } + else if (__first == __middle) + __middle = __i; + } + } + return __r; +} + +template<typename _Integral> +inline _LIBCPP_INLINE_VISIBILITY +_Integral +__gcd(_Integral __x, _Integral __y) +{ + do + { + _Integral __t = __x % __y; + __x = __y; + __y = __t; + } while (__y); + return __x; +} + +template<typename _RandomAccessIterator> +_RandomAccessIterator +__rotate(_RandomAccessIterator __first, _RandomAccessIterator __middle, _RandomAccessIterator __last, true_type) +{ + typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type; + typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type; + + if (__first == __middle) + return __last; + if (__middle == __last) + return __first; + const difference_type __m1 = __middle - __first; + const difference_type __m2 = __last - __middle; + if (__m1 == __m2) + { + _VSTD::swap_ranges(__first, __middle, __middle); + return __middle; + } + const difference_type __g = __gcd(__m1, __m2); + for (_RandomAccessIterator __p = __first + __g; __p != __first;) + { + value_type __t(*--__p); + _RandomAccessIterator __p1 = __p; + _RandomAccessIterator __p2 = __p1 + __m1; + do + { + *__p1 = *__p2; + __p1 = __p2; + const difference_type __d = __last - __p2; + if (__m1 < __d) + __p2 += __m1; + else + __p2 = __first + (__m1 - __d); + } while (__p2 != __p); + *__p1 = __t; + } + return __first + __m2; +} + +template <class _ForwardIterator> +inline _LIBCPP_INLINE_VISIBILITY +_ForwardIterator +rotate(_ForwardIterator __first, _ForwardIterator __middle, _ForwardIterator __last) +{ + return _VSTD::__rotate(__first, __middle, __last, + integral_constant + < + bool, + is_convertible + < + typename iterator_traits<_ForwardIterator>::iterator_category, + random_access_iterator_tag + >::value && + is_trivially_copy_assignable + < + typename iterator_traits<_ForwardIterator>::value_type + >::value + >()); +} + +// rotate_copy + +template <class _ForwardIterator, class _OutputIterator> +inline _LIBCPP_INLINE_VISIBILITY +_OutputIterator +rotate_copy(_ForwardIterator __first, _ForwardIterator __middle, _ForwardIterator __last, _OutputIterator __result) +{ + return _VSTD::copy(__first, __middle, _VSTD::copy(__middle, __last, __result)); +} + +// min_element + +template <class _ForwardIterator, class _Compare> +inline _LIBCPP_INLINE_VISIBILITY +_ForwardIterator +min_element(_ForwardIterator __first, _ForwardIterator __last, _Compare __comp) +{ + if (__first != __last) + { + _ForwardIterator __i = __first; + while (++__i != __last) + if (__comp(*__i, *__first)) + __first = __i; + } + return __first; +} + +template <class _ForwardIterator> +inline _LIBCPP_INLINE_VISIBILITY +_ForwardIterator +min_element(_ForwardIterator __first, _ForwardIterator __last) +{ + return _VSTD::min_element(__first, __last, + __less<typename iterator_traits<_ForwardIterator>::value_type>()); +} + +// min + +template <class _Tp, class _Compare> +inline _LIBCPP_INLINE_VISIBILITY +const _Tp& +min(const _Tp& __a, const _Tp& __b, _Compare __comp) +{ + return __comp(__b, __a) ? __b : __a; +} + +template <class _Tp> +inline _LIBCPP_INLINE_VISIBILITY +const _Tp& +min(const _Tp& __a, const _Tp& __b) +{ + return _VSTD::min(__a, __b, __less<_Tp>()); +} + +#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS + +template<class _Tp, class _Compare> +inline _LIBCPP_INLINE_VISIBILITY +_Tp +min(initializer_list<_Tp> __t, _Compare __comp) +{ + return *_VSTD::min_element(__t.begin(), __t.end(), __comp); +} + +template<class _Tp> +inline _LIBCPP_INLINE_VISIBILITY +_Tp +min(initializer_list<_Tp> __t) +{ + return *_VSTD::min_element(__t.begin(), __t.end()); +} + +#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS + +// max_element + +template <class _ForwardIterator, class _Compare> +inline _LIBCPP_INLINE_VISIBILITY +_ForwardIterator +max_element(_ForwardIterator __first, _ForwardIterator __last, _Compare __comp) +{ + if (__first != __last) + { + _ForwardIterator __i = __first; + while (++__i != __last) + if (__comp(*__first, *__i)) + __first = __i; + } + return __first; +} + +template <class _ForwardIterator> +inline _LIBCPP_INLINE_VISIBILITY +_ForwardIterator +max_element(_ForwardIterator __first, _ForwardIterator __last) +{ + return _VSTD::max_element(__first, __last, + __less<typename iterator_traits<_ForwardIterator>::value_type>()); +} + +// max + +template <class _Tp, class _Compare> +inline _LIBCPP_INLINE_VISIBILITY +const _Tp& +max(const _Tp& __a, const _Tp& __b, _Compare __comp) +{ + return __comp(__a, __b) ? __b : __a; +} + +template <class _Tp> +inline _LIBCPP_INLINE_VISIBILITY +const _Tp& +max(const _Tp& __a, const _Tp& __b) +{ + return _VSTD::max(__a, __b, __less<_Tp>()); +} + +#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS + +template<class _Tp, class _Compare> +inline _LIBCPP_INLINE_VISIBILITY +_Tp +max(initializer_list<_Tp> __t, _Compare __comp) +{ + return *_VSTD::max_element(__t.begin(), __t.end(), __comp); +} + +template<class _Tp> +inline _LIBCPP_INLINE_VISIBILITY +_Tp +max(initializer_list<_Tp> __t) +{ + return *_VSTD::max_element(__t.begin(), __t.end()); +} + +#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS + +// minmax_element + +template <class _ForwardIterator, class _Compare> +std::pair<_ForwardIterator, _ForwardIterator> +minmax_element(_ForwardIterator __first, _ForwardIterator __last, _Compare __comp) +{ + std::pair<_ForwardIterator, _ForwardIterator> __result(__first, __first); + if (__first != __last) + { + if (++__first != __last) + { + if (__comp(*__first, *__result.first)) + { + __result.second = __result.first; + __result.first = __first; + } + else + __result.second = __first; + while (++__first != __last) + { + _ForwardIterator __i = __first; + if (++__first == __last) + { + if (__comp(*__i, *__result.first)) + __result.first = __i; + else if (!__comp(*__i, *__result.second)) + __result.second = __i; + break; + } + else + { + if (__comp(*__first, *__i)) + { + if (__comp(*__first, *__result.first)) + __result.first = __first; + if (!__comp(*__i, *__result.second)) + __result.second = __i; + } + else + { + if (__comp(*__i, *__result.first)) + __result.first = __i; + if (!__comp(*__first, *__result.second)) + __result.second = __first; + } + } + } + } + } + return __result; +} + +template <class _ForwardIterator> +inline _LIBCPP_INLINE_VISIBILITY +std::pair<_ForwardIterator, _ForwardIterator> +minmax_element(_ForwardIterator __first, _ForwardIterator __last) +{ + return _VSTD::minmax_element(__first, __last, __less<typename iterator_traits<_ForwardIterator>::value_type>()); +} + +// minmax + +template<class _Tp, class _Compare> +inline _LIBCPP_INLINE_VISIBILITY +pair<const _Tp&, const _Tp&> +minmax(const _Tp& __a, const _Tp& __b, _Compare __comp) +{ + return __comp(__b, __a) ? pair<const _Tp&, const _Tp&>(__b, __a) : + pair<const _Tp&, const _Tp&>(__a, __b); +} + +template<class _Tp> +inline _LIBCPP_INLINE_VISIBILITY +pair<const _Tp&, const _Tp&> +minmax(const _Tp& __a, const _Tp& __b) +{ + return _VSTD::minmax(__a, __b, __less<_Tp>()); +} + +#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS + +template<class _Tp> +inline _LIBCPP_INLINE_VISIBILITY +pair<_Tp, _Tp> +minmax(initializer_list<_Tp> __t) +{ + pair<const _Tp*, const _Tp*> __p = + _VSTD::minmax_element(__t.begin(), __t.end()); + return pair<_Tp, _Tp>(*__p.first, *__p.second); +} + +template<class _Tp, class _Compare> +inline _LIBCPP_INLINE_VISIBILITY +pair<_Tp, _Tp> +minmax(initializer_list<_Tp> __t, _Compare __comp) +{ + pair<const _Tp*, const _Tp*> __p = + _VSTD::minmax_element(__t.begin(), __t.end(), __comp); + return pair<_Tp, _Tp>(*__p.first, *__p.second); +} + +#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS + +// random_shuffle + +// __independent_bits_engine + +template <unsigned long long _X, size_t _R> +struct __log2_imp +{ + static const size_t value = _X & ((unsigned long long)(1) << _R) ? _R + : __log2_imp<_X, _R - 1>::value; +}; + +template <unsigned long long _X> +struct __log2_imp<_X, 0> +{ + static const size_t value = 0; +}; + +template <size_t _R> +struct __log2_imp<0, _R> +{ + static const size_t value = _R + 1; +}; + +template <class _UI, _UI _X> +struct __log2 +{ + static const size_t value = __log2_imp<_X, + sizeof(_UI) * __CHAR_BIT__ - 1>::value; +}; + +template<class _Engine, class _UIntType> +class __independent_bits_engine +{ +public: + // types + typedef _UIntType result_type; + +private: + typedef typename _Engine::result_type _Engine_result_type; + typedef typename conditional + < + sizeof(_Engine_result_type) <= sizeof(result_type), + result_type, + _Engine_result_type + >::type _Working_result_type; + + _Engine& __e_; + size_t __w_; + size_t __w0_; + size_t __n_; + size_t __n0_; + _Working_result_type __y0_; + _Working_result_type __y1_; + _Engine_result_type __mask0_; + _Engine_result_type __mask1_; + + static const _Working_result_type _R = _Engine::_Max - _Engine::_Min + + _Working_result_type(1); + static const size_t __m = __log2<_Working_result_type, _R>::value; + static const size_t _WDt = numeric_limits<_Working_result_type>::digits; + static const size_t _EDt = numeric_limits<_Engine_result_type>::digits; + +public: + // constructors and seeding functions + __independent_bits_engine(_Engine& __e, size_t __w); + + // generating functions + result_type operator()() {return __eval(integral_constant<bool, _R != 0>());} + +private: + result_type __eval(false_type); + result_type __eval(true_type); +}; + +template<class _Engine, class _UIntType> +__independent_bits_engine<_Engine, _UIntType> + ::__independent_bits_engine(_Engine& __e, size_t __w) + : __e_(__e), + __w_(__w) +{ + __n_ = __w_ / __m + (__w_ % __m != 0); + __w0_ = __w_ / __n_; + if (_R == 0) + __y0_ = _R; + else if (__w0_ < _WDt) + __y0_ = (_R >> __w0_) << __w0_; + else + __y0_ = 0; + if (_R - __y0_ > __y0_ / __n_) + { + ++__n_; + __w0_ = __w_ / __n_; + if (__w0_ < _WDt) + __y0_ = (_R >> __w0_) << __w0_; + else + __y0_ = 0; + } + __n0_ = __n_ - __w_ % __n_; + if (__w0_ < _WDt - 1) + __y1_ = (_R >> (__w0_ + 1)) << (__w0_ + 1); + else + __y1_ = 0; + __mask0_ = __w0_ > 0 ? _Engine_result_type(~0) >> (_EDt - __w0_) : + _Engine_result_type(0); + __mask1_ = __w0_ < _EDt - 1 ? + _Engine_result_type(~0) >> (_EDt - (__w0_ + 1)) : + _Engine_result_type(~0); +} + +template<class _Engine, class _UIntType> +inline +_UIntType +__independent_bits_engine<_Engine, _UIntType>::__eval(false_type) +{ + return static_cast<result_type>(__e_() & __mask0_); +} + +template<class _Engine, class _UIntType> +_UIntType +__independent_bits_engine<_Engine, _UIntType>::__eval(true_type) +{ + result_type _S = 0; + for (size_t __k = 0; __k < __n0_; ++__k) + { + _Engine_result_type __u; + do + { + __u = __e_() - _Engine::min(); + } while (__u >= __y0_); + if (__w0_ < _WDt) + _S <<= __w0_; + else + _S = 0; + _S += __u & __mask0_; + } + for (size_t __k = __n0_; __k < __n_; ++__k) + { + _Engine_result_type __u; + do + { + __u = __e_() - _Engine::min(); + } while (__u >= __y1_); + if (__w0_ < _WDt - 1) + _S <<= __w0_ + 1; + else + _S = 0; + _S += __u & __mask1_; + } + return _S; +} + +// uniform_int_distribution + +template<class _IntType = int> +class uniform_int_distribution +{ +public: + // types + typedef _IntType result_type; + + class param_type + { + result_type __a_; + result_type __b_; + public: + typedef uniform_int_distribution distribution_type; + + explicit param_type(result_type __a = 0, + result_type __b = numeric_limits<result_type>::max()) + : __a_(__a), __b_(__b) {} + + result_type a() const {return __a_;} + result_type b() const {return __b_;} + + friend bool operator==(const param_type& __x, const param_type& __y) + {return __x.__a_ == __y.__a_ && __x.__b_ == __y.__b_;} + friend bool operator!=(const param_type& __x, const param_type& __y) + {return !(__x == __y);} + }; + +private: + param_type __p_; + +public: + // constructors and reset functions + explicit uniform_int_distribution(result_type __a = 0, + result_type __b = numeric_limits<result_type>::max()) + : __p_(param_type(__a, __b)) {} + explicit uniform_int_distribution(const param_type& __p) : __p_(__p) {} + void reset() {} + + // generating functions + template<class _URNG> result_type operator()(_URNG& __g) + {return (*this)(__g, __p_);} + template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p); + + // property functions + result_type a() const {return __p_.a();} + result_type b() const {return __p_.b();} + + param_type param() const {return __p_;} + void param(const param_type& __p) {__p_ = __p;} + + result_type min() const {return a();} + result_type max() const {return b();} + + friend bool operator==(const uniform_int_distribution& __x, + const uniform_int_distribution& __y) + {return __x.__p_ == __y.__p_;} + friend bool operator!=(const uniform_int_distribution& __x, + const uniform_int_distribution& __y) + {return !(__x == __y);} +}; + +template<class _IntType> +template<class _URNG> +typename uniform_int_distribution<_IntType>::result_type +uniform_int_distribution<_IntType>::operator()(_URNG& __g, const param_type& __p) +{ + typedef typename conditional<sizeof(result_type) <= sizeof(uint32_t), + uint32_t, uint64_t>::type _UIntType; + const _UIntType _R = __p.b() - __p.a() + _UIntType(1); + if (_R == 1) + return __p.a(); + const size_t _Dt = numeric_limits<_UIntType>::digits; + typedef __independent_bits_engine<_URNG, _UIntType> _Eng; + if (_R == 0) + return static_cast<result_type>(_Eng(__g, _Dt)()); + size_t __w = _Dt - __clz(_R) - 1; + if ((_R & (_UIntType(~0) >> (_Dt - __w))) != 0) + ++__w; + _Eng __e(__g, __w); + _UIntType __u; + do + { + __u = __e(); + } while (__u >= _R); + return static_cast<result_type>(__u + __p.a()); +} + +class __rs_default; + +__rs_default __rs_get(); + +class __rs_default +{ + static unsigned __c_; + + __rs_default(); +public: + typedef unsigned result_type; + + static const result_type _Min = 0; + static const result_type _Max = 0xFFFFFFFF; + + __rs_default(const __rs_default&); + ~__rs_default(); + + result_type operator()(); + + static const/*expr*/ result_type min() {return _Min;} + static const/*expr*/ result_type max() {return _Max;} + + friend __rs_default __rs_get(); +}; + +__rs_default __rs_get(); + +template <class _RandomAccessIterator> +void +random_shuffle(_RandomAccessIterator __first, _RandomAccessIterator __last) +{ + typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type; + typedef uniform_int_distribution<ptrdiff_t> _D; + typedef typename _D::param_type _P; + difference_type __d = __last - __first; + if (__d > 1) + { + _D __uid; + __rs_default __g = __rs_get(); + for (--__last, --__d; __first < __last; ++__first, --__d) + { + difference_type __i = __uid(__g, _P(0, __d)); + if (__i != difference_type(0)) + swap(*__first, *(__first + __i)); + } + } +} + +template <class _RandomAccessIterator, class _RandomNumberGenerator> +void +random_shuffle(_RandomAccessIterator __first, _RandomAccessIterator __last, +#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES + _RandomNumberGenerator&& __rand) +#else + _RandomNumberGenerator& __rand) +#endif +{ + typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type; + difference_type __d = __last - __first; + if (__d > 1) + { + for (--__last; __first < __last; ++__first, --__d) + { + difference_type __i = __rand(__d); + swap(*__first, *(__first + __i)); + } + } +} + +template<class _RandomAccessIterator, class _UniformRandomNumberGenerator> + void shuffle(_RandomAccessIterator __first, _RandomAccessIterator __last, +#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES + _UniformRandomNumberGenerator&& __g) +#else + _UniformRandomNumberGenerator& __g) +#endif +{ + typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type; + typedef uniform_int_distribution<ptrdiff_t> _D; + typedef typename _D::param_type _P; + difference_type __d = __last - __first; + if (__d > 1) + { + _D __uid; + for (--__last, --__d; __first < __last; ++__first, --__d) + { + difference_type __i = __uid(__g, _P(0, __d)); + if (__i != difference_type(0)) + swap(*__first, *(__first + __i)); + } + } +} + +template <class _InputIterator, class _Predicate> +bool +is_partitioned(_InputIterator __first, _InputIterator __last, _Predicate __pred) +{ + for (; __first != __last; ++__first) + if (!__pred(*__first)) + break; + for (; __first != __last; ++__first) + if (__pred(*__first)) + return false; + return true; +} + +// partition + +template <class _Predicate, class _ForwardIterator> +_ForwardIterator +__partition(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred, forward_iterator_tag) +{ + while (true) + { + if (__first == __last) + return __first; + if (!__pred(*__first)) + break; + ++__first; + } + for (_ForwardIterator __p = __first; ++__p != __last;) + { + if (__pred(*__p)) + { + swap(*__first, *__p); + ++__first; + } + } + return __first; +} + +template <class _Predicate, class _BidirectionalIterator> +_BidirectionalIterator +__partition(_BidirectionalIterator __first, _BidirectionalIterator __last, _Predicate __pred, + bidirectional_iterator_tag) +{ + while (true) + { + while (true) + { + if (__first == __last) + return __first; + if (!__pred(*__first)) + break; + ++__first; + } + do + { + if (__first == --__last) + return __first; + } while (!__pred(*__last)); + swap(*__first, *__last); + ++__first; + } +} + +template <class _ForwardIterator, class _Predicate> +inline _LIBCPP_INLINE_VISIBILITY +_ForwardIterator +partition(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred) +{ + return _VSTD::__partition<typename add_lvalue_reference<_Predicate>::type> + (__first, __last, __pred, typename iterator_traits<_ForwardIterator>::iterator_category()); +} + +// partition_copy + +template <class _InputIterator, class _OutputIterator1, + class _OutputIterator2, class _Predicate> +pair<_OutputIterator1, _OutputIterator2> +partition_copy(_InputIterator __first, _InputIterator __last, + _OutputIterator1 __out_true, _OutputIterator2 __out_false, + _Predicate __pred) +{ + for (; __first != __last; ++__first) + { + if (__pred(*__first)) + { + *__out_true = *__first; + ++__out_true; + } + else + { + *__out_false = *__first; + ++__out_false; + } + } + return pair<_OutputIterator1, _OutputIterator2>(__out_true, __out_false); +} + +// partition_point + +template<class _ForwardIterator, class _Predicate> +_ForwardIterator +partition_point(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred) +{ + typedef typename iterator_traits<_ForwardIterator>::difference_type difference_type; + difference_type __len = _VSTD::distance(__first, __last); + while (__len != 0) + { + difference_type __l2 = __len / 2; + _ForwardIterator __m = __first; + _VSTD::advance(__m, __l2); + if (__pred(*__m)) + { + __first = ++__m; + __len -= __l2 + 1; + } + else + __len = __l2; + } + return __first; +} + +// stable_partition + +template <class _Predicate, class _ForwardIterator, class _Distance, class _Pair> +_ForwardIterator +__stable_partition(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred, + _Distance __len, _Pair __p, forward_iterator_tag __fit) +{ + // *__first is known to be false + // __len >= 1 + if (__len == 1) + return __first; + if (__len == 2) + { + _ForwardIterator __m = __first; + if (__pred(*++__m)) + { + swap(*__first, *__m); + return __m; + } + return __first; + } + if (__len <= __p.second) + { // The buffer is big enough to use + typedef typename iterator_traits<_ForwardIterator>::value_type value_type; + __destruct_n __d(0); + unique_ptr<value_type, __destruct_n&> __h(__p.first, __d); + // Move the falses into the temporary buffer, and the trues to the front of the line + // Update __first to always point to the end of the trues + value_type* __t = __p.first; + ::new(__t) value_type(_VSTD::move(*__first)); + __d.__incr((value_type*)0); + ++__t; + _ForwardIterator __i = __first; + while (++__i != __last) + { + if (__pred(*__i)) + { + *__first = _VSTD::move(*__i); + ++__first; + } + else + { + ::new(__t) value_type(_VSTD::move(*__i)); + __d.__incr((value_type*)0); + ++__t; + } + } + // All trues now at start of range, all falses in buffer + // Move falses back into range, but don't mess up __first which points to first false + __i = __first; + for (value_type* __t2 = __p.first; __t2 < __t; ++__t2, ++__i) + *__i = _VSTD::move(*__t2); + // __h destructs moved-from values out of the temp buffer, but doesn't deallocate buffer + return __first; + } + // Else not enough buffer, do in place + // __len >= 3 + _ForwardIterator __m = __first; + _Distance __len2 = __len / 2; // __len2 >= 2 + _VSTD::advance(__m, __len2); + // recurse on [__first, __m), *__first know to be false + // F????????????????? + // f m l + typedef typename add_lvalue_reference<_Predicate>::type _PredRef; + _ForwardIterator __first_false = __stable_partition<_PredRef>(__first, __m, __pred, __len2, __p, __fit); + // TTTFFFFF?????????? + // f ff m l + // recurse on [__m, __last], except increase __m until *(__m) is false, *__last know to be true + _ForwardIterator __m1 = __m; + _ForwardIterator __second_false = __last; + _Distance __len_half = __len - __len2; + while (__pred(*__m1)) + { + if (++__m1 == __last) + goto __second_half_done; + --__len_half; + } + // TTTFFFFFTTTF?????? + // f ff m m1 l + __second_false = __stable_partition<_PredRef>(__m1, __last, __pred, __len_half, __p, __fit); +__second_half_done: + // TTTFFFFFTTTTTFFFFF + // f ff m sf l + return _VSTD::rotate(__first_false, __m, __second_false); + // TTTTTTTTFFFFFFFFFF + // | +} + +struct __return_temporary_buffer +{ + template <class _Tp> + _LIBCPP_INLINE_VISIBILITY void operator()(_Tp* __p) const {_VSTD::return_temporary_buffer(__p);} +}; + +template <class _Predicate, class _ForwardIterator> +_ForwardIterator +__stable_partition(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred, + forward_iterator_tag) +{ + const unsigned __alloc_limit = 3; // might want to make this a function of trivial assignment + // Either prove all true and return __first or point to first false + while (true) + { + if (__first == __last) + return __first; + if (!__pred(*__first)) + break; + ++__first; + } + // We now have a reduced range [__first, __last) + // *__first is known to be false + typedef typename iterator_traits<_ForwardIterator>::difference_type difference_type; + typedef typename iterator_traits<_ForwardIterator>::value_type value_type; + difference_type __len = _VSTD::distance(__first, __last); + pair<value_type*, ptrdiff_t> __p(0, 0); + unique_ptr<value_type, __return_temporary_buffer> __h; + if (__len >= __alloc_limit) + { + __p = _VSTD::get_temporary_buffer<value_type>(__len); + __h.reset(__p.first); + } + return __stable_partition<typename add_lvalue_reference<_Predicate>::type> + (__first, __last, __pred, __len, __p, forward_iterator_tag()); +} + +template <class _Predicate, class _BidirectionalIterator, class _Distance, class _Pair> +_BidirectionalIterator +__stable_partition(_BidirectionalIterator __first, _BidirectionalIterator __last, _Predicate __pred, + _Distance __len, _Pair __p, bidirectional_iterator_tag __bit) +{ + // *__first is known to be false + // *__last is known to be true + // __len >= 2 + if (__len == 2) + { + swap(*__first, *__last); + return __last; + } + if (__len == 3) + { + _BidirectionalIterator __m = __first; + if (__pred(*++__m)) + { + swap(*__first, *__m); + swap(*__m, *__last); + return __last; + } + swap(*__m, *__last); + swap(*__first, *__m); + return __m; + } + if (__len <= __p.second) + { // The buffer is big enough to use + typedef typename iterator_traits<_BidirectionalIterator>::value_type value_type; + __destruct_n __d(0); + unique_ptr<value_type, __destruct_n&> __h(__p.first, __d); + // Move the falses into the temporary buffer, and the trues to the front of the line + // Update __first to always point to the end of the trues + value_type* __t = __p.first; + ::new(__t) value_type(_VSTD::move(*__first)); + __d.__incr((value_type*)0); + ++__t; + _BidirectionalIterator __i = __first; + while (++__i != __last) + { + if (__pred(*__i)) + { + *__first = _VSTD::move(*__i); + ++__first; + } + else + { + ::new(__t) value_type(_VSTD::move(*__i)); + __d.__incr((value_type*)0); + ++__t; + } + } + // move *__last, known to be true + *__first = _VSTD::move(*__i); + __i = ++__first; + // All trues now at start of range, all falses in buffer + // Move falses back into range, but don't mess up __first which points to first false + for (value_type* __t2 = __p.first; __t2 < __t; ++__t2, ++__i) + *__i = _VSTD::move(*__t2); + // __h destructs moved-from values out of the temp buffer, but doesn't deallocate buffer + return __first; + } + // Else not enough buffer, do in place + // __len >= 4 + _BidirectionalIterator __m = __first; + _Distance __len2 = __len / 2; // __len2 >= 2 + _VSTD::advance(__m, __len2); + // recurse on [__first, __m-1], except reduce __m-1 until *(__m-1) is true, *__first know to be false + // F????????????????T + // f m l + _BidirectionalIterator __m1 = __m; + _BidirectionalIterator __first_false = __first; + _Distance __len_half = __len2; + while (!__pred(*--__m1)) + { + if (__m1 == __first) + goto __first_half_done; + --__len_half; + } + // F???TFFF?????????T + // f m1 m l + typedef typename add_lvalue_reference<_Predicate>::type _PredRef; + __first_false = __stable_partition<_PredRef>(__first, __m1, __pred, __len_half, __p, __bit); +__first_half_done: + // TTTFFFFF?????????T + // f ff m l + // recurse on [__m, __last], except increase __m until *(__m) is false, *__last know to be true + __m1 = __m; + _BidirectionalIterator __second_false = __last; + ++__second_false; + __len_half = __len - __len2; + while (__pred(*__m1)) + { + if (++__m1 == __last) + goto __second_half_done; + --__len_half; + } + // TTTFFFFFTTTF?????T + // f ff m m1 l + __second_false = __stable_partition<_PredRef>(__m1, __last, __pred, __len_half, __p, __bit); +__second_half_done: + // TTTFFFFFTTTTTFFFFF + // f ff m sf l + return _VSTD::rotate(__first_false, __m, __second_false); + // TTTTTTTTFFFFFFFFFF + // | +} + +template <class _Predicate, class _BidirectionalIterator> +_BidirectionalIterator +__stable_partition(_BidirectionalIterator __first, _BidirectionalIterator __last, _Predicate __pred, + bidirectional_iterator_tag) +{ + typedef typename iterator_traits<_BidirectionalIterator>::difference_type difference_type; + typedef typename iterator_traits<_BidirectionalIterator>::value_type value_type; + const difference_type __alloc_limit = 4; // might want to make this a function of trivial assignment + // Either prove all true and return __first or point to first false + while (true) + { + if (__first == __last) + return __first; + if (!__pred(*__first)) + break; + ++__first; + } + // __first points to first false, everything prior to __first is already set. + // Either prove [__first, __last) is all false and return __first, or point __last to last true + do + { + if (__first == --__last) + return __first; + } while (!__pred(*__last)); + // We now have a reduced range [__first, __last] + // *__first is known to be false + // *__last is known to be true + // __len >= 2 + difference_type __len = _VSTD::distance(__first, __last) + 1; + pair<value_type*, ptrdiff_t> __p(0, 0); + unique_ptr<value_type, __return_temporary_buffer> __h; + if (__len >= __alloc_limit) + { + __p = _VSTD::get_temporary_buffer<value_type>(__len); + __h.reset(__p.first); + } + return __stable_partition<typename add_lvalue_reference<_Predicate>::type> + (__first, __last, __pred, __len, __p, bidirectional_iterator_tag()); +} + +template <class _ForwardIterator, class _Predicate> +inline _LIBCPP_INLINE_VISIBILITY +_ForwardIterator +stable_partition(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred) +{ + return __stable_partition<typename add_lvalue_reference<_Predicate>::type> + (__first, __last, __pred, typename iterator_traits<_ForwardIterator>::iterator_category()); +} + +// is_sorted_until + +template <class _ForwardIterator, class _Compare> +_ForwardIterator +is_sorted_until(_ForwardIterator __first, _ForwardIterator __last, _Compare __comp) +{ + if (__first != __last) + { + _ForwardIterator __i = __first; + while (++__i != __last) + { + if (__comp(*__i, *__first)) + return __i; + __first = __i; + } + } + return __last; +} + +template<class _ForwardIterator> +inline _LIBCPP_INLINE_VISIBILITY +_ForwardIterator +is_sorted_until(_ForwardIterator __first, _ForwardIterator __last) +{ + return _VSTD::is_sorted_until(__first, __last, __less<typename iterator_traits<_ForwardIterator>::value_type>()); +} + +// is_sorted + +template <class _ForwardIterator, class _Compare> +inline _LIBCPP_INLINE_VISIBILITY +bool +is_sorted(_ForwardIterator __first, _ForwardIterator __last, _Compare __comp) +{ + return _VSTD::is_sorted_until(__first, __last, __comp) == __last; +} + +template<class _ForwardIterator> +inline _LIBCPP_INLINE_VISIBILITY +bool +is_sorted(_ForwardIterator __first, _ForwardIterator __last) +{ + return _VSTD::is_sorted(__first, __last, __less<typename iterator_traits<_ForwardIterator>::value_type>()); +} + +// sort + +// stable, 2-3 compares, 0-2 swaps + +template <class _Compare, class _ForwardIterator> +unsigned +__sort3(_ForwardIterator __x, _ForwardIterator __y, _ForwardIterator __z, _Compare __c) +{ + unsigned __r = 0; + if (!__c(*__y, *__x)) // if x <= y + { + if (!__c(*__z, *__y)) // if y <= z + return __r; // x <= y && y <= z + // x <= y && y > z + swap(*__y, *__z); // x <= z && y < z + __r = 1; + if (__c(*__y, *__x)) // if x > y + { + swap(*__x, *__y); // x < y && y <= z + __r = 2; + } + return __r; // x <= y && y < z + } + if (__c(*__z, *__y)) // x > y, if y > z + { + swap(*__x, *__z); // x < y && y < z + __r = 1; + return __r; + } + swap(*__x, *__y); // x > y && y <= z + __r = 1; // x < y && x <= z + if (__c(*__z, *__y)) // if y > z + { + swap(*__y, *__z); // x <= y && y < z + __r = 2; + } + return __r; +} // x <= y && y <= z + +// stable, 3-6 compares, 0-5 swaps + +template <class _Compare, class _ForwardIterator> +unsigned +__sort4(_ForwardIterator __x1, _ForwardIterator __x2, _ForwardIterator __x3, + _ForwardIterator __x4, _Compare __c) +{ + unsigned __r = __sort3<_Compare>(__x1, __x2, __x3, __c); + if (__c(*__x4, *__x3)) + { + swap(*__x3, *__x4); + ++__r; + if (__c(*__x3, *__x2)) + { + swap(*__x2, *__x3); + ++__r; + if (__c(*__x2, *__x1)) + { + swap(*__x1, *__x2); + ++__r; + } + } + } + return __r; +} + +// stable, 4-10 compares, 0-9 swaps + +template <class _Compare, class _ForwardIterator> +unsigned +__sort5(_ForwardIterator __x1, _ForwardIterator __x2, _ForwardIterator __x3, + _ForwardIterator __x4, _ForwardIterator __x5, _Compare __c) +{ + unsigned __r = __sort4<_Compare>(__x1, __x2, __x3, __x4, __c); + if (__c(*__x5, *__x4)) + { + swap(*__x4, *__x5); + ++__r; + if (__c(*__x4, *__x3)) + { + swap(*__x3, *__x4); + ++__r; + if (__c(*__x3, *__x2)) + { + swap(*__x2, *__x3); + ++__r; + if (__c(*__x2, *__x1)) + { + swap(*__x1, *__x2); + ++__r; + } + } + } + } + return __r; +} + +// Assumes size > 0 +template <class _Compare, class _BirdirectionalIterator> +void +__selection_sort(_BirdirectionalIterator __first, _BirdirectionalIterator __last, _Compare __comp) +{ + _BirdirectionalIterator __lm1 = __last; + for (--__lm1; __first != __lm1; ++__first) + { + _BirdirectionalIterator __i = _VSTD::min_element<_BirdirectionalIterator, + typename add_lvalue_reference<_Compare>::type> + (__first, __last, __comp); + if (__i != __first) + swap(*__first, *__i); + } +} + +template <class _Compare, class _BirdirectionalIterator> +void +__insertion_sort(_BirdirectionalIterator __first, _BirdirectionalIterator __last, _Compare __comp) +{ + typedef typename iterator_traits<_BirdirectionalIterator>::value_type value_type; + if (__first != __last) + { + _BirdirectionalIterator __i = __first; + for (++__i; __i != __last; ++__i) + { + _BirdirectionalIterator __j = __i; + value_type __t(_VSTD::move(*__j)); + for (_BirdirectionalIterator __k = __i; __k != __first && __comp(__t, *--__k); --__j) + *__j = _VSTD::move(*__k); + *__j = _VSTD::move(__t); + } + } +} + +template <class _Compare, class _RandomAccessIterator> +void +__insertion_sort_3(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp) +{ + typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type; + _RandomAccessIterator __j = __first+2; + __sort3<_Compare>(__first, __first+1, __j, __comp); + for (_RandomAccessIterator __i = __j+1; __i != __last; ++__i) + { + if (__comp(*__i, *__j)) + { + value_type __t(_VSTD::move(*__i)); + _RandomAccessIterator __k = __j; + __j = __i; + do + { + *__j = _VSTD::move(*__k); + __j = __k; + } while (__j != __first && __comp(__t, *--__k)); + *__j = _VSTD::move(__t); + } + __j = __i; + } +} + +template <class _Compare, class _RandomAccessIterator> +bool +__insertion_sort_incomplete(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp) +{ + switch (__last - __first) + { + case 0: + case 1: + return true; + case 2: + if (__comp(*--__last, *__first)) + swap(*__first, *__last); + return true; + case 3: + _VSTD::__sort3<_Compare>(__first, __first+1, --__last, __comp); + return true; + case 4: + _VSTD::__sort4<_Compare>(__first, __first+1, __first+2, --__last, __comp); + return true; + case 5: + _VSTD::__sort5<_Compare>(__first, __first+1, __first+2, __first+3, --__last, __comp); + return true; + } + typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type; + _RandomAccessIterator __j = __first+2; + __sort3<_Compare>(__first, __first+1, __j, __comp); + const unsigned __limit = 8; + unsigned __count = 0; + for (_RandomAccessIterator __i = __j+1; __i != __last; ++__i) + { + if (__comp(*__i, *__j)) + { + value_type __t(_VSTD::move(*__i)); + _RandomAccessIterator __k = __j; + __j = __i; + do + { + *__j = _VSTD::move(*__k); + __j = __k; + } while (__j != __first && __comp(__t, *--__k)); + *__j = _VSTD::move(__t); + if (++__count == __limit) + return ++__i == __last; + } + __j = __i; + } + return true; +} + +template <class _Compare, class _BirdirectionalIterator> +void +__insertion_sort_move(_BirdirectionalIterator __first1, _BirdirectionalIterator __last1, + typename iterator_traits<_BirdirectionalIterator>::value_type* __first2, _Compare __comp) +{ + typedef typename iterator_traits<_BirdirectionalIterator>::value_type value_type; + if (__first1 != __last1) + { + __destruct_n __d(0); + unique_ptr<value_type, __destruct_n&> __h(__first2, __d); + value_type* __last2 = __first2; + ::new(__last2) value_type(_VSTD::move(*__first1)); + __d.__incr((value_type*)0); + for (++__last2; ++__first1 != __last1; ++__last2) + { + value_type* __j2 = __last2; + value_type* __i2 = __j2; + if (__comp(*__first1, *--__i2)) + { + ::new(__j2) value_type(_VSTD::move(*__i2)); + __d.__incr((value_type*)0); + for (--__j2; __i2 != __first2 && __comp(*__first1, *--__i2); --__j2) + *__j2 = _VSTD::move(*__i2); + *__j2 = _VSTD::move(*__first1); + } + else + { + ::new(__j2) value_type(_VSTD::move(*__first1)); + __d.__incr((value_type*)0); + } + } + __h.release(); + } +} + +template <class _Compare, class _RandomAccessIterator> +void +__sort(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp) +{ + // _Compare is known to be a reference type + typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type; + typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type; + const difference_type __limit = is_trivially_copy_constructible<value_type>::value && + is_trivially_copy_assignable<value_type>::value ? 30 : 6; + while (true) + { + __restart: + difference_type __len = __last - __first; + switch (__len) + { + case 0: + case 1: + return; + case 2: + if (__comp(*--__last, *__first)) + swap(*__first, *__last); + return; + case 3: + _VSTD::__sort3<_Compare>(__first, __first+1, --__last, __comp); + return; + case 4: + _VSTD::__sort4<_Compare>(__first, __first+1, __first+2, --__last, __comp); + return; + case 5: + _VSTD::__sort5<_Compare>(__first, __first+1, __first+2, __first+3, --__last, __comp); + return; + } + if (__len <= __limit) + { + _VSTD::__insertion_sort_3<_Compare>(__first, __last, __comp); + return; + } + // __len > 5 + _RandomAccessIterator __m = __first; + _RandomAccessIterator __lm1 = __last; + --__lm1; + unsigned __n_swaps; + { + difference_type __delta; + if (__len >= 1000) + { + __delta = __len/2; + __m += __delta; + __delta /= 2; + __n_swaps = _VSTD::__sort5<_Compare>(__first, __first + __delta, __m, __m+__delta, __lm1, __comp); + } + else + { + __delta = __len/2; + __m += __delta; + __n_swaps = _VSTD::__sort3<_Compare>(__first, __m, __lm1, __comp); + } + } + // *__m is median + // partition [__first, __m) < *__m and *__m <= [__m, __last) + // (this inhibits tossing elements equivalent to __m around unnecessarily) + _RandomAccessIterator __i = __first; + _RandomAccessIterator __j = __lm1; + // j points beyond range to be tested, *__m is known to be <= *__lm1 + // The search going up is known to be guarded but the search coming down isn't. + // Prime the downward search with a guard. + if (!__comp(*__i, *__m)) // if *__first == *__m + { + // *__first == *__m, *__first doesn't go in first part + // manually guard downward moving __j against __i + while (true) + { + if (__i == --__j) + { + // *__first == *__m, *__m <= all other elements + // Parition instead into [__first, __i) == *__first and *__first < [__i, __last) + ++__i; // __first + 1 + __j = __last; + if (!__comp(*__first, *--__j)) // we need a guard if *__first == *(__last-1) + { + while (true) + { + if (__i == __j) + return; // [__first, __last) all equivalent elements + if (__comp(*__first, *__i)) + { + swap(*__i, *__j); + ++__n_swaps; + ++__i; + break; + } + ++__i; + } + } + // [__first, __i) == *__first and *__first < [__j, __last) and __j == __last - 1 + if (__i == __j) + return; + while (true) + { + while (!__comp(*__first, *__i)) + ++__i; + while (__comp(*__first, *--__j)) + ; + if (__i >= __j) + break; + swap(*__i, *__j); + ++__n_swaps; + ++__i; + } + // [__first, __i) == *__first and *__first < [__i, __last) + // The first part is sorted, sort the secod part + // _VSTD::__sort<_Compare>(__i, __last, __comp); + __first = __i; + goto __restart; + } + if (__comp(*__j, *__m)) + { + swap(*__i, *__j); + ++__n_swaps; + break; // found guard for downward moving __j, now use unguarded partition + } + } + } + // It is known that *__i < *__m + ++__i; + // j points beyond range to be tested, *__m is known to be <= *__lm1 + // if not yet partitioned... + if (__i < __j) + { + // known that *(__i - 1) < *__m + // known that __i <= __m + while (true) + { + // __m still guards upward moving __i + while (__comp(*__i, *__m)) + ++__i; + // It is now known that a guard exists for downward moving __j + while (!__comp(*--__j, *__m)) + ; + if (__i > __j) + break; + swap(*__i, *__j); + ++__n_swaps; + // It is known that __m != __j + // If __m just moved, follow it + if (__m == __i) + __m = __j; + ++__i; + } + } + // [__first, __i) < *__m and *__m <= [__i, __last) + if (__i != __m && __comp(*__m, *__i)) + { + swap(*__i, *__m); + ++__n_swaps; + } + // [__first, __i) < *__i and *__i <= [__i+1, __last) + // If we were given a perfect partition, see if insertion sort is quick... + if (__n_swaps == 0) + { + bool __fs = _VSTD::__insertion_sort_incomplete<_Compare>(__first, __i, __comp); + if (_VSTD::__insertion_sort_incomplete<_Compare>(__i+1, __last, __comp)) + { + if (__fs) + return; + __last = __i; + continue; + } + else + { + if (__fs) + { + __first = ++__i; + continue; + } + } + } + // sort smaller range with recursive call and larger with tail recursion elimination + if (__i - __first < __last - __i) + { + _VSTD::__sort<_Compare>(__first, __i, __comp); + // _VSTD::__sort<_Compare>(__i+1, __last, __comp); + __first = ++__i; + } + else + { + _VSTD::__sort<_Compare>(__i+1, __last, __comp); + // _VSTD::__sort<_Compare>(__first, __i, __comp); + __last = __i; + } + } +} + +// This forwarder keeps the top call and the recursive calls using the same instantiation, forcing a reference _Compare +template <class _RandomAccessIterator, class _Compare> +inline _LIBCPP_INLINE_VISIBILITY +void +sort(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp) +{ +#ifdef _LIBCPP_DEBUG2 + typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref; + __debug_less<_Compare> __c(__comp); + __sort<_Comp_ref>(__first, __last, __c); +#else // _LIBCPP_DEBUG2 + typedef typename add_lvalue_reference<_Compare>::type _Comp_ref; + __sort<_Comp_ref>(__first, __last, __comp); +#endif // _LIBCPP_DEBUG2 +} + +template <class _RandomAccessIterator> +inline _LIBCPP_INLINE_VISIBILITY +void +sort(_RandomAccessIterator __first, _RandomAccessIterator __last) +{ + _VSTD::sort(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>()); +} + +template <class _Tp> +inline _LIBCPP_INLINE_VISIBILITY +void +sort(_Tp** __first, _Tp** __last) +{ + _VSTD::sort((size_t*)__first, (size_t*)__last, __less<size_t>()); +} + +template <class _Tp> +inline _LIBCPP_INLINE_VISIBILITY +void +sort(__wrap_iter<_Tp*> __first, __wrap_iter<_Tp*> __last) +{ + _VSTD::sort(__first.base(), __last.base()); +} + +template <class _Tp, class _Compare> +inline _LIBCPP_INLINE_VISIBILITY +void +sort(__wrap_iter<_Tp*> __first, __wrap_iter<_Tp*> __last, _Compare __comp) +{ + typedef typename add_lvalue_reference<_Compare>::type _Comp_ref; + _VSTD::sort<_Tp*, _Comp_ref>(__first.base(), __last.base(), __comp); +} + +#ifdef _MSC_VER +#pragma warning( push ) +#pragma warning( disable: 4231) +#endif // _MSC_VER +extern template void __sort<__less<char>&, char*>(char*, char*, __less<char>&); +extern template void __sort<__less<wchar_t>&, wchar_t*>(wchar_t*, wchar_t*, __less<wchar_t>&); +extern template void __sort<__less<signed char>&, signed char*>(signed char*, signed char*, __less<signed char>&); +extern template void __sort<__less<unsigned char>&, unsigned char*>(unsigned char*, unsigned char*, __less<unsigned char>&); +extern template void __sort<__less<short>&, short*>(short*, short*, __less<short>&); +extern template void __sort<__less<unsigned short>&, unsigned short*>(unsigned short*, unsigned short*, __less<unsigned short>&); +extern template void __sort<__less<int>&, int*>(int*, int*, __less<int>&); +extern template void __sort<__less<unsigned>&, unsigned*>(unsigned*, unsigned*, __less<unsigned>&); +extern template void __sort<__less<long>&, long*>(long*, long*, __less<long>&); +extern template void __sort<__less<unsigned long>&, unsigned long*>(unsigned long*, unsigned long*, __less<unsigned long>&); +extern template void __sort<__less<long long>&, long long*>(long long*, long long*, __less<long long>&); +extern template void __sort<__less<unsigned long long>&, unsigned long long*>(unsigned long long*, unsigned long long*, __less<unsigned long long>&); +extern template void __sort<__less<float>&, float*>(float*, float*, __less<float>&); +extern template void __sort<__less<double>&, double*>(double*, double*, __less<double>&); +extern template void __sort<__less<long double>&, long double*>(long double*, long double*, __less<long double>&); + +extern template bool __insertion_sort_incomplete<__less<char>&, char*>(char*, char*, __less<char>&); +extern template bool __insertion_sort_incomplete<__less<wchar_t>&, wchar_t*>(wchar_t*, wchar_t*, __less<wchar_t>&); +extern template bool __insertion_sort_incomplete<__less<signed char>&, signed char*>(signed char*, signed char*, __less<signed char>&); +extern template bool __insertion_sort_incomplete<__less<unsigned char>&, unsigned char*>(unsigned char*, unsigned char*, __less<unsigned char>&); +extern template bool __insertion_sort_incomplete<__less<short>&, short*>(short*, short*, __less<short>&); +extern template bool __insertion_sort_incomplete<__less<unsigned short>&, unsigned short*>(unsigned short*, unsigned short*, __less<unsigned short>&); +extern template bool __insertion_sort_incomplete<__less<int>&, int*>(int*, int*, __less<int>&); +extern template bool __insertion_sort_incomplete<__less<unsigned>&, unsigned*>(unsigned*, unsigned*, __less<unsigned>&); +extern template bool __insertion_sort_incomplete<__less<long>&, long*>(long*, long*, __less<long>&); +extern template bool __insertion_sort_incomplete<__less<unsigned long>&, unsigned long*>(unsigned long*, unsigned long*, __less<unsigned long>&); +extern template bool __insertion_sort_incomplete<__less<long long>&, long long*>(long long*, long long*, __less<long long>&); +extern template bool __insertion_sort_incomplete<__less<unsigned long long>&, unsigned long long*>(unsigned long long*, unsigned long long*, __less<unsigned long long>&); +extern template bool __insertion_sort_incomplete<__less<float>&, float*>(float*, float*, __less<float>&); +extern template bool __insertion_sort_incomplete<__less<double>&, double*>(double*, double*, __less<double>&); +extern template bool __insertion_sort_incomplete<__less<long double>&, long double*>(long double*, long double*, __less<long double>&); + +extern template unsigned __sort5<__less<long double>&, long double*>(long double*, long double*, long double*, long double*, long double*, __less<long double>&); +#ifdef _MSC_VER +#pragma warning( pop ) +#endif _MSC_VER + +// lower_bound + +template <class _Compare, class _ForwardIterator, class _Tp> +_ForwardIterator +__lower_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp) +{ + typedef typename iterator_traits<_ForwardIterator>::difference_type difference_type; + difference_type __len = _VSTD::distance(__first, __last); + while (__len != 0) + { + difference_type __l2 = __len / 2; + _ForwardIterator __m = __first; + _VSTD::advance(__m, __l2); + if (__comp(*__m, __value_)) + { + __first = ++__m; + __len -= __l2 + 1; + } + else + __len = __l2; + } + return __first; +} + +template <class _ForwardIterator, class _Tp, class _Compare> +inline _LIBCPP_INLINE_VISIBILITY +_ForwardIterator +lower_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp) +{ +#ifdef _LIBCPP_DEBUG2 + typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref; + __debug_less<_Compare> __c(__comp); + return __lower_bound<_Comp_ref>(__first, __last, __value_, __c); +#else // _LIBCPP_DEBUG2 + typedef typename add_lvalue_reference<_Compare>::type _Comp_ref; + return __lower_bound<_Comp_ref>(__first, __last, __value_, __comp); +#endif // _LIBCPP_DEBUG2 +} + +template <class _ForwardIterator, class _Tp> +inline _LIBCPP_INLINE_VISIBILITY +_ForwardIterator +lower_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_) +{ + return _VSTD::lower_bound(__first, __last, __value_, + __less<typename iterator_traits<_ForwardIterator>::value_type, _Tp>()); +} + +// upper_bound + +template <class _Compare, class _ForwardIterator, class _Tp> +_ForwardIterator +__upper_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp) +{ + typedef typename iterator_traits<_ForwardIterator>::difference_type difference_type; + difference_type __len = _VSTD::distance(__first, __last); + while (__len != 0) + { + difference_type __l2 = __len / 2; + _ForwardIterator __m = __first; + _VSTD::advance(__m, __l2); + if (__comp(__value_, *__m)) + __len = __l2; + else + { + __first = ++__m; + __len -= __l2 + 1; + } + } + return __first; +} + +template <class _ForwardIterator, class _Tp, class _Compare> +inline _LIBCPP_INLINE_VISIBILITY +_ForwardIterator +upper_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp) +{ +#ifdef _LIBCPP_DEBUG2 + typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref; + __debug_less<_Compare> __c(__comp); + return __upper_bound<_Comp_ref>(__first, __last, __value_, __c); +#else // _LIBCPP_DEBUG2 + typedef typename add_lvalue_reference<_Compare>::type _Comp_ref; + return __upper_bound<_Comp_ref>(__first, __last, __value_, __comp); +#endif // _LIBCPP_DEBUG2 +} + +template <class _ForwardIterator, class _Tp> +inline _LIBCPP_INLINE_VISIBILITY +_ForwardIterator +upper_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_) +{ + return _VSTD::upper_bound(__first, __last, __value_, + __less<_Tp, typename iterator_traits<_ForwardIterator>::value_type>()); +} + +// equal_range + +template <class _Compare, class _ForwardIterator, class _Tp> +pair<_ForwardIterator, _ForwardIterator> +__equal_range(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp) +{ + typedef typename iterator_traits<_ForwardIterator>::difference_type difference_type; + difference_type __len = _VSTD::distance(__first, __last); + while (__len != 0) + { + difference_type __l2 = __len / 2; + _ForwardIterator __m = __first; + _VSTD::advance(__m, __l2); + if (__comp(*__m, __value_)) + { + __first = ++__m; + __len -= __l2 + 1; + } + else if (__comp(__value_, *__m)) + { + __last = __m; + __len = __l2; + } + else + { + _ForwardIterator __mp1 = __m; + return pair<_ForwardIterator, _ForwardIterator> + ( + __lower_bound<_Compare>(__first, __m, __value_, __comp), + __upper_bound<_Compare>(++__mp1, __last, __value_, __comp) + ); + } + } + return pair<_ForwardIterator, _ForwardIterator>(__first, __first); +} + +template <class _ForwardIterator, class _Tp, class _Compare> +inline _LIBCPP_INLINE_VISIBILITY +pair<_ForwardIterator, _ForwardIterator> +equal_range(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp) +{ +#ifdef _LIBCPP_DEBUG2 + typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref; + __debug_less<_Compare> __c(__comp); + return __equal_range<_Comp_ref>(__first, __last, __value_, __c); +#else // _LIBCPP_DEBUG2 + typedef typename add_lvalue_reference<_Compare>::type _Comp_ref; + return __equal_range<_Comp_ref>(__first, __last, __value_, __comp); +#endif // _LIBCPP_DEBUG2 +} + +template <class _ForwardIterator, class _Tp> +inline _LIBCPP_INLINE_VISIBILITY +pair<_ForwardIterator, _ForwardIterator> +equal_range(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_) +{ + return _VSTD::equal_range(__first, __last, __value_, + __less<typename iterator_traits<_ForwardIterator>::value_type, _Tp>()); +} + +// binary_search + +template <class _Compare, class _ForwardIterator, class _Tp> +inline _LIBCPP_INLINE_VISIBILITY +bool +__binary_search(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp) +{ + __first = __lower_bound<_Compare>(__first, __last, __value_, __comp); + return __first != __last && !__comp(__value_, *__first); +} + +template <class _ForwardIterator, class _Tp, class _Compare> +inline _LIBCPP_INLINE_VISIBILITY +bool +binary_search(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp) +{ +#ifdef _LIBCPP_DEBUG2 + typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref; + __debug_less<_Compare> __c(__comp); + return __binary_search<_Com |