1
0
Fork 0
mirror of https://github.com/bolero-MURAKAMI/Sprout synced 2024-11-12 21:09:01 +00:00

add non-member function nth(), index_of()

This commit is contained in:
bolero-MURAKAMI 2015-03-23 00:26:20 +09:00
parent 6a9cda9d4c
commit c3636c10d2
7 changed files with 390 additions and 6 deletions

View file

@ -26,9 +26,7 @@
# include <sprout/preprocessor/cat.hpp>
# include <sprout/preprocessor/variadic/size.hpp>
#endif
#if !(defined(SPROUT_DISABLE_ASSERTS) || defined(NDEBUG))
# include <sprout/preprocessor/stringize.hpp>
#endif
#include <sprout/preprocessor/stringize.hpp>
//
// SPROUT_ASSERTION_FAILED_FORMAT

View file

@ -337,6 +337,150 @@ namespace sprout {
}
};
template<typename T>
struct has_mem_nth_test {
public:
template<
typename U = T,
typename = typename sprout::identity<decltype(std::declval<U>().nth(std::declval<typename sprout::container_traits<U>::size_type>()))>::type
>
static sprout::true_type test(int);
static sprout::false_type test(...);
};
#if defined(_MSC_VER)
template<typename T, typename Base_ = typename sprout::identity<decltype(sprout::detail::has_mem_nth_test<T>::test(0))>::type>
struct has_mem_nth
: public Base_
{};
#else
template<typename T>
struct has_mem_nth
: public sprout::identity<decltype(sprout::detail::has_mem_nth_test<T>::test(0))>::type
{};
#endif
template<typename Container, typename = void>
struct container_range_traits_range_nth_impl;
template<typename Container>
struct container_range_traits_range_nth_impl<
Container,
typename std::enable_if<sprout::detail::has_mem_nth<Container>::value>::type
> {
public:
static SPROUT_CONSTEXPR typename sprout::container_traits<Container>::iterator
range_nth(Container& cont, typename sprout::container_traits<Container>::size_type i) {
return cont.nth(i);
}
};
template<typename Container>
struct container_range_traits_range_nth_impl<
Container,
typename std::enable_if<!sprout::detail::has_mem_nth<Container>::value>::type
> {
public:
static SPROUT_CONSTEXPR typename sprout::container_traits<Container>::iterator
range_nth(Container& cont, typename sprout::container_traits<Container>::size_type i) {
return sprout::next(sprout::begin(cont), i);
}
};
template<typename Container, typename = void>
struct container_range_traits_range_nth_const_impl;
template<typename Container>
struct container_range_traits_range_nth_const_impl<
Container,
typename std::enable_if<sprout::detail::has_mem_nth<Container const>::value>::type
> {
public:
static SPROUT_CONSTEXPR typename sprout::container_traits<Container const>::iterator
range_nth(Container const& cont, typename sprout::container_traits<Container const>::size_type i) {
return cont.nth(i);
}
};
template<typename Container>
struct container_range_traits_range_nth_const_impl<
Container,
typename std::enable_if<!sprout::detail::has_mem_nth<Container const>::value>::type
> {
public:
static SPROUT_CONSTEXPR typename sprout::container_traits<Container const>::iterator
range_nth(Container const& cont, typename sprout::container_traits<Container const>::size_type i) {
return sprout::next(sprout::begin(cont), i);
}
};
template<typename T>
struct has_mem_index_of_test {
public:
template<
typename U = T,
typename = typename sprout::identity<decltype(std::declval<U>().index_of(std::declval<typename sprout::container_traits<U>::iterator>()))>::type
>
static sprout::true_type test(int);
static sprout::false_type test(...);
};
#if defined(_MSC_VER)
template<typename T, typename Base_ = typename sprout::identity<decltype(sprout::detail::has_mem_index_of_test<T>::test(0))>::type>
struct has_mem_index_of
: public Base_
{};
#else
template<typename T>
struct has_mem_index_of
: public sprout::identity<decltype(sprout::detail::has_mem_index_of_test<T>::test(0))>::type
{};
#endif
template<typename Container, typename = void>
struct container_range_traits_range_index_of_impl;
template<typename Container>
struct container_range_traits_range_index_of_impl<
Container,
typename std::enable_if<sprout::detail::has_mem_index_of<Container>::value>::type
> {
public:
static SPROUT_CONSTEXPR typename sprout::container_traits<Container>::size_type
range_index_of(Container& cont, typename sprout::container_traits<Container>::iterator p) {
return cont.index_of(p);
}
};
template<typename Container>
struct container_range_traits_range_index_of_impl<
Container,
typename std::enable_if<!sprout::detail::has_mem_index_of<Container>::value>::type
> {
public:
static SPROUT_CONSTEXPR typename sprout::container_traits<Container>::size_type
range_index_of(Container& cont, typename sprout::container_traits<Container>::iterator p) {
return sprout::distance(sprout::begin(cont), p);
}
};
template<typename Container, typename = void>
struct container_range_traits_range_index_of_const_impl;
template<typename Container>
struct container_range_traits_range_index_of_const_impl<
Container,
typename std::enable_if<sprout::detail::has_mem_index_of<Container const>::value>::type
> {
public:
static SPROUT_CONSTEXPR typename sprout::container_traits<Container const>::size_type
range_index_of(Container const& cont, typename sprout::container_traits<Container const>::iterator p) {
return cont.index_of(p);
}
};
template<typename Container>
struct container_range_traits_range_index_of_const_impl<
Container,
typename std::enable_if<!sprout::detail::has_mem_index_of<Container const>::value>::type
> {
public:
static SPROUT_CONSTEXPR typename sprout::container_traits<Container const>::size_type
range_index_of(Container const& cont, typename sprout::container_traits<Container const>::iterator p) {
return sprout::distance(sprout::begin(cont), p);
}
};
//
// container_range_traits_default
//
@ -350,6 +494,10 @@ namespace sprout {
, public sprout::detail::container_range_traits_range_back_const_impl<Container>
, public sprout::detail::container_range_traits_range_at_impl<Container>
, public sprout::detail::container_range_traits_range_at_const_impl<Container>
, public sprout::detail::container_range_traits_range_nth_impl<Container>
, public sprout::detail::container_range_traits_range_nth_const_impl<Container>
, public sprout::detail::container_range_traits_range_index_of_impl<Container>
, public sprout::detail::container_range_traits_range_index_of_const_impl<Container>
{
using sprout::detail::container_range_traits_range_front_impl<Container>::range_front;
using sprout::detail::container_range_traits_range_front_const_impl<Container>::range_front;
@ -357,6 +505,10 @@ namespace sprout {
using sprout::detail::container_range_traits_range_back_const_impl<Container>::range_back;
using sprout::detail::container_range_traits_range_at_impl<Container>::range_at;
using sprout::detail::container_range_traits_range_at_const_impl<Container>::range_at;
using sprout::detail::container_range_traits_range_nth_impl<Container>::range_nth;
using sprout::detail::container_range_traits_range_nth_const_impl<Container>::range_nth;
using sprout::detail::container_range_traits_range_index_of_impl<Container>::range_index_of;
using sprout::detail::container_range_traits_range_index_of_const_impl<Container>::range_index_of;
public:
// iterators:
static SPROUT_CONSTEXPR typename sprout::container_traits<Container>::iterator
@ -422,6 +574,14 @@ namespace sprout {
range_at(Container const& cont, typename sprout::container_traits<Container const>::size_type i) {
return sprout::container_range_traits<Container>::range_at(cont, i);
}
static SPROUT_CONSTEXPR typename sprout::container_traits<Container const>::iterator
range_nth(Container const& cont, typename sprout::container_traits<Container const>::size_type i) {
return sprout::container_range_traits<Container>::range_nth(cont, i);
}
static SPROUT_CONSTEXPR typename sprout::container_traits<Container const>::size_type
range_index_of(Container const& cont, typename sprout::container_traits<Container const>::iterator p) {
return sprout::container_range_traits<Container>::range_index_of(cont, p);
}
// data access:
static SPROUT_CONSTEXPR typename sprout::container_traits<Container const>::pointer
range_data(Container const& cont) {
@ -500,14 +660,36 @@ namespace sprout {
range_at(T const (& arr)[N], typename sprout::container_traits<T const[N]>::size_type i) {
return arr[i];
}
static SPROUT_CONSTEXPR typename sprout::container_traits<T[N]>::iterator
range_nth(T (& arr)[N], typename sprout::container_traits<T[N]>::size_type i) {
typedef typename sprout::container_traits<T[N]>::iterator type;
return type(arr) + i;
}
static SPROUT_CONSTEXPR typename sprout::container_traits<T const[N]>::iterator
range_nth(T const (& arr)[N], typename sprout::container_traits<T const[N]>::size_type i) {
typedef typename sprout::container_traits<T const[N]>::iterator type;
return type(arr) + i;
}
static SPROUT_CONSTEXPR typename sprout::container_traits<T[N]>::size_type
range_index_of(T (& arr)[N], typename sprout::container_traits<T[N]>::iterator p) {
typedef typename sprout::container_traits<T[N]>::iterator type;
return sprout::distance(type(arr), p);
}
static SPROUT_CONSTEXPR typename sprout::container_traits<T const[N]>::size_type
range_index_of(T const (& arr)[N], typename sprout::container_traits<T const[N]>::iterator p) {
typedef typename sprout::container_traits<T const[N]>::iterator type;
return sprout::distance(type(arr), p);
}
// data access:
static SPROUT_CONSTEXPR typename sprout::container_traits<T[N]>::pointer
range_data(T (& arr)[N]) {
return &arr[0];
typedef typename sprout::container_traits<T[N]>::pointer type;
return type(arr);
}
static SPROUT_CONSTEXPR typename sprout::container_traits<T const[N]>::pointer
range_data(T const (& arr)[N]) {
return &arr[0];
typedef typename sprout::container_traits<T const[N]>::pointer type;
return type(arr);
}
};
template<typename T, std::size_t N>
@ -544,6 +726,14 @@ namespace sprout {
range_at(T const (& arr)[N], typename sprout::container_traits<T const[N]>::size_type i) {
return sprout::container_range_traits<T[N]>::range_at(arr, i);
}
static SPROUT_CONSTEXPR typename sprout::container_traits<T const[N]>::iterator
range_nth(T const (& arr)[N], typename sprout::container_traits<T const[N]>::size_type i) {
return sprout::container_range_traits<T[N]>::range_nth(arr, i);
}
static SPROUT_CONSTEXPR typename sprout::container_traits<T const[N]>::size_type
range_index_of(T const (& arr)[N], typename sprout::container_traits<T const[N]>::iterator p) {
return sprout::container_range_traits<T[N]>::range_index_of(arr, p);
}
// data access:
static SPROUT_CONSTEXPR typename sprout::container_traits<T const[N]>::pointer
range_data(T const (& arr)[N]) {

View file

@ -0,0 +1,87 @@
/*=============================================================================
Copyright (c) 2011-2015 Bolero MURAKAMI
https://github.com/bolero-MURAKAMI/Sprout
Distributed under the Boost Software License, Version 1.0. (See accompanying
file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
=============================================================================*/
#ifndef SPROUT_CONTAINER_INDEX_OF_HPP
#define SPROUT_CONTAINER_INDEX_OF_HPP
#include <sprout/config.hpp>
#include <sprout/workaround/std/cstddef.hpp>
#include <sprout/container/traits_fwd.hpp>
#include <sprout/container/container_traits.hpp>
#include <sprout/adl/not_found.hpp>
namespace sprout_adl {
sprout::not_found_via_adl range_index_of(...);
} // namespace sprout_adl
namespace sprout {
namespace container_detail {
template<typename Container>
inline SPROUT_CONSTEXPR typename sprout::container_traits<Container>::size_type
range_index_of(Container& cont, typename sprout::container_traits<Container>::iterator p) {
return sprout::container_range_traits<Container>::range_index_of(cont, p);
}
template<typename Container>
inline SPROUT_CONSTEXPR typename sprout::container_traits<Container const>::size_type
range_index_of(Container const& cont, typename sprout::container_traits<Container const>::iterator p) {
return sprout::container_range_traits<Container>::range_index_of(cont, p);
}
} // namespace container_detail
//
// index_of
//
// effect:
// ADL callable range_index_of(cont, p) -> range_index_of(cont, p)
// otherwise -> sprout::container_range_traits<Container>::range_index_of(cont, p)
// [default]
// callable cont.index_of(p) -> cont.index_of(p)
// otherwise -> *next(begin(cont), p)
//
template<typename Container>
inline SPROUT_CONSTEXPR typename sprout::container_traits<Container>::size_type
index_of(Container& cont, typename sprout::container_traits<Container>::iterator p) {
using sprout::container_detail::range_index_of;
using sprout_adl::range_index_of;
return range_index_of(cont, p);
}
template<typename Container>
inline SPROUT_CONSTEXPR typename sprout::container_traits<Container const>::size_type
index_of(Container const& cont, typename sprout::container_traits<Container const>::iterator p) {
using sprout::container_detail::range_index_of;
using sprout_adl::range_index_of;
return range_index_of(cont, p);
}
template<typename T, std::size_t N>
inline SPROUT_CONSTEXPR typename sprout::container_traits<T[N]>::size_type
index_of(T (& arr)[N], typename sprout::container_traits<T[N]>::iterator p) {
return sprout::container_detail::range_index_of(arr, p);
}
template<typename T, std::size_t N>
inline SPROUT_CONSTEXPR typename sprout::container_traits<T const[N]>::size_type
index_of(T const (& arr)[N], typename sprout::container_traits<T const[N]>::iterator p) {
return sprout::container_detail::range_index_of(arr, p);
}
//
// cindex_of
//
template<typename Container>
inline SPROUT_CONSTEXPR typename sprout::container_traits<Container const>::size_type
cindex_of(Container const& cont, typename sprout::container_traits<Container const>::iterator p) {
return sprout::index_of(cont, p);
}
template<typename T, std::size_t N>
inline SPROUT_CONSTEXPR typename sprout::container_traits<T const[N]>::size_type
cindex_of(T const (& arr)[N], typename sprout::container_traits<T const[N]>::iterator p) {
return sprout::index_of(arr, p);
}
} // namespace sprout
#include <sprout/container/container_range_traits.hpp>
#endif // #ifndef SPROUT_CONTAINER_INDEX_OF_HPP

87
sprout/container/nth.hpp Normal file
View file

@ -0,0 +1,87 @@
/*=============================================================================
Copyright (c) 2011-2015 Bolero MURAKAMI
https://github.com/bolero-MURAKAMI/Sprout
Distributed under the Boost Software License, Version 1.0. (See accompanying
file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
=============================================================================*/
#ifndef SPROUT_CONTAINER_NTH_HPP
#define SPROUT_CONTAINER_NTH_HPP
#include <sprout/config.hpp>
#include <sprout/workaround/std/cstddef.hpp>
#include <sprout/container/traits_fwd.hpp>
#include <sprout/container/container_traits.hpp>
#include <sprout/adl/not_found.hpp>
namespace sprout_adl {
sprout::not_found_via_adl range_nth(...);
} // namespace sprout_adl
namespace sprout {
namespace container_detail {
template<typename Container>
inline SPROUT_CONSTEXPR typename sprout::container_traits<Container>::iterator
range_nth(Container& cont, typename sprout::container_traits<Container>::size_type i) {
return sprout::container_range_traits<Container>::range_nth(cont, i);
}
template<typename Container>
inline SPROUT_CONSTEXPR typename sprout::container_traits<Container const>::iterator
range_nth(Container const& cont, typename sprout::container_traits<Container const>::size_type i) {
return sprout::container_range_traits<Container>::range_nth(cont, i);
}
} // namespace container_detail
//
// nth
//
// effect:
// ADL callable range_nth(cont, i) -> range_nth(cont, i)
// otherwise -> sprout::container_range_traits<Container>::range_nth(cont, i)
// [default]
// callable cont.nth(i) -> cont.nth(i)
// otherwise -> *next(begin(cont), i)
//
template<typename Container>
inline SPROUT_CONSTEXPR typename sprout::container_traits<Container>::iterator
nth(Container& cont, typename sprout::container_traits<Container>::size_type i) {
using sprout::container_detail::range_nth;
using sprout_adl::range_nth;
return range_nth(cont, i);
}
template<typename Container>
inline SPROUT_CONSTEXPR typename sprout::container_traits<Container const>::iterator
nth(Container const& cont, typename sprout::container_traits<Container const>::size_type i) {
using sprout::container_detail::range_nth;
using sprout_adl::range_nth;
return range_nth(cont, i);
}
template<typename T, std::size_t N>
inline SPROUT_CONSTEXPR typename sprout::container_traits<T[N]>::iterator
nth(T (& arr)[N], typename sprout::container_traits<T[N]>::size_type i) {
return sprout::container_detail::range_nth(arr, i);
}
template<typename T, std::size_t N>
inline SPROUT_CONSTEXPR typename sprout::container_traits<T const[N]>::iterator
nth(T const (& arr)[N], typename sprout::container_traits<T const[N]>::size_type i) {
return sprout::container_detail::range_nth(arr, i);
}
//
// cnth
//
template<typename Container>
inline SPROUT_CONSTEXPR typename sprout::container_traits<Container const>::iterator
cnth(Container const& cont, typename sprout::container_traits<Container const>::size_type i) {
return sprout::nth(cont, i);
}
template<typename T, std::size_t N>
inline SPROUT_CONSTEXPR typename sprout::container_traits<T const[N]>::iterator
cnth(T const (& arr)[N], typename sprout::container_traits<T const[N]>::size_type i) {
return sprout::nth(arr, i);
}
} // namespace sprout
#include <sprout/container/container_range_traits.hpp>
#endif // #ifndef SPROUT_CONTAINER_NTH_HPP

View file

@ -17,6 +17,8 @@
#include <sprout/container/front.hpp>
#include <sprout/container/back.hpp>
#include <sprout/container/at.hpp>
#include <sprout/container/nth.hpp>
#include <sprout/container/index_of.hpp>
#include <sprout/container/data.hpp>
#endif // #ifndef SPROUT_CONTAINER_RANGE_FUNCTIONS_HPP

View file

@ -76,6 +76,26 @@ namespace sprout {
SPROUT_CONSTEXPR typename sprout::container_traits<Container const>::reference
at(Container const& cont, typename sprout::container_traits<Container const>::size_type i);
//
// nth
//
template<typename Container>
SPROUT_CONSTEXPR typename sprout::container_traits<Container>::iterator
nth(Container& cont, typename sprout::container_traits<Container>::size_type i);
template<typename Container>
SPROUT_CONSTEXPR typename sprout::container_traits<Container const>::iterator
nth(Container const& cont, typename sprout::container_traits<Container const>::size_type i);
//
// index_of
//
template<typename Container>
SPROUT_CONSTEXPR typename sprout::container_traits<Container>::size_type
index_of(Container& cont, typename sprout::container_traits<Container>::iterator p);
template<typename Container>
SPROUT_CONSTEXPR typename sprout::container_traits<Container const>::size_type
index_of(Container const& cont, typename sprout::container_traits<Container const>::iterator p);
//
// data
//

View file

@ -54,7 +54,7 @@ namespace testspr {
#define TESTSPR_STATIC_ASSERT(expr) \
static_assert(expr, #expr)
#define TESTSPR_ASSERT(expr) \
((void)sprout::detail::assertion_check((expr), SPROUT_ASSERTION_FAILED_FORMAT(expr, __FILE__, __LINE__)))
SPROUT_ASSERT(expr)
//
// TESTSPR_BOTH_ASSERT
//