iset.c (13181B)
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/iset.h" 20 #include "stdlib/ndarray/ctor/iget_ptr.h" 21 #include "stdlib/ndarray/ctor/set_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 located at a specified linear index. 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 * - For zero-dimensional arrays, the function sets the first (and only) indexed element, regardless of the value of `idx`. 37 * 38 * @param arr input ndarray 39 * @param idx linear view index 40 * @param v value to set 41 * @return status code 42 */ 43 int8_t stdlib_ndarray_iset( const struct ndarray *arr, const int64_t idx, const void *v ) { 44 uint8_t *ind = stdlib_ndarray_iget_ptr( arr, idx ); 45 if ( ind == NULL ) { 46 return -1; 47 } 48 return stdlib_ndarray_set_ptr_value( arr, ind, v ); 49 } 50 51 /** 52 * Sets a double-precision floating-point ndarray data element located at a specified linear index. 53 * 54 * ## Notes 55 * 56 * - 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. 57 * - The function returns `-1` if unable to set an element and `0` otherwise. 58 * - For zero-dimensional arrays, the function sets the first (and only) indexed element, regardless of the value of `idx`. 59 * 60 * @param arr input ndarray 61 * @param idx linear view index 62 * @param v value to set 63 * @return status code 64 */ 65 int8_t stdlib_ndarray_iset_float64( const struct ndarray *arr, const int64_t idx, const double v ) { 66 uint8_t *ind = stdlib_ndarray_iget_ptr( arr, idx ); 67 if ( ind == NULL ) { 68 return -1; 69 } 70 return stdlib_ndarray_set_ptr_float64( ind, v ); 71 } 72 73 /** 74 * Sets a single-precision floating-point ndarray data element located at a specified linear index. 75 * 76 * ## Notes 77 * 78 * - 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. 79 * - The function returns `-1` if unable to set an element and `0` otherwise. 80 * - For zero-dimensional arrays, the function sets the first (and only) indexed element, regardless of the value of `idx`. 81 * 82 * @param arr input ndarray 83 * @param idx linear view index 84 * @param v value to set 85 * @return status code 86 */ 87 int8_t stdlib_ndarray_iset_float32( const struct ndarray *arr, const int64_t idx, const float v ) { 88 uint8_t *ind = stdlib_ndarray_iget_ptr( arr, idx ); 89 if ( ind == NULL ) { 90 return -1; 91 } 92 return stdlib_ndarray_set_ptr_float32( ind, v ); 93 } 94 95 /** 96 * Sets an unsigned 64-bit integer ndarray data element located at a specified linear index. 97 * 98 * ## Notes 99 * 100 * - 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. 101 * - The function returns `-1` if unable to set an element and `0` otherwise. 102 * - For zero-dimensional arrays, the function sets the first (and only) indexed element, regardless of the value of `idx`. 103 * 104 * @param arr input ndarray 105 * @param idx linear view index 106 * @param v value to set 107 * @return status code 108 */ 109 int8_t stdlib_ndarray_iset_uint64( const struct ndarray *arr, const int64_t idx, const uint64_t v ) { 110 uint8_t *ind = stdlib_ndarray_iget_ptr( arr, idx ); 111 if ( ind == NULL ) { 112 return -1; 113 } 114 return stdlib_ndarray_set_ptr_uint64( ind, v ); 115 } 116 117 /** 118 * Sets a signed 64-bit integer ndarray data element located at a specified linear index. 119 * 120 * ## Notes 121 * 122 * - 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. 123 * - The function returns `-1` if unable to set an element and `0` otherwise. 124 * - For zero-dimensional arrays, the function sets the first (and only) indexed element, regardless of the value of `idx`. 125 * 126 * @param arr input ndarray 127 * @param idx linear view index 128 * @param v value to set 129 * @return status code 130 */ 131 int8_t stdlib_ndarray_iset_int64( const struct ndarray *arr, const int64_t idx, const int64_t v ) { 132 uint8_t *ind = stdlib_ndarray_iget_ptr( arr, idx ); 133 if ( ind == NULL ) { 134 return -1; 135 } 136 return stdlib_ndarray_set_ptr_int64( ind, v ); 137 } 138 139 /** 140 * Sets an unsigned 32-bit integer ndarray data element located at a specified linear index. 141 * 142 * ## Notes 143 * 144 * - 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. 145 * - The function returns `-1` if unable to set an element and `0` otherwise. 146 * - For zero-dimensional arrays, the function sets the first (and only) indexed element, regardless of the value of `idx`. 147 * 148 * @param arr input ndarray 149 * @param idx linear view index 150 * @param v value to set 151 * @return status code 152 */ 153 int8_t stdlib_ndarray_iset_uint32( const struct ndarray *arr, const int64_t idx, const uint32_t v ) { 154 uint8_t *ind = stdlib_ndarray_iget_ptr( arr, idx ); 155 if ( ind == NULL ) { 156 return -1; 157 } 158 return stdlib_ndarray_set_ptr_uint32( ind, v ); 159 } 160 161 /** 162 * Sets a signed 32-bit integer ndarray data element located at a specified linear index. 163 * 164 * ## Notes 165 * 166 * - 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. 167 * - The function returns `-1` if unable to set an element and `0` otherwise. 168 * - For zero-dimensional arrays, the function sets the first (and only) indexed element, regardless of the value of `idx`. 169 * 170 * @param arr input ndarray 171 * @param idx linear view index 172 * @param v value to set 173 * @return status code 174 */ 175 int8_t stdlib_ndarray_iset_int32( const struct ndarray *arr, const int64_t idx, const int32_t v ) { 176 uint8_t *ind = stdlib_ndarray_iget_ptr( arr, idx ); 177 if ( ind == NULL ) { 178 return -1; 179 } 180 return stdlib_ndarray_set_ptr_int32( ind, v ); 181 } 182 183 /** 184 * Sets an unsigned 16-bit integer ndarray data element located at a specified linear index. 185 * 186 * ## Notes 187 * 188 * - 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. 189 * - The function returns `-1` if unable to set an element and `0` otherwise. 190 * - For zero-dimensional arrays, the function sets the first (and only) indexed element, regardless of the value of `idx`. 191 * 192 * @param arr input ndarray 193 * @param idx linear view index 194 * @param v value to set 195 * @return status code 196 */ 197 int8_t stdlib_ndarray_iset_uint16( const struct ndarray *arr, const int64_t idx, const uint16_t v ) { 198 uint8_t *ind = stdlib_ndarray_iget_ptr( arr, idx ); 199 if ( ind == NULL ) { 200 return -1; 201 } 202 return stdlib_ndarray_set_ptr_uint16( ind, v ); 203 } 204 205 /** 206 * Sets a signed 16-bit integer ndarray data element located at a specified linear index. 207 * 208 * ## Notes 209 * 210 * - 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. 211 * - The function returns `-1` if unable to set an element and `0` otherwise. 212 * - For zero-dimensional arrays, the function sets the first (and only) indexed element, regardless of the value of `idx`. 213 * 214 * @param arr input ndarray 215 * @param idx linear view index 216 * @param v value to set 217 * @return status code 218 */ 219 int8_t stdlib_ndarray_iset_int16( const struct ndarray *arr, const int64_t idx, const int16_t v ) { 220 uint8_t *ind = stdlib_ndarray_iget_ptr( arr, idx ); 221 if ( ind == NULL ) { 222 return -1; 223 } 224 return stdlib_ndarray_set_ptr_int16( ind, v ); 225 } 226 227 /** 228 * Sets an unsigned 8-bit integer ndarray data element located at a specified linear index. 229 * 230 * ## Notes 231 * 232 * - The function returns `-1` if unable to set an element and `0` otherwise. 233 * - For zero-dimensional arrays, the function sets the first (and only) indexed element, regardless of the value of `idx`. 234 * 235 * @param arr input ndarray 236 * @param idx linear view index 237 * @param v value to set 238 * @return status code 239 */ 240 int8_t stdlib_ndarray_iset_uint8( const struct ndarray *arr, const int64_t idx, const uint8_t v ) { 241 uint8_t *ind = stdlib_ndarray_iget_ptr( arr, idx ); 242 if ( ind == NULL ) { 243 return -1; 244 } 245 return stdlib_ndarray_set_ptr_uint8( ind, v ); 246 } 247 248 /** 249 * Sets a signed 8-bit integer ndarray data element located at a specified linear index. 250 * 251 * ## Notes 252 * 253 * - The function returns `-1` if unable to set an element and `0` otherwise. 254 * - For zero-dimensional arrays, the function sets the first (and only) indexed element, regardless of the value of `idx`. 255 * 256 * @param arr input ndarray 257 * @param idx linear view index 258 * @param v value to set 259 * @return status code 260 */ 261 int8_t stdlib_ndarray_iset_int8( const struct ndarray *arr, const int64_t idx, const int8_t v ) { 262 uint8_t *ind = stdlib_ndarray_iget_ptr( arr, idx ); 263 if ( ind == NULL ) { 264 return -1; 265 } 266 return stdlib_ndarray_set_ptr_int8( ind, v ); 267 } 268 269 /** 270 * Sets a double-precision complex floating-point ndarray data element located at a specified linear index. 271 * 272 * ## Notes 273 * 274 * - 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. 275 * - The function returns `-1` if unable to set an element and `0` otherwise. 276 * - For zero-dimensional arrays, the function sets the first (and only) indexed element, regardless of the value of `idx`. 277 * 278 * @param arr input ndarray 279 * @param idx linear view index 280 * @param v value to set 281 * @return status code 282 */ 283 int8_t stdlib_ndarray_iset_complex128( const struct ndarray *arr, const int64_t idx, const double complex v ) { 284 uint8_t *ind = stdlib_ndarray_iget_ptr( arr, idx ); 285 if ( ind == NULL ) { 286 return -1; 287 } 288 return stdlib_ndarray_set_ptr_complex128( ind, v ); 289 } 290 291 /** 292 * Sets a single-precision complex floating-point ndarray data element located at a specified linear index. 293 * 294 * ## Notes 295 * 296 * - 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. 297 * - The function returns `-1` if unable to set an element and `0` otherwise. 298 * - For zero-dimensional arrays, the function sets the first (and only) indexed element, regardless of the value of `idx`. 299 * 300 * @param arr input ndarray 301 * @param idx linear view index 302 * @param v value to set 303 * @return status code 304 */ 305 int8_t stdlib_ndarray_iset_complex64( const struct ndarray *arr, const int64_t idx, const float complex v ) { 306 uint8_t *ind = stdlib_ndarray_iget_ptr( arr, idx ); 307 if ( ind == NULL ) { 308 return -1; 309 } 310 return stdlib_ndarray_set_ptr_complex64( ind, v ); 311 } 312 313 /** 314 * Sets a boolean ndarray data element located at a specified linear index. 315 * 316 * ## Notes 317 * 318 * - 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. 319 * - The function returns `-1` if unable to set an element and `0` otherwise. 320 * - For zero-dimensional arrays, the function sets the first (and only) indexed element, regardless of the value of `idx`. 321 * 322 * @param arr input ndarray 323 * @param idx linear view index 324 * @param v value to set 325 * @return status code 326 */ 327 int8_t stdlib_ndarray_iset_bool( const struct ndarray *arr, const int64_t idx, const bool v ) { 328 uint8_t *ind = stdlib_ndarray_iget_ptr( arr, idx ); 329 if ( ind == NULL ) { 330 return -1; 331 } 332 return stdlib_ndarray_set_ptr_bool( ind, v ); 333 }