Commit 88d9bf7b authored by Yedidya Feldblum's avatar Yedidya Feldblum Committed by Facebook GitHub Bot

no-recursion constexpr_min and constexpr_max

Reviewed By: Mizuchi

Differential Revision: D33008210

fbshipit-source-id: 1615b4a0a268343d79b7b83c558c0b60d65be616
parent c06378a2
...@@ -26,24 +26,24 @@ namespace folly { ...@@ -26,24 +26,24 @@ namespace folly {
// a and b are equivalent objects, we return b to make // a and b are equivalent objects, we return b to make
// sorting stable. // sorting stable.
// See http://stepanovpapers.com/notes.pdf for details. // See http://stepanovpapers.com/notes.pdf for details.
template <typename T>
constexpr T constexpr_max(T a) {
return a;
}
template <typename T, typename... Ts> template <typename T, typename... Ts>
constexpr T constexpr_max(T a, T b, Ts... ts) { constexpr T constexpr_max(T a, Ts... ts) {
return b < a ? constexpr_max(a, ts...) : constexpr_max(b, ts...); T list[] = {ts..., a}; // 0-length arrays are illegal
for (auto i = 0u; i < sizeof...(Ts); ++i) {
a = list[i] < a ? a : list[i];
}
return a;
} }
// When a and b are equivalent objects, we return a to // When a and b are equivalent objects, we return a to
// make sorting stable. // make sorting stable.
template <typename T>
constexpr T constexpr_min(T a) {
return a;
}
template <typename T, typename... Ts> template <typename T, typename... Ts>
constexpr T constexpr_min(T a, T b, Ts... ts) { constexpr T constexpr_min(T a, Ts... ts) {
return b < a ? constexpr_min(b, ts...) : constexpr_min(a, ts...); T list[] = {ts..., a}; // 0-length arrays are illegal
for (auto i = 0u; i < sizeof...(Ts); ++i) {
a = list[i] < a ? list[i] : a;
}
return a;
} }
template <typename T, typename Less> template <typename T, typename Less>
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment