[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[lmi-commits] [lmi] odd/eraseme_limits 700e166 5/6: Define 'minmax' of a
From: |
Greg Chicares |
Subject: |
[lmi-commits] [lmi] odd/eraseme_limits 700e166 5/6: Define 'minmax' of an empty vector |
Date: |
Sat, 6 Mar 2021 17:57:42 -0500 (EST) |
branch: odd/eraseme_limits
commit 700e166fbecf1cc47d42c1e764115a55a36803ce
Author: Gregory W. Chicares <gchicares@sbcglobal.net>
Commit: Gregory W. Chicares <gchicares@sbcglobal.net>
Define 'minmax' of an empty vector
Akin to APL's ⌊/⍳0 and ⌈/⍳0, define the minimum (maximum) of an empty
vector to be positive (negative) N for some "largest" N: here, whatever
std::numeric_limits<T>::max() (min()) return. The intention is to use
an identity element for std::max() (std::min()), so, for floating point,
-∞ and ∞ would be better.
---
miscellany.hpp | 6 ++++++
miscellany_test.cpp | 6 ++++++
2 files changed, 12 insertions(+)
diff --git a/miscellany.hpp b/miscellany.hpp
index 8ab6f06..cb803a7 100644
--- a/miscellany.hpp
+++ b/miscellany.hpp
@@ -102,7 +102,13 @@ class minmax
}
explicit minmax(std::vector<T> const& v)
+ :minmax {}
{
+ if(v.empty())
+ {
+ return;
+ }
+
auto const& extrema = std::minmax_element(v.begin(), v.end());
minimum_ = *extrema.first ;
maximum_ = *extrema.second;
diff --git a/miscellany_test.cpp b/miscellany_test.cpp
index 19e25ff..c92bec3 100644
--- a/miscellany_test.cpp
+++ b/miscellany_test.cpp
@@ -145,6 +145,12 @@ void test_minmax()
LMI_TEST( DBL_MAX == dbl_minmax.minimum());
LMI_TEST(-DBL_MAX == dbl_minmax.maximum());
+ // Test explicit ctor with zero-element argument.
+ std::vector<int> const empty_vector {};
+ minmax<int> const empty_minmax(empty_vector);
+ LMI_TEST_EQUAL(empty_minmax.minimum(), std::numeric_limits<int>::max());
+ LMI_TEST_EQUAL(empty_minmax.maximum(), std::numeric_limits<int>::min());
+
// Test const-correctness.
std::vector<double> const v = w;
minmax<double> const m(v);
- [lmi-commits] [lmi] branch odd/eraseme_limits created (now 5e7ba88), Greg Chicares, 2021/03/06
- [lmi-commits] [lmi] odd/eraseme_limits 2a83f41 1/6: Improve unit test, Greg Chicares, 2021/03/06
- [lmi-commits] [lmi] odd/eraseme_limits e43dd18 2/6: Don't write lowest() as "min()" without asserting they're the same, Greg Chicares, 2021/03/06
- [lmi-commits] [lmi] odd/eraseme_limits 7e36141 3/6: Don't write lowest() as "min()" even it they're the same, Greg Chicares, 2021/03/06
- [lmi-commits] [lmi] odd/eraseme_limits 5bf69bf 4/6: Fix defect introduced 20180319T1957Z: min() isn't lowest(), Greg Chicares, 2021/03/06
- [lmi-commits] [lmi] odd/eraseme_limits 700e166 5/6: Define 'minmax' of an empty vector,
Greg Chicares <=
- [lmi-commits] [lmi] odd/eraseme_limits 5e7ba88 6/6: Specialize std::numeric_limits, sort of, Greg Chicares, 2021/03/06