Added equal method to compare ranges of objects.
equal is needed for the vector implementation.
diff --git a/include/algorithm b/include/algorithm
index 588a267..55dfc54 100644
--- a/include/algorithm
+++ b/include/algorithm
@@ -45,6 +45,7 @@
// - swap
// - fill
// - fill_n
+// - equal
template<typename _T> inline const _T& min(const _T& left, const _T& right)
{
@@ -189,6 +190,49 @@
return begin + num;
}
+
+// Test a range for element-wise equality using operator==
+// @param begin1 An input iterator.
+// @param end1 An input iterator.
+// @param begin2 An input iterator.
+// @return true if all the elements of the range are equal.
+// TODO: When we have a proper structure for iterator as opposed to
+// just pointers, we should be able to the get the type for the values
+// referenced by the iterator and default to memcmp for simple types.
+template<typename _InputIterator1, typename _InputIterator2>
+inline bool equal(_InputIterator1 begin1, _InputIterator1 end1,
+ _InputIterator2 begin2)
+{
+ for (; begin1 < end1; ++begin1, ++begin2)
+ {
+ if (!(*begin1 == *begin2))
+ {
+ return false;
+ }
+ }
+ return true;
+}
+
+// Test a range for element-wise equality using operator==
+// @param begin1 An input iterator.
+// @param end1 An input iterator.
+// @param begin2 An input iterator.
+// @param binary_pred A binary predicate function.
+// @return true if all the elements of the range are equal.
+template<typename _InputIterator1, typename _InputIterator2, typename _BinaryPredicated>
+inline bool equal(_InputIterator1 begin1, _InputIterator1 end1,
+ _InputIterator2 begin2, _BinaryPredicated binary_predicate)
+{
+ for (; begin1 < end1; ++begin1, ++begin2)
+ {
+ if (!bool(binary_predicate(*begin1, *begin2)))
+ {
+ return false;
+ }
+ }
+ return true;
+}
+
} // namespace std
diff --git a/tests/test_algorithm.cpp b/tests/test_algorithm.cpp
index 694e8e9..831e1fd 100644
--- a/tests/test_algorithm.cpp
+++ b/tests/test_algorithm.cpp
@@ -114,6 +114,26 @@
return true;
}
+struct Left1 { };
+struct Right1 { };
+bool operator==(const Left1&, const Right1&) {return true;}
+
+struct Left2 { };
+struct Right2 { };
+bool predicate(const Left2&, const Right2&) {return true;}
+
+bool testEqual()
+{
+ Left1 left1;
+ Right1 right1;
+ Left2 left2;
+ Right2 right2;
+
+ EXPECT_TRUE(std::equal(&left1, &left1, &right1));
+ EXPECT_TRUE(std::equal(&left2, &left2, &right2, predicate));
+ return true;
+}
+
} // namespace android
int main(int argc, char **argv)
@@ -123,5 +143,6 @@
FAIL_UNLESS(testMax);
FAIL_UNLESS(testFill);
FAIL_UNLESS(testFill_N);
+ FAIL_UNLESS(testEqual);
return kPassed;
}