From fa57059414919bea3cd6d7d62b69dc733f8b8f6d Mon Sep 17 00:00:00 2001 From: Erik Schnetter Date: Sun, 26 May 2013 16:15:36 -0400 Subject: CarpetLib: Use C++ standard methods to compare containers Remove CarpetLib's "equals" method. Implement equal_to, less, greater, greater_equal, less_equal, not_equal for vect and bbox classes. --- Carpet/CarpetLib/src/bbox.cc | 46 +++++++++++++++++++++++++++ Carpet/CarpetLib/src/bbox.hh | 61 +++++++++++++++++++++++++++++++++++ Carpet/CarpetLib/src/defs.hh | 13 -------- Carpet/CarpetLib/src/vect.hh | 76 ++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 183 insertions(+), 13 deletions(-) diff --git a/Carpet/CarpetLib/src/bbox.cc b/Carpet/CarpetLib/src/bbox.cc index d2ea8dd7d..a0041edae 100644 --- a/Carpet/CarpetLib/src/bbox.cc +++ b/Carpet/CarpetLib/src/bbox.cc @@ -328,6 +328,44 @@ void bbox::output (ostream& os) const { +// Comparison + +namespace std { + // == + template + bool equal_to >::operator()(const bbox& x, const bbox& y) + const + { + if (x.empty() and y.empty()) return true; + if (x.empty() or y.empty()) return false; + /*const*/ equal_to > vect_equal_to; + if (not vect_equal_to(x.stride(), y.stride())) return false; + if (not vect_equal_to(x.lower(), y.lower())) return false; + if (not vect_equal_to(x.upper(), y.upper())) return false; + return true; + } + + // < + template + bool less >::operator()(const bbox& x, const bbox& y) + const + { + // Empty bboxes compare less than any non-empty bbox + if (y.empty()) return false; + if (x.empty()) return true; + /*const*/ less > vect_less; + if (vect_less(x.stride(), y.stride())) return true; + if (vect_less(y.stride(), x.stride())) return false; + if (vect_less(x.lower(), y.lower())) return true; + if (vect_less(y.lower(), x.lower())) return false; + if (vect_less(x.upper(), y.upper())) return true; + if (vect_less(y.upper(), x.upper())) return false; + return false; + } +} + + + // Note: We need all dimensions all the time. template class bbox; template class bbox; @@ -335,3 +373,11 @@ template class bbox; template class bbox; template class bbox; template class bbox; + +namespace std { + template struct less >; + template struct less >; + template struct less >; + template struct less >; + template struct less >; +} diff --git a/Carpet/CarpetLib/src/bbox.hh b/Carpet/CarpetLib/src/bbox.hh index 2847576b5..85af2fcd3 100644 --- a/Carpet/CarpetLib/src/bbox.hh +++ b/Carpet/CarpetLib/src/bbox.hh @@ -3,6 +3,7 @@ #include #include +#include #include #include @@ -271,4 +272,64 @@ inline ostream& operator<< (ostream& os, const bbox& b) { +// Comparison + +namespace std { + // == + template + struct equal_to >: binary_function, bbox, bool> + { + bool operator()(const bbox& x, const bbox& y) const; + }; + + // < + template + struct less >: binary_function, bbox, bool> + { + bool operator()(const bbox& x, const bbox& y) const; + }; + + // > + template + struct greater >: binary_function, bbox, bool> + { + bool operator()(const bbox& x, const bbox& y) const + { + return less >()(y, x); + } + }; + + // >= + template + struct greater_equal >: binary_function, bbox, bool> + { + bool operator()(const bbox& x, const bbox& y) const + { + return not less >()(x, y); + } + }; + + // <= + template + struct less_equal >: binary_function, bbox, bool> + { + bool operator()(const bbox& x, const bbox& y) const + { + return not greater >()(x, y); + } + }; + + // != + template + struct not_equal_to >: binary_function, bbox, bool> + { + bool operator()(const bbox& x, const bbox& y) const + { + return not equal_to >()(x, y); + } + }; +} + + + #endif // BBOX_HH diff --git a/Carpet/CarpetLib/src/defs.hh b/Carpet/CarpetLib/src/defs.hh index e2c6325af..7968300a2 100644 --- a/Carpet/CarpetLib/src/defs.hh +++ b/Carpet/CarpetLib/src/defs.hh @@ -278,19 +278,6 @@ namespace std { -// Container equality -template -bool equals (vector const& v, vector const& w) -{ - if (v.size() != w.size()) return false; - for (size_t i=0; i #include #include +#include #include #include "defs.hh" @@ -677,6 +678,81 @@ inline ostream& operator<< (ostream& os, const vect& a) { +// Comparison + +namespace std { + // == + template + struct equal_to >: binary_function, vect, bool> + { + bool operator()(const vect& x, const vect& y) const + { + /*const*/ equal_to T_equal_to; + for (int d=0; d + struct less >: binary_function, vect, bool> + { + bool operator()(const vect& x, const vect& y) const + { + /*const*/ less T_less; + for (int d=D-1; d>=0; --d) { + if (T_less(x[d], y[d])) return true; + if (T_less(y[d], x[d])) return false; + } + return false; + } + }; + + // > + template + struct greater >: binary_function, vect, bool> + { + bool operator()(const vect& x, const vect& y) const + { + return less >()(y, x); + } + }; + + // >= + template + struct greater_equal >: binary_function, vect, bool> + { + bool operator()(const vect& x, const vect& y) const + { + return not less >()(x, y); + } + }; + + // <= + template + struct less_equal >: binary_function, vect, bool> + { + bool operator()(const vect& x, const vect& y) const + { + return not greater >()(x, y); + } + }; + + // != + template + struct not_equal_to >: binary_function, vect, bool> + { + bool operator()(const vect& x, const vect& y) const + { + return not equal_to >()(x, y); + } + }; +} + + + // MPI template -- cgit v1.2.3