-
-
Notifications
You must be signed in to change notification settings - Fork 804
/
Copy pathndarray.c
108 lines (102 loc) · 3.47 KB
/
ndarray.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/**
* @license Apache-2.0
*
* Copyright (c) 2018 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <stdlib.h>
#include <stdint.h>
#include "stdlib/ndarray_bytes_per_element.h"
#include "stdlib/ndarray_dtypes.h"
#include "stdlib/ndarray_dtype_char.h"
#include "stdlib/ndarray_iteration_order.h"
#include "stdlib/ndarray_numel.h"
#include "stdlib/ndarray_orders.h"
#include "stdlib/ndarray.h"
/**
* Returns a pointer to a dynamically allocated ndarray.
*
* ## Notes
*
* - The user is responsible for freeing the allocated memory.
*
* @param data pointer to the underlying byte array
* @param ndims number of dimensions
* @param shape array shape (dimensions)
* @param strides array strides (in bytes)
* @param offset byte offset specifying the location of the first element
* @param order specifies whether an array is row-major (C-style) or column-major (Fortran-style)
* @param dtype data type
* @return pointer to a dynamically allocated ndarray or, if unable to allocate memory, a null pointer
*
* @example
* #include <stdlib.h>
* #include <stdio.h>
* #include <stdint.h>
* #include "stdlib/ndarray_bytes_per_element.h"
* #include "stdlib/ndarray_dtypes.h"
* #include "stdlib/ndarray_orders.h"
* #include "stdlib/ndarray.h"
*
* // Create an underlying byte array:
* 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 };
*
* // Specify the underlying data type:
* enum STDLIB_NDARRAY_DTYPE dtype = STDLIB_NDARRAY_FLOAT64;
*
* // Specify the number of array dimensions:
* uint64_t ndims = 1;
*
* // Specify the array shape:
* int64_t shape[] = { 3 }; // vector consisting of 3 doubles
*
* // Specify the array strides:
* int64_t strides[] = { STDLIB_NDARRAY_FLOAT64_BYTES_PER_ELEMENT };
*
* // Specify the byte offset:
* uint64_t offset = 0;
*
* // Specify the array order (note: this does not matter for a 1-dimensional array):
* enum STDLIB_NDARRAY_ORDER order = STDLIB_NDARRAY_ROW_MAJOR;
*
* // Create an ndarray:
* struct ndarray *x = stdlib_ndarray_constructor( buffer, ndims, shape, strides, offset, order, dtype );
* if ( x == NULL ) {
* printf( "Error allocating memory.\n" );
* exit( 1 );
* }
*
* // Free allocated memory:
* free( x );
*/
struct ndarray * stdlib_ndarray_constructor( uint8_t *data, uint64_t ndims, int64_t *shape, int64_t *strides, uint64_t offset, enum STDLIB_NDARRAY_ORDER order, enum STDLIB_NDARRAY_DTYPE dtype ) {
struct ndarray *arr = malloc( sizeof( struct ndarray ) );
if ( arr == NULL ) {
return NULL;
}
arr->data = data;
arr->dtype = dtype;
arr->ndims = ndims;
arr->offset = offset;
arr->order = order;
arr->shape = shape;
arr->strides = strides;
arr->BYTES_PER_ELEMENT = stdlib_ndarray_bytes_per_element( dtype );
arr->length = stdlib_ndarray_numel( ndims, shape );
arr->byteLength = (arr->length) * (arr->BYTES_PER_ELEMENT);
arr->ltr = stdlib_ndarray_dtype_char( dtype );
arr->_iterationOrder = stdlib_ndarray_iteration_order( ndims, strides );
// TODO: compute flags
return arr;
}