MemMove
Usage
use MemMove;
or
import MemMove;
Support for move-initializing and deinitializing values.
The MemMove module provides functions which enable users to
move-initialize and deinitialize values.
The functionality provided by this module can be used to implement
collections in a manner similar to those implemented by the Chapel standard
modules (such as List or Map).
Note
Throughout documentation, the term variable also includes non-const formals, array elements, record or class fields, and tuple components.
- proc needsDestroy(type t) param : bool
Check to see if a given type would normally be destroyed automatically by the compiler when its lifetime ends. For example, passing an
ownedclass would see this function returntrue, whereas passing anunmanagedclass would result infalse.A
sharedclass would also result intruebecause the compiler destroyssharedvariables to decrement the reference count (and possibly the object being managed, as well).- Arguments:
t : type – A type to check
- Returns:
trueiftneeds to be automatically destroyed- Return type:
param bool
- proc destroy(ref obj: ?t)
Explicitly destroy a variable as the compiler would when its lifetime ends. The variable referred to by
objshould be considered unusable after a call to this function, and its particular state is undefined.This function has no effect if
needsDestroyreturnsfalsefor the argument’s type.Warning
At present the compiler does not account for manual destruction performed upon a call to
destroy(). It should only be called when automatic destruction would not occur otherwise.- Arguments:
obj – A variable to deinitialize
- proc moveInitialize(ref dst, in src)
Move-initialize
dstwith the value insrc. The contents ofdstare not deinitialized before the move, andsrcis not deinitialized after the move.Warning
If
dstreferences an already initialized variable, it will be overwritten by the contents ofsrcwithout being deinitialized first. Calldestroy()to deinitializedstif necessary.Warning
The static types of
dstandsrcmust match, or else a compile-time error will be issued.Note
If the compiler inserts a copy for the argument to
src, then a compile-time error will be issued. The most likely cause is that the argument is used elsewhere following the call tomoveInitialize. ThemoveFromfunction can be used with thesrcargument to avoid the copy when certain of the variable’s usage:moveInitialize(myDst, moveFrom(mySrc));
- Arguments:
dst – A variable to move-initialize, whose type matches
srcsrc – A value to move-initialize from
- proc moveFrom(const ref src: ?t)
Move the contents of the variable or constant referred to by
srcinto a new returned value.Warning
The variable or constant referred to by
srcshould be considered uninitialized after a call to this function.- Arguments:
src – A variable or constant to move
- Returns:
The contents of
srcmoved into a new value
- proc moveSwap(ref x: ?t, ref y: t)
Swap the contents of the variables referred to by
xandy. This function does not call the<=>operator. Unlike the<=>operator,moveSwap()does not perform assignment or initialization.- Arguments:
x – A variable to swap
y – A variable to swap
- proc moveArrayElements(ref dst: [] ?eltType, const dstRegion, const ref src: [] eltType, const srcRegion) : void throws
Warning
‘moveArrayElements’ is unstable and subject to change in the future
Move-initialize a group of array elements from a group of elements in another array. This function is equivalent to a sequence of individual calls to
moveInitialize().This function only accepts rectangular arrays, rectangular domains, or ranges. The index type of the regions must also match the index type of their corresponding arrays. Any range arguments must have both an upper and lower bound.
- Arguments:
dst – The destination array
dstRegion – A domain or range of indices in
dstsrc – The source array
srcRegion – A domain or range of indices in
src
- Throws:
IllegalArgumentError – if region sizes do not match, if a region contains indices not in the corresponding array, if
srcanddstare determined to be aliases and the regions overlap.
- proc moveArrayElements(ref dst: [] ?eltType, const ref src: [] eltType) : void throws
Warning
‘moveArrayElements’ is unstable and subject to change in the future
Move-initialize a group of array elements from a group of elements in another array. This function is equivalent to a sequence of individual calls to
moveInitialize().This function only accepts rectangular arrays.
- Arguments:
dst – The destination array
src – The source array
- Throws:
IllegalArgumentError – if the array sizes do not match, if
srcanddstare determined to be aliases and overlap.