From ba79d0b42d20b05e7b8a3018cfadcf0e1aa7c954 Mon Sep 17 00:00:00 2001 From: bolero-MURAKAMI Date: Sat, 8 Oct 2011 17:44:50 +0900 Subject: [PATCH] =?UTF-8?q?=E3=82=B3=E3=83=B3=E3=83=86=E3=83=8A=E3=81=AE?= =?UTF-8?q?=E3=82=B3=E3=83=BC=E3=83=89=E6=95=B4=E7=90=86=20index=5Fiterato?= =?UTF-8?q?r=20=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- sprout/array.hpp | 154 ++++---- sprout/iterator/index_iterator.hpp | 2 +- sprout/null_array.hpp | 62 ++-- sprout/string.hpp | 566 +++++++++++++++-------------- sprout/sub_array.hpp | 108 +++--- 5 files changed, 480 insertions(+), 412 deletions(-) diff --git a/sprout/array.hpp b/sprout/array.hpp index 3ca32ffc..6b85d320 100644 --- a/sprout/array.hpp +++ b/sprout/array.hpp @@ -45,20 +45,13 @@ namespace sprout { public: T elems[N ? N : 1]; public: - SPROUT_CONSTEXPR size_type size() const SPROUT_NOEXCEPT { - return N; + void fill(const_reference value) { + std::fill_n(begin(), size(), value); } - SPROUT_CONSTEXPR bool empty() const SPROUT_NOEXCEPT { - return N == 0; - } - SPROUT_CONSTEXPR size_type max_size() const SPROUT_NOEXCEPT { - return size(); - } - void rangecheck(size_type i) const { - if (i >= size()) { - throw std::out_of_range("array<>: index out of range"); - } + void swap(array& other) SPROUT_NOEXCEPT_EXPR(SPROUT_NOEXCEPT_EXPR(std::swap(std::declval(), std::declval()))) { + std::swap_ranges(other.begin(), other.end(), begin()); } + // iterators: #if SPROUT_USE_INDEX_ITERATOR_IMPLEMENTATION iterator begin() SPROUT_NOEXCEPT { return iterator(*this, 0); @@ -66,18 +59,12 @@ namespace sprout { SPROUT_CONSTEXPR const_iterator begin() const SPROUT_NOEXCEPT { return const_iterator(*this, 0); } - SPROUT_CONSTEXPR const_iterator cbegin() const SPROUT_NOEXCEPT { - return const_iterator(*this, 0); - } iterator end() SPROUT_NOEXCEPT { return iterator(*this, size()); } SPROUT_CONSTEXPR const_iterator end() const SPROUT_NOEXCEPT { return const_iterator(*this, size()); } - SPROUT_CONSTEXPR const_iterator cend() const SPROUT_NOEXCEPT { - return const_iterator(*this, size()); - } #else iterator begin() SPROUT_NOEXCEPT { return &elems[0]; @@ -85,18 +72,12 @@ namespace sprout { SPROUT_CONSTEXPR const_iterator begin() const SPROUT_NOEXCEPT { return &elems[0]; } - SPROUT_CONSTEXPR const_iterator cbegin() const SPROUT_NOEXCEPT { - return &elems[0]; - } iterator end() SPROUT_NOEXCEPT { return &elems[0] + size(); } SPROUT_CONSTEXPR const_iterator end() const SPROUT_NOEXCEPT { return &elems[0] + size(); } - SPROUT_CONSTEXPR const_iterator cend() const SPROUT_NOEXCEPT { - return &elems[0] + size(); - } #endif reverse_iterator rbegin() SPROUT_NOEXCEPT { return reverse_iterator(end()); @@ -104,18 +85,44 @@ namespace sprout { SPROUT_CONSTEXPR const_reverse_iterator rbegin() const SPROUT_NOEXCEPT { return const_reverse_iterator(end()); } - SPROUT_CONSTEXPR const_reverse_iterator crbegin() const SPROUT_NOEXCEPT { - return const_reverse_iterator(end()); - } reverse_iterator rend() SPROUT_NOEXCEPT { return reverse_iterator(begin()); } SPROUT_CONSTEXPR const_reverse_iterator rend() const SPROUT_NOEXCEPT { return const_reverse_iterator(begin()); } +#if SPROUT_USE_INDEX_ITERATOR_IMPLEMENTATION + SPROUT_CONSTEXPR const_iterator cbegin() const SPROUT_NOEXCEPT { + return const_iterator(*this, 0); + } + SPROUT_CONSTEXPR const_iterator cend() const SPROUT_NOEXCEPT { + return const_iterator(*this, size()); + } +#else + SPROUT_CONSTEXPR const_iterator cbegin() const SPROUT_NOEXCEPT { + return &elems[0]; + } + SPROUT_CONSTEXPR const_iterator cend() const SPROUT_NOEXCEPT { + return &elems[0] + size(); + } +#endif + SPROUT_CONSTEXPR const_reverse_iterator crbegin() const SPROUT_NOEXCEPT { + return const_reverse_iterator(end()); + } SPROUT_CONSTEXPR const_reverse_iterator crend() const SPROUT_NOEXCEPT { return const_reverse_iterator(begin()); } + // capacity: + SPROUT_CONSTEXPR size_type size() const SPROUT_NOEXCEPT { + return N; + } + SPROUT_CONSTEXPR size_type max_size() const SPROUT_NOEXCEPT { + return size(); + } + SPROUT_CONSTEXPR bool empty() const SPROUT_NOEXCEPT { + return N == 0; + } + // element access: reference operator[](size_type i) { return elems[i]; } @@ -142,24 +149,14 @@ namespace sprout { SPROUT_CONSTEXPR const_reference back() const { return elems[size() - 1]; } + pointer data() SPROUT_NOEXCEPT { return &elems[0]; } SPROUT_CONSTEXPR const_pointer data() const SPROUT_NOEXCEPT { return &elems[0]; } - pointer c_array() SPROUT_NOEXCEPT { - return &elems[0]; - } - void assign(const_reference value) { - fill(value); - } - void fill(const_reference value) { - std::fill_n(begin(), size(), value); - } - void swap(array& other) SPROUT_NOEXCEPT_EXPR(SPROUT_NOEXCEPT_EXPR(std::swap(std::declval(), std::declval()))) { - std::swap_ranges(other.begin(), other.end(), begin()); - } + // others: template array& operator=(array const& rhs) { std::copy(rhs.begin(), rhs.end(), begin()); @@ -170,7 +167,26 @@ namespace sprout { std::move(rhs.begin(), rhs.end(), begin()); return *this; } + pointer c_array() SPROUT_NOEXCEPT { + return &elems[0]; + } + void assign(const_reference value) { + fill(value); + } + void rangecheck(size_type i) const { + if (i >= size()) { + throw std::out_of_range("array<>: index out of range"); + } + } }; + + // + // operator!= + // operator< + // operator> + // operator<= + // operator>= + // template SPROUT_CONSTEXPR inline bool operator==(sprout::array const& lhs, sprout::array const& rhs) { return NS_SSCRISK_CEL_OR_SPROUT_DETAIL::equal(lhs.begin(), lhs.end(), rhs.begin()); @@ -217,36 +233,6 @@ namespace sprout { }; }; - namespace detail { - template - struct is_array_impl { - public: - typedef std::integral_constant type; - SPROUT_STATIC_CONSTEXPR bool value = type::value; - }; - template - struct is_array_impl< - T, - typename std::enable_if< - std::is_same< - T, - sprout::array - >::value - >::type - > { - public: - typedef std::integral_constant type; - SPROUT_STATIC_CONSTEXPR bool value = type::value; - }; - } // namespace detail - // - // is_array - // - template - struct is_array - : public sprout::detail::is_array_impl - {}; - // // make_array // @@ -276,6 +262,36 @@ namespace sprout { SPROUT_CONSTEXPR inline sprout::array to_array(T const (& arr)[N]) { return sprout::detail::to_array_impl(arr, typename sprout::index_range<0, N>::type()); } + + namespace detail { + template + struct is_array_impl { + public: + typedef std::integral_constant type; + SPROUT_STATIC_CONSTEXPR bool value = type::value; + }; + template + struct is_array_impl< + T, + typename std::enable_if< + std::is_same< + T, + sprout::array + >::value + >::type + > { + public: + typedef std::integral_constant type; + SPROUT_STATIC_CONSTEXPR bool value = type::value; + }; + } // namespace detail + // + // is_array + // + template + struct is_array + : public sprout::detail::is_array_impl + {}; } // namespace sprout namespace std { diff --git a/sprout/iterator/index_iterator.hpp b/sprout/iterator/index_iterator.hpp index ccd604c6..40515446 100644 --- a/sprout/iterator/index_iterator.hpp +++ b/sprout/iterator/index_iterator.hpp @@ -160,7 +160,7 @@ namespace sprout { return holder_.get()[index_ + n]; } friend difference_type operator-(index_iterator const& lhs, index_iterator const& rhs) { - return static_cast(rhs.index_) - static_cast(lhs.index_); + return static_cast(lhs.index_) - static_cast(rhs.index_); } friend SPROUT_CONSTEXPR index_iterator operator+(difference_type n, index_iterator const& it) { return it + n; diff --git a/sprout/null_array.hpp b/sprout/null_array.hpp index cfc54da4..bfcbaf1c 100644 --- a/sprout/null_array.hpp +++ b/sprout/null_array.hpp @@ -39,56 +39,58 @@ namespace sprout { value_type elem; public: null_array() = default; - SPROUT_CONSTEXPR size_type size() const SPROUT_NOEXCEPT { - return static_size; - } - SPROUT_CONSTEXPR bool empty() const SPROUT_NOEXCEPT { - return static_size == 0; - } - SPROUT_CONSTEXPR size_type max_size() const SPROUT_NOEXCEPT { - return size(); - } - void rangecheck(size_type i) const { - if (i >= size()) { - throw std::out_of_range("null_array<>: index out of range"); - } + void swap(null_array& other) SPROUT_NOEXCEPT_EXPR(SPROUT_NOEXCEPT_EXPR(std::swap(std::declval(), std::declval()))) { + using std::swap; + swap(elem, other.elem); } + // iterators: iterator begin() { return iterator(elem, static_size); } SPROUT_CONSTEXPR const_iterator begin() const { return const_iterator(elem, static_size); } - SPROUT_CONSTEXPR const_iterator cbegin() const SPROUT_NOEXCEPT { - return const_iterator(elem, static_size); - } iterator end() SPROUT_NOEXCEPT { return iterator(); } SPROUT_CONSTEXPR const_iterator end() const SPROUT_NOEXCEPT { return const_iterator(); } - SPROUT_CONSTEXPR const_iterator cend() const SPROUT_NOEXCEPT { - return const_iterator(); - } reverse_iterator rbegin() SPROUT_NOEXCEPT { return reverse_iterator(end()); } SPROUT_CONSTEXPR const_reverse_iterator rbegin() const SPROUT_NOEXCEPT { return const_reverse_iterator(end()); } - SPROUT_CONSTEXPR const_reverse_iterator crbegin() const SPROUT_NOEXCEPT { - return const_reverse_iterator(end()); - } reverse_iterator rend() SPROUT_NOEXCEPT { return reverse_iterator(begin()); } SPROUT_CONSTEXPR const_reverse_iterator rend() const SPROUT_NOEXCEPT { return const_reverse_iterator(begin()); } + SPROUT_CONSTEXPR const_iterator cbegin() const SPROUT_NOEXCEPT { + return const_iterator(elem, static_size); + } + SPROUT_CONSTEXPR const_iterator cend() const SPROUT_NOEXCEPT { + return const_iterator(); + } + SPROUT_CONSTEXPR const_reverse_iterator crbegin() const SPROUT_NOEXCEPT { + return const_reverse_iterator(end()); + } SPROUT_CONSTEXPR const_reverse_iterator crend() const SPROUT_NOEXCEPT { return const_reverse_iterator(begin()); } + // capacity: + SPROUT_CONSTEXPR size_type size() const SPROUT_NOEXCEPT { + return static_size; + } + SPROUT_CONSTEXPR size_type max_size() const SPROUT_NOEXCEPT { + return size(); + } + SPROUT_CONSTEXPR bool empty() const SPROUT_NOEXCEPT { + return static_size == 0; + } + // element access: reference operator[](size_type i) { return elem; } @@ -115,11 +117,21 @@ namespace sprout { SPROUT_CONSTEXPR const_reference back() const { return elem; } - void swap(null_array& other) SPROUT_NOEXCEPT_EXPR(SPROUT_NOEXCEPT_EXPR(std::swap(std::declval(), std::declval()))) { - using std::swap; - swap(elem, other.elem); + // others: + void rangecheck(size_type i) const { + if (i >= size()) { + throw std::out_of_range("null_array<>: index out of range"); + } } }; + + // + // operator!= + // operator< + // operator> + // operator<= + // operator>= + // template SPROUT_CONSTEXPR inline bool operator==(sprout::null_array const& lhs, sprout::null_array const& rhs) { return lhs.front() == rhs.front(); diff --git a/sprout/string.hpp b/sprout/string.hpp index 9544b664..164abd70 100644 --- a/sprout/string.hpp +++ b/sprout/string.hpp @@ -202,28 +202,18 @@ namespace sprout { T elems[N + 1]; size_type len; public: - SPROUT_CONSTEXPR size_type size() const SPROUT_NOEXCEPT { - return len; + // construct/copy/destroy: + template + basic_string& operator=(basic_string const& rhs) { + return assign(rhs); } - SPROUT_CONSTEXPR size_type length() const SPROUT_NOEXCEPT { - return size(); + basic_string& operator=(value_type const* rhs) { + return assign(rhs); } - SPROUT_CONSTEXPR bool empty() const SPROUT_NOEXCEPT { - return size() == 0; - } - SPROUT_CONSTEXPR size_type max_size() const SPROUT_NOEXCEPT { - return N; - } - void rangecheck(size_type i) const { - if (i >= size()) { - throw std::out_of_range("basic_string<>: index out of range"); - } - } - void maxcheck(size_type n) const { - if (n > max_size()) { - throw std::out_of_range("basic_string<>: index out of range"); - } + basic_string& operator=(value_type rhs) { + return assign(1, rhs); } + // iterators: #if SPROUT_USE_INDEX_ITERATOR_IMPLEMENTATION iterator begin() SPROUT_NOEXCEPT { return iterator(*this, 0); @@ -231,18 +221,12 @@ namespace sprout { SPROUT_CONSTEXPR const_iterator begin() const SPROUT_NOEXCEPT { return const_iterator(*this, 0); } - SPROUT_CONSTEXPR const_iterator cbegin() const SPROUT_NOEXCEPT { - return const_iterator(*this, 0); - } iterator end() SPROUT_NOEXCEPT { return iterator(*this, size()); } SPROUT_CONSTEXPR const_iterator end() const SPROUT_NOEXCEPT { return const_iterator(*this, size()); } - SPROUT_CONSTEXPR const_iterator cend() const SPROUT_NOEXCEPT { - return const_iterator(*this, size()); - } #else iterator begin() SPROUT_NOEXCEPT { return &elems[0]; @@ -250,18 +234,12 @@ namespace sprout { SPROUT_CONSTEXPR const_iterator begin() const SPROUT_NOEXCEPT { return &elems[0]; } - SPROUT_CONSTEXPR const_iterator cbegin() const SPROUT_NOEXCEPT { - return &elems[0]; - } iterator end() SPROUT_NOEXCEPT { return &elems[0] + size(); } SPROUT_CONSTEXPR const_iterator end() const SPROUT_NOEXCEPT { return &elems[0] + size(); } - SPROUT_CONSTEXPR const_iterator cend() const SPROUT_NOEXCEPT { - return &elems[0] + size(); - } #endif reverse_iterator rbegin() SPROUT_NOEXCEPT { return const_reverse_iterator(end()); @@ -269,18 +247,62 @@ namespace sprout { SPROUT_CONSTEXPR const_reverse_iterator rbegin() const SPROUT_NOEXCEPT { return const_reverse_iterator(end()); } - SPROUT_CONSTEXPR const_reverse_iterator crbegin() const SPROUT_NOEXCEPT { - return const_reverse_iterator(end()); - } reverse_iterator rend() SPROUT_NOEXCEPT { return const_reverse_iterator(begin()); } SPROUT_CONSTEXPR const_reverse_iterator rend() const SPROUT_NOEXCEPT { return const_reverse_iterator(begin()); } +#if SPROUT_USE_INDEX_ITERATOR_IMPLEMENTATION + SPROUT_CONSTEXPR const_iterator cbegin() const SPROUT_NOEXCEPT { + return const_iterator(*this, 0); + } + SPROUT_CONSTEXPR const_iterator cend() const SPROUT_NOEXCEPT { + return const_iterator(*this, size()); + } +#else + SPROUT_CONSTEXPR const_iterator cbegin() const SPROUT_NOEXCEPT { + return &elems[0]; + } + SPROUT_CONSTEXPR const_iterator cend() const SPROUT_NOEXCEPT { + return &elems[0] + size(); + } +#endif + SPROUT_CONSTEXPR const_reverse_iterator crbegin() const SPROUT_NOEXCEPT { + return const_reverse_iterator(end()); + } SPROUT_CONSTEXPR const_reverse_iterator crend() const SPROUT_NOEXCEPT { return const_reverse_iterator(begin()); } + // capacity: + SPROUT_CONSTEXPR size_type size() const SPROUT_NOEXCEPT { + return len; + } + SPROUT_CONSTEXPR size_type length() const SPROUT_NOEXCEPT { + return size(); + } + SPROUT_CONSTEXPR size_type max_size() const SPROUT_NOEXCEPT { + return N; + } + void resize(size_type n, value_type c) { + maxcheck(n); + if (n > size()) { + traits_type::assign(end(), n - size(), c); + } + traits_type::assign(begin() + n, max_size() - n, value_type()); + len = n; + } + void resize(size_type n) { + resize(n, value_type()); + } + void clear() { + traits_type::assign(begin(), max_size(), value_type()); + len = 0; + } + SPROUT_CONSTEXPR bool empty() const SPROUT_NOEXCEPT { + return size() == 0; + } + // element access: reference operator[](size_type i) { return elems[i]; } @@ -307,33 +329,7 @@ namespace sprout { SPROUT_CONSTEXPR const_reference back() const { return elems[size() - 1]; } - pointer data() SPROUT_NOEXCEPT { - return &elems[0]; - } - SPROUT_CONSTEXPR const_pointer data() const SPROUT_NOEXCEPT { - return &elems[0]; - } - pointer c_array() SPROUT_NOEXCEPT { - return &elems[0]; - } - SPROUT_CONSTEXPR const_pointer c_str() const SPROUT_NOEXCEPT { - return &elems[0]; - } - void resize(size_type n, value_type c) { - maxcheck(n); - if (n > size()) { - traits_type::assign(end(), n - size(), c); - } - traits_type::assign(begin() + n, max_size() - n, value_type()); - len = n; - } - void resize(size_type n) { - resize(n, value_type()); - } - void clear() { - traits_type::assign(begin(), max_size(), value_type()); - len = 0; - } + // modifiers: template basic_string& assign(basic_string const& str) { return assign(str.begin(), str.size()); @@ -385,15 +381,23 @@ namespace sprout { swap(len, other.len); } } - template - basic_string& operator=(basic_string const& rhs) { - return assign(rhs); + // string operations: + SPROUT_CONSTEXPR const_pointer c_str() const SPROUT_NOEXCEPT { + return &elems[0]; } - basic_string& operator=(value_type const* rhs) { - return assign(rhs); + pointer data() SPROUT_NOEXCEPT { + return &elems[0]; } - basic_string& operator=(value_type rhs) { - return assign(1, rhs); + SPROUT_CONSTEXPR const_pointer data() const SPROUT_NOEXCEPT { + return &elems[0]; + } + SPROUT_CONSTEXPR basic_string substr(size_type pos = 0, size_type n = npos) const { + return !(size() < pos) + ? n == npos + ? substr(pos, size() - pos) + : from_c_str(c_str() + pos, n) + : throw "basic_string<>: index out of range" + ; } template SPROUT_CONSTEXPR int compare(basic_string const& str) const { @@ -422,13 +426,19 @@ namespace sprout { : throw "basic_string<>: index out of range" ; } - SPROUT_CONSTEXPR basic_string substr(size_type pos = 0, size_type n = npos) const { - return !(size() < pos) - ? n == npos - ? substr(pos, size() - pos) - : from_c_str(c_str() + pos, n) - : throw "basic_string<>: index out of range" - ; + // others: + pointer c_array() SPROUT_NOEXCEPT { + return &elems[0]; + } + void rangecheck(size_type i) const { + if (i >= size()) { + throw std::out_of_range("basic_string<>: index out of range"); + } + } + void maxcheck(size_type n) const { + if (n > max_size()) { + throw std::out_of_range("basic_string<>: index out of range"); + } } #if SPROUT_USE_INDEX_ITERATOR_IMPLEMENTATION basic_string& assign(const_iterator s, size_type n) { @@ -462,6 +472,14 @@ namespace sprout { } #endif }; + + // + // operator!= + // operator< + // operator> + // operator<= + // operator>= + // template SPROUT_CONSTEXPR inline bool operator==(sprout::basic_string const& lhs, sprout::basic_string const& rhs) { return lhs.compare(rhs) == 0; @@ -547,6 +565,60 @@ namespace sprout { lhs.swap(rhs); } + // + // operator>> + // operator<< + // + template + inline std::basic_istream& operator>>(std::basic_istream& lhs, sprout::basic_string& rhs) { + typedef T elem_type; + typedef StreamTraits traits_type; + typedef std::basic_istream istream_type; + typedef sprout::basic_string string_type; + typedef std::ctype ctype_type; + typedef typename string_type::size_type size_type; + std::ios_base::iostate state = std::ios_base::goodbit; + bool changed = false; + size_type current = 0; + if (typename istream_type::sentry(lhs)) { + ctype_type const& ctype_fac = std::use_facet(lhs.getloc()); + try { + size_type remain = 0 < lhs.width() && static_cast(lhs.width()) < rhs.max_size() + ? static_cast(lhs.width()) + : rhs.max_size() + ; + typename traits_type::int_type meta = lhs.rdbuf()->sgetc(); + for (; remain; --remain, meta = lhs.rdbuf()->snextc()) + if (traits_type::eq_int_type(traits_type::eof(), meta)) { + state |= std::ios_base::eofbit; + break; + } else if (ctype_fac.is(ctype_type::space, traits_type::to_char_type(meta))) { + break; + } else { + rhs[current] = traits_type::to_char_type(meta); + changed = true; + ++current; + } + } catch (...) { + state |= std::ios_base::badbit; + } + } + lhs.width(0); + if (!changed) { + state |= std::ios_base::failbit; + } + lhs.setstate(state); + rhs.len = current; + for (; current != rhs.max_size(); ++current) { + rhs[current] = T(); + } + return lhs; + } + template + inline std::basic_ostream& operator<<(std::basic_ostream& lhs, sprout::basic_string const& rhs) { + return lhs << rhs.c_str(); + } + // // rebind_fixed_size // @@ -647,6 +719,154 @@ namespace sprout { } }; + namespace detail { + template + SPROUT_CONSTEXPR inline sprout::basic_string to_string_impl_1( + T const(& arr)[N], + typename sprout::basic_string::size_type n, + sprout::index_tuple + ) + { + return sprout::basic_string{{(Indexes < n ? arr[Indexes] : T())...}, n}; + } + template + SPROUT_CONSTEXPR inline sprout::basic_string to_string_impl( + T const(& arr)[N], + sprout::index_tuple + ) + { + return to_string_impl_1(arr, sprout::char_traits::length(arr), sprout::index_tuple()); + } + } // namespace detail + // + // to_string + // + template + SPROUT_CONSTEXPR inline sprout::basic_string to_string(T const(& arr)[N]) { + return sprout::detail::to_string_impl(arr, typename sprout::index_range<0, N - 1>::type()); + } + + // + // string_from_c_str + // + template + SPROUT_CONSTEXPR inline sprout::basic_string string_from_c_str(T const* s, std::size_t n) { + return sprout::basic_string::from_c_str(s, n); + } + template + SPROUT_CONSTEXPR inline sprout::basic_string string_from_c_str(T const* s) { + return sprout::basic_string::from_c_str(s); + } + + // + // operator+ + // + template + SPROUT_CONSTEXPR inline typename sprout::fixed::result_of::push_back, T>::type operator+( + sprout::basic_string const& lhs, + T const& rhs + ) + { + return sprout::fixed::push_back(lhs, rhs); + } + template + SPROUT_CONSTEXPR inline typename sprout::fixed::result_of::push_front, T>::type operator+( + T const& lhs, + sprout::basic_string const& rhs + ) + { + return sprout::fixed::push_front(rhs, lhs); + } + template + SPROUT_CONSTEXPR inline typename sprout::fixed::result_of::append_back< + sprout::basic_string, + sprout::basic_string + >::type operator+( + sprout::basic_string const& lhs, + T const (& rhs)[N2] + ) + { + return sprout::fixed::append_back(lhs, sprout::to_string(rhs)); + } + template + SPROUT_CONSTEXPR inline typename sprout::fixed::result_of::append_front< + sprout::basic_string, + sprout::basic_string + >::type operator+( + T const (& lhs)[N2], + sprout::basic_string const& rhs + ) + { + return sprout::fixed::append_front(rhs, sprout::to_string(lhs)); + } + template + SPROUT_CONSTEXPR inline typename sprout::fixed::result_of::append_back< + sprout::basic_string, + sprout::basic_string + >::type operator+( + sprout::basic_string const& lhs, + sprout::basic_string const& rhs + ) + { + return sprout::fixed::append_back(lhs, rhs); + } + +#if 0 + // + // string + // + template + using string = sprout::basic_string; + // + // wstring + // + template + using wstring = sprout::basic_string; + // + // u16string + // + template + using u16string = sprout::basic_string; + // + // u32string + // + template + using u32string = sprout::basic_string; +#endif // #if 0 + + // + // string_t + // + template + struct string_t { + public: + typedef sprout::basic_string type; + }; + // + // wstring_t + // + template + struct wstring_t { + public: + typedef sprout::basic_string type; + }; + // + // u16string_t + // + template + struct u16string_t { + public: + typedef sprout::basic_string type; + }; + // + // u32string_t + // + template + struct u32string_t { + public: + typedef sprout::basic_string type; + }; + namespace detail { template struct is_basic_string_impl { @@ -736,201 +956,6 @@ namespace sprout { struct is_u32string : public sprout::is_string_of {}; - - namespace detail { - template - SPROUT_CONSTEXPR inline sprout::basic_string to_string_impl_1( - T const(& arr)[N], - typename sprout::basic_string::size_type n, - sprout::index_tuple - ) - { - return sprout::basic_string{{(Indexes < n ? arr[Indexes] : T())...}, n}; - } - template - SPROUT_CONSTEXPR inline sprout::basic_string to_string_impl( - T const(& arr)[N], - sprout::index_tuple - ) - { - return to_string_impl_1(arr, sprout::char_traits::length(arr), sprout::index_tuple()); - } - } // namespace detail - // - // to_string - // - template - SPROUT_CONSTEXPR inline sprout::basic_string to_string(T const(& arr)[N]) { - return sprout::detail::to_string_impl(arr, typename sprout::index_range<0, N - 1>::type()); - } - - // - // string_from_c_str - // - template - SPROUT_CONSTEXPR inline sprout::basic_string string_from_c_str(T const* s, std::size_t n) { - return sprout::basic_string::from_c_str(s, n); - } - template - SPROUT_CONSTEXPR inline sprout::basic_string string_from_c_str(T const* s) { - return sprout::basic_string::from_c_str(s); - } - - template - SPROUT_CONSTEXPR inline typename sprout::fixed::result_of::push_back, T>::type operator+( - sprout::basic_string const& lhs, - T const& rhs - ) - { - return sprout::fixed::push_back(lhs, rhs); - } - template - SPROUT_CONSTEXPR inline typename sprout::fixed::result_of::push_front, T>::type operator+( - T const& lhs, - sprout::basic_string const& rhs - ) - { - return sprout::fixed::push_front(rhs, lhs); - } - template - SPROUT_CONSTEXPR inline typename sprout::fixed::result_of::append_back< - sprout::basic_string, - sprout::basic_string - >::type operator+( - sprout::basic_string const& lhs, - T const (& rhs)[N2] - ) - { - return sprout::fixed::append_back(lhs, sprout::to_string(rhs)); - } - template - SPROUT_CONSTEXPR inline typename sprout::fixed::result_of::append_front< - sprout::basic_string, - sprout::basic_string - >::type operator+( - T const (& lhs)[N2], - sprout::basic_string const& rhs - ) - { - return sprout::fixed::append_front(rhs, sprout::to_string(lhs)); - } - template - SPROUT_CONSTEXPR inline typename sprout::fixed::result_of::append_back< - sprout::basic_string, - sprout::basic_string - >::type operator+( - sprout::basic_string const& lhs, - sprout::basic_string const& rhs - ) - { - return sprout::fixed::append_back(lhs, rhs); - } - - template - inline std::basic_ostream& operator<<(std::basic_ostream& lhs, sprout::basic_string const& rhs) { - return lhs << rhs.c_str(); - } - template - inline std::basic_istream& operator>>(std::basic_istream& lhs, sprout::basic_string& rhs) { - typedef T elem_type; - typedef StreamTraits traits_type; - typedef std::basic_istream istream_type; - typedef sprout::basic_string string_type; - typedef std::ctype ctype_type; - typedef typename string_type::size_type size_type; - std::ios_base::iostate state = std::ios_base::goodbit; - bool changed = false; - size_type current = 0; - if (typename istream_type::sentry(lhs)) { - ctype_type const& ctype_fac = std::use_facet(lhs.getloc()); - try { - size_type remain = 0 < lhs.width() && static_cast(lhs.width()) < rhs.max_size() - ? static_cast(lhs.width()) - : rhs.max_size() - ; - typename traits_type::int_type meta = lhs.rdbuf()->sgetc(); - for (; remain; --remain, meta = lhs.rdbuf()->snextc()) - if (traits_type::eq_int_type(traits_type::eof(), meta)) { - state |= std::ios_base::eofbit; - break; - } else if (ctype_fac.is(ctype_type::space, traits_type::to_char_type(meta))) { - break; - } else { - rhs[current] = traits_type::to_char_type(meta); - changed = true; - ++current; - } - } catch (...) { - state |= std::ios_base::badbit; - } - } - lhs.width(0); - if (!changed) { - state |= std::ios_base::failbit; - } - lhs.setstate(state); - rhs.len = current; - for (; current != rhs.max_size(); ++current) { - rhs[current] = T(); - } - return lhs; - } - -#if 0 - // - // string - // - template - using string = sprout::basic_string; - // - // wstring - // - template - using wstring = sprout::basic_string; - // - // u16string - // - template - using u16string = sprout::basic_string; - // - // u32string - // - template - using u32string = sprout::basic_string; -#endif // #if 0 - - // - // string_t - // - template - struct string_t { - public: - typedef sprout::basic_string type; - }; - // - // wstring_t - // - template - struct wstring_t { - public: - typedef sprout::basic_string type; - }; - // - // u16string_t - // - template - struct u16string_t { - public: - typedef sprout::basic_string type; - }; - // - // u32string_t - // - template - struct u32string_t { - public: - typedef sprout::basic_string type; - }; } // namespace sprout namespace std { @@ -974,4 +999,3 @@ namespace std { } // namespace std #endif // #ifndef SPROUT_STRING_HPP - diff --git a/sprout/sub_array.hpp b/sprout/sub_array.hpp index cf30f541..da8600e5 100644 --- a/sprout/sub_array.hpp +++ b/sprout/sub_array.hpp @@ -224,6 +224,7 @@ namespace sprout { using impl_type::first_; using impl_type::last_; public: + // construct/copy/destroy: sub_array() = default; SPROUT_CONSTEXPR sub_array(param_type arr, const_iterator first, const_iterator last) : impl_type( @@ -261,53 +262,47 @@ namespace sprout { last + other.first_ ) {} - param_type get_fixed() { - return impl_type::template to_param(array_); + + void fill(const_reference value) { + std::fill_n(begin(), size(), value); } - SPROUT_CONSTEXPR const_param_type get_fixed() const { - return impl_type::template to_const_param(array_); - } - SPROUT_CONSTEXPR const_param_type get_cfixed() const { - return impl_type::template to_const_param(array_); - } - param_type get_array() { - return impl_type::template to_param(array_); - } - SPROUT_CONSTEXPR const_param_type get_array() const { - return impl_type::template to_const_param(array_); - } - SPROUT_CONSTEXPR size_type size() const { - return last_ - first_; - } - SPROUT_CONSTEXPR bool empty() const { - return size() == 0; - } - SPROUT_CONSTEXPR size_type max_size() const { - return size(); - } - void rangecheck(size_type i) const { - if (i >= size()) { - throw std::out_of_range("sub_array<>: index out of range"); - } + template + void swap(sub_array& other) { + using std::swap; + swap(other.array_, array_); + swap(other.first_, first_); + swap(other.last_, last_); } + // iterators: iterator begin() { return sprout::next(sprout::begin(get_array()), first_); } SPROUT_CONSTEXPR const_iterator begin() const { return sprout::next(sprout::begin(get_array()), first_); } - SPROUT_CONSTEXPR const_iterator cbegin() const { - return sprout::next(sprout::begin(get_array()), first_); - } iterator end() { return sprout::next(sprout::begin(get_array()), last_); } SPROUT_CONSTEXPR const_iterator end() const { return sprout::next(sprout::begin(get_array()), last_); } + SPROUT_CONSTEXPR const_iterator cbegin() const { + return sprout::next(sprout::begin(get_array()), first_); + } SPROUT_CONSTEXPR const_iterator cend() const { return sprout::next(sprout::begin(get_array()), last_); } + // capacity: + SPROUT_CONSTEXPR size_type size() const { + return last_ - first_; + } + SPROUT_CONSTEXPR size_type max_size() const { + return size(); + } + SPROUT_CONSTEXPR bool empty() const { + return size() == 0; + } + // element access: reference operator[](size_type i) { return *sprout::next(sprout::begin(get_array()), first_ + i); } @@ -334,28 +329,14 @@ namespace sprout { SPROUT_CONSTEXPR const_reference back() const { return *sprout::next(sprout::begin(get_array()), last_ - 1); } + pointer data() { return get_array().data() + first_; } SPROUT_CONSTEXPR const_pointer data() const { return get_array().data() + first_; } - pointer c_array() { - return data(); - } - void assign (const_reference value) { - fill(value); - } - void fill(const_reference value) { - std::fill_n(begin(), size(), value); - } - template - void swap(sub_array& other) { - using std::swap; - swap(other.array_, array_); - swap(other.first_, first_); - swap(other.last_, last_); - } + // others: template sub_array& operator=(sub_array const& rhs) { array_ = rhs.array_; @@ -370,7 +351,42 @@ namespace sprout { last_ = std::move(rhs.last_); return *this; } + pointer c_array() { + return data(); + } + void assign(const_reference value) { + fill(value); + } + void rangecheck(size_type i) const { + if (i >= size()) { + throw std::out_of_range("sub_array<>: index out of range"); + } + } + + param_type get_fixed() { + return impl_type::template to_param(array_); + } + SPROUT_CONSTEXPR const_param_type get_fixed() const { + return impl_type::template to_const_param(array_); + } + SPROUT_CONSTEXPR const_param_type get_cfixed() const { + return impl_type::template to_const_param(array_); + } + param_type get_array() { + return impl_type::template to_param(array_); + } + SPROUT_CONSTEXPR const_param_type get_array() const { + return impl_type::template to_const_param(array_); + } }; + + // + // operator!= + // operator< + // operator> + // operator<= + // operator>= + // template SPROUT_CONSTEXPR inline bool operator==(sprout::sub_array const& lhs, sprout::sub_array const& rhs) { return NS_SSCRISK_CEL_OR_SPROUT_DETAIL::equal(sprout::begin(lhs), sprout::end(lhs), sprout::begin(rhs));