-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Implement ranges::minmax* * Fix
- Loading branch information
Showing
5 changed files
with
287 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
// | ||
// Created by yonggyulee on 2024.12.01 | ||
// | ||
|
||
#ifndef PREVIEW_ALGORITHM_RANGES_MINMAX_H_ | ||
#define PREVIEW_ALGORITHM_RANGES_MINMAX_H_ | ||
|
||
#include <initializer_list> | ||
#include <type_traits> | ||
|
||
#include "preview/__algorithm/ranges/min_max_result.h" | ||
#include "preview/__algorithm/ranges/minmax_element.h" | ||
#include "preview/__concepts/copyable.h" | ||
#include "preview/__core/inline_variable.h" | ||
#include "preview/__functional/identity.h" | ||
#include "preview/__functional/invoke.h" | ||
#include "preview/__functional/less.h" | ||
#include "preview/__functional/wrap_functor.h" | ||
#include "preview/__iterator/indirectly_copyable_storable.h" | ||
#include "preview/__iterator/indirect_strict_weak_order.h" | ||
#include "preview/__iterator/projected.h" | ||
#include "preview/__ranges/input_range.h" | ||
#include "preview/__ranges/iterator_t.h" | ||
#include "preview/__ranges/range_value_t.h" | ||
#include "preview/__type_traits/conjunction.h" | ||
|
||
namespace preview { | ||
namespace ranges { | ||
|
||
template<typename T> | ||
using minmax_result = min_max_result<T>; | ||
|
||
namespace detail { | ||
|
||
struct minmax_niebloid { | ||
private: | ||
template<typename I, typename Proj, typename Comp, bool = /* false */ projectable<I, Proj>::value> | ||
struct check_range : std::false_type {}; | ||
template<typename I, typename Proj, typename Comp> | ||
struct check_range<I, Proj, Comp, true> : indirect_strict_weak_order<Comp, projected<I, Proj>> {}; | ||
|
||
public: | ||
template<typename T, typename Proj = identity, typename Comp = ranges::less, std::enable_if_t< | ||
indirect_strict_weak_order<Comp, projected<const T*, Proj>>::value, int> = 0> | ||
constexpr ranges::minmax_result<const T&> | ||
operator()(const T& a, const T& b, Comp comp = {}, Proj proj = {}) const { | ||
return preview::invoke(comp, preview::invoke(proj, b), preview::invoke(proj, a)) | ||
? ranges::minmax_result<const T&>{b, a} | ||
: ranges::minmax_result<const T&>{a, b}; | ||
} | ||
|
||
template<typename T, typename Proj = identity, typename Comp = ranges::less, std::enable_if_t<conjunction_v< | ||
copyable<T>, | ||
check_range<const T*, Proj, Comp>>, int> = 0> | ||
constexpr ranges::minmax_result<const T&> | ||
operator()(std::initializer_list<T> il, Comp comp = {}, Proj proj = {}) const { | ||
auto result = ranges::minmax_element(il, preview::wrap_functor(comp), preview::wrap_functor(proj)); | ||
return {*result.min, *result.max}; | ||
} | ||
|
||
template<typename R, typename Proj = identity, typename Comp = ranges::less, std::enable_if_t<conjunction_v< | ||
input_range<R>, | ||
check_range<ranges::iterator_t<R>, Proj, Comp>, | ||
indirectly_copyable_storable<iterator_t<R>, range_value_t<R>*> | ||
>, int> = 0> | ||
constexpr ranges::minmax_result<range_value_t<R>> | ||
operator()(R&& r, Comp comp = {}, Proj proj = {}) const { | ||
auto result = ranges::minmax_element(r, preview::wrap_functor(comp), preview::wrap_functor(proj)); | ||
return {std::move(*result.min), std::move(*result.max)}; | ||
} | ||
}; | ||
|
||
} // namespace detail | ||
|
||
PREVIEW_INLINE_VARIABLE constexpr detail::minmax_niebloid minmax{}; | ||
|
||
} // namespace ranges | ||
} // namespace preview | ||
|
||
#endif // PREVIEW_ALGORITHM_RANGES_MINMAX_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
// | ||
// Created by yonggyulee on 2024.12.01 | ||
// | ||
|
||
#ifndef PREVIEW_ALGORITHM_RANGES_MINMAX_ELEMENT_H_ | ||
#define PREVIEW_ALGORITHM_RANGES_MINMAX_ELEMENT_H_ | ||
|
||
#include <type_traits> | ||
|
||
#include "preview/__algorithm/ranges/min_max_result.h" | ||
#include "preview/__core/inline_variable.h" | ||
#include "preview/__functional/identity.h" | ||
#include "preview/__functional/invoke.h" | ||
#include "preview/__functional/less.h" | ||
#include "preview/__functional/wrap_functor.h" | ||
#include "preview/__iterator/forward_iterator.h" | ||
#include "preview/__iterator/indirect_strict_weak_order.h" | ||
#include "preview/__iterator/projected.h" | ||
#include "preview/__iterator/sentinel_for.h" | ||
#include "preview/__ranges/begin.h" | ||
#include "preview/__ranges/borrowed_iterator_t.h" | ||
#include "preview/__ranges/end.h" | ||
#include "preview/__ranges/iterator_t.h" | ||
#include "preview/__type_traits/conjunction.h" | ||
#include "preview/__utility/cxx20_rel_ops.h" | ||
|
||
namespace preview { | ||
namespace ranges { | ||
|
||
template<typename I> | ||
using minmax_element_result = min_max_result<I>; | ||
|
||
namespace detail { | ||
|
||
struct minmax_element_niebloid { | ||
private: | ||
template<typename I, typename Proj, typename Comp, bool = /* false */ projectable<I, Proj>::value> | ||
struct check_range : std::false_type {}; | ||
template<typename I, typename Proj, typename Comp> | ||
struct check_range<I, Proj, Comp, true> : indirect_strict_weak_order<Comp, projected<I, Proj>> {}; | ||
|
||
// lambda is not constexpr until C++17 | ||
template<typename Proj, typename Comp> | ||
struct minmax_element_less { | ||
Proj& proj; | ||
Comp& comp; | ||
|
||
template<typename I> | ||
constexpr auto operator()(const I& x, const I& y) const { | ||
return preview::invoke(comp, preview::invoke(proj, *x), preview::invoke(proj, *y)); | ||
} | ||
}; | ||
|
||
public: | ||
template<typename I, typename S, typename Proj = identity, typename Comp = ranges::less, std::enable_if_t<conjunction< | ||
forward_iterator<I>, | ||
sentinel_for<S, I>, | ||
check_range<I, Proj, Comp> | ||
>::value, int> = 0> | ||
constexpr minmax_element_result<I> operator()(I first, S last, Comp comp = {}, Proj proj = {}) const { | ||
using namespace preview::rel_ops; | ||
|
||
I min_ = first; | ||
I max_ = first; | ||
if (first == last || ++first == last) { | ||
return {min_, max_}; | ||
} | ||
|
||
minmax_element_less<Proj, Comp> less{proj, comp}; | ||
|
||
if (less(first, min_)) | ||
min_ = first; | ||
else | ||
max_ = first; | ||
|
||
while (++first != last) { | ||
I i = first; | ||
if (++first == last) { | ||
if (less(i, min_)) | ||
min_ = i; | ||
else if (!less(i, max_)) | ||
max_ = i; | ||
break; | ||
} | ||
|
||
if (less(first, i)) { | ||
if (less(first, min_)) | ||
min_ = first; | ||
if (!less(i, max_)) | ||
max_ = i; | ||
} else { | ||
if (less(i, min_)) | ||
min_ = i; | ||
if (!less(first, max_)) | ||
max_ = first; | ||
} | ||
} | ||
|
||
return {min_, max_}; | ||
} | ||
|
||
template<typename R, typename Proj = identity, typename Comp = ranges::less, std::enable_if_t<conjunction< | ||
forward_range<R>, | ||
check_range<iterator_t<R>, Proj, Comp> | ||
>::value, int> = 0> | ||
constexpr minmax_element_result<borrowed_iterator_t<R>> operator()(R&& r, Comp comp = {}, Proj proj = {}) const { | ||
return (*this)(ranges::begin(r), ranges::end(r), preview::wrap_functor(comp), preview::wrap_functor(proj)); | ||
} | ||
}; | ||
|
||
} // namespace detail | ||
|
||
PREVIEW_INLINE_VARIABLE constexpr detail::minmax_element_niebloid minmax_element{}; | ||
|
||
} // namespace ranges | ||
} // namespace preview | ||
|
||
#endif // PREVIEW_ALGORITHM_RANGES_MINMAX_ELEMENT_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters