new functions min and max for 2 - 2+n parameters - now in C++11 vith variadics

master
Marc Wäckerlin 10 years ago
parent 3a8795996f
commit d48ffc8e92
  1. 41
      src/mrw/stdext.hxx
  2. 25
      test/stdext_test.cxx

@ -16,6 +16,7 @@
#include <string> #include <string>
#include <iostream> #include <iostream>
#include <algorithm> #include <algorithm>
#include <mrw/checkcxx11.hxx>
namespace mrw { namespace mrw {
@ -52,6 +53,11 @@ namespace mrw {
template<typename T> T& max(T& t1, T& t2) { template<typename T> T& max(T& t1, T& t2) {
return t2<t1?t1:t2; return t2<t1?t1:t2;
} }
//! Get the maximum out of two values
template<typename T> const T& max(const T& t1, const T& t2) {
return t2<t1?t1:t2;
}
#ifdef MRW__OLD_PRE11_COMPILER
//! Get the maximum out of three values //! Get the maximum out of three values
template<typename T> T& max(T& t1, T& t2, T& t3) { template<typename T> T& max(T& t1, T& t2, T& t3) {
return max(t1, max(t2, t3)); return max(t1, max(t2, t3));
@ -68,10 +74,6 @@ namespace mrw {
template<typename T> T& max(T& t1, T& t2, T& t3, T& t4, T& t5, T& t6) { template<typename T> T& max(T& t1, T& t2, T& t3, T& t4, T& t5, T& t6) {
return max(max(t1, t2, t3), max(t4, t5, t6)); return max(max(t1, t2, t3), max(t4, t5, t6));
} }
//! Get the maximum out of two values
template<typename T> const T& max(const T& t1, const T& t2) {
return t2<t1?t1:t2;
}
//! Get the maximum out of three values //! Get the maximum out of three values
template<typename T> const T& max(const T& t1, const T& t2, const T& t3) { template<typename T> const T& max(const T& t1, const T& t2, const T& t3) {
return max(t1, max(t2, t3)); return max(t1, max(t2, t3));
@ -91,6 +93,17 @@ namespace mrw {
const T& t4, const T& t5, const T& t6) { const T& t4, const T& t5, const T& t6) {
return max(max(t1, t2, t3), max(t4, t5, t6)); return max(max(t1, t2, t3), max(t4, t5, t6));
} }
#else
//! Get the maximum out of any number of arguments
template<typename T, typename... T2> T& max(T& t1, T& t2, T2&... args) {
return max(t1, max(t2, args...));
}
//! Get the maximum out of any number of arguments
template<typename T, typename... T2> const T& max(const T& t1, const T& t2,
const T2&... args) {
return max(t1, max(t2, args...));
}
#endif
//@} //@}
/** @defgroup getmin Get the Minimum of Values */ /** @defgroup getmin Get the Minimum of Values */
@ -99,6 +112,11 @@ namespace mrw {
template<typename T> T& min(T& t1, T& t2) { template<typename T> T& min(T& t1, T& t2) {
return t1<t2?t1:t2; return t1<t2?t1:t2;
} }
//! Get the minimum out of two values
template<typename T> const T& min(const T& t1, const T& t2) {
return t1<t2?t1:t2;
}
#ifdef MRW__OLD_PRE11_COMPILER
//! Get the minimum out of three values //! Get the minimum out of three values
template<typename T> T& min(T& t1, T& t2, T& t3) { template<typename T> T& min(T& t1, T& t2, T& t3) {
return min(t1, min(t2, t3)); return min(t1, min(t2, t3));
@ -115,10 +133,6 @@ namespace mrw {
template<typename T> T& min(T& t1, T& t2, T& t3, T& t4, T& t5, T& t6) { template<typename T> T& min(T& t1, T& t2, T& t3, T& t4, T& t5, T& t6) {
return min(min(t1, t2, t3), min(t4, t5, t6)); return min(min(t1, t2, t3), min(t4, t5, t6));
} }
//! Get the minimum out of two values
template<typename T> const T& min(const T& t1, const T& t2) {
return t2<t1?t1:t2;
}
//! Get the minimum out of three values //! Get the minimum out of three values
template<typename T> const T& min(const T& t1, const T& t2, const T& t3) { template<typename T> const T& min(const T& t1, const T& t2, const T& t3) {
return min(t1, min(t2, t3)); return min(t1, min(t2, t3));
@ -138,6 +152,17 @@ namespace mrw {
const T& t4, const T& t5, const T& t6) { const T& t4, const T& t5, const T& t6) {
return min(min(t1, t2, t3), min(t4, t5, t6)); return min(min(t1, t2, t3), min(t4, t5, t6));
} }
#else
//! Get the minimum out of any number of arguments
template<typename T, typename... T2> T& min(T& t1, T& t2, T2&... args) {
return min(t1, min(t2, args...));
}
//! Get the minimum out of any number of arguments
template<typename T, typename... T2> const T& min(const T& t1, const T& t2,
const T2&... args) {
return min(t1, min(t2, args...));
}
#endif
//@} //@}
/** @brief Dual <code>?:</code> operation: /** @brief Dual <code>?:</code> operation:

@ -29,6 +29,7 @@
#include <mrw/multiset.hxx> #include <mrw/multiset.hxx>
#include <mrw/multimap.hxx> #include <mrw/multimap.hxx>
#include <mrw/stacktrace.hxx> #include <mrw/stacktrace.hxx>
#include <mrw/stdext.hxx>
#include <cppunit/TestFixture.h> #include <cppunit/TestFixture.h>
#include <cppunit/ui/text/TestRunner.h> #include <cppunit/ui/text/TestRunner.h>
#include <cppunit/extensions/HelperMacros.h> #include <cppunit/extensions/HelperMacros.h>
@ -64,7 +65,8 @@ public:
void StringAdd() { void StringAdd() {
std::string s; std::string s;
s=s+(signed short)1+(signed int)2+(signed long)3+(signed char)'A'+ s=s+(signed short)1+(signed int)2+(signed long)3+(signed char)'A'+
(unsigned short)1+(unsigned int)2+(unsigned long)3+(unsigned char)'A'+'c'; (unsigned short)1+(unsigned int)2+(unsigned long)3+(unsigned char)'A'
+'c';
CPPUNIT_ASSERT_EQUAL(std::string("1236512365c"), s); CPPUNIT_ASSERT_EQUAL(std::string("1236512365c"), s);
s=(signed short)-4+s; s=(signed short)-4+s;
s=(signed int)5+s; s=(signed int)5+s;
@ -250,6 +252,25 @@ public:
std::string s("Hello World"); std::string s("Hello World");
mrw::to<int>(s); // not an int, exception expected mrw::to<int>(s); // not an int, exception expected
} }
void Max() {
int a1(3), a2(23), a3(2), a4(432), a5(73), a6(32);
CPPUNIT_ASSERT_EQUAL(432, mrw::max(a1, a2, a3, a4, a5, a6));
CPPUNIT_ASSERT_EQUAL(432, mrw::max(a1, a2, a3, a4, a5, a6));
CPPUNIT_ASSERT_EQUAL(73, mrw::max(a1, a2, a3, a5, a6));
CPPUNIT_ASSERT_EQUAL(32, mrw::max(a1, a2, a3, a6));
CPPUNIT_ASSERT_EQUAL(23, mrw::max(a1, a2, a3));
CPPUNIT_ASSERT_EQUAL(3, mrw::max(a1, a3));
CPPUNIT_ASSERT_EQUAL(42, mrw::max(3, 42, 6, 1));
}
void Min() {
int a1(3), a2(23), a3(2), a4(432), a5(73), a6(32);
CPPUNIT_ASSERT_EQUAL(2, mrw::min(a1, a2, a3, a4, a5, a6));
CPPUNIT_ASSERT_EQUAL(3, mrw::min(a1, a2, a4, a5, a6));
CPPUNIT_ASSERT_EQUAL(23, mrw::min(a2, a4, a5, a6));
CPPUNIT_ASSERT_EQUAL(32, mrw::min(a4, a5, a6));
CPPUNIT_ASSERT_EQUAL(73, mrw::min(a4, a5));
CPPUNIT_ASSERT_EQUAL(42, mrw::min(323, 42, 126, 91));
}
CPPUNIT_TEST_SUITE(StdExtTest); CPPUNIT_TEST_SUITE(StdExtTest);
CPPUNIT_TEST(StringConv); CPPUNIT_TEST(StringConv);
CPPUNIT_TEST(StringShift); CPPUNIT_TEST(StringShift);
@ -261,6 +282,8 @@ public:
CPPUNIT_TEST(MapShift); CPPUNIT_TEST(MapShift);
CPPUNIT_TEST(MultisetShift); CPPUNIT_TEST(MultisetShift);
CPPUNIT_TEST(MultimapShift); CPPUNIT_TEST(MultimapShift);
CPPUNIT_TEST(Max);
CPPUNIT_TEST(Min);
CPPUNIT_TEST_EXCEPTION(StringException1, mrw::invalid_argument); CPPUNIT_TEST_EXCEPTION(StringException1, mrw::invalid_argument);
CPPUNIT_TEST_EXCEPTION(StringException2, mrw::invalid_argument); CPPUNIT_TEST_EXCEPTION(StringException2, mrw::invalid_argument);
CPPUNIT_TEST_SUITE_END(); CPPUNIT_TEST_SUITE_END();

Loading…
Cancel
Save