summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/include/iterator48
-rw-r--r--src/usr/testcore/lib/stltest.H32
2 files changed, 70 insertions, 10 deletions
diff --git a/src/include/iterator b/src/include/iterator
index 396e1b594..dbd712b9e 100644
--- a/src/include/iterator
+++ b/src/include/iterator
@@ -5,7 +5,9 @@
/* */
/* OpenPOWER HostBoot Project */
/* */
-/* COPYRIGHT International Business Machines Corp. 2012,2014 */
+/* Contributors Listed Below - COPYRIGHT 2012,2019 */
+/* [+] International Business Machines Corp. */
+/* */
/* */
/* Licensed under the Apache License, Version 2.0 (the "License"); */
/* you may not use this file except in compliance with the License. */
@@ -62,11 +64,11 @@ struct iterator_traits<T*>
* @param[in] i - The iterator to advance.
* @param[in] n - The distance to advance the iterator.
*
- * This function is equivalent to calling (++i) n times.
+ * This function is equivalent to calling (++i) n times.
*
- * If the iterator supports random access then this function will be
+ * If the iterator supports random access then this function will be
* implemented in linear time with respect to n.
- *
+ *
*/
template <typename InputIterator, typename Distance>
void advance(InputIterator& i, Distance n)
@@ -90,11 +92,11 @@ void advance(InputIterator& i, Distance n)
* access iterators.
*/
template <typename InputIterator>
-typename iterator_traits<InputIterator>::difference_type
+typename iterator_traits<InputIterator>::difference_type
distance(InputIterator first, InputIterator last)
{
return Util::__Util_Iterator_Impl::distance<
- InputIterator,
+ InputIterator,
typename iterator_traits<InputIterator>::difference_type>
(first, last);
}
@@ -129,7 +131,7 @@ class back_insert_iterator
/** Dereference operator.
*
* This is used to make the standard pattern '*i = x' work on
- * an iterator. Since we need to 'push_back' into the
+ * an iterator. Since we need to 'push_back' into the
* container we don't actually return anything except ourself,
* which allows the operator= to be called.
*/
@@ -168,16 +170,44 @@ class back_insert_iterator
* copy(v.rbegin(), v.rend(), back_inserter(v2));
*
* @param[in] s - Sequence to create an iterator for.
- *
+ *
* @return The back_insert_iterator.
*/
template <typename BackInsertionSequence>
-back_insert_iterator<BackInsertionSequence>
+back_insert_iterator<BackInsertionSequence>
back_inserter(BackInsertionSequence& s)
{
return back_insert_iterator<BackInsertionSequence>(s);
}
+/**
+ * begin(array)
+ * Returns pointer to beginning of array
+ *
+ * Example:
+ * int c_array[] = {0, 1, 2};
+ * vector<int> l_cpp_array (begin(c_array), end(c_array));
+ */
+template <typename T, size_t size>
+T* begin(T (&c_array)[size])
+{
+ return &c_array[0];
+}
+
+/**
+ * end(array)
+ * Returns pointer to end of array (i.e. the element after the last element)
+ *
+ * Example:
+ * int c_array[] = {0, 1, 2};
+ * vector<int> l_cpp_array (begin(c_array), end(c_array));
+ */
+template <typename T, size_t size>
+T* end(T (&c_array)[size])
+{
+ return &c_array[0] + size;
+}
+
}; // namespace std.
#endif
diff --git a/src/usr/testcore/lib/stltest.H b/src/usr/testcore/lib/stltest.H
index 31cc3cbe9..34cb64d4a 100644
--- a/src/usr/testcore/lib/stltest.H
+++ b/src/usr/testcore/lib/stltest.H
@@ -5,7 +5,7 @@
/* */
/* OpenPOWER HostBoot Project */
/* */
-/* Contributors Listed Below - COPYRIGHT 2011,2017 */
+/* Contributors Listed Below - COPYRIGHT 2011,2019 */
/* [+] International Business Machines Corp. */
/* */
/* */
@@ -782,5 +782,35 @@ class STLTest : public CxxTest::TestSuite
}
}
+ /// Test std::begin() and std::end() on base-type array
+ void testBaseTypeBeginEnd()
+ {
+ const int MAX_ENTRIES = 1025;
+ int baseA[MAX_ENTRIES] = {0}; // base-type array
+
+ // Initialize base array to known values
+ for (int i = 0; i < MAX_ENTRIES; i++)
+ {
+ baseA[i] = i;
+ }
+
+ // use std::begin() and std::end() to copy base-type array to vector
+ std::vector<int> baseV ( std::begin(baseA), std::end(baseA));
+
+ if (baseV.size() != MAX_ENTRIES)
+ {
+ TS_FAIL("testBaseTypeBeginEnd: expected %d elements, found %d in vector",
+ MAX_ENTRIES, baseV.size());
+ }
+
+ for (int i = 0; i < MAX_ENTRIES; i++)
+ {
+ if (baseA[i] != baseV[i])
+ {
+ TS_FAIL("testBaseTypeBeginEnd: No match at index %d (base %d vs vector %d)",
+ i, baseA[i], baseV[i]);
+ }
+ }
+ }
};
#endif
OpenPOWER on IntegriCloud