Skip to content

Commit 94205a0

Browse files
Add drop functions for ranget
This is useful for advancing the range without having to manipulate the private `begin_value` field. We have two versions of this, both return a new range, but one is used when the range on which the method is called can be moved.
1 parent 9468913 commit 94205a0

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

src/util/range.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,26 @@ struct ranget final
341341
return begin_value == end_value;
342342
}
343343

344+
/// Drop the first \p count elements.
345+
/// If the range has fewer elements, drops everything.
346+
/// Similar to \ref skip but modifies the range in-place.
347+
/// \return true if the range still contains element after the drop
348+
ranget<iteratort> drop(std::size_t count) &&
349+
{
350+
for(; count > 0 && begin_value != end_value; --count)
351+
++begin_value;
352+
return ranget<iteratort>{std::move(begin_value), std::move(end_value)};
353+
}
354+
355+
/// Return an new range containing the same elements except for the first
356+
/// \p count elements.
357+
/// If the range has fewer elements, returns an empty range.
358+
/// Similar to \ref drop but returns a new range object.
359+
ranget<iteratort> drop(std::size_t count) const &
360+
{
361+
return ranget<iteratort>{begin(), end()}.drop(count);
362+
}
363+
344364
iteratort begin() const
345365
{
346366
return begin_value;

0 commit comments

Comments
 (0)