summaryrefslogtreecommitdiffstats
path: root/src/include/algorithm
diff options
context:
space:
mode:
authorStephen Cprek <smcprek@us.ibm.com>2016-09-30 16:55:15 -0500
committerDaniel M. Crowell <dcrowell@us.ibm.com>2016-10-30 22:44:23 -0400
commita6f34cd9b2c79e803e564b917951b63f9252d02a (patch)
treea3afd7a876addc0b5a27b97fb37dc06c46103bfc /src/include/algorithm
parent99552570de2dfc1f11f22746c5cdb86820f154b8 (diff)
downloadtalos-hostboot-a6f34cd9b2c79e803e564b917951b63f9252d02a.tar.gz
talos-hostboot-a6f34cd9b2c79e803e564b917951b63f9252d02a.zip
Implement std::array
Change-Id: I86149816b46f213502801fb710dc2f68d4a4b0f7 Reviewed-on: http://ralgit01.raleigh.ibm.com/gerrit1/30647 Tested-by: Jenkins Server <pfd-jenkins+hostboot@us.ibm.com> Reviewed-by: Martin Gloff <mgloff@us.ibm.com> Tested-by: FSP CI Jenkins <fsp-CI-jenkins+hostboot@us.ibm.com> Reviewed-by: Andres A. Lugo-Reyes <aalugore@us.ibm.com> Reviewed-by: A. P. Williams III <iawillia@us.ibm.com> Reviewed-by: Daniel M. Crowell <dcrowell@us.ibm.com>
Diffstat (limited to 'src/include/algorithm')
-rw-r--r--src/include/algorithm140
1 files changed, 140 insertions, 0 deletions
diff --git a/src/include/algorithm b/src/include/algorithm
index 9641a797a..cc3886ff5 100644
--- a/src/include/algorithm
+++ b/src/include/algorithm
@@ -78,6 +78,38 @@ namespace std
}
/**
+ * Swaps the values of the elements the given iterators are pointing to.
+ * @param[in] a iterator to the elements to swap
+ * @param[in] b iterator to the elements to swap
+ */
+ template<class ForwardIterator1, class ForwardIterator2>
+ inline void iter_swap(ForwardIterator1 a, ForwardIterator2 b)
+ {
+ swap(*a, *b);
+ }
+
+ /**
+ * Swaps the values of the elements the given iterators are pointing to.
+ * @param[in] first1 iterator of the beginning of first range
+ * @param[in] last1 iterator of the end of first range
+ * @param[in] first2 iterator of the beginning of the second range
+ * @return Iterator to the element past the last element exchanged in the
+ * range beginning with first2.
+ */
+
+ template< class ForwardIterator1, class ForwardIterator2 >
+ inline ForwardIterator2 swap_ranges(ForwardIterator1 first1,
+ ForwardIterator1 last1,
+ ForwardIterator2 first2)
+ {
+ while (first1 != last1)
+ {
+ iter_swap(first1++, first2++);
+ }
+ return first2;
+ }
+
+ /**
* Exchange values of two objects
* @param[in] a reference to an object to be swaped with b
* @param[in] b reference to an object to be swaped with a
@@ -87,12 +119,24 @@ namespace std
inline void
swap(T& a, T&b )
{
+ // @TODO RTC:162287 use std::move()
T c(a);
a=b;
b=c;
}
/**
+ * Swaps the arrays a and b. In effect calls std::swap_ranges(a, a+N, b)
+ * @param[in] a reference to an object to be swaped with b
+ * @param[in] b reference to an object to be swaped with a
+ */
+ template< class T, size_t N>
+ inline void swap(T(&a)[N], T(&b)[N])
+ {
+ swap_ranges(a, a+N, b);
+ }
+
+ /**
* Fill a range with value
* @param[in] first ForwardIterator to the first position in the source range.
* @param[in] last ForwardIterator to the last position +1 in the source range.
@@ -874,7 +918,103 @@ namespace std
return result;
}
+ /** Returns true if the range [first1, last1) is equal to the range
+ * [first2, first2 + (last1 - first1)), and false otherwise
+ *
+ * The two ranges are considered equal if, for every iterator i in the range
+ * [first1,last1), *i equals *(first2 + (i - first1)). The overloads use
+ * operator== to determine if two elements are equal.
+ *
+ * @param[in] first1 - The beginning of the first range.
+ * @param[in] last1 - The end of the first range.
+ * @param[in] first2 - The beginning of the second range.
+ * @return - Returns true equal, false otherwise
+ */
+ template < typename InputIterator1, typename InputIterator2 >
+ bool equal(InputIterator1 first1, InputIterator1 last1,
+ InputIterator2 first2)
+ {
+ while(first1 != last1)
+ {
+ if (!(*first1 == *first2))
+ {
+ return false;
+ }
+ ++first1; ++first2;
+ }
+ return true;
+ }
+ /** Returns true if the range [first1, last1) is equal, using a predicate,
+ * to the range [first2, first2 + (last1 - first1)), and false otherwise
+ *
+ * The two ranges are considered equal if, for every iterator i in the range
+ * [first1,last1), *i equals *(first2 + (i - first1)). The overloads use
+ * operator== to determine if two elements are equal.
+ *
+ * @param[in] first1 - The beginning of the first range.
+ * @param[in] last1 - The end of the first range.
+ * @param[in] first2 - The beginning of the second range.
+ * @param[in] comp - Binary predicate which returns true if the elements
+ * should be treated as equal.
+ * @return - Returns true equal, false otherwise
+ */
+ template < typename InputIterator1, typename InputIterator2,
+ typename BinaryPredicate >
+ bool equal( InputIterator1 first1, InputIterator1 last1,
+ InputIterator2 first2, BinaryPredicate comp )
+ {
+ while(first1 != last1)
+ {
+ if (!comp(*first1, *first2))
+ {
+ return false;
+ }
+ ++first1; ++first2;
+ }
+ return true;
+ }
+
+ /** Checks if the first range [first1, last1) is lexicographically less than
+ * the second range [first2, last2).
+ *
+ * Elements are compared using operator<.
+ * Lexicographical comparison is a operation with the following properties:
+ * - Two ranges are compared element by element.
+ * - The first mismatching element defines which range is lexicographically
+ * less or greater than the other.
+ * - If one range is a prefix of another, the shorter range is
+ * lexicographically less than the other.
+ * - If two ranges have equivalent elements and are of the same length,
+ * then the ranges are lexicographically equal.
+ * - An empty range is lexicographically less than any non-empty range.
+ * - Two empty ranges are lexicographically equal.
+ *
+ * @param[in] first1 - The beginning of the first range.
+ * @param[in] last1 - The end of the first range.
+ * @param[in] first2 - The beginning of the second range.
+ * @param[in] last2 - The end of the second range.
+ * @return - true if the first range is lexicographically less than the
+ * second.
+ */
+ template < typename InputIterator1, typename InputIterator2 >
+ bool lexicographical_compare(InputIterator1 first1, InputIterator1 last1,
+ InputIterator2 first2, InputIterator2 last2)
+ {
+ while( (first1 != last1) && (first2 != last2) )
+ {
+ if (*first1 < *first2)
+ {
+ return true;
+ }
+ if (*first2 < *first1)
+ {
+ return false;
+ }
+ first1++; first2++;
+ }
+ return (first1 == last1) && (first2 != last2);
+ }
};
#endif
OpenPOWER on IntegriCloud