std.algorithm.mutation.remove
- multiple declarations
- Function remove
- Function remove
Function remove
Eliminates elements at given offsets from
and returns the
shortened range
range
. In the simplest call, one element is removed.
int[] a = [ 3, 5, 7, 8 ]; assert(remove(a, 1) == [ 3, 7, 8 ]); assert(a == [ 3, 7, 8, 8 ]);
In the case above the element at offset
1
is removed and
returns the remove
range
smaller by one element. The original array
has remained of the same length because all functions in std.algorithm
only change content, not topology. The value
8
is repeated because move
was invoked to
move
elements around and on integers
simply copies the source to
the destination. To replace move
a
with the effect of the removal,
simply assign a =
. The slice will be rebound to the
shorter array and the operation completes with maximal efficiency.
remove
(a, 1)
Multiple indices can be passed into
. In that case,
elements at the respective indices are all removed. The indices must
be passed in increasing order, otherwise an exception occurs.
remove
int[] a = [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ]; assert(remove(a, 1, 3, 5) == [ 0, 2, 4, 6, 7, 8, 9, 10 ]);
(Note how all indices refer to slots in the original array, not in the array as it is being progressively shortened.) Finally, any combination of integral offsets and tuples composed of two integral offsets can be passed in.
int[] a = [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ]; assert(remove(a, 1, tuple(3, 5), 9) == [ 0, 2, 6, 7, 8, 10 ]);
In this case, the slots at positions 1, 3, 4, and 9 are removed from
the array. The tuple passes in a range
closed to the left and open to
the right (consistent with built-in slices), e.g. tuple(3, 5)
means indices 3
and 4
but not 5
.
If the need is to remove
some elements in the range
but the order of
the remaining elements does not have to be preserved, you may want to
pass
to SwapStrategy.unstable
.
remove
int[] a = [ 0, 1, 2, 3 ]; assert(remove!(SwapStrategy.unstable)(a, 1) == [ 0, 3, 2 ]);
In the case above, the element at slot 1
is removed, but replaced
with the last element of the range
. Taking advantage of the relaxation
of the stability requirement,
moved elements from the end
of the array over the slots to be removed. This way there is less data
movement to be done which improves the execution time of the function.
remove
The function
works on any forward remove
range
. The moving
strategy is (listed from fastest to slowest):
- If
s ==
, then elements are moved from the end of theSwapStrategy.unstable
&& isRandomAccessRange!Range && hasLength!Range && hasLvalueElements!Rangerange
into the slots to be filled. In this case, the absolute minimum of moves is performed. - Otherwise, if
s ==
, then elements are still moved from the end of theSwapStrategy.unstable
&& isBidirectionalRange!Range && hasLength!Range && hasLvalueElements!Rangerange
, but time is spent on advancing between slots by repeated calls torange.popFront
. - Otherwise, elements are moved
incrementally towards the front of
; a given element is never moved several times, but more elements are moved than in the previous cases.range
Prototype
Range remove(std.algorithm.mutation.SwapStrategy s, Range, Offset...)( Range range, Offset offset ) if (s != SwapStrategy.stable && isBidirectionalRange!Range && hasLvalueElements!Range && hasLength!Range && Offset.length >= 1);
Function remove
Reduces the length of the bidirectional range
by removing
elements that satisfy range
pred
. If s =
,
elements are moved from the right end of the SwapStrategy.unstable
range
over the elements
to eliminate. If s =
(the default),
elements are moved progressively to front such that their relative
order is preserved. Returns the filtered SwapStrategy.stable
range
.
Prototype
Range remove(alias pred, std.algorithm.mutation.SwapStrategy s, Range)( Range range ) if (isBidirectionalRange!Range && hasLvalueElements!Range);
Example
static immutable base = [1, 2, 3, 2, 4, 2, 5, 2]; int[] arr = base[].dup; // using a string-based predicate assert(remove!("a == 2")(arr) == [ 1, 3, 4, 5 ]); // The original array contents have been modified, // so we need to reset it to its original state. // The length is unmodified however. arr[] = base[]; // using a lambda predicate assert(remove!(a => a == 2)(arr) == [ 1, 3, 4, 5 ]);