random/bernoulli_distribution.hpp 追加

string.hpp 修正
This commit is contained in:
bolero-MURAKAMI 2011-10-10 10:20:21 +09:00
parent ba79d0b42d
commit 99f1a4f741
10 changed files with 384 additions and 28 deletions

View file

@ -6,7 +6,7 @@
#include <sprout/config.hpp>
#include <sprout/fixed_container/traits.hpp>
#include <sprout/fixed_container/functions.hpp>
#include <sprout/iterator/operation.hpp>
#include <sprout/iterator/operation.hpp>
#include <sprout/algorithm/fixed/result_of.hpp>
namespace sprout {
@ -82,7 +82,7 @@ namespace sprout {
Args const&... args
)
{
return sizeof...(Args) < offset
return sizeof...(Args) < static_cast<std::size_t>(offset)
? generate_impl_3<InitSize>(cont, gen, offset, args..., call_gen<InitSize, Container>(gen, args...))
: generate_impl_4(cont, args...)
;
@ -161,7 +161,7 @@ namespace sprout {
Args const&... args
)
{
return sizeof...(Args) - InitSize < offset
return sizeof...(Args) - InitSize < static_cast<std::size_t>(offset)
? generate_impl_1<InitSize>(cont, gen, offset, size, args..., *sprout::next(sprout::fixed_begin(cont), sizeof...(Args) - InitSize))
: generate_impl_2<InitSize>(cont, gen, offset, size, InitSize, args...)
;

View file

@ -0,0 +1,7 @@
#ifndef SPROUT_ALGORITHM_STRING_HPP
#define SPROUT_ALGORITHM_STRING_HPP
#include <sprout/config.hpp>
#include <sprout/algorithm/string/join.hpp>
#endif // #ifndef SPROUT_ALGORITHM_STRING_HPP

View file

@ -0,0 +1,108 @@
#ifndef SPROUT_ALGORITHM_STRING_JOIN_HPP
#define SPROUT_ALGORITHM_STRING_JOIN_HPP
#include <type_traits>
#include <sprout/config.hpp>
#include <sprout/fixed_container/traits.hpp>
#include <sprout/fixed_container/functions.hpp>
#include <sprout/iterator/operation.hpp>
#include <sprout/operation/fixed/append_back.hpp>
namespace sprout {
namespace algorithm {
namespace result_of {
//
// join
//
template<typename ContainerContainer, typename Separator>
struct join {
public:
typedef typename sprout::rebind_fixed_size<
typename sprout::fixed_container_traits<ContainerContainer>::value_type
>::template apply<
sprout::fixed_container_traits<ContainerContainer>::fixed_size != 0
? (
sprout::fixed_container_traits<
typename sprout::fixed_container_traits<ContainerContainer>::value_type
>::fixed_size
+ (sprout::fixed_container_traits<ContainerContainer>::fixed_size - 1) * (
sprout::fixed_container_traits<Separator>::fixed_size
+ sprout::fixed_container_traits<
typename sprout::fixed_container_traits<ContainerContainer>::value_type
>::fixed_size
)
)
: 0
>::type type;
};
} // namespace result_of
namespace detail {
template<typename Result, typename ContainerIterator, typename Separator, typename Container>
SPROUT_CONSTEXPR inline typename std::enable_if<
(sprout::fixed_container_traits<Container>::fixed_size == sprout::fixed_container_traits<Result>::fixed_size),
Result
>::type join_impl_1(
ContainerIterator first,
ContainerIterator last,
Separator const& separator,
Container const& current
)
{
return current;
}
template<typename Result, typename ContainerIterator, typename Separator, typename Container>
SPROUT_CONSTEXPR inline typename std::enable_if<
(sprout::fixed_container_traits<Container>::fixed_size < sprout::fixed_container_traits<Result>::fixed_size),
Result
>::type join_impl_1(
ContainerIterator first,
ContainerIterator last,
Separator const& separator,
Container const& current
)
{
return sprout::algorithm::detail::join_impl_1<Result>(
sprout::next(first),
last,
separator,
sprout::fixed::append_back(sprout::fixed::append_back(current, separator), *first)
);
}
template<typename Result, typename ContainerIterator, typename Separator>
SPROUT_CONSTEXPR inline Result join_impl(
ContainerIterator first,
ContainerIterator last,
Separator const& separator
)
{
return first != last
? sprout::algorithm::detail::join_impl_1<Result>(
sprout::next(first),
last,
separator,
*first
)
: sprout::make_clone<Result>()
;
}
} // namespace detail
//
// join
//
template<typename ContainerContainer, typename Separator>
SPROUT_CONSTEXPR inline typename sprout::algorithm::result_of::join<ContainerContainer, Separator>::type join(
ContainerContainer const& cont_cont,
Separator const& separator
)
{
return sprout::algorithm::detail::join_impl<typename sprout::algorithm::result_of::join<ContainerContainer, Separator>::type>(
sprout::begin(cont_cont),
sprout::end(cont_cont),
separator
);
}
} // namespace algorithm
} // namespace sprout
#endif // #ifndef SPROUT_ALGORITHM_STRING_JOIN_HPP

View file

@ -181,6 +181,7 @@ namespace sprout {
};
//
// operator==
// operator!=
// operator<
// operator>
@ -267,7 +268,7 @@ namespace sprout {
template<typename T, typename Enable = void>
struct is_array_impl {
public:
typedef std::integral_constant<bool, false> type;
typedef std::false_type type;
SPROUT_STATIC_CONSTEXPR bool value = type::value;
};
template<typename T>
@ -281,7 +282,7 @@ namespace sprout {
>::type
> {
public:
typedef std::integral_constant<bool, true> type;
typedef std::true_type type;
SPROUT_STATIC_CONSTEXPR bool value = type::value;
};
} // namespace detail

View file

@ -126,6 +126,7 @@ namespace sprout {
};
//
// operator==
// operator!=
// operator<
// operator>

View file

@ -8,6 +8,7 @@
#include <sprout/random/uniform_int_distribution.hpp>
#include <sprout/random/uniform_01.hpp>
#include <sprout/random/uniform_real_distribution.hpp>
#include <sprout/random/bernoulli_distribution.hpp>
#include <sprout/random/variate_generator.hpp>
#include <sprout/random/random_result.hpp>
#include <sprout/random/random_iterator.hpp>

View file

@ -0,0 +1,142 @@
#ifndef SPROUT_RANDOM_BERNOULLI_DISTRIBUTION_HPP
#define SPROUT_RANDOM_BERNOULLI_DISTRIBUTION_HPP
#include <iosfwd>
#include <sprout/config.hpp>
#include <sprout/random/random_result.hpp>
namespace sprout {
namespace random {
//
// bernoulli_distribution
//
template<typename RealType = double>
class bernoulli_distribution {
public:
typedef int input_type;
typedef bool result_type;
private:
static SPROUT_CONSTEXPR RealType arg_check(RealType p_arg) {
return p_arg >= 0 && p_arg <= 1
? p_arg
: throw "assert(p_arg >= 0 && p_arg <= 1)"
;
}
public:
//
// param_type
//
class param_type {
public:
typedef bernoulli_distribution distribution_type;
private:
RealType p_;
public:
SPROUT_CONSTEXPR param_type()
: p_(RealType(0.5))
{}
SPROUT_CONSTEXPR explicit param_type(RealType p_arg = RealType(0.5))
: p_(arg_check(p_arg))
{}
SPROUT_CONSTEXPR RealType p() const {
return p_;
}
template<typename Elem, typename Traits>
friend std::basic_ostream<Elem, Traits>& operator>>(
std::basic_istream<Elem, Traits>& lhs,
param_type const& rhs
)
{
return lhs >> rhs.p_;
}
template<typename Elem, typename Traits>
friend std::basic_ostream<Elem, Traits>& operator<<(
std::basic_ostream<Elem, Traits>& lhs,
param_type const& rhs
)
{
return lhs << rhs.p_;
}
SPROUT_CONSTEXPR friend bool operator==(param_type const& lhs, param_type const& rhs) {
return lhs.p_ == rhs.p_;
}
SPROUT_CONSTEXPR friend bool operator!=(param_type const& lhs, param_type const& rhs) {
return !(lhs == rhs);
}
};
private:
RealType p_;
private:
template<typename Engine>
SPROUT_CONSTEXPR sprout::random::random_result<Engine, bernoulli_distribution> generate(
sprout::random::random_result<Engine> const& rnd
) const
{
return sprout::random::random_result<Engine, bernoulli_distribution>(
RealType(rnd.result() - rnd.engine().min()) <= p_ * RealType(rnd.engine().max() - rnd.engine().min()),
rnd.engine(),
*this
);
}
public:
SPROUT_CONSTEXPR bernoulli_distribution()
: p_(RealType(0.5))
{}
SPROUT_CONSTEXPR explicit bernoulli_distribution(RealType p_arg = RealType(0.5))
: p_(arg_check(p_arg))
{}
SPROUT_CONSTEXPR RealType p() const {
return p_;
}
SPROUT_CONSTEXPR result_type min() const {
return false;
}
SPROUT_CONSTEXPR result_type max() const {
return true;
}
SPROUT_CONSTEXPR param_type param() const {
return param_type(p_);
}
void param(param_type const& parm) {
p_ = parm.p();
}
template<typename Engine>
SPROUT_CONSTEXPR sprout::random::random_result<Engine, bernoulli_distribution> operator()(Engine const& eng) const {
return p_ == RealType(0)
? sprout::random::random_result<Engine, bernoulli_distribution>(false, eng, *this)
: generate(eng())
;
}
template<typename Elem, typename Traits>
friend std::basic_ostream<Elem, Traits>& operator>>(
std::basic_istream<Elem, Traits>& lhs,
bernoulli_distribution const& rhs
)
{
param_type parm;
return lhs >> parm;
param(parm);
return lhs;
}
template<typename Elem, typename Traits>
friend std::basic_ostream<Elem, Traits>& operator<<(
std::basic_ostream<Elem, Traits>& lhs,
bernoulli_distribution const& rhs
)
{
return lhs << param();
}
SPROUT_CONSTEXPR friend bool operator==(bernoulli_distribution const& lhs, bernoulli_distribution const& rhs) {
return lhs.param() == rhs.param();
}
SPROUT_CONSTEXPR friend bool operator!=(bernoulli_distribution const& lhs, bernoulli_distribution const& rhs) {
return !(lhs == rhs);
}
};
} // namespace random
using sprout::random::bernoulli_distribution;
} // namespace sprout
#endif // #ifndef SPROUT_RANDOM_BERNOULLI_DISTRIBUTION_HPP

View file

@ -2,6 +2,7 @@
#define SPROUT_RANDOM_UNIFORM_01_HPP
#include <iosfwd>
#include <istream>
#include <limits>
#include <sprout/config.hpp>
#include <sprout/random/random_result.hpp>

View file

@ -89,39 +89,55 @@ namespace sprout {
return impl_type::eof();
}
#if SPROUT_USE_INDEX_ITERATOR_IMPLEMENTATION
template<typename CharIterator>
static SPROUT_CONSTEXPR int compare(CharIterator s1, CharIterator s2, std::size_t n) {
template<typename ConstIterator>
static SPROUT_CONSTEXPR int compare(char_type const* s1, ConstIterator s2, std::size_t n) {
return !n ? 0
: lt(*s1, *s2) ? -1
: lt(*s2, *s1) ? 1
: compare(s1 + 1, s2 + 1, n - 1)
;
}
template<typename CharIterator>
static SPROUT_CONSTEXPR std::size_t length(CharIterator s) {
template<typename ConstIterator>
static SPROUT_CONSTEXPR int compare(ConstIterator s1, char_type const* s2, std::size_t n) {
return !n ? 0
: lt(*s1, *s2) ? -1
: lt(*s2, *s1) ? 1
: compare(s1 + 1, s2 + 1, n - 1)
;
}
template<typename ConstIterator1, typename ConstIterator2>
static SPROUT_CONSTEXPR int compare(ConstIterator1 s1, ConstIterator2 s2, std::size_t n) {
return !n ? 0
: lt(*s1, *s2) ? -1
: lt(*s2, *s1) ? 1
: compare(s1 + 1, s2 + 1, n - 1)
;
}
template<typename ConstIterator>
static SPROUT_CONSTEXPR std::size_t length(ConstIterator s) {
return !*s ? 0
: 1 + length(s + 1)
;
}
template<typename CharIterator>
static SPROUT_CONSTEXPR CharIterator find(CharIterator s, std::size_t n, char_type const& a) {
template<typename ConstIterator>
static SPROUT_CONSTEXPR ConstIterator find(ConstIterator s, std::size_t n, char_type const& a) {
return !n ? nullptr
: eq(*s, a) ? s
: find(s + 1, n - 1, a)
;
}
template<typename CharIterator1, typename CharIterator2>
static CharIterator1 move(CharIterator1 s1, CharIterator2 s2, std::size_t n) {
template<typename Iterator, typename ConstIterator>
static Iterator move(Iterator s1, ConstIterator s2, std::size_t n) {
std::copy_backward(s2, s2 + n, s1);
return s1;
}
template<typename CharIterator1, typename CharIterator2>
static CharIterator1 copy(CharIterator1 s1, CharIterator2 s2, std::size_t n) {
template<typename Iterator, typename ConstIterator>
static Iterator copy(Iterator s1, ConstIterator s2, std::size_t n) {
std::copy(s2, s2 + n, s1);
return s1;
}
template<typename CharIterator>
static CharIterator assign(CharIterator s, std::size_t n, char_type a) {
template<typename Iterator>
static Iterator assign(Iterator s, std::size_t n, char_type a) {
std::fill(s, s + n, a);
return s;
}
@ -151,6 +167,37 @@ namespace sprout {
typedef sprout::reverse_iterator<iterator> reverse_iterator;
typedef sprout::reverse_iterator<const_iterator> const_reverse_iterator;
typedef Traits traits_type;
#if SPROUT_USE_INDEX_ITERATOR_IMPLEMENTATION
private:
template<typename U, typename Enable = void>
struct is_index_iterator_impl {
public:
typedef std::false_type type;
SPROUT_STATIC_CONSTEXPR bool value = type::value;
};
template<typename U>
struct is_index_iterator_impl<
U,
typename std::enable_if<
std::is_same<
U,
sprout::index_iterator<typename U::container_type>
>::value
&& std::is_same<
typename std::iterator_traits<U>::value_type,
value_type
>::value
>::type
> {
public:
typedef std::true_type type;
SPROUT_STATIC_CONSTEXPR bool value = type::value;
};
template<typename U>
struct is_index_iterator
: public is_index_iterator_impl<U>
{};
#endif
public:
SPROUT_STATIC_CONSTEXPR size_type npos = -1;
SPROUT_STATIC_CONSTEXPR size_type static_size = N;
@ -180,7 +227,29 @@ namespace sprout {
return sprout::basic_string<T, N, Traits>{{(Indexes < n ? s[Indexes] : T())...}, n};
}
#if SPROUT_USE_INDEX_ITERATOR_IMPLEMENTATION
static SPROUT_CONSTEXPR int compare_impl_1(const_iterator dest, size_type pos1, size_type n1, const_iterator s, size_type n2) {
template<typename ConstIterator>
static SPROUT_CONSTEXPR typename std::enable_if<
is_index_iterator<ConstIterator>::value,
int
>::type compare_impl_1(value_type const* dest, size_type pos1, size_type n1, ConstIterator s, size_type n2) {
return compare_impl_2(
traits_type::compare(dest + pos1, s, NS_SSCRISK_CEL_OR_SPROUT_DETAIL::min(n1, n2)),
n1,
n2
);
}
static SPROUT_CONSTEXPR int compare_impl_1(const_iterator dest, size_type pos1, size_type n1, value_type const* s, size_type n2) {
return compare_impl_2(
traits_type::compare(dest + pos1, s, NS_SSCRISK_CEL_OR_SPROUT_DETAIL::min(n1, n2)),
n1,
n2
);
}
template<typename ConstIterator>
static SPROUT_CONSTEXPR typename std::enable_if<
is_index_iterator<ConstIterator>::value,
int
>::type compare_impl_1(const_iterator dest, size_type pos1, size_type n1, ConstIterator s, size_type n2) {
return compare_impl_2(
traits_type::compare(dest + pos1, s, NS_SSCRISK_CEL_OR_SPROUT_DETAIL::min(n1, n2)),
n1,
@ -401,7 +470,7 @@ namespace sprout {
}
template<std::size_t N2>
SPROUT_CONSTEXPR int compare(basic_string<T, N2, Traits> const& str) const {
return compare(0, size(), str.c_str(), str.size());
return compare(0, size(), str.begin(), str.size());
}
SPROUT_CONSTEXPR int compare(value_type const* s) const {
return compare(0, size(), s, traits_type::length(s));
@ -416,13 +485,13 @@ namespace sprout {
template<std::size_t N2>
SPROUT_CONSTEXPR int compare(size_type pos1, size_type n1, basic_string<T, N2, Traits> const& str, size_type pos2, size_type n2) const {
return !(str.size() < pos2)
? compare(pos1, n1, str.c_str() + pos2, NS_SSCRISK_CEL_OR_SPROUT_DETAIL::min(n2, str.size() - pos2))
? compare(pos1, n1, str.begin() + pos2, NS_SSCRISK_CEL_OR_SPROUT_DETAIL::min(n2, str.size() - pos2))
: throw "basic_string<>: index out of range"
;
}
SPROUT_CONSTEXPR int compare(size_type pos1, size_type n1, value_type const* s, size_type n2) const {
return !(size() < pos1)
? compare_impl_1(c_str(), pos1, NS_SSCRISK_CEL_OR_SPROUT_DETAIL::min(n1, size() - pos1), s, n2)
? compare_impl_1(begin(), pos1, NS_SSCRISK_CEL_OR_SPROUT_DETAIL::min(n1, size() - pos1), s, n2)
: throw "basic_string<>: index out of range"
;
}
@ -441,7 +510,11 @@ namespace sprout {
}
}
#if SPROUT_USE_INDEX_ITERATOR_IMPLEMENTATION
basic_string<T, N, Traits>& assign(const_iterator s, size_type n) {
template<typename ConstIterator>
typename std::enable_if<
is_index_iterator<ConstIterator>::value,
basic_string<T, N, Traits>&
>::type assign(ConstIterator s, size_type n) {
maxcheck(n);
for (size_type i = 0; i < n; ++i) {
traits_type::assign(elems[i], s[i]);
@ -452,21 +525,41 @@ namespace sprout {
len = n;
return *this;
}
basic_string<T, N, Traits>& assign(const_iterator s) {
template<typename ConstIterator>
typename std::enable_if<
is_index_iterator<ConstIterator>::value,
basic_string<T, N, Traits>&
>::type assign(ConstIterator s) {
return assign(s, traits_type::length(s));
}
basic_string<T, N, Traits>& operator=(const_iterator rhs) {
template<typename ConstIterator>
typename std::enable_if<
is_index_iterator<ConstIterator>::value,
basic_string<T, N, Traits>&
>::type operator=(ConstIterator rhs) {
return assign(rhs);
}
SPROUT_CONSTEXPR int compare(const_iterator s) const {
template<typename ConstIterator>
SPROUT_CONSTEXPR typename std::enable_if<
is_index_iterator<ConstIterator>::value,
int
>::type compare(ConstIterator s) const {
return compare(0, size(), s, traits_type::length(s));
}
SPROUT_CONSTEXPR int compare(size_type pos1, size_type n1, const_iterator s) const {
template<typename ConstIterator>
SPROUT_CONSTEXPR typename std::enable_if<
is_index_iterator<ConstIterator>::value,
int
>::type compare(size_type pos1, size_type n1, ConstIterator s) const {
return compare(pos1, n1, s, traits_type::length(s));
}
SPROUT_CONSTEXPR int compare(size_type pos1, size_type n1, const_iterator s, size_type n2) const {
template<typename ConstIterator>
SPROUT_CONSTEXPR typename std::enable_if<
is_index_iterator<ConstIterator>::value,
int
>::type compare(size_type pos1, size_type n1, ConstIterator s, size_type n2) const {
return !(size() < pos1)
? compare_impl_1(c_str(), pos1, NS_SSCRISK_CEL_OR_SPROUT_DETAIL::min(n1, size() - pos1), s, n2)
? compare_impl_1(begin(), pos1, NS_SSCRISK_CEL_OR_SPROUT_DETAIL::min(n1, size() - pos1), s, n2)
: throw "basic_string<>: index out of range"
;
}
@ -474,6 +567,7 @@ namespace sprout {
};
//
// operator==
// operator!=
// operator<
// operator>

View file

@ -381,6 +381,7 @@ namespace sprout {
};
//
// operator==
// operator!=
// operator<
// operator>