add front, back, at : container access non-member functions

This commit is contained in:
bolero-MURAKAMI 2014-07-12 18:04:45 +09:00
parent fa426a413a
commit 909ccfe73a
6 changed files with 572 additions and 17 deletions

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

@ -0,0 +1,87 @@
/*=============================================================================
Copyright (c) 2011-2014 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_AT_HPP
#define SPROUT_CONTAINER_AT_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_at(...);
} // namespace sprout_adl
namespace sprout {
namespace container_detail {
template<typename Container>
inline SPROUT_CONSTEXPR typename sprout::container_traits<Container>::reference
range_at(Container& cont, typename sprout::container_traits<Container>::size_type i) {
return sprout::container_range_traits<Container>::range_at(cont, i);
}
template<typename Container>
inline SPROUT_CONSTEXPR typename sprout::container_traits<Container const>::reference
range_at(Container const& cont, typename sprout::container_traits<Container const>::size_type i) {
return sprout::container_range_traits<Container>::range_at(cont, i);
}
} // namespace container_detail
//
// at
//
// effect:
// ADL callable range_at(cont, i) -> range_at(cont, i)
// otherwise -> sprout::container_range_traits<Container>::range_at(cont, i)
// [default]
// callable cont.at(i) -> cont.at(i)
// otherwise -> *next(begin(cont), i)
//
template<typename Container>
inline SPROUT_CONSTEXPR typename sprout::container_traits<Container>::reference
at(Container& cont, typename sprout::container_traits<Container>::size_type i) {
using sprout::container_detail::range_at;
using sprout_adl::range_at;
return range_at(cont, i);
}
template<typename Container>
inline SPROUT_CONSTEXPR typename sprout::container_traits<Container const>::reference
at(Container const& cont, typename sprout::container_traits<Container const>::size_type i) {
using sprout::container_detail::range_at;
using sprout_adl::range_at;
return range_at(cont, i);
}
template<typename T, std::size_t N>
inline SPROUT_CONSTEXPR typename sprout::container_traits<T[N]>::reference
at(T (& arr)[N], typename sprout::container_traits<T[N]>::size_type i) {
return sprout::container_detail::range_at(arr, i);
}
template<typename T, std::size_t N>
inline SPROUT_CONSTEXPR typename sprout::container_traits<T const[N]>::reference
at(T const (& arr)[N], typename sprout::container_traits<T const[N]>::size_type i) {
return sprout::container_detail::range_at(arr, i);
}
//
// cat
//
template<typename Container>
inline SPROUT_CONSTEXPR typename sprout::container_traits<Container const>::reference
cat(Container const& cont, typename sprout::container_traits<Container const>::size_type i) {
return sprout::at(cont, i);
}
template<typename T, std::size_t N>
inline SPROUT_CONSTEXPR typename sprout::container_traits<T const[N]>::reference
cat(T const (& arr)[N], typename sprout::container_traits<T const[N]>::size_type i) {
return sprout::at(arr, i);
}
} // namespace sprout
#include <sprout/container/container_range_traits.hpp>
#endif // #ifndef SPROUT_CONTAINER_AT_HPP

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

@ -0,0 +1,87 @@
/*=============================================================================
Copyright (c) 2011-2014 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_BACK_HPP
#define SPROUT_CONTAINER_BACK_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_back(...);
} // namespace sprout_adl
namespace sprout {
namespace container_detail {
template<typename Container>
inline SPROUT_CONSTEXPR typename sprout::container_traits<Container>::reference
range_back(Container& cont) {
return sprout::container_range_traits<Container>::range_back(cont);
}
template<typename Container>
inline SPROUT_CONSTEXPR typename sprout::container_traits<Container const>::reference
range_back(Container const& cont) {
return sprout::container_range_traits<Container>::range_back(cont);
}
} // namespace container_detail
//
// back
//
// effect:
// ADL callable range_back(cont) -> range_back(cont)
// otherwise -> sprout::container_range_traits<Container>::range_back(cont)
// [default]
// callable cont.back() -> cont.back()
// otherwise -> *prev(end(cont))
//
template<typename Container>
inline SPROUT_CONSTEXPR typename sprout::container_traits<Container>::reference
back(Container& cont) {
using sprout::container_detail::range_back;
using sprout_adl::range_back;
return range_back(cont);
}
template<typename Container>
inline SPROUT_CONSTEXPR typename sprout::container_traits<Container const>::reference
back(Container const& cont) {
using sprout::container_detail::range_back;
using sprout_adl::range_back;
return range_back(cont);
}
template<typename T, std::size_t N>
inline SPROUT_CONSTEXPR typename sprout::container_traits<T[N]>::reference
back(T (& arr)[N]) {
return sprout::container_detail::range_back(arr);
}
template<typename T, std::size_t N>
inline SPROUT_CONSTEXPR typename sprout::container_traits<T const[N]>::reference
back(T const (& arr)[N]) {
return sprout::container_detail::range_back(arr);
}
//
// cback
//
template<typename Container>
inline SPROUT_CONSTEXPR typename sprout::container_traits<Container const>::reference
cback(Container const& cont) {
return sprout::back(cont);
}
template<typename T, std::size_t N>
inline SPROUT_CONSTEXPR typename sprout::container_traits<T const[N]>::reference
cback(T const (& arr)[N]) {
return sprout::back(arr);
}
} // namespace sprout
#include <sprout/container/container_range_traits.hpp>
#endif // #ifndef SPROUT_CONTAINER_BACK_HPP

View file

@ -53,7 +53,7 @@ namespace sprout {
template<typename Container>
struct container_range_traits_range_size_impl<
Container,
typename std::enable_if<sprout::detail::has_mem_size<Container>::value>::type
typename std::enable_if<sprout::detail::has_mem_size<Container const>::value>::type
> {
public:
static SPROUT_CONSTEXPR typename sprout::container_traits<Container>::size_type
@ -64,7 +64,7 @@ namespace sprout {
template<typename Container>
struct container_range_traits_range_size_impl<
Container,
typename std::enable_if<!sprout::detail::has_mem_size<Container>::value>::type
typename std::enable_if<!sprout::detail::has_mem_size<Container const>::value>::type
> {
public:
static SPROUT_CONSTEXPR typename sprout::container_traits<Container>::size_type
@ -100,7 +100,7 @@ namespace sprout {
template<typename Container>
struct container_range_traits_range_empty_impl<
Container,
typename std::enable_if<sprout::detail::has_mem_empty<Container>::value>::type
typename std::enable_if<sprout::detail::has_mem_empty<Container const>::value>::type
> {
public:
static SPROUT_CONSTEXPR bool
@ -111,7 +111,7 @@ namespace sprout {
template<typename Container>
struct container_range_traits_range_empty_impl<
Container,
typename std::enable_if<!sprout::detail::has_mem_empty<Container>::value>::type
typename std::enable_if<!sprout::detail::has_mem_empty<Container const>::value>::type
> {
public:
static SPROUT_CONSTEXPR bool
@ -119,12 +119,234 @@ namespace sprout {
return sprout::size(cont) == 0;
}
};
template<typename T>
struct has_mem_front_test {
public:
template<
typename U = T,
typename = typename sprout::identity<decltype(std::declval<U>().front())>::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_front_test<T>::test(0))>::type>
struct has_mem_front
: public Base_
{};
#else
template<typename T>
struct has_mem_front
: public sprout::identity<decltype(sprout::detail::has_mem_front_test<T>::test(0))>::type
{};
#endif
template<typename Container, typename = void>
struct container_range_traits_range_front_impl;
template<typename Container>
struct container_range_traits_range_front_impl<
Container,
typename std::enable_if<sprout::detail::has_mem_front<Container>::value>::type
> {
public:
static SPROUT_CONSTEXPR typename sprout::container_traits<Container>::reference
range_front(Container& cont) {
return cont.front();
}
};
template<typename Container>
struct container_range_traits_range_front_impl<
Container,
typename std::enable_if<!sprout::detail::has_mem_front<Container>::value>::type
> {
public:
static SPROUT_CONSTEXPR typename sprout::container_traits<Container>::reference
range_front(Container& cont) {
return *sprout::begin(cont);
}
};
template<typename Container, typename = void>
struct container_range_traits_range_front_const_impl;
template<typename Container>
struct container_range_traits_range_front_const_impl<
Container,
typename std::enable_if<sprout::detail::has_mem_front<Container const>::value>::type
> {
public:
static SPROUT_CONSTEXPR typename sprout::container_traits<Container const>::reference
range_front(Container const& cont) {
return cont.front();
}
};
template<typename Container>
struct container_range_traits_range_front_const_impl<
Container,
typename std::enable_if<!sprout::detail::has_mem_front<Container const>::value>::type
> {
public:
static SPROUT_CONSTEXPR typename sprout::container_traits<Container const>::reference
range_front(Container const& cont) {
return *sprout::begin(cont);
}
};
template<typename T>
struct has_mem_back_test {
public:
template<
typename U = T,
typename = typename sprout::identity<decltype(std::declval<U>().back())>::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_back_test<T>::test(0))>::type>
struct has_mem_back
: public Base_
{};
#else
template<typename T>
struct has_mem_back
: public sprout::identity<decltype(sprout::detail::has_mem_back_test<T>::test(0))>::type
{};
#endif
template<typename Container, typename = void>
struct container_range_traits_range_back_impl;
template<typename Container>
struct container_range_traits_range_back_impl<
Container,
typename std::enable_if<sprout::detail::has_mem_back<Container>::value>::type
> {
public:
static SPROUT_CONSTEXPR typename sprout::container_traits<Container>::reference
range_back(Container& cont) {
return cont.back();
}
};
template<typename Container>
struct container_range_traits_range_back_impl<
Container,
typename std::enable_if<!sprout::detail::has_mem_back<Container>::value>::type
> {
public:
static SPROUT_CONSTEXPR typename sprout::container_traits<Container>::reference
range_back(Container& cont) {
return *sprout::prev(sprout::end(cont));
}
};
template<typename Container, typename = void>
struct container_range_traits_range_back_const_impl;
template<typename Container>
struct container_range_traits_range_back_const_impl<
Container,
typename std::enable_if<sprout::detail::has_mem_back<Container const>::value>::type
> {
public:
static SPROUT_CONSTEXPR typename sprout::container_traits<Container const>::reference
range_back(Container const& cont) {
return cont.back();
}
};
template<typename Container>
struct container_range_traits_range_back_const_impl<
Container,
typename std::enable_if<!sprout::detail::has_mem_back<Container const>::value>::type
> {
public:
static SPROUT_CONSTEXPR typename sprout::container_traits<Container const>::reference
range_back(Container const& cont) {
return *sprout::prev(sprout::end(cont));
}
};
template<typename T>
struct has_mem_at_test {
public:
template<
typename U = T,
typename = typename sprout::identity<decltype(std::declval<U>().at(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_at_test<T>::test(0))>::type>
struct has_mem_at
: public Base_
{};
#else
template<typename T>
struct has_mem_at
: public sprout::identity<decltype(sprout::detail::has_mem_at_test<T>::test(0))>::type
{};
#endif
template<typename Container, typename = void>
struct container_range_traits_range_at_impl;
template<typename Container>
struct container_range_traits_range_at_impl<
Container,
typename std::enable_if<sprout::detail::has_mem_at<Container>::value>::type
> {
public:
static SPROUT_CONSTEXPR typename sprout::container_traits<Container>::reference
range_at(Container& cont, typename sprout::container_traits<Container>::size_type i) {
return cont.at(i);
}
};
template<typename Container>
struct container_range_traits_range_at_impl<
Container,
typename std::enable_if<!sprout::detail::has_mem_at<Container>::value>::type
> {
public:
static SPROUT_CONSTEXPR typename sprout::container_traits<Container>::reference
range_at(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_at_const_impl;
template<typename Container>
struct container_range_traits_range_at_const_impl<
Container,
typename std::enable_if<sprout::detail::has_mem_at<Container const>::value>::type
> {
public:
static SPROUT_CONSTEXPR typename sprout::container_traits<Container const>::reference
range_at(Container const& cont, typename sprout::container_traits<Container const>::size_type i) {
return cont.at(i);
}
};
template<typename Container>
struct container_range_traits_range_at_const_impl<
Container,
typename std::enable_if<!sprout::detail::has_mem_at<Container const>::value>::type
> {
public:
static SPROUT_CONSTEXPR typename sprout::container_traits<Container const>::reference
range_at(Container const& cont, typename sprout::container_traits<Container const>::size_type i) {
return *sprout::next(sprout::begin(cont), i);
}
};
} // namespace detail
template<typename Container>
struct container_range_traits
: public sprout::detail::container_range_traits_range_size_impl<Container>
, public sprout::detail::container_range_traits_range_empty_impl<Container>
, public sprout::detail::container_range_traits_range_front_impl<Container>
, public sprout::detail::container_range_traits_range_front_const_impl<Container>
, public sprout::detail::container_range_traits_range_back_impl<Container>
, 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:
// iterators:
@ -166,6 +388,19 @@ namespace sprout {
range_empty(Container const& cont) {
return sprout::container_range_traits<Container>::range_empty(cont);
}
// element access:
static SPROUT_CONSTEXPR typename sprout::container_traits<Container const>::reference
range_front(Container const& cont) {
return sprout::container_range_traits<Container>::range_front(cont);
}
static SPROUT_CONSTEXPR typename sprout::container_traits<Container const>::reference
range_back(Container const& cont) {
return sprout::container_range_traits<Container>::range_back(cont);
}
static SPROUT_CONSTEXPR typename sprout::container_traits<Container const>::reference
range_at(Container const& cont, typename sprout::container_traits<Container const>::size_type i) {
return sprout::container_range_traits<Container>::range_at(cont, i);
}
};
template<typename T, std::size_t N>
@ -201,6 +436,31 @@ namespace sprout {
range_empty(T const (&)[N]) {
return false;
}
// element access:
static SPROUT_CONSTEXPR typename sprout::container_traits<T[N]>::reference
range_front(T (& arr)[N]) {
return arr[0];
}
static SPROUT_CONSTEXPR typename sprout::container_traits<T const[N]>::reference
range_front(T const (& arr)[N]) {
return arr[0];
}
static SPROUT_CONSTEXPR typename sprout::container_traits<T[N]>::reference
range_back(T (& arr)[N]) {
return arr[N - 1];
}
static SPROUT_CONSTEXPR typename sprout::container_traits<T const[N]>::reference
range_back(T const (& arr)[N]) {
return arr[N - 1];
}
static SPROUT_CONSTEXPR typename sprout::container_traits<T[N]>::reference
range_at(T (& arr)[N], typename sprout::container_traits<T[N]>::size_type i) {
return arr[i];
}
static SPROUT_CONSTEXPR typename sprout::container_traits<T const[N]>::reference
range_at(T const (& arr)[N], typename sprout::container_traits<T const[N]>::size_type i) {
return arr[i];
}
};
template<typename T, std::size_t N>
struct container_range_traits<T const[N]> {
@ -210,7 +470,7 @@ namespace sprout {
range_begin(T const (& arr)[N]) {
return sprout::container_range_traits<T[N]>::range_begin(arr);
}
static SPROUT_CONSTEXPR typename sprout::container_traits<T const[N]>::const_iterator
static SPROUT_CONSTEXPR typename sprout::container_traits<T const[N]>::iterator
range_end(T const (& arr)[N]) {
return sprout::container_range_traits<T[N]>::range_end(arr);
}
@ -223,6 +483,19 @@ namespace sprout {
range_empty(T const (& arr)[N]) {
return sprout::container_range_traits<T[N]>::range_empty(arr);
}
// element access:
static SPROUT_CONSTEXPR typename sprout::container_traits<T const[N]>::reference
range_front(T const (& arr)[N]) {
return sprout::container_range_traits<T[N]>::range_front(arr);
}
static SPROUT_CONSTEXPR typename sprout::container_traits<T const[N]>::reference
range_back(T const (& arr)[N]) {
return sprout::container_range_traits<T[N]>::range_back(arr);
}
static SPROUT_CONSTEXPR typename sprout::container_traits<T const[N]>::reference
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);
}
};
} // namespace sprout

View file

@ -0,0 +1,87 @@
/*=============================================================================
Copyright (c) 2011-2014 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_FRONT_HPP
#define SPROUT_CONTAINER_FRONT_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_front(...);
} // namespace sprout_adl
namespace sprout {
namespace container_detail {
template<typename Container>
inline SPROUT_CONSTEXPR typename sprout::container_traits<Container>::reference
range_front(Container& cont) {
return sprout::container_range_traits<Container>::range_front(cont);
}
template<typename Container>
inline SPROUT_CONSTEXPR typename sprout::container_traits<Container const>::reference
range_front(Container const& cont) {
return sprout::container_range_traits<Container>::range_front(cont);
}
} // namespace container_detail
//
// front
//
// effect:
// ADL callable range_front(cont) -> range_front(cont)
// otherwise -> sprout::container_range_traits<Container>::range_front(cont)
// [default]
// callable cont.front() -> cont.front()
// otherwise -> *begin(cont)
//
template<typename Container>
inline SPROUT_CONSTEXPR typename sprout::container_traits<Container>::reference
front(Container& cont) {
using sprout::container_detail::range_front;
using sprout_adl::range_front;
return range_front(cont);
}
template<typename Container>
inline SPROUT_CONSTEXPR typename sprout::container_traits<Container const>::reference
front(Container const& cont) {
using sprout::container_detail::range_front;
using sprout_adl::range_front;
return range_front(cont);
}
template<typename T, std::size_t N>
inline SPROUT_CONSTEXPR typename sprout::container_traits<T[N]>::reference
front(T (& arr)[N]) {
return sprout::container_detail::range_front(arr);
}
template<typename T, std::size_t N>
inline SPROUT_CONSTEXPR typename sprout::container_traits<T const[N]>::reference
front(T const (& arr)[N]) {
return sprout::container_detail::range_front(arr);
}
//
// cfront
//
template<typename Container>
inline SPROUT_CONSTEXPR typename sprout::container_traits<Container const>::reference
cfront(Container const& cont) {
return sprout::front(cont);
}
template<typename T, std::size_t N>
inline SPROUT_CONSTEXPR typename sprout::container_traits<T const[N]>::reference
cfront(T const (& arr)[N]) {
return sprout::front(arr);
}
} // namespace sprout
#include <sprout/container/container_range_traits.hpp>
#endif // #ifndef SPROUT_CONTAINER_FRONT_HPP

View file

@ -14,5 +14,8 @@
#include <sprout/container/end.hpp>
#include <sprout/container/size.hpp>
#include <sprout/container/empty.hpp>
#include <sprout/container/front.hpp>
#include <sprout/container/back.hpp>
#include <sprout/container/at.hpp>
#endif // #ifndef SPROUT_CONTAINER_RANGE_FUNCTIONS_HPP

View file

@ -21,12 +21,6 @@ namespace sprout {
template<typename Container>
SPROUT_CONSTEXPR typename sprout::container_traits<Container const>::iterator
begin(Container const& cont);
//
// cbegin
//
template<typename Container>
inline SPROUT_CONSTEXPR typename sprout::container_traits<Container const>::iterator
cbegin(Container const& cont);
//
// end
@ -37,12 +31,6 @@ namespace sprout {
template<typename Container>
SPROUT_CONSTEXPR typename sprout::container_traits<Container const>::iterator
end(Container const& cont);
//
// cend
//
template<typename Container>
inline SPROUT_CONSTEXPR typename sprout::container_traits<Container const>::iterator
cend(Container const& cont);
//
// size
@ -57,6 +45,36 @@ namespace sprout {
template<typename Container>
SPROUT_CONSTEXPR bool
empty(Container const& cont);
//
// front
//
template<typename Container>
SPROUT_CONSTEXPR typename sprout::container_traits<Container>::reference
front(Container& cont);
template<typename Container>
SPROUT_CONSTEXPR typename sprout::container_traits<Container const>::reference
front(Container const& cont);
//
// back
//
template<typename Container>
SPROUT_CONSTEXPR typename sprout::container_traits<Container>::reference
back(Container& cont);
template<typename Container>
SPROUT_CONSTEXPR typename sprout::container_traits<Container const>::reference
back(Container const& cont);
//
// at
//
template<typename Container>
SPROUT_CONSTEXPR typename sprout::container_traits<Container>::reference
at(Container& cont, typename sprout::container_traits<Container>::size_type i);
template<typename Container>
SPROUT_CONSTEXPR typename sprout::container_traits<Container const>::reference
at(Container const& cont, typename sprout::container_traits<Container const>::size_type i);
} // namespace sprout