[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[lmi-commits] [lmi] master 5004bd4 5/7: Define 'minmax' of an empty vect
From: |
Greg Chicares |
Subject: |
[lmi-commits] [lmi] master 5004bd4 5/7: Define 'minmax' of an empty vector |
Date: |
Sat, 6 Mar 2021 17:55:55 -0500 (EST) |
branch: master
commit 5004bd4161f06812ce53fee477c5db80194be8c4
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] master updated (27b708a -> 7d2794b), Greg Chicares, 2021/03/06
- [lmi-commits] [lmi] master 869de45 4/7: Fix defect introduced 20180319T1957Z: min() isn't lowest(), Greg Chicares, 2021/03/06
- [lmi-commits] [lmi] master 7d2794b 7/7: Use minmax<T> iff std::numeric_limits<T>::is_bounded, Greg Chicares, 2021/03/06
- [lmi-commits] [lmi] master 5004bd4 5/7: Define 'minmax' of an empty vector,
Greg Chicares <=
- [lmi-commits] [lmi] master e23c058 6/7: Prefer data-member initializers to ctor delegation, Greg Chicares, 2021/03/06
- [lmi-commits] [lmi] master 24611b1 1/7: Improve a unit test, Greg Chicares, 2021/03/06
- [lmi-commits] [lmi] master 8e69f1d 2/7: Don't write lowest() as "min()" without asserting they're the same, Greg Chicares, 2021/03/06
- [lmi-commits] [lmi] master 3330a9a 3/7: Don't write lowest() as "min()" even it they're the same, Greg Chicares, 2021/03/06