Commit c04f6480 by Martin Liska Committed by Martin Liska

Add vec::reverse.

2018-05-29  Martin Liska  <mliska@suse.cz>
	    David Malcolm  <dmalcolm@redhat.com>

	* vec.c (test_reverse): New.
	(vec_c_tests): Add new test.
	* vec.h (vl_ptr>::reverse): New function.

Co-Authored-By: David Malcolm <dmalcolm@redhat.com>

From-SVN: r260890
parent 86e74d58
2018-05-29 Martin Liska <mliska@suse.cz>
David Malcolm <dmalcolm@redhat.com>
* vec.c (test_reverse): New.
(vec_c_tests): Add new test.
* vec.h (vl_ptr>::reverse): New function.
2018-05-29 Gerald Pfeifer <gerald@pfeifer.com> 2018-05-29 Gerald Pfeifer <gerald@pfeifer.com>
* config.gcc: Identify FreeBSD 3.x and 4.x as unsupported. * config.gcc: Identify FreeBSD 3.x and 4.x as unsupported.
......
...@@ -476,6 +476,43 @@ test_qsort () ...@@ -476,6 +476,43 @@ test_qsort ()
ASSERT_EQ (10, v.length ()); ASSERT_EQ (10, v.length ());
} }
/* Verify that vec::reverse works correctly. */
static void
test_reverse ()
{
/* Reversing an empty vec ought to be a no-op. */
{
auto_vec <int> v;
ASSERT_EQ (0, v.length ());
v.reverse ();
ASSERT_EQ (0, v.length ());
}
/* Verify reversing a vec with even length. */
{
auto_vec <int> v;
safe_push_range (v, 0, 4);
v.reverse ();
ASSERT_EQ (3, v[0]);
ASSERT_EQ (2, v[1]);
ASSERT_EQ (1, v[2]);
ASSERT_EQ (0, v[3]);
ASSERT_EQ (4, v.length ());
}
/* Verify reversing a vec with odd length. */
{
auto_vec <int> v;
safe_push_range (v, 0, 3);
v.reverse ();
ASSERT_EQ (2, v[0]);
ASSERT_EQ (1, v[1]);
ASSERT_EQ (0, v[2]);
ASSERT_EQ (3, v.length ());
}
}
/* Run all of the selftests within this file. */ /* Run all of the selftests within this file. */
void void
...@@ -492,6 +529,7 @@ vec_c_tests () ...@@ -492,6 +529,7 @@ vec_c_tests ()
test_unordered_remove (); test_unordered_remove ();
test_block_remove (); test_block_remove ();
test_qsort (); test_qsort ();
test_reverse ();
} }
} // namespace selftest } // namespace selftest
......
...@@ -1389,6 +1389,7 @@ public: ...@@ -1389,6 +1389,7 @@ public:
T *bsearch (const void *key, int (*compar)(const void *, const void *)); T *bsearch (const void *key, int (*compar)(const void *, const void *));
unsigned lower_bound (T, bool (*)(const T &, const T &)) const; unsigned lower_bound (T, bool (*)(const T &, const T &)) const;
bool contains (const T &search) const; bool contains (const T &search) const;
void reverse (void);
bool using_auto_storage () const; bool using_auto_storage () const;
...@@ -1900,6 +1901,19 @@ vec<T, va_heap, vl_ptr>::contains (const T &search) const ...@@ -1900,6 +1901,19 @@ vec<T, va_heap, vl_ptr>::contains (const T &search) const
return m_vec ? m_vec->contains (search) : false; return m_vec ? m_vec->contains (search) : false;
} }
/* Reverse content of the vector. */
template<typename T>
inline void
vec<T, va_heap, vl_ptr>::reverse (void)
{
unsigned l = length ();
T *ptr = address ();
for (unsigned i = 0; i < l / 2; i++)
std::swap (ptr[i], ptr[l - i - 1]);
}
template<typename T> template<typename T>
inline bool inline bool
vec<T, va_heap, vl_ptr>::using_auto_storage () const vec<T, va_heap, vl_ptr>::using_auto_storage () const
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment