mirror of
https://github.com/bolero-MURAKAMI/Sprout.git
synced 2024-12-23 21:25:49 +00:00
fix recursion depth: all_of_equal, any_of_equal, none_of_equal, one_of_equal
This commit is contained in:
parent
750e3b944d
commit
bc04943aaa
10 changed files with 394 additions and 9 deletions
40
libs/algorithm/test/all_of_equal.cpp
Normal file
40
libs/algorithm/test/all_of_equal.cpp
Normal file
|
@ -0,0 +1,40 @@
|
|||
#ifndef SPROUT_LIBS_ALGORITHM_TEST_ALL_OF_EQUAL_CPP
|
||||
#define SPROUT_LIBS_ALGORITHM_TEST_ALL_OF_EQUAL_CPP
|
||||
|
||||
#include <sprout/algorithm/all_of_equal.hpp>
|
||||
#include <sprout/array.hpp>
|
||||
#include <sprout/container.hpp>
|
||||
#include <testspr/tools.hpp>
|
||||
|
||||
namespace testspr {
|
||||
static void algorithm_all_of_equal_test() {
|
||||
using namespace sprout;
|
||||
{
|
||||
SPROUT_STATIC_CONSTEXPR auto arr1 = array<int, 10>{{1, 1, 1, 1, 1, 10, 10, 10, 10, 10}};
|
||||
|
||||
{
|
||||
SPROUT_STATIC_CONSTEXPR auto result = sprout::all_of_equal(
|
||||
sprout::begin(arr1),
|
||||
sprout::end(arr1),
|
||||
1
|
||||
);
|
||||
TESTSPR_BOTH_ASSERT(!result);
|
||||
}
|
||||
{
|
||||
SPROUT_STATIC_CONSTEXPR auto result = sprout::all_of_equal(
|
||||
sprout::begin(arr1),
|
||||
sprout::begin(arr1) + 5,
|
||||
1
|
||||
);
|
||||
TESTSPR_BOTH_ASSERT(result);
|
||||
}
|
||||
}
|
||||
}
|
||||
} // namespace testspr
|
||||
|
||||
#ifndef TESTSPR_CPP_INCLUDE
|
||||
# define TESTSPR_TEST_FUNCTION testspr::algorithm_all_of_equal_test
|
||||
# include <testspr/include_main.hpp>
|
||||
#endif
|
||||
|
||||
#endif // #ifndef SPROUT_LIBS_ALGORITHM_TEST_ALL_OF_EQUAL_CPP
|
56
libs/algorithm/test/any_of_equal.cpp
Normal file
56
libs/algorithm/test/any_of_equal.cpp
Normal file
|
@ -0,0 +1,56 @@
|
|||
#ifndef SPROUT_LIBS_ALGORITHM_TEST_ANY_OF_EQUAL_CPP
|
||||
#define SPROUT_LIBS_ALGORITHM_TEST_ANY_OF_EQUAL_CPP
|
||||
|
||||
#include <sprout/algorithm/any_of_equal.hpp>
|
||||
#include <sprout/array.hpp>
|
||||
#include <sprout/container.hpp>
|
||||
#include <testspr/tools.hpp>
|
||||
|
||||
namespace testspr {
|
||||
static void algorithm_any_of_equal_test() {
|
||||
using namespace sprout;
|
||||
{
|
||||
SPROUT_STATIC_CONSTEXPR auto arr1 = array<int, 10>{{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}};
|
||||
|
||||
{
|
||||
SPROUT_STATIC_CONSTEXPR auto result = sprout::any_of_equal(
|
||||
sprout::begin(arr1),
|
||||
sprout::end(arr1),
|
||||
10
|
||||
);
|
||||
TESTSPR_BOTH_ASSERT(result);
|
||||
}
|
||||
{
|
||||
SPROUT_STATIC_CONSTEXPR auto result = sprout::any_of_equal(
|
||||
sprout::begin(arr1),
|
||||
sprout::end(arr1),
|
||||
11
|
||||
);
|
||||
TESTSPR_BOTH_ASSERT(!result);
|
||||
}
|
||||
{
|
||||
SPROUT_STATIC_CONSTEXPR auto result = sprout::any_of_equal(
|
||||
sprout::begin(arr1),
|
||||
sprout::begin(arr1) + 5,
|
||||
10
|
||||
);
|
||||
TESTSPR_BOTH_ASSERT(!result);
|
||||
}
|
||||
{
|
||||
SPROUT_STATIC_CONSTEXPR auto result = sprout::any_of_equal(
|
||||
sprout::begin(arr1),
|
||||
sprout::begin(arr1) + 5,
|
||||
11
|
||||
);
|
||||
TESTSPR_BOTH_ASSERT(!result);
|
||||
}
|
||||
}
|
||||
}
|
||||
} // namespace testspr
|
||||
|
||||
#ifndef TESTSPR_CPP_INCLUDE
|
||||
# define TESTSPR_TEST_FUNCTION testspr::algorithm_any_of_equal_test
|
||||
# include <testspr/include_main.hpp>
|
||||
#endif
|
||||
|
||||
#endif // #ifndef SPROUT_LIBS_ALGORITHM_TEST_ANY_OF_EQUAL_CPP
|
|
@ -41,6 +41,10 @@
|
|||
#include "./max_element.cpp"
|
||||
#include "./minmax_element.cpp"
|
||||
#include "./lexicographical_compare.cpp"
|
||||
#include "./all_of_equal.cpp"
|
||||
#include "./any_of_equal.cpp"
|
||||
#include "./none_of_equal.cpp"
|
||||
#include "./one_of_equal.cpp"
|
||||
|
||||
#ifdef TESTSPR_CPP_INCLUDE_DISABLE_SPROUT_LIBS_ALGORITHM_TEST_NON_MODIFYIING_CPP
|
||||
# undef TESTSPR_CPP_INCLUDE
|
||||
|
@ -83,6 +87,10 @@ namespace testspr {
|
|||
testspr::algorithm_max_element_test();
|
||||
testspr::algorithm_minmax_element_test();
|
||||
testspr::algorithm_lexicographical_compare_test();
|
||||
testspr::algorithm_all_of_equal_test();
|
||||
testspr::algorithm_any_of_equal_test();
|
||||
testspr::algorithm_none_of_equal_test();
|
||||
testspr::algorithm_one_of_equal_test();
|
||||
}
|
||||
} // namespace testspr
|
||||
|
||||
|
|
56
libs/algorithm/test/none_of_equal.cpp
Normal file
56
libs/algorithm/test/none_of_equal.cpp
Normal file
|
@ -0,0 +1,56 @@
|
|||
#ifndef SPROUT_LIBS_ALGORITHM_TEST_NONE_OF_EQUAL_CPP
|
||||
#define SPROUT_LIBS_ALGORITHM_TEST_NONE_OF_EQUAL_CPP
|
||||
|
||||
#include <sprout/algorithm/none_of_equal.hpp>
|
||||
#include <sprout/array.hpp>
|
||||
#include <sprout/container.hpp>
|
||||
#include <testspr/tools.hpp>
|
||||
|
||||
namespace testspr {
|
||||
static void algorithm_none_of_equal_test() {
|
||||
using namespace sprout;
|
||||
{
|
||||
SPROUT_STATIC_CONSTEXPR auto arr1 = array<int, 10>{{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}};
|
||||
|
||||
{
|
||||
SPROUT_STATIC_CONSTEXPR auto result = sprout::none_of_equal(
|
||||
sprout::begin(arr1),
|
||||
sprout::end(arr1),
|
||||
10
|
||||
);
|
||||
TESTSPR_BOTH_ASSERT(!result);
|
||||
}
|
||||
{
|
||||
SPROUT_STATIC_CONSTEXPR auto result = sprout::none_of_equal(
|
||||
sprout::begin(arr1),
|
||||
sprout::end(arr1),
|
||||
11
|
||||
);
|
||||
TESTSPR_BOTH_ASSERT(result);
|
||||
}
|
||||
{
|
||||
SPROUT_STATIC_CONSTEXPR auto result = sprout::none_of_equal(
|
||||
sprout::begin(arr1),
|
||||
sprout::begin(arr1) + 5,
|
||||
10
|
||||
);
|
||||
TESTSPR_BOTH_ASSERT(result);
|
||||
}
|
||||
{
|
||||
SPROUT_STATIC_CONSTEXPR auto result = sprout::none_of_equal(
|
||||
sprout::begin(arr1),
|
||||
sprout::begin(arr1) + 5,
|
||||
11
|
||||
);
|
||||
TESTSPR_BOTH_ASSERT(result);
|
||||
}
|
||||
}
|
||||
}
|
||||
} // namespace testspr
|
||||
|
||||
#ifndef TESTSPR_CPP_INCLUDE
|
||||
# define TESTSPR_TEST_FUNCTION testspr::algorithm_none_of_equal_test
|
||||
# include <testspr/include_main.hpp>
|
||||
#endif
|
||||
|
||||
#endif // #ifndef SPROUT_LIBS_ALGORITHM_TEST_NONE_OF_EQUAL_CPP
|
56
libs/algorithm/test/one_of_equal.cpp
Normal file
56
libs/algorithm/test/one_of_equal.cpp
Normal file
|
@ -0,0 +1,56 @@
|
|||
#ifndef SPROUT_LIBS_ALGORITHM_TEST_ONE_OF_EQUAL_CPP
|
||||
#define SPROUT_LIBS_ALGORITHM_TEST_ONE_OF_EQUAL_CPP
|
||||
|
||||
#include <sprout/algorithm/one_of_equal.hpp>
|
||||
#include <sprout/array.hpp>
|
||||
#include <sprout/container.hpp>
|
||||
#include <testspr/tools.hpp>
|
||||
|
||||
namespace testspr {
|
||||
static void algorithm_one_of_equal_test() {
|
||||
using namespace sprout;
|
||||
{
|
||||
SPROUT_STATIC_CONSTEXPR auto arr1 = array<int, 10>{{1, 2, 3, 4, 5, 5, 5, 5, 5, 10}};
|
||||
|
||||
{
|
||||
SPROUT_STATIC_CONSTEXPR auto result = sprout::one_of_equal(
|
||||
sprout::begin(arr1),
|
||||
sprout::end(arr1),
|
||||
5
|
||||
);
|
||||
TESTSPR_BOTH_ASSERT(!result);
|
||||
}
|
||||
{
|
||||
SPROUT_STATIC_CONSTEXPR auto result = sprout::one_of_equal(
|
||||
sprout::begin(arr1),
|
||||
sprout::end(arr1),
|
||||
10
|
||||
);
|
||||
TESTSPR_BOTH_ASSERT(result);
|
||||
}
|
||||
{
|
||||
SPROUT_STATIC_CONSTEXPR auto result = sprout::one_of_equal(
|
||||
sprout::begin(arr1),
|
||||
sprout::begin(arr1) + 5,
|
||||
5
|
||||
);
|
||||
TESTSPR_BOTH_ASSERT(result);
|
||||
}
|
||||
{
|
||||
SPROUT_STATIC_CONSTEXPR auto result = sprout::one_of_equal(
|
||||
sprout::begin(arr1),
|
||||
sprout::begin(arr1) + 5,
|
||||
10
|
||||
);
|
||||
TESTSPR_BOTH_ASSERT(!result);
|
||||
}
|
||||
}
|
||||
}
|
||||
} // namespace testspr
|
||||
|
||||
#ifndef TESTSPR_CPP_INCLUDE
|
||||
# define TESTSPR_TEST_FUNCTION testspr::algorithm_one_of_equal_test
|
||||
# include <testspr/include_main.hpp>
|
||||
#endif
|
||||
|
||||
#endif // #ifndef SPROUT_LIBS_ALGORITHM_TEST_ONE_OF_EQUAL_CPP
|
|
@ -1,19 +1,73 @@
|
|||
#ifndef SPROUT_ALGORITHM_ALL_OF_EQUAL_HPP
|
||||
#define SPROUT_ALGORITHM_ALL_OF_EQUAL_HPP
|
||||
|
||||
#include <iterator>
|
||||
#include <sprout/config.hpp>
|
||||
#include <sprout/iterator/operation.hpp>
|
||||
#include HDR_ITERATOR_SSCRISK_CEL_OR_SPROUT
|
||||
|
||||
namespace sprout {
|
||||
namespace detail {
|
||||
template<typename RandomAccessIterator, typename T>
|
||||
inline SPROUT_CONSTEXPR bool
|
||||
all_of_equal_impl_ra(
|
||||
RandomAccessIterator first, RandomAccessIterator last, T const& value,
|
||||
typename std::iterator_traits<RandomAccessIterator>::difference_type pivot
|
||||
)
|
||||
{
|
||||
return pivot == 0 ? *first == value
|
||||
: sprout::detail::all_of_equal_impl_ra(
|
||||
first, sprout::next(first, pivot), value,
|
||||
pivot / 2
|
||||
)
|
||||
&& sprout::detail::all_of_equal_impl_ra(
|
||||
sprout::next(first, pivot), last, value,
|
||||
(NS_SSCRISK_CEL_OR_SPROUT::distance(first, last) - pivot) / 2
|
||||
)
|
||||
;
|
||||
}
|
||||
template<typename RandomAccessIterator, typename T>
|
||||
inline SPROUT_CONSTEXPR bool
|
||||
all_of_equal(
|
||||
RandomAccessIterator first, RandomAccessIterator last, T const& value,
|
||||
std::random_access_iterator_tag*
|
||||
)
|
||||
{
|
||||
return first == last ? true
|
||||
: sprout::detail::all_of_equal_impl_ra(first, last, value, NS_SSCRISK_CEL_OR_SPROUT::distance(first, last) / 2)
|
||||
;
|
||||
}
|
||||
|
||||
template<typename InputIterator, typename T>
|
||||
inline SPROUT_CONSTEXPR bool
|
||||
all_of_equal_impl(InputIterator first, InputIterator last, T const& value) {
|
||||
return first == last ? true
|
||||
: *first == value && sprout::detail::all_of_equal_impl(sprout::next(first), last, value)
|
||||
;
|
||||
}
|
||||
template<typename InputIterator, typename T>
|
||||
inline SPROUT_CONSTEXPR bool
|
||||
all_of_equal(
|
||||
InputIterator first, InputIterator last, T const& value,
|
||||
void*
|
||||
)
|
||||
{
|
||||
return sprout::detail::all_of_equal_impl(first, last, value);
|
||||
}
|
||||
} //namespace detail
|
||||
|
||||
//
|
||||
// all_of_equal
|
||||
//
|
||||
// recursion depth:
|
||||
// [first, last) is RandomAccessIterator -> O(log N)
|
||||
// otherwise -> O(N)
|
||||
//
|
||||
template<typename InputIterator, typename T>
|
||||
inline SPROUT_CONSTEXPR bool
|
||||
all_of_equal(InputIterator first, InputIterator last, T const& value) {
|
||||
return first == last ? true
|
||||
: *first == value && sprout::all_of_equal(sprout::next(first), last, value)
|
||||
;
|
||||
typedef typename std::iterator_traits<InputIterator>::iterator_category* category;
|
||||
return sprout::detail::all_of_equal(first, last, value, category());
|
||||
}
|
||||
} // namespace sprout
|
||||
|
||||
|
|
|
@ -1,19 +1,73 @@
|
|||
#ifndef SPROUT_ALGORITHM_ANY_OF_EQUAL_HPP
|
||||
#define SPROUT_ALGORITHM_ANY_OF_EQUAL_HPP
|
||||
|
||||
#include <iterator>
|
||||
#include <sprout/config.hpp>
|
||||
#include <sprout/iterator/operation.hpp>
|
||||
#include HDR_ITERATOR_SSCRISK_CEL_OR_SPROUT
|
||||
|
||||
namespace sprout {
|
||||
namespace detail {
|
||||
template<typename RandomAccessIterator, typename T>
|
||||
inline SPROUT_CONSTEXPR bool
|
||||
any_of_equal_impl_ra(
|
||||
RandomAccessIterator first, RandomAccessIterator last, T const& value,
|
||||
typename std::iterator_traits<RandomAccessIterator>::difference_type pivot
|
||||
)
|
||||
{
|
||||
return pivot == 0 ? *first == value
|
||||
: sprout::detail::any_of_equal_impl_ra(
|
||||
first, sprout::next(first, pivot), value,
|
||||
pivot / 2
|
||||
)
|
||||
|| sprout::detail::any_of_equal_impl_ra(
|
||||
sprout::next(first, pivot), last, value,
|
||||
(NS_SSCRISK_CEL_OR_SPROUT::distance(first, last) - pivot) / 2
|
||||
)
|
||||
;
|
||||
}
|
||||
template<typename RandomAccessIterator, typename T>
|
||||
inline SPROUT_CONSTEXPR bool
|
||||
any_of_equal(
|
||||
RandomAccessIterator first, RandomAccessIterator last, T const& value,
|
||||
std::random_access_iterator_tag*
|
||||
)
|
||||
{
|
||||
return first == last ? false
|
||||
: sprout::detail::any_of_equal_impl_ra(first, last, value, NS_SSCRISK_CEL_OR_SPROUT::distance(first, last) / 2)
|
||||
;
|
||||
}
|
||||
|
||||
template<typename InputIterator, typename T>
|
||||
inline SPROUT_CONSTEXPR bool
|
||||
any_of_equal_impl(InputIterator first, InputIterator last, T const& value) {
|
||||
return first == last ? false
|
||||
: *first == value || sprout::detail::any_of_equal_impl(sprout::next(first), last, value)
|
||||
;
|
||||
}
|
||||
template<typename InputIterator, typename T>
|
||||
inline SPROUT_CONSTEXPR bool
|
||||
any_of_equal(
|
||||
InputIterator first, InputIterator last, T const& value,
|
||||
void*
|
||||
)
|
||||
{
|
||||
return sprout::detail::any_of_equal_impl(first, last, value);
|
||||
}
|
||||
} //namespace detail
|
||||
|
||||
//
|
||||
// any_of_equal
|
||||
//
|
||||
// recursion depth:
|
||||
// [first, last) is RandomAccessIterator -> O(log N)
|
||||
// otherwise -> O(N)
|
||||
//
|
||||
template<typename InputIterator, typename T>
|
||||
inline SPROUT_CONSTEXPR bool
|
||||
any_of_equal(InputIterator first, InputIterator last, T const& value) {
|
||||
return first == last ? false
|
||||
: *first == value || sprout::any_of_equal(sprout::next(first), last, value)
|
||||
;
|
||||
typedef typename std::iterator_traits<InputIterator>::iterator_category* category;
|
||||
return sprout::detail::any_of_equal(first, last, value, category());
|
||||
}
|
||||
} // namespace sprout
|
||||
|
||||
|
|
|
@ -1,8 +1,10 @@
|
|||
#ifndef SPROUT_ALGORITHM_NONE_OF_HPP
|
||||
#define SPROUT_ALGORITHM_NONE_OF_HPP
|
||||
|
||||
#include <iterator>
|
||||
#include <sprout/config.hpp>
|
||||
#include <sprout/iterator/operation.hpp>
|
||||
#include HDR_ITERATOR_SSCRISK_CEL_OR_SPROUT
|
||||
|
||||
namespace sprout {
|
||||
namespace detail {
|
||||
|
|
|
@ -1,19 +1,73 @@
|
|||
#ifndef SPROUT_ALGORITHM_NONE_OF_EQUAL_HPP
|
||||
#define SPROUT_ALGORITHM_NONE_OF_EQUAL_HPP
|
||||
|
||||
#include <iterator>
|
||||
#include <sprout/config.hpp>
|
||||
#include <sprout/iterator/operation.hpp>
|
||||
#include HDR_ITERATOR_SSCRISK_CEL_OR_SPROUT
|
||||
|
||||
namespace sprout {
|
||||
namespace detail {
|
||||
template<typename RandomAccessIterator, typename T>
|
||||
inline SPROUT_CONSTEXPR bool
|
||||
none_of_equal_impl_ra(
|
||||
RandomAccessIterator first, RandomAccessIterator last, T const& value,
|
||||
typename std::iterator_traits<RandomAccessIterator>::difference_type pivot
|
||||
)
|
||||
{
|
||||
return pivot == 0 ? !(*first == value)
|
||||
: sprout::detail::none_of_equal_impl_ra(
|
||||
first, sprout::next(first, pivot), value,
|
||||
pivot / 2
|
||||
)
|
||||
&& sprout::detail::none_of_equal_impl_ra(
|
||||
sprout::next(first, pivot), last, value,
|
||||
(NS_SSCRISK_CEL_OR_SPROUT::distance(first, last) - pivot) / 2
|
||||
)
|
||||
;
|
||||
}
|
||||
template<typename RandomAccessIterator, typename T>
|
||||
inline SPROUT_CONSTEXPR bool
|
||||
none_of_equal(
|
||||
RandomAccessIterator first, RandomAccessIterator last, T const& value,
|
||||
std::random_access_iterator_tag*
|
||||
)
|
||||
{
|
||||
return first == last ? true
|
||||
: sprout::detail::none_of_equal_impl_ra(first, last, value, NS_SSCRISK_CEL_OR_SPROUT::distance(first, last) / 2)
|
||||
;
|
||||
}
|
||||
|
||||
template<typename InputIterator, typename T>
|
||||
inline SPROUT_CONSTEXPR bool
|
||||
none_of_equal_impl(InputIterator first, InputIterator last, T const& value) {
|
||||
return first == last ? true
|
||||
: !(*first == value) && sprout::detail::none_of_equal_impl(sprout::next(first), last, value)
|
||||
;
|
||||
}
|
||||
template<typename InputIterator, typename T>
|
||||
inline SPROUT_CONSTEXPR bool
|
||||
none_of_equal(
|
||||
InputIterator first, InputIterator last, T const& value,
|
||||
void*
|
||||
)
|
||||
{
|
||||
return sprout::detail::none_of_equal_impl(first, last, value);
|
||||
}
|
||||
} //namespace detail
|
||||
|
||||
//
|
||||
// none_of_equal
|
||||
//
|
||||
// recursion depth:
|
||||
// [first, last) is RandomAccessIterator -> O(log N)
|
||||
// otherwise -> O(N)
|
||||
//
|
||||
template<typename InputIterator, typename T>
|
||||
inline SPROUT_CONSTEXPR bool
|
||||
none_of_equal(InputIterator first, InputIterator last, T const& value) {
|
||||
return first == last ? true
|
||||
: !(*first == value) && sprout::none_of_equal(sprout::next(first), last, value)
|
||||
;
|
||||
typedef typename std::iterator_traits<InputIterator>::iterator_category* category;
|
||||
return sprout::detail::none_of_equal(first, last, value, category());
|
||||
}
|
||||
} // namespace sprout
|
||||
|
||||
|
|
|
@ -16,9 +16,14 @@ namespace sprout {
|
|||
;
|
||||
}
|
||||
} // namespace detail
|
||||
|
||||
//
|
||||
// one_of_equal
|
||||
//
|
||||
// recursion depth:
|
||||
// [first, last) is RandomAccessIterator -> O(log N)
|
||||
// otherwise -> O(N)
|
||||
//
|
||||
template<typename InputIterator, typename T>
|
||||
inline SPROUT_CONSTEXPR bool
|
||||
one_of_equal(InputIterator first, InputIterator last, T const& value) {
|
||||
|
|
Loading…
Reference in a new issue