Initialization¶
Usage
use Memory.Initialization;
or
import Memory.Initialization;
Support for move-initializing and deinitializing values.
The Initialization
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.
Warning
The Memory.Initialization module has been deprecated; please use the ‘MemMove’ module instead
- proc needsDeinit(type t) param¶
Check to see if a given type needs to be deinitialized.
- Arguments
t : type – A type to check for deinitialization
- Returns
true
ift
needs to be deinitialized- Return type
param bool
- proc explicitDeinit(ref arg: ?t)¶
Explicitly deinitialize a variable. The variable referred to by
arg
should be considered uninitialized after a call to this function.Warning
At present the compiler does not account for deinitialization performed upon a call to
explicitDeinit()
. It should only be called when deinitialization would not occur otherwise.- Arg
A variable to deinitialize
- proc moveInitialize(ref lhs, in rhs)¶
Move-initialize
lhs
with the value inrhs
. The contents oflhs
are not deinitialized before the move, andrhs
is not deinitialized after the move.Warning
If
lhs
references an already initialized variable, it will be overwritten by the contents ofrhs
without being deinitialized first. CallexplicitDeinit()
to deinitializelhs
if necessary.- Arguments
lhs – A variable to move-initialize, whose type matches
rhs
rhs – A value to move-initialize from
Warning
The formals ‘lhs’ and ‘rhs’ are deprecated, please use ‘dst’ and ‘src’ instead
- proc moveInitialize(ref dst, in src)
Move-initialize
dst
with the value insrc
. The contents ofdst
are not deinitialized before the move, andsrc
is not deinitialized after the move.Warning
If
dst
references an already initialized variable, it will be overwritten by the contents ofsrc
without being deinitialized first. CallexplicitDeinit()
to deinitializedst
if necessary.Warning
The static types of
dst
andsrc
must 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
. ThemoveFrom
function can be used with thesrc
argument 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
src
src – A value to move-initialize from
- proc moveToValue(const ref arg: ?t)¶
Move the contents of the variable or constant referred to by
arg
into a new value.Warning
The variable or constant referred to by
arg
should be considered uninitialized after a call to this function.- Arguments
arg – A variable or constant to move
- Returns
The contents of
arg
moved into a new value
Warning
‘moveToValue’ is deprecated; please use ‘moveFrom’ instead
- proc moveFrom(const ref src: ?t)¶
Move the contents of the variable or constant referred to by
src
into a new returned value.Warning
The variable or constant referred to by
src
should be considered uninitialized after a call to this function.- Arguments
src – A variable or constant to move
- Returns
The contents of
src
moved into a new value
- proc moveSwap(ref lhs: ?t, ref rhs: t)¶
Swap the contents of the variables referred to by
lhs
andrhs
. This function does not call the<=>
operator. Unlike the<=>
operator,moveSwap()
does not perform assignment or initialization.- Arguments
lhs – A variable to swap
rhs – A variable to swap
Warning
the formals ‘lhs’ and ‘rhs’ are deprecated, please use ‘x’ and ‘y’ instead
- proc moveSwap(ref x: ?t, ref y: t)
Swap the contents of the variables referred to by
x
andy
. 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 moveInitializeArrayElements(ref a: [?d], dstStartIndex: a.idxType, srcStartIndex: a.idxType, numElements: int)¶
Move-initialize a group of array elements from a group of elements in the same array. This function is equivalent to a sequence of individual calls to
moveInitialize()
.Warning
This function will halt if the value of
numElements
is negative, or if any of the elements indstStartIndex..#numElements
orsrcStartIndex..#numElements
are out of bounds.This function will halt if the ranges
dstStartIndex..#numElements
andsrcStartIndex..#numElements
intersect.No checks will occur when the –fast or –no-checks flags are used.
- Arguments
a – The array with source and destination elements
dstStartIndex : a.idxType – Destination index of elements to move-initialize
srcStartIndex : a.idxType – Source index of elements to move-initialize from
numElements :
int
– The number of elements to move-initialize
- proc moveInitializeArrayElements(ref dstA: [] ?t, dstStartIndex: dstA.idxType, srcA: [] t, srcStartIndex: srcA.idxType, numElements: int)
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()
.Warning
This function will halt if the value of
numElements
is negative, or if any of the elements indstStartIndex..#numElements
orsrcStartIndex..#numElements
are out of bounds.This function will halt if
dstA
andsrcA
are the same array and the rangesdstStartIndex..#numElements
andsrcStartIndex..#numElements
intersect.No checks will occur when the –fast or –no-checks flags are used.
- Arguments
dstA – The array with destination elements
dstStartIndex : dstA.idxType – Destination index of elements to move-initialize
srcA : An array with the same element type as dstA.eltType – The array with source elements
srcStartIndex : srcA.idxType – Source index of elements to move-initialize from
numElements :
int
– The number of elements to move-initialize