📄 arrays-ctors.texi
字号:
@node Array ctors@section Constructors@subsection Default constructor@cindex Array default ctor@exampleArray();Array(GeneralArrayStorage<N_rank> storage)@end exampleThe default constructor creates a C-style array of zero size. Any attemptto access data in the array may result in a run-time error, because thereisn't any data to access!An optional argument specifies a storage order for the array.Arrays created using the default constructor can subsequently be given databy the @code{resize()}, @code{resizeAndPreserve()}, or @code{reference()}member functions.@subsection Creating an array from an expression@exampleArray(expression...)@end exampleYou may create an array from an array expression. For example,@exampleArray<float,2> A(4,3), B(4,3); // ...Array<float,2> C(A*2.0+B);@end exampleThis is an explicit constructor (it will not be used to perform implicittype conversions). The newly constructed array will have the same storageformat as the arrays in the expression. If arrays with different storageformats appear in the expression, an error will result. (In this case, youmust first construct the array, then assign the expression to it).@subsection Constructors which take extent parameters@cindex Array ctors with extent parameters@exampleArray(int extent1);Array(int extent1, int extent2);Array(int extent1, int extent2, int extent3);...Array(int extent1, int extent2, int extent3, ..., int extent11)@end exampleThese constructors take arguments which specify the size of the array to beconstructed. You should provide as many arguments as there are dimensionsin the array.@footnote{If you provide fewer than @code{N_rank} arguments,the missing arguments will be filled in using the last provided argument.However, for code clarity, it makes sense to provide all @code{N_rank}parameters.}An optional last parameter specifies a storage format:@exampleArray(int extent1, GeneralArrayStorage<N_rank> storage);Array(int extent1, int extent2, GeneralArrayStorage<N_rank> storage);...@end exampleFor high-rank arrays, it may be convenient to use this constructor:@cindex Array high-rank@exampleArray(const TinyVector<int, N_rank>& extent);Array(const TinyVector<int, N_rank>& extent, GeneralArrayStorage<N_rank> storage);@end exampleThe argument @code{extent} is a vector containing the extent (length) of thearray in each dimension. The optional second parameter indicates a storageformat. Note that you can construct @code{TinyVector<int,N>} objects on thefly with the @code{shape(i1,i2,...)} global function. For example,@code{Array<int,2> A(shape(3,5))} will create a 3x5 array.A similar constructor lets you provide both a vector of base index values(lbounds) and extents:@exampleArray(const TinyVector<int, N_rank>& lbound, const TinyVector<int, N_rank>& extent);Array(const TinyVector<int, N_rank>& lbound, const TinyVector<int, N_rank>& extent, GeneralArrayStorage<N_rank> storage);@end exampleThe argument @code{lbound} is a vector containing the base index value (orlbound) of the array in each dimension. The argument @code{extent} is avector containing the extent (length) of the array in each dimension. Theoptional third parameter indicates a storage format. As with the aboveconstructor, you can use the @code{shape(i1,i2,...)} global function tocreate the @code{lbound} and @code{extent} parameters.@subsection Constructors with Range arguments@cindex Array ctor with Range argsThese constructors allow arbitrary bases (starting indices) to be set:@exampleArray(Range r1);Array(Range r1, Range r2);Array(Range r1, Range r2, Range r3);...Array(Range r1, Range r2, Range r3, ..., Range r11);@end exampleFor example, this code:@exampleArray<int,2> A(Range(10,20), Range(20,30));@end examplewill create an 11x11 array whose indices are 10..20 and 20..30. An optionallast parameter provides a storage order:@exampleArray(Range r1, GeneralArrayStorage<N_rank> storage);Array(Range r1, Range r2, GeneralArrayStorage<N_rank> storage);...@end example@subsection Referencing another array@cindex Array referencing another arrayThis constructor makes a shared view of another array's data:@cindex Array creating a reference of another array@exampleArray(Array<T_numtype, N_rank>& array);@end exampleAfter this constructor is used, both @code{Array} objects refer to the@emph{same data}. Any changes made to one array will appear in the otherarray. If you want to make a duplicate copy of an array, use the@code{copy()} member function.@subsection Constructing an array from an expressionArrays may be constructed from expressions, which are described in@ref{Array expressions}. The syntax is:@exampleArray(...array expression...);@end exampleFor example, this code creates an array B which contains the square roots ofthe elements in A:@exampleArray<float,2> A(N,N); // ...Array<float,2> B(sqrt(A));@end example@subsection Creating an array from pre-existing data@cindex Array creating from pre-existing dataWhen creating an array using a pointer to already existing data, you havethree choices for how Blitz++ will handle the data. These choices areenumerated by the enum type @code{preexistingMemoryPolicy}:@cindex Array creating a reference of another array@exampleenum preexistingMemoryPolicy @{ duplicateData, deleteDataWhenDone, neverDeleteData @};@end example@findex preexistingMemoryPolicy@findex duplicateData@findex deleteDataWhenDone@findex neverDeleteDataIf you choose @code{duplicateData}, Blitz++ will create an array objectusing a copy of the data you provide. If you choose@code{deleteDataWhenDone}, Blitz++ will not create a copy of the data; andwhen no array objects refer to the data anymore, it will deallocate the datausing @code{delete []}. Note that to use @code{deleteDataWhenDone}, yourarray data must have been allocated using the C++ @code{new} operator -- forexample, you cannot allocate array data using Fortran or @code{malloc}, thencreate a Blitz++ array from it using the @code{deleteDataWhenDone} flag.The third option is @code{neverDeleteData}, which means that Blitz++ willnot never deallocate the array data. This means it is your responsibilityto determine when the array data is no longer needed, and deallocate it.You should use this option for memory which has not been allocated using theC++ @code{new} operator.These constructors create array objects from pre-existing data:@exampleArray(T_numtype* dataFirst, TinyVector<int, N_rank> shape, preexistingMemoryPolicy deletePolicy);Array(T_numtype* dataFirst, TinyVector<int, N_rank> shape, preexistingMemoryPolicy deletePolicy, GeneralArrayStorage<N_rank> storage);@end exampleThe first argument is a pointer to the array data. It should point to theelement of the array which is stored first in memory. The second argumentindicates the shape of the array. You can create this argument using the@code{shape()} function. For example:@exampledouble data[] = @{ 1, 2, 3, 4 @};Array<double,2> A(data, shape(2,2), neverDeleteData); // Make a 2x2 array@end example@findex shape()The @code{shape()} function takes N integer arguments and returns a@code{TinyVector<int,N>}.By default, Blitz++ arrays are row-major. If you want to work with datawhich is stored in column-major order (e.g. a Fortran array), use the secondversion of the constructor: @cindex Array creating from Fortran arrays@exampleArray<double,2> B(data, shape(2,2), neverDeleteData, FortranArray<2>());@end exampleThis is a tad awkward, so Blitz++ provides the global object@code{fortranArray} which will convert to an instance of@code{GeneralArrayStorage<N_rank>}:@exampleArray<double,2> B(data, shape(2,2), neverDeleteData, fortranArray);@end exampleAnother version of this constructor allows you to pass an arbitraryvector of strides:@exampleArray(T_numtype* _bz_restrict dataFirst, TinyVector<int, N_rank> shape, TinyVector<int, N_rank> stride, preexistingMemoryPolicy deletePolicy, GeneralArrayStorage<N_rank> storage = GeneralArrayStorage<N_rank>())@end example@subsection Interlacing arrays@cindex Array interlacing@findex interlaceArrays()@findex allocateArrays()For some platforms, it can be advantageous to store a set of arraysinterlaced together in memory. Blitz++ provides support for this throughthe routines @code{interlaceArrays()} and @code{allocateArrays()}. Anexample:@exampleArray<int,2> A, B;interlaceArrays(shape(10,10), A, B);@end exampleThe first parameter of @code{interlaceArrays()} is the shape for the arrays(10x10). The subsequent arguments are the set of arrays to be interlacedtogether. Up to 11 arrays may be interlaced. All arrays must store thesame data type and be of the same rank. In the above example, storage isallocated so that @code{A(0,0)} is followed immediately by @code{B(0,0)} inmemory, which is folloed by @code{A(0,1)} and @code{B(0,1)}, and so on.A related routine is @code{allocateArrays()}, which has identical syntax:@exampleArray<int,2> A, B;allocateArrays(shape(10,10), A, B);@end exampleUnlike @code{interlaceArrays()}, which always interlaces the arrays, theroutine @code{allocateArrays()} may or may not interlace them, depending onwhether interlacing is considered advantageous for your platform. If thetuning flag @code{BZ_INTERLACE_ARRAYS} is defined in@code{<blitz/tuning.h>}, then the arrays are interlaced.Note that the performance effects of interlacing are unpredictable: in somesituations it can be a benefit, and in most others it can slow your codedown substantially. You should only use @code{interlaceArrays()} afterrunning some benchmarks to determine whether interlacing is beneficial foryour particular algorithm and architecture.@subsection A note about reference counting@cindex Array reference counting@cindex reference countingBlitz++ arrays use reference counting. When you create a new array, amemory block is allocated. The @code{Array} object acts like a handle forthis memory block. A memory block can be shared among multiple @code{Array}objects -- for example, when you take subarrays and slices. The memoryblock keeps track of how many @code{Array} objects are referring to it.When a memory block is orphaned -- when no @code{Array} objects arereferring to it -- it automatically deletes itself and frees the allocatedmemory.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -