aboutsummaryrefslogtreecommitdiff
path: root/Carpet/CarpetLib
diff options
context:
space:
mode:
authorErik Schnetter <schnetter@gmail.com>2013-05-26 16:15:36 -0400
committerErik Schnetter <schnetter@gmail.com>2013-05-26 16:18:25 -0400
commitfa57059414919bea3cd6d7d62b69dc733f8b8f6d (patch)
treea7db559a64f53e5f36008a3d57dcbebac3915a74 /Carpet/CarpetLib
parent62d962f1a5140c37b494ab08174d1d5f42a52e8a (diff)
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.
Diffstat (limited to 'Carpet/CarpetLib')
-rw-r--r--Carpet/CarpetLib/src/bbox.cc46
-rw-r--r--Carpet/CarpetLib/src/bbox.hh61
-rw-r--r--Carpet/CarpetLib/src/defs.hh13
-rw-r--r--Carpet/CarpetLib/src/vect.hh76
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<T,D>::output (ostream& os) const {
+// Comparison
+
+namespace std {
+ // ==
+ template<typename T, int D>
+ bool equal_to<bbox<T,D> >::operator()(const bbox<T,D>& x, const bbox<T,D>& y)
+ const
+ {
+ if (x.empty() and y.empty()) return true;
+ if (x.empty() or y.empty()) return false;
+ /*const*/ equal_to<vect<T,D> > 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<typename T, int D>
+ bool less<bbox<T,D> >::operator()(const bbox<T,D>& x, const bbox<T,D>& y)
+ const
+ {
+ // Empty bboxes compare less than any non-empty bbox
+ if (y.empty()) return false;
+ if (x.empty()) return true;
+ /*const*/ less<vect<T,D> > 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<int,0>;
template class bbox<int,1>;
@@ -335,3 +373,11 @@ template class bbox<int,2>;
template class bbox<int,3>;
template class bbox<int,4>;
template class bbox<CCTK_REAL,dim>;
+
+namespace std {
+ template struct less<bbox<int,0> >;
+ template struct less<bbox<int,1> >;
+ template struct less<bbox<int,2> >;
+ template struct less<bbox<int,3> >;
+ template struct less<bbox<int,4> >;
+}
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 <cassert>
#include <cstdlib>
+#include <functional>
#include <iostream>
#include <limits>
@@ -271,4 +272,64 @@ inline ostream& operator<< (ostream& os, const bbox<T,D>& b) {
+// Comparison
+
+namespace std {
+ // ==
+ template<typename T, int D>
+ struct equal_to<bbox<T,D> >: binary_function<bbox<T,D>, bbox<T,D>, bool>
+ {
+ bool operator()(const bbox<T,D>& x, const bbox<T,D>& y) const;
+ };
+
+ // <
+ template<typename T, int D>
+ struct less<bbox<T,D> >: binary_function<bbox<T,D>, bbox<T,D>, bool>
+ {
+ bool operator()(const bbox<T,D>& x, const bbox<T,D>& y) const;
+ };
+
+ // >
+ template<typename T, int D>
+ struct greater<bbox<T,D> >: binary_function<bbox<T,D>, bbox<T,D>, bool>
+ {
+ bool operator()(const bbox<T,D>& x, const bbox<T,D>& y) const
+ {
+ return less<bbox<T,D> >()(y, x);
+ }
+ };
+
+ // >=
+ template<typename T, int D>
+ struct greater_equal<bbox<T,D> >: binary_function<bbox<T,D>, bbox<T,D>, bool>
+ {
+ bool operator()(const bbox<T,D>& x, const bbox<T,D>& y) const
+ {
+ return not less<bbox<T,D> >()(x, y);
+ }
+ };
+
+ // <=
+ template<typename T, int D>
+ struct less_equal<bbox<T,D> >: binary_function<bbox<T,D>, bbox<T,D>, bool>
+ {
+ bool operator()(const bbox<T,D>& x, const bbox<T,D>& y) const
+ {
+ return not greater<bbox<T,D> >()(x, y);
+ }
+ };
+
+ // !=
+ template<typename T, int D>
+ struct not_equal_to<bbox<T,D> >: binary_function<bbox<T,D>, bbox<T,D>, bool>
+ {
+ bool operator()(const bbox<T,D>& x, const bbox<T,D>& y) const
+ {
+ return not equal_to<bbox<T,D> >()(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 <typename T>
-bool equals (vector<T> const& v, vector<T> const& w)
-{
- if (v.size() != w.size()) return false;
- for (size_t i=0; i<v.size(); ++i) {
- if (v.AT(i) != w.AT(i)) return false;
- }
- return true;
-}
-
-
-
// Container memory usage
inline size_t memoryof (char const & e) { return sizeof e; }
inline size_t memoryof (short const & e) { return sizeof e; }
diff --git a/Carpet/CarpetLib/src/vect.hh b/Carpet/CarpetLib/src/vect.hh
index af27f3094..c7b2c7581 100644
--- a/Carpet/CarpetLib/src/vect.hh
+++ b/Carpet/CarpetLib/src/vect.hh
@@ -6,6 +6,7 @@
#include <algorithm>
#include <cassert>
#include <cmath>
+#include <functional>
#include <iostream>
#include "defs.hh"
@@ -677,6 +678,81 @@ inline ostream& operator<< (ostream& os, const vect<T,D>& a) {
+// Comparison
+
+namespace std {
+ // ==
+ template<typename T, int D>
+ struct equal_to<vect<T,D> >: binary_function<vect<T,D>, vect<T,D>, bool>
+ {
+ bool operator()(const vect<T,D>& x, const vect<T,D>& y) const
+ {
+ /*const*/ equal_to<T> T_equal_to;
+ for (int d=0; d<D; ++d) {
+ if (not T_equal_to(x[d], y[d])) return false;
+ }
+ return true;
+ }
+ };
+
+ // <
+ template<typename T, int D>
+ struct less<vect<T,D> >: binary_function<vect<T,D>, vect<T,D>, bool>
+ {
+ bool operator()(const vect<T,D>& x, const vect<T,D>& y) const
+ {
+ /*const*/ less<T> 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<typename T, int D>
+ struct greater<vect<T,D> >: binary_function<vect<T,D>, vect<T,D>, bool>
+ {
+ bool operator()(const vect<T,D>& x, const vect<T,D>& y) const
+ {
+ return less<vect<T,D> >()(y, x);
+ }
+ };
+
+ // >=
+ template<typename T, int D>
+ struct greater_equal<vect<T,D> >: binary_function<vect<T,D>, vect<T,D>, bool>
+ {
+ bool operator()(const vect<T,D>& x, const vect<T,D>& y) const
+ {
+ return not less<vect<T,D> >()(x, y);
+ }
+ };
+
+ // <=
+ template<typename T, int D>
+ struct less_equal<vect<T,D> >: binary_function<vect<T,D>, vect<T,D>, bool>
+ {
+ bool operator()(const vect<T,D>& x, const vect<T,D>& y) const
+ {
+ return not greater<vect<T,D> >()(x, y);
+ }
+ };
+
+ // !=
+ template<typename T, int D>
+ struct not_equal_to<vect<T,D> >: binary_function<vect<T,D>, vect<T,D>, bool>
+ {
+ bool operator()(const vect<T,D>& x, const vect<T,D>& y) const
+ {
+ return not equal_to<vect<T,D> >()(x, y);
+ }
+ };
+}
+
+
+
// MPI
template<typename T,int D>