std.algorithm.mutation

This is a submodule of std.algorithm. It contains generic mutation algorithms.

Cheat Sheet
Function NameDescription
bringToFrontIf a = [1, 2, 3] and b = [4, 5, 6, 7], bringToFront(a, b) leaves a = [4, 5, 6] and b = [7, 1, 2, 3].
copyCopies a range to another. If a = [1, 2, 3] and b = new int[5], then copy(a, b) leaves b = [1, 2, 3, 0, 0] and returns b[3 .. $].
fillFills a range with a pattern, e.g., if a = new int[3], then fill(a, 4) leaves a = [4, 4, 4] and fill(a, [3, 4]) leaves a = [3, 4, 3].
initializeAllIf a = [1.2, 3.4], then initializeAll(a) leaves a = [double.init, double.init].
movemove(a, b) moves a into b. move(a) reads a destructively when necessary.
moveEmplaceSimilar to move but assumes target is uninitialized.
moveAllMoves all elements from one range to another.
moveEmplaceAllSimilar to moveAll but assumes all elements in target are uninitialized.
moveSomeMoves as many elements as possible from one range to another.
moveEmplaceSomeSimilar to moveSome but assumes all elements in target are uninitialized.
removeRemoves elements from a range in-place, and returns the shortened range.
reverseIf a = [1, 2, 3], reverse(a) changes it to [3, 2, 1].
stripStrips all leading and trailing elements equal to a value, or that satisfy a predicate. If a = [1, 1, 0, 1, 1], then strip(a, 1) and strip!(e => e == 1)(a) returns [0].
stripLeftStrips all leading elements equal to a value, or that satisfy a predicate. If a = [1, 1, 0, 1, 1], then stripLeft(a, 1) and stripLeft!(e => e == 1)(a) returns [0, 1, 1].
stripRightStrips all trailing elements equal to a value, or that satisfy a predicate. If a = [1, 1, 0, 1, 1], then stripRight(a, 1) and stripRight!(e => e == 1)(a) returns [1, 1, 0].
swapSwaps two values.
swapAtSwaps two values by indices.
swapRangesSwaps all elements of two ranges.
uninitializedFillFills a range (assumed uninitialized) with a value.

Members

Enums

SwapStrategy
enum SwapStrategy

Defines the swapping strategy for algorithms that need to swap elements in a range (such as partition and sort). The strategy concerns the swapping of elements that are not the core concern of the algorithm. For example, consider an algorithm that sorts [ "abc", "b", "aBc" ] according to toUpper(a) < toUpper(b). That algorithm might choose to swap the two equivalent strings "abc" and "aBc". That does not affect the sorting since both ["abc", "aBc", "b" ] and [ "aBc", "abc", "b" ] are valid outcomes.

Functions

bringToFront
size_t bringToFront(InputRange front, ForwardRange back)

bringToFront takes two ranges front and back, which may be of different types. Considering the concatenation of front and back one unified range, bringToFront rotates that unified range such that all elements in back are brought to the beginning of the unified range. The relative ordering of elements in front and back, respectively, remains unchanged.

copy
TargetRange copy(SourceRange source, TargetRange target)

Copies the content of source into target and returns the remaining (unfilled) part of target.

fill
void fill(Range range, Value value)
void fill(InputRange range, ForwardRange filler)

Assigns value to each element of input range range.

initializeAll
void initializeAll(Range range)

Initializes all elements of range with their .init value. Assumes that the elements of the range are uninitialized.

move
void move(T source, T target)
T move(T source)

Moves source into target, via a destructive copy when necessary.

moveAll
InputRange2 moveAll(InputRange1 src, InputRange2 tgt)

Calls move(a, b) for each element a in src and the corresponding element b in tgt, in increasing order.

moveEmplace
void moveEmplace(T source, T target)

Similar to move but assumes target is uninitialized. This is more efficient because source can be blitted over target without destroying or initializing it first.

moveEmplaceAll
InputRange2 moveEmplaceAll(InputRange1 src, InputRange2 tgt)

Similar to moveAll but assumes all elements in tgt are uninitialized. Uses moveEmplace to move elements from src over elements from tgt.

moveEmplaceSome
Tuple!(InputRange1, InputRange2) moveEmplaceSome(InputRange1 src, InputRange2 tgt)

Same as moveSome but assumes all elements in tgt are uninitialized. Uses moveEmplace to move elements from src over elements from tgt.

moveSome
Tuple!(InputRange1, InputRange2) moveSome(InputRange1 src, InputRange2 tgt)

Calls move(a, b) for each element a in src and the corresponding element b in tgt, in increasing order, stopping when either range has been exhausted.

remove
Range remove(Range range, Offset offset)
deprecated Range remove(Range range, Offset offset)

Eliminates elements at given offsets from range and returns the shortened range.

remove
Range remove(Range range)

Reduces the length of the bidirectional range range by removing elements that satisfy pred. If s = SwapStrategy.unstable, elements are moved from the right end of the range over the elements to eliminate. If s = SwapStrategy.stable (the default), elements are moved progressively to front such that their relative order is preserved. Returns the filtered range.

reverse
Range reverse(Range r)

Reverses r in-place. Performs r.length / 2 evaluations of swap. UTF sequences consisting of multiple code units are preserved properly.

strip
Range strip(Range range, E element)
Range strip(Range range)
stripLeft
Range stripLeft(Range range, E element)
Range stripLeft(Range range)
stripRight
Range stripRight(Range range, E element)
Range stripRight(Range range)

The strip group of functions allow stripping of either leading, trailing, or both leading and trailing elements.

swap
void swap(T lhs, T rhs)

Swaps lhs and rhs. The instances lhs and rhs are moved in memory, without ever calling opAssign, nor any other function. T need not be assignable at all to be swapped.

swapAt
void swapAt(R r, size_t i1, size_t i2)

Swaps two elements in-place of a range r, specified by their indices i1 and i2.

swapRanges
Tuple!(InputRange1, InputRange2) swapRanges(InputRange1 r1, InputRange2 r2)

Swaps all elements of r1 with successive elements in r2. Returns a tuple containing the remainder portions of r1 and r2 that were not swapped (one of them will be empty). The ranges may be of different types but must have the same element type and support swapping.

uninitializedFill
void uninitializedFill(Range range, Value value)

Initializes each element of range with value. Assumes that the elements of the range are uninitialized. This is of interest for structs that define copy constructors (for all other types, fill and uninitializedFill are equivalent).

Meta