darray: add insert_n overload
This commit is contained in:
parent
f44c7d5b46
commit
26edf25b82
@ -103,15 +103,28 @@ namespace cruft {
|
||||
--m_size;
|
||||
}
|
||||
|
||||
void insert (iterator pos, ValueT const &val)
|
||||
|
||||
/// Insert one copy of `value' before `pos'
|
||||
iterator insert (iterator pos, ValueT const &val)
|
||||
{
|
||||
return insert (pos, 1u, val);
|
||||
}
|
||||
|
||||
|
||||
/// Insert `count' copies of `value' before `pos'.
|
||||
iterator insert (const_iterator pos, size_type count, ValueT const &val)
|
||||
{
|
||||
// Ensure we have enough space
|
||||
CHECK_LIMIT (pos, begin (), end ());
|
||||
CHECK_LT (m_size, CapacityV);
|
||||
CHECK_LE (m_size, CapacityV - count);
|
||||
|
||||
*pos = val;
|
||||
std::move_backward (pos, cend (), end () + count);
|
||||
m_size += count;
|
||||
|
||||
for (auto cursor = pos + 1; cursor != end (); ++cursor)
|
||||
*cursor = *(cursor - 1);
|
||||
auto dst = const_cast<iterator> (pos);
|
||||
std::fill_n (dst, count, val);
|
||||
|
||||
return dst;
|
||||
}
|
||||
|
||||
|
||||
|
@ -6,29 +6,47 @@ main (int, char**)
|
||||
{
|
||||
cruft::TAP::logger tap;
|
||||
|
||||
cruft::darray<8,int> val ({ 0, 1, 2, 3, 4, 5, 6, 7});
|
||||
|
||||
tap.expect_eq (val.size (), 8u, "query for size");
|
||||
|
||||
tap.expect_eq (val.capacity (), decltype(val)::elements, "static vs dynamic query for capacity");
|
||||
|
||||
{
|
||||
auto const sum = std::accumulate (val.cbegin (), val.cend (), 0u);
|
||||
tap.expect_eq (sum, 28u, "accumulate over initializer_list range");
|
||||
cruft::darray<8,int> val ({ 0, 1, 2, 3, 4, 5, 6, 7});
|
||||
|
||||
tap.expect_eq (val.size (), 8u, "query for size");
|
||||
|
||||
tap.expect_eq (val.capacity (), decltype(val)::elements, "static vs dynamic query for capacity");
|
||||
|
||||
{
|
||||
auto const sum = std::accumulate (val.cbegin (), val.cend (), 0u);
|
||||
tap.expect_eq (sum, 28u, "accumulate over initializer_list range");
|
||||
}
|
||||
|
||||
{
|
||||
val.erase (val.begin () + 2);
|
||||
tap.expect_eq (val.size (), 7u, "erasing removes one element");
|
||||
|
||||
auto const sum = std::accumulate (val.cbegin (), val.cend (), 0u);
|
||||
tap.expect_eq (sum, 26u, "erase one element");
|
||||
}
|
||||
|
||||
{
|
||||
// Try inserting a value and see if it sticks.
|
||||
//
|
||||
// Ensure that a unique value is used here.
|
||||
// Perform an accumulate to check the rest of the values are there too.
|
||||
val.insert (val.begin () + 4, 13);
|
||||
tap.expect_eq (val[4], 13, "inserted values matches indexed value");
|
||||
auto const sum = std::accumulate (val.cbegin (), val.cend (), 0u);
|
||||
tap.expect_eq (sum, 39u, "accumulate over new array suggests all still present");
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
val.erase (val.begin () + 2);
|
||||
tap.expect_eq (val.size (), 7u, "erasing removes one element");
|
||||
|
||||
auto const sum = std::accumulate (val.cbegin (), val.cend (), 0u);
|
||||
tap.expect_eq (sum, 26u, "erase one element");
|
||||
}
|
||||
|
||||
{
|
||||
val.insert (val.begin () + 4, 9);
|
||||
tap.expect_eq (val[5], 9, "inserted values matches indexed value");
|
||||
cruft::darray<8,int> val ({ 0, 1, 2, 3 });
|
||||
cruft::darray<8,int> const res ({ 0, 1, 13, 13, 13, 2, 3 });
|
||||
|
||||
val.insert (val.begin () + 2, 3, 13);
|
||||
tap.expect_eq (val, res, "trivial insert_n");
|
||||
}
|
||||
|
||||
|
||||
return tap.status ();
|
||||
}
|
Loading…
Reference in New Issue
Block a user