|
| 1 | +/** |
| 2 | +* @license Apache-2.0 |
| 3 | +* |
| 4 | +* Copyright (c) 2018 The Stdlib Authors. |
| 5 | +* |
| 6 | +* Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | +* you may not use this file except in compliance with the License. |
| 8 | +* You may obtain a copy of the License at |
| 9 | +* |
| 10 | +* http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +* |
| 12 | +* Unless required by applicable law or agreed to in writing, software |
| 13 | +* distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +* See the License for the specific language governing permissions and |
| 16 | +* limitations under the License. |
| 17 | +*/ |
| 18 | + |
| 19 | +#ifndef STDLIB_NDARRAY_CTOR_NDARRAY_H |
| 20 | +#define STDLIB_NDARRAY_CTOR_NDARRAY_H |
| 21 | + |
| 22 | +#include <stdint.h> |
| 23 | +#include "stdlib/ndarray/base/bytes_per_element.h" |
| 24 | +#include "stdlib/ndarray/dtypes.h" |
| 25 | +#include "stdlib/ndarray/index_modes.h" |
| 26 | +#include "stdlib/ndarray/orders.h" |
| 27 | + |
| 28 | +/** |
| 29 | +* ndarray structure. |
| 30 | +* |
| 31 | +* @example |
| 32 | +* #include <stdlib.h> |
| 33 | +* #include <stdio.h> |
| 34 | +* #include <stdint.h> |
| 35 | +* #include "stdlib/ndarray/base/bytes_per_element.h" |
| 36 | +* #include "stdlib/ndarray/dtypes.h" |
| 37 | +* #include "stdlib/ndarray/index_modes.h" |
| 38 | +* #include "stdlib/ndarray/orders.h" |
| 39 | +* #include "stdlib/ndarray/ctor.h" |
| 40 | +* |
| 41 | +* struct ndarray *x = malloc( sizeof( struct ndarray ) ); |
| 42 | +* if ( x == NULL ) { |
| 43 | +* fprintf( stderr, "Error allocating memory.\n" ); |
| 44 | +* exit( 1 ); |
| 45 | +* } |
| 46 | +* |
| 47 | +* // Create an underlying byte array: |
| 48 | +* uint8_t buffer[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; |
| 49 | +* x->data = buffer; |
| 50 | +* |
| 51 | +* // Specify the underlying data type: |
| 52 | +* x->dtype = STDLIB_NDARRAY_FLOAT64; |
| 53 | +* |
| 54 | +* // Explicitly specify the number of bytes per element: |
| 55 | +* x->BYTES_PER_ELEMENT = STDLIB_NDARRAY_FLOAT64_BYTES_PER_ELEMENT; |
| 56 | +* |
| 57 | +* // Specify the array shape: |
| 58 | +* int64_t shape[] = { 3 }; // vector consisting of 3 doubles |
| 59 | +* x->shape = shape; |
| 60 | +* |
| 61 | +* // Specify the array strides: |
| 62 | +* int64_t strides[] = { x->BYTES_PER_ELEMENT }; |
| 63 | +* x->strides = strides; |
| 64 | +* |
| 65 | +* // Specify the byte offset: |
| 66 | +* x->offset = 0; |
| 67 | +* |
| 68 | +* // Specify the array order (note: this does not matter for a 1-dimensional array): |
| 69 | +* x->order = STDLIB_NDARRAY_ROW_MAJOR; |
| 70 | +* |
| 71 | +* // Specify the index mode: |
| 72 | +* x->imode = STDLIB_NDARRAY_INDEX_ERROR; |
| 73 | +* |
| 74 | +* // Specify subscript index modes: |
| 75 | +* x->submodes = { STDLIB_NDARRAY_INDEX_ERROR }; |
| 76 | +* x->nsubmodes = 1; |
| 77 | +* |
| 78 | +* // Explicitly specify the number of array dimensions: |
| 79 | +* x->ndims = 1; // vector |
| 80 | +* |
| 81 | +* // Explicitly specify the number of array elements (doubles): |
| 82 | +* x->length = x->shape[ 0 ]; |
| 83 | +* |
| 84 | +* // Explicitly specify the number of bytes: |
| 85 | +* x->byteLength = (x->length) * (x->BYTES_PER_ELEMENT); |
| 86 | +* |
| 87 | +* // Explicitly set the array flags: |
| 88 | +* x->flags = stdlib_ndarray_flags( x ); |
| 89 | +* |
| 90 | +* // Free allocated memory: |
| 91 | +* free( x ); |
| 92 | +*/ |
| 93 | +struct ndarray { |
| 94 | + // Underlying data type: |
| 95 | + enum STDLIB_NDARRAY_DTYPE dtype; |
| 96 | + |
| 97 | + // Pointer to the underlying byte array: |
| 98 | + uint8_t *data; |
| 99 | + |
| 100 | + // Number of array dimensions: |
| 101 | + int64_t ndims; |
| 102 | + |
| 103 | + // Array shape (dimensions): |
| 104 | + int64_t *shape; |
| 105 | + |
| 106 | + // Array strides (in bytes) specifying how to iterate over a strided array: |
| 107 | + int64_t *strides; |
| 108 | + |
| 109 | + // Byte offset which specifies the location at which to start iterating over array elements: |
| 110 | + int64_t offset; |
| 111 | + |
| 112 | + // Array order (either row-major (C-style) or column-major (Fortran-style)): |
| 113 | + enum STDLIB_NDARRAY_ORDER order; |
| 114 | + |
| 115 | + // Mode specifying how to handle indices which exceed array dimensions: |
| 116 | + enum STDLIB_NDARRAY_INDEX_MODE imode; |
| 117 | + |
| 118 | + // Number of subscript modes: |
| 119 | + int64_t nsubmodes; |
| 120 | + |
| 121 | + // Mode(s) specifying how to handle subscripts which exceed array dimensions on a per dimension basis: |
| 122 | + enum STDLIB_NDARRAY_INDEX_MODE *submodes; |
| 123 | + |
| 124 | + // Number of array elements: |
| 125 | + int64_t length; |
| 126 | + |
| 127 | + // Size in bytes: |
| 128 | + int64_t byteLength; |
| 129 | + |
| 130 | + // Number of bytes per element (i.e., item size): |
| 131 | + enum STDLIB_NDARRAY_BYTES_PER_ELEMENT BYTES_PER_ELEMENT; |
| 132 | + |
| 133 | + // Bit mask providing information regarding the memory layout of the array (e.g., see macros in this file): |
| 134 | + int flags; |
| 135 | +}; |
| 136 | + |
| 137 | +#endif // !STDLIB_NDARRAY_CTOR_NDARRAY_H |
0 commit comments