set.c (10940B)
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 #include "stdlib/ndarray/ctor/set.h" 20 #include "stdlib/ndarray/ctor/set_ptr.h" 21 #include "stdlib/ndarray/ctor/get_ptr.h" 22 #include "stdlib/ndarray/ctor/ndarray.h" 23 #include <stdlib.h> 24 #include <stdint.h> 25 #include <stdbool.h> 26 #include <complex.h> 27 28 /** 29 * Sets an ndarray data element. 30 * 31 * ## Notes 32 * 33 * - The function returns `-1` if unable to set an element and `0` otherwise. 34 * - The function requires a pointer to a data value `v` in order to provide a generic API supporting ndarrays having different data types. 35 * - The function has no way of determining whether `v` actually points to a memory address compatible with the underlying input ndarray data type. Accordingly, accessing **unowned** memory is possible, and this function **assumes** you know what you are doing. 36 * 37 * @param arr input ndarray 38 * @param sub ndarray subscripts 39 * @param v value to set 40 * @return status code 41 */ 42 int8_t stdlib_ndarray_set( const struct ndarray *arr, const int64_t *sub, const void *v ) { 43 uint8_t *idx = stdlib_ndarray_get_ptr( arr, sub ); 44 if ( idx == NULL ) { 45 return -1; 46 } 47 return stdlib_ndarray_set_ptr_value( arr, idx, v ); 48 } 49 50 /** 51 * Sets a double-precision floating-point ndarray data element. 52 * 53 * ## Notes 54 * 55 * - The function does **not** verify that the type of `v` matches the underlying input ndarray data type, and, thus, overwriting **unowned** memory is possible. The function **assumes** that you know what you are doing. 56 * - The function returns `-1` if unable to set an element and `0` otherwise. 57 * 58 * @param arr input ndarray 59 * @param sub ndarray subscripts 60 * @param v value to set 61 * @return status code 62 */ 63 int8_t stdlib_ndarray_set_float64( const struct ndarray *arr, const int64_t *sub, const double v ) { 64 uint8_t *idx = stdlib_ndarray_get_ptr( arr, sub ); 65 if ( idx == NULL ) { 66 return -1; 67 } 68 return stdlib_ndarray_set_ptr_float64( idx, v ); 69 } 70 71 /** 72 * Sets a single-precision floating-point ndarray data element. 73 * 74 * ## Notes 75 * 76 * - The function does **not** verify that the type of `v` matches the underlying input ndarray data type, and, thus, overwriting **unowned** memory is possible. The function **assumes** that you know what you are doing. 77 * - The function returns `-1` if unable to set an element and `0` otherwise. 78 * 79 * @param arr input ndarray 80 * @param sub ndarray subscripts 81 * @param v value to set 82 * @return status code 83 */ 84 int8_t stdlib_ndarray_set_float32( const struct ndarray *arr, const int64_t *sub, const float v ) { 85 uint8_t *idx = stdlib_ndarray_get_ptr( arr, sub ); 86 if ( idx == NULL ) { 87 return -1; 88 } 89 return stdlib_ndarray_set_ptr_float32( idx, v ); 90 } 91 92 /** 93 * Sets an unsigned 64-bit integer ndarray data element. 94 * 95 * ## Notes 96 * 97 * - The function does **not** verify that the type of `v` matches the underlying input ndarray data type, and, thus, overwriting **unowned** memory is possible. The function **assumes** that you know what you are doing. 98 * - The function returns `-1` if unable to set an element and `0` otherwise. 99 * 100 * @param arr input ndarray 101 * @param sub ndarray subscripts 102 * @param v value to set 103 * @return status code 104 */ 105 int8_t stdlib_ndarray_set_uint64( const struct ndarray *arr, const int64_t *sub, const uint64_t v ) { 106 uint8_t *idx = stdlib_ndarray_get_ptr( arr, sub ); 107 if ( idx == NULL ) { 108 return -1; 109 } 110 return stdlib_ndarray_set_ptr_uint64( idx, v ); 111 } 112 113 /** 114 * Sets a signed 64-bit integer ndarray data element. 115 * 116 * ## Notes 117 * 118 * - The function does **not** verify that the type of `v` matches the underlying input ndarray data type, and, thus, overwriting **unowned** memory is possible. The function **assumes** that you know what you are doing. 119 * - The function returns `-1` if unable to set an element and `0` otherwise. 120 * 121 * @param arr input ndarray 122 * @param sub ndarray subscripts 123 * @param v value to set 124 * @return status code 125 */ 126 int8_t stdlib_ndarray_set_int64( const struct ndarray *arr, const int64_t *sub, const int64_t v ) { 127 uint8_t *idx = stdlib_ndarray_get_ptr( arr, sub ); 128 if ( idx == NULL ) { 129 return -1; 130 } 131 return stdlib_ndarray_set_ptr_int64( idx, v ); 132 } 133 134 /** 135 * Sets an unsigned 32-bit integer ndarray data element. 136 * 137 * ## Notes 138 * 139 * - The function does **not** verify that the type of `v` matches the underlying input ndarray data type, and, thus, overwriting **unowned** memory is possible. The function **assumes** that you know what you are doing. 140 * - The function returns `-1` if unable to set an element and `0` otherwise. 141 * 142 * @param arr input ndarray 143 * @param sub ndarray subscripts 144 * @param v value to set 145 * @return status code 146 */ 147 int8_t stdlib_ndarray_set_uint32( const struct ndarray *arr, const int64_t *sub, const uint32_t v ) { 148 uint8_t *idx = stdlib_ndarray_get_ptr( arr, sub ); 149 if ( idx == NULL ) { 150 return -1; 151 } 152 return stdlib_ndarray_set_ptr_uint32( idx, v ); 153 } 154 155 /** 156 * Sets a signed 32-bit integer ndarray data element. 157 * 158 * ## Notes 159 * 160 * - The function does **not** verify that the type of `v` matches the underlying input ndarray data type, and, thus, overwriting **unowned** memory is possible. The function **assumes** that you know what you are doing. 161 * - The function returns `-1` if unable to set an element and `0` otherwise. 162 * 163 * @param arr input ndarray 164 * @param sub ndarray subscripts 165 * @param v value to set 166 * @return status code 167 */ 168 int8_t stdlib_ndarray_set_int32( const struct ndarray *arr, const int64_t *sub, const int32_t v ) { 169 uint8_t *idx = stdlib_ndarray_get_ptr( arr, sub ); 170 if ( idx == NULL ) { 171 return -1; 172 } 173 return stdlib_ndarray_set_ptr_int32( idx, v ); 174 } 175 176 /** 177 * Sets an unsigned 16-bit integer ndarray data element. 178 * 179 * ## Notes 180 * 181 * - The function does **not** verify that the type of `v` matches the underlying input ndarray data type, and, thus, overwriting **unowned** memory is possible. The function **assumes** that you know what you are doing. 182 * - The function returns `-1` if unable to set an element and `0` otherwise. 183 * 184 * @param arr input ndarray 185 * @param sub ndarray subscripts 186 * @param v value to set 187 * @return status code 188 */ 189 int8_t stdlib_ndarray_set_uint16( const struct ndarray *arr, const int64_t *sub, const uint16_t v ) { 190 uint8_t *idx = stdlib_ndarray_get_ptr( arr, sub ); 191 if ( idx == NULL ) { 192 return -1; 193 } 194 return stdlib_ndarray_set_ptr_uint16( idx, v ); 195 } 196 197 /** 198 * Sets a signed 16-bit integer ndarray data element. 199 * 200 * ## Notes 201 * 202 * - The function does **not** verify that the type of `v` matches the underlying input ndarray data type, and, thus, overwriting **unowned** memory is possible. The function **assumes** that you know what you are doing. 203 * - The function returns `-1` if unable to set an element and `0` otherwise. 204 * 205 * @param arr input ndarray 206 * @param sub ndarray subscripts 207 * @param v value to set 208 * @return status code 209 */ 210 int8_t stdlib_ndarray_set_int16( const struct ndarray *arr, const int64_t *sub, const int16_t v ) { 211 uint8_t *idx = stdlib_ndarray_get_ptr( arr, sub ); 212 if ( idx == NULL ) { 213 return -1; 214 } 215 return stdlib_ndarray_set_ptr_int16( idx, v ); 216 } 217 218 /** 219 * Sets an unsigned 8-bit integer ndarray data element. 220 * 221 * ## Notes 222 * 223 * - The function returns `-1` if unable to set an element and `0` otherwise. 224 * 225 * @param arr input ndarray 226 * @param sub ndarray subscripts 227 * @param v value to set 228 * @return status code 229 */ 230 int8_t stdlib_ndarray_set_uint8( const struct ndarray *arr, const int64_t *sub, const uint8_t v ) { 231 uint8_t *idx = stdlib_ndarray_get_ptr( arr, sub ); 232 if ( idx == NULL ) { 233 return -1; 234 } 235 return stdlib_ndarray_set_ptr_uint8( idx, v ); 236 } 237 238 /** 239 * Sets a signed 8-bit integer ndarray data element. 240 * 241 * ## Notes 242 * 243 * - The function returns `-1` if unable to set an element and `0` otherwise. 244 * 245 * @param arr input ndarray 246 * @param sub ndarray subscripts 247 * @param v value to set 248 * @return status code 249 */ 250 int8_t stdlib_ndarray_set_int8( const struct ndarray *arr, const int64_t *sub, const int8_t v ) { 251 uint8_t *idx = stdlib_ndarray_get_ptr( arr, sub ); 252 if ( idx == NULL ) { 253 return -1; 254 } 255 return stdlib_ndarray_set_ptr_int8( idx, v ); 256 } 257 258 /** 259 * Sets a double-precision complex floating-point ndarray data element. 260 * 261 * ## Notes 262 * 263 * - The function does **not** verify that the type of `v` matches the underlying input ndarray data type, and, thus, overwriting **unowned** memory is possible. The function **assumes** that you know what you are doing. 264 * - The function returns `-1` if unable to set an element and `0` otherwise. 265 * 266 * @param arr input ndarray 267 * @param sub ndarray subscripts 268 * @param v value to set 269 * @return status code 270 */ 271 int8_t stdlib_ndarray_set_complex128( const struct ndarray *arr, const int64_t *sub, const double complex v ) { 272 uint8_t *idx = stdlib_ndarray_get_ptr( arr, sub ); 273 if ( idx == NULL ) { 274 return -1; 275 } 276 return stdlib_ndarray_set_ptr_complex128( idx, v ); 277 } 278 279 /** 280 * Sets a single-precision complex floating-point ndarray data element. 281 * 282 * ## Notes 283 * 284 * - The function does **not** verify that the type of `v` matches the underlying input ndarray data type, and, thus, overwriting **unowned** memory is possible. The function **assumes** that you know what you are doing. 285 * - The function returns `-1` if unable to set an element and `0` otherwise. 286 * 287 * @param arr input ndarray 288 * @param sub ndarray subscripts 289 * @param v value to set 290 * @return status code 291 */ 292 int8_t stdlib_ndarray_set_complex64( const struct ndarray *arr, const int64_t *sub, const float complex v ) { 293 uint8_t *idx = stdlib_ndarray_get_ptr( arr, sub ); 294 if ( idx == NULL ) { 295 return -1; 296 } 297 return stdlib_ndarray_set_ptr_complex64( idx, v ); 298 } 299 300 /** 301 * Sets a boolean floating-point ndarray data element. 302 * 303 * ## Notes 304 * 305 * - The function does **not** verify that the type of `v` matches the underlying input ndarray data type, and, thus, overwriting **unowned** memory is possible. The function **assumes** that you know what you are doing. 306 * - The function returns `-1` if unable to set an element and `0` otherwise. 307 * 308 * @param arr input ndarray 309 * @param sub ndarray subscripts 310 * @param v value to set 311 * @return status code 312 */ 313 int8_t stdlib_ndarray_set_bool( const struct ndarray *arr, const int64_t *sub, const bool v ) { 314 uint8_t *idx = stdlib_ndarray_get_ptr( arr, sub ); 315 if ( idx == NULL ) { 316 return -1; 317 } 318 return stdlib_ndarray_set_ptr_bool( idx, v ); 319 }