Skip to content

Commit

Permalink
Implement std::begin() and std::end() for base types
Browse files Browse the repository at this point in the history
begin() returns an iterator to first element in array.
end() returns an iterator to one past the last element in array.

Change-Id: Idf97d3fbcf361f8318b84aa95452fd4fa5809d6b
Reviewed-on: http://rchgit01.rchland.ibm.com/gerrit1/82665
Tested-by: Jenkins Server <pfd-jenkins+hostboot@us.ibm.com>
Tested-by: Jenkins OP Build CI <op-jenkins+hostboot@us.ibm.com>
Tested-by: FSP CI Jenkins <fsp-CI-jenkins+hostboot@us.ibm.com>
Tested-by: Jenkins OP HW <op-hw-jenkins+hostboot@us.ibm.com>
Reviewed-by: Christian R Geddes <crgeddes@us.ibm.com>
Reviewed-by: Glenn Miles <milesg@ibm.com>
Reviewed-by: Nicholas E Bofferding <bofferdn@us.ibm.com>
  • Loading branch information
mderkse1 authored and Nicholas E Bofferding committed Sep 5, 2019
1 parent f7dab51 commit 582ed66
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 10 deletions.
48 changes: 39 additions & 9 deletions src/include/iterator
Expand Up @@ -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. */
Expand Down Expand Up @@ -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)
Expand All @@ -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);
}
Expand Down Expand Up @@ -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.
*/
Expand Down Expand Up @@ -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

Expand Down
32 changes: 31 additions & 1 deletion src/usr/testcore/lib/stltest.H
Expand Up @@ -5,7 +5,7 @@
/* */
/* OpenPOWER HostBoot Project */
/* */
/* Contributors Listed Below - COPYRIGHT 2011,2017 */
/* Contributors Listed Below - COPYRIGHT 2011,2019 */
/* [+] International Business Machines Corp. */
/* */
/* */
Expand Down Expand Up @@ -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

0 comments on commit 582ed66

Please sign in to comment.