| 1 | #ifndef _melder_tensor_h_ |
| 2 | #define _melder_tensor_h_ |
| 3 | /* melder_tensor.h |
| 4 | * |
| 5 | * Copyright (C) 1992-2026 Paul Boersma |
| 6 | * |
| 7 | * This code is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 3 of the License, or (at |
| 10 | * your option) any later version. |
| 11 | * |
| 12 | * This code is distributed in the hope that it will be useful, but |
| 13 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 15 | * See the GNU General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License |
| 18 | * along with this work. If not, see <http://www.gnu.org/licenses/>. |
| 19 | */ |
| 20 | |
| 21 | #pragma mark - TENSOR |
| 22 | /* |
| 23 | Base-1 tensors, for parallellism with the scripting language. |
| 24 | |
| 25 | Initialization (tested in praat.cpp): |
| 26 | VEC x; // initializes x.cells to nullptr and x.size to 0 |
| 27 | double a [] = { 3.14, 2.718 }; |
| 28 | VEC x3 (a, 2); // initializes x3 to 100 values from a base-0 array |
| 29 | Melder_assert (x3 [2] == 2.718); |
| 30 | constVEC x4 = { 3.14, 2.718 }; |
| 31 | Melder_assert (x4 [2] == 2.718); |
| 32 | |
| 33 | autoVEC y; // initializes y.cells to nullptr and y.size to 0 |
| 34 | autoVEC y2 = zero_VEC (100); // initializes y to 100 zeroes, having ownership |
| 35 | autoVEC y1 = raw_VEC (100); // initializes y to 100 uninitialized values (caution!), having ownership |
| 36 | y.adoptFromAmbiguousOwner (x); // initializes y to the content of x, taking ownership (explicit, so not "y = x") |
| 37 | VEC z = y.releaseToAmbiguousOwner(); // releases ownership, y.cells becoming nullptr |
| 38 | "}" // end of scope destroys y.cells if not nullptr |
| 39 | autoVEC z1 = y2.move() // moves the content of y2 to z1, emptying y2 |
| 40 | |
| 41 | To return an autoVEC from a function, transfer ownership like this: |
| 42 | autoVEC foo () { |
| 43 | autoVEC x = zero_VEC (100); |
| 44 | ... // fill in the 100 values |
| 45 | return x; |
| 46 | } |
| 47 | */ |
| 48 | |
| 49 | /* |
| 50 | Forward declarations, needed because of all the conversions and deletions. |
| 51 | */ |
| 52 | template <typename T> class vector; |
| 53 | template <typename T> class constvector; |
| 54 | template <typename T> class vectorview; |
| 55 | template <typename T> class constvectorview; |
| 56 | template <typename T> class autovector; |
| 57 | template <typename T> class matrix; |
| 58 | template <typename T> class constmatrix; |
| 59 | template <typename T> class matrixview; |
| 60 | template <typename T> class constmatrixview; |
| 61 | template <typename T> class automatrix; |
| 62 | |
| 63 | template <typename T> |
| 64 | struct vector { |
| 65 | T *cells = nullptr; |
| 66 | integer size = 0; |
| 67 | vector () |
| 68 | = default; |
| 69 | explicit vector (T *givenCells, integer givenSize) |
| 70 | : cells (givenCells), size (givenSize) { } |
| 71 | explicit vector (matrix<T> const& mat) |
| 72 | : vector (mat.cells, mat.nrow * mat.ncol) { } |
| 73 | inline T& operator[] (integer i) const { |
| 74 | return our cells [i - 1]; |
| 75 | } |
| 76 | /* |
| 77 | part (first, last) should crash under the exact |
| 78 | same conditions as a rising for-loop over the elements |
| 79 | from first to last (or a falling for-loop over |
| 80 | the elements from last to first) should crash. |
| 81 | */ |
| 82 | vector<T> part (integer first, integer last) const { |
| 83 | const integer newSize = last - (first - 1); |
| 84 | /* |
| 85 | for-loops don't crash if the number of elements is zero. |
| 86 | */ |
| 87 | if (newSize <= 0) |
| 88 | return vector<T> (); |
| 89 | Melder_assert (first >= 1 && first <= our size); |
| 90 | Melder_assert (last >= 1 && last <= our size); |
| 91 | return vector<T> (& our cells [first - 1], newSize); |
| 92 | } |
| 93 | matrix<T> asmatrix (integer nrow, integer ncol) { |
| 94 | Melder_assert (nrow * ncol <= our size); |
| 95 | return matrix (our cells, nrow, ncol); |
| 96 | } |
| 97 | T *begin () const { return our cells; } |
| 98 | T *end () const { return our cells + our size; } |
| 99 | T *asArgumentToFunctionThatExpectsZeroBasedArray () const { return our cells; } |
| 100 | T *asArgumentToFunctionThatExpectsOneBasedArray () const { return our cells - 1; } |
| 101 | }; |
| 102 | |
| 103 | template <typename T> |
| 104 | struct constvector { |
| 105 | const T *cells = nullptr; |
| 106 | integer size = 0; |
| 107 | constvector () |
| 108 | = default; |
| 109 | explicit constvector (const T *givenCells, integer givenSize) |
| 110 | : cells (givenCells), size (givenSize) { } |
| 111 | explicit constvector (constmatrix<T> const& mat) |
| 112 | : constvector (mat.cells, mat.nrow * mat.ncol) { } |
| 113 | constvector (vector<T> const& other) |
| 114 | : constvector (other.cells, other.size) { } |
| 115 | const T& operator[] (integer i) const { // it's still a reference, because we need to be able to take its address |
| 116 | return our cells [i - 1]; |
| 117 | } |
| 118 | constvector<T> part (integer first, integer last) const { |
| 119 | const integer newSize = last - (first - 1); |
| 120 | if (newSize <= 0) |
| 121 | return constvector<T> (); |
| 122 | Melder_assert (first >= 1 && first <= our size); |
| 123 | Melder_assert (last >= 1 && last <= our size); |
| 124 | return constvector<T> (& our cells [first - 1], newSize); |
| 125 | } |
| 126 | constmatrix<T> asmatrix (integer nrow, integer ncol) { |
| 127 | Melder_assert (nrow * ncol <= our size); |
| 128 | return constmatrix (our cells, nrow, ncol); |
| 129 | } |
| 130 | const T *begin () const { return our cells; } |
| 131 | const T *end () const { return our cells + our size; } |
| 132 | const T *asArgumentToFunctionThatExpectsZeroBasedArray () const { return our cells; } |
| 133 | const T *asArgumentToFunctionThatExpectsOneBasedArray () const { return our cells - 1; } |
| 134 | }; |
| 135 | |
| 136 | template <typename T> |
| 137 | struct vectorview { |
| 138 | T * firstCell = nullptr; |
| 139 | integer size = 0; |
| 140 | integer stride = 1; |
| 141 | vectorview () |
| 142 | = default; |
| 143 | explicit vectorview (T *const firstCell_, integer const size_, integer const stride_) |
| 144 | : firstCell (firstCell_), size (size_), stride (stride_) { } |
| 145 | vectorview (vector<T> const& other) |
| 146 | : firstCell (other.cells), size (other.size), stride (1) { } |
| 147 | T& operator[] (integer i) const { |
| 148 | return our firstCell [(i - 1) * our stride]; |
| 149 | } |
| 150 | vectorview<T> part (integer first, integer last) const { |
| 151 | const integer newSize = last - (first - 1); |
| 152 | if (newSize <= 0) |
| 153 | return vectorview<T> (); |
| 154 | Melder_assert (first >= 1 && first <= our size); |
| 155 | Melder_assert (last >= 1 && last <= our size); |
| 156 | return vectorview<T> (& our operator[] (first), newSize, our stride); |
| 157 | } |
| 158 | matrixview<T> asmatrixview (integer nrow, integer ncol) { |
| 159 | Melder_assert (nrow * ncol <= our size); |
| 160 | return matrixview (our firstCell, nrow, ncol, ncol * our stride, our stride); |
| 161 | } |
| 162 | T *asArgumentToFunctionThatExpectsZeroBasedArray () const { return & our operator[] (1); } |
| 163 | T *asArgumentToFunctionThatExpectsOneBasedArray () const { return & our operator[] (0); } |
| 164 | }; |
| 165 | |
| 166 | template <typename T> |
| 167 | struct constvectorview { |
| 168 | const T * firstCell = nullptr; |
| 169 | integer size = 0; |
| 170 | integer stride = 1; |
| 171 | constvectorview () = default; |
| 172 | explicit constvectorview (const T *const firstCell_, integer const size_, integer const stride_) |
| 173 | : firstCell (firstCell_), size (size_), stride (stride_) { } |
| 174 | constvectorview (constvector<T> const& other) |
| 175 | : constvectorview (other.cells, other.size, 1) { } |
| 176 | constvectorview (vectorview<T> const& other) |
| 177 | : constvectorview (other.firstCell, other.size, other.stride) { } |
| 178 | constvectorview (vector<T> const& other) |
| 179 | : constvectorview (other.cells, other.size, 1) { } |
| 180 | T const& operator[] (integer i) const { |
| 181 | return our firstCell [(i - 1) * our stride]; |
| 182 | } |
| 183 | constvectorview<T> part (integer first, integer last) const { |
| 184 | const integer newSize = last - (first - 1); |
| 185 | if (newSize <= 0) |
| 186 | return constvectorview<T> (); |
| 187 | Melder_assert (first >= 1 && first <= our size); |
| 188 | Melder_assert (last >= 1 && last <= our size); |
| 189 | return constvectorview<T> (& our operator[] (first), newSize, our stride); |
| 190 | } |
| 191 | constmatrixview<T> asmatrixview (integer nrow, integer ncol) { |
| 192 | Melder_assert (nrow * ncol <= our size); |
| 193 | return constmatrixview (our firstCell, nrow, ncol, ncol * our stride, our stride); |
| 194 | } |
| 195 | const T *asArgumentToFunctionThatExpectsZeroBasedArray () const { return & our operator[] (1); } |
| 196 | const T *asArgumentToFunctionThatExpectsOneBasedArray () const { return & our operator[] (0); } |
| 197 | }; |
| 198 | |
| 199 | /* |
| 200 | An autovector is the sole owner of its payload, which is a vector. |
| 201 | When the autovector ends its life (goes out of scope), |
| 202 | it should destroy its payload (if it has not sold it), |
| 203 | because keeping a payload alive when the owner is dead |
| 204 | would continue to use some of the computer's resources (namely, memory). |
| 205 | */ |
| 206 | template <typename T> |
| 207 | struct autovector { |
| 208 | T *cells = nullptr; |
| 209 | integer size = 0; |
| 210 | integer _capacity = 0; |
| 211 | autovector () // come into existence without a payload |
| 212 | = default; |
| 213 | explicit autovector (integer givenSize, MelderArray::kInitializationType initializationType) { // come into existence and manufacture a payload |
| 214 | Melder_assert (givenSize >= 0); |
| 215 | our cells = MelderArray:: _alloc <T> (givenSize, initializationType); |
| 216 | our size = givenSize; |
| 217 | our _capacity = givenSize; |
| 218 | } |
| 219 | autovector (std::initializer_list <const T> list) { |
| 220 | our size = uinteger_to_integer_a (list.size()); |
| 221 | our cells = MelderArray:: _alloc <T> (our size, MelderArray::kInitializationType::RAW); // raw is possible because T is copyable data |
| 222 | T *p = our cells; |
| 223 | for (auto cell : list) |
| 224 | * (p ++) = cell; |
| 225 | } |
| 226 | void reset () noexcept { // on behalf of ambiguous owners (otherwise this could be in autovector<>) |
| 227 | if (our cells) { |
| 228 | MelderArray:: _free (our cells, our _capacity); |
| 229 | our cells = nullptr; |
| 230 | } |
| 231 | our size = 0; |
| 232 | our _capacity = 0; |
| 233 | } |
| 234 | ~autovector () { // destroy the payload (if any) |
| 235 | our reset (); |
| 236 | } |
| 237 | T& operator[] (integer i) const { |
| 238 | return our cells [i - 1]; |
| 239 | } |
| 240 | vector<T> get () const { return vector<T> (our cells, our size); } // let the public use the payload (they may change the values of the elements but not the at-pointer or the size) |
| 241 | vectorview<T> all () const { return vectorview<T> (our cells, our size, 1); } |
| 242 | void adoptFromAmbiguousOwner (vector<T> given) { // buy the payload from a non-autovector |
| 243 | our reset(); |
| 244 | our cells = given.cells; |
| 245 | our size = given.size; |
| 246 | our _capacity = given.size; |
| 247 | } |
| 248 | vector<T> releaseToAmbiguousOwner () { // sell the payload to a non-autovector |
| 249 | T *oldCells = our cells; |
| 250 | our cells = nullptr; // disown ourselves, preventing automatic destruction of the payload |
| 251 | integer oldSize = our size; |
| 252 | our size = 0; |
| 253 | our _capacity = 0; |
| 254 | return vector<T> (oldCells, oldSize); |
| 255 | } |
| 256 | vector<T> part (integer first, integer last) const { |
| 257 | const integer newSize = last - (first - 1); |
| 258 | /* |
| 259 | for-loops don't crash if the number of elements is zero. |
| 260 | */ |
| 261 | if (newSize <= 0) |
| 262 | return vector<T> (); |
| 263 | Melder_assert (first >= 1 && first <= our size); |
| 264 | Melder_assert (last >= 1 && last <= our size); |
| 265 | return vector<T> (& our cells [first - 1], newSize); |
| 266 | } |
| 267 | T *begin () const { return our cells; } |
| 268 | T *end () const { return our cells + our size; } |
| 269 | T *asArgumentToFunctionThatExpectsZeroBasedArray () const { return our cells; } |
| 270 | T *asArgumentToFunctionThatExpectsOneBasedArray () const { return our cells - 1; } |
| 271 | /* |
| 272 | Disable copying via construction or assignment (which would violate unique ownership of the payload). |
| 273 | */ |
| 274 | autovector (autovector const& other) = delete; // disable copy construction |
| 275 | autovector& operator= (autovector const& other) = delete; // disable copy assignment |
| 276 | /* |
| 277 | Enable moving of r-values (temporaries, implicitly) or l-values (for variables, via an explicit move()). |
| 278 | This implements buying a payload from another autovector (which involves destroying our current payload). |
| 279 | */ |
| 280 | autovector (autovector&& other) noexcept : cells (other.cells), size (other.size), _capacity (other._capacity) { // enable move construction |
| 281 | other.cells = nullptr; // disown source |
| 282 | other.size = 0; // to keep the source in a valid state |
| 283 | other._capacity = 0; |
| 284 | } |
| 285 | autovector& operator= (autovector&& other) noexcept { // enable move assignment |
| 286 | if (other.cells != our cells) { |
| 287 | our reset (); |
| 288 | our cells = other.cells; |
| 289 | our size = other.size; |
| 290 | our _capacity = other._capacity; |
| 291 | other.cells = nullptr; // disown source |
| 292 | other.size = 0; // to keep the source in a valid state |
| 293 | other._capacity = 0; |
| 294 | } |
| 295 | return *this; |
| 296 | } |
| 297 | autovector&& move () noexcept { return static_cast <autovector&&> (*this); } // enable construction and assignment for l-values (variables) via explicit move() |
| 298 | /* |
| 299 | Some of the following functions are capable of keeping a valid `cells` pointer |
| 300 | while `size` can at the same time be zero. |
| 301 | */ |
| 302 | void initWithCapacity (integer capacity, MelderArray::kInitializationType initializationType = MelderArray::kInitializationType::ZERO) { |
| 303 | if (capacity > 0) |
| 304 | our cells = MelderArray:: _alloc <T> (capacity, initializationType); |
| 305 | our size = 0; |
| 306 | our _capacity = capacity; |
| 307 | } |
| 308 | /* |
| 309 | If the new size N is less than the current size S, |
| 310 | then the first N elements of the vector are kept, |
| 311 | so if you want to keep a different range than the |
| 312 | first N elements of your original vector, |
| 313 | you should shift the elements before resizing. |
| 314 | |
| 315 | If the new size N is greater than the current size S, |
| 316 | then all S elements of the vector are kept, |
| 317 | and they are the first S elements of the new vector |
| 318 | the remaining S - N elements may be initialized to zero. |
| 319 | If you want the original S elements to show up |
| 320 | elsewhere than at the head of the vector, |
| 321 | you should shift the elements after resizing. |
| 322 | */ |
| 323 | void resize (integer newSize, MelderArray::kInitializationType initializationType = MelderArray::kInitializationType::ZERO) { |
| 324 | if (newSize > our _capacity) { |
| 325 | /* |
| 326 | The new capacity is at least twice the old capacity. |
| 327 | When starting at a capacity of 0, and continually upsizing by one, |
| 328 | the capacity sequence will be: 0, 11, 33, 77, 165, 341, 693, 1397, |
| 329 | 2805, 5621, 11253, 22517, 45045, 90101, 180213, 360437, 720885... |
| 330 | */ |
| 331 | integer newCapacity = newSize + our size + 10; |
| 332 | /* |
| 333 | Create without change. |
| 334 | */ |
| 335 | T *newCells = MelderArray:: _alloc <T> (newCapacity, initializationType); |
| 336 | /* |
| 337 | Change without error. |
| 338 | */ |
| 339 | for (integer i = 1; i <= our size; i ++) |
| 340 | newCells [i - 1] = std::move (our cells [i - 1]); |
| 341 | if (our cells) |
| 342 | MelderArray:: _free (our cells, our _capacity); |
| 343 | our cells = newCells; |
| 344 | our _capacity = newCapacity; |
| 345 | } |
| 346 | our size = newSize; |
| 347 | } |
| 348 | void insert (integer position, const T& value) { |
| 349 | if (position == 0) |
| 350 | position = our size + 1; |
| 351 | resize (our size + 1, MelderArray::kInitializationType::RAW); |
| 352 | Melder_assert (position >= 1 && position <= our size); |
| 353 | for (integer i = our size; i > position; i --) |
| 354 | our cells [i - 1] = std::move (our cells [i - 2]); |
| 355 | our cells [position - 1] = value; |
| 356 | } |
| 357 | T* append () { |
| 358 | resize (our size + 1, MelderArray::kInitializationType::ZERO); |
| 359 | return & our cells [our size - 1]; |
| 360 | } |
| 361 | void remove (integer position) { |
| 362 | Melder_assert (position >= 1 && position <= our size); |
| 363 | for (integer i = position; i < our size; i ++) |
| 364 | our cells [i - 1] = std::move (our cells [i]); |
| 365 | resize (our size - 1); |
| 366 | } |
| 367 | }; |
| 368 | |
| 369 | template <typename T> |
| 370 | autovector<T> newvectorraw (integer size) { |
| 371 | return autovector<T> (size, MelderArray::kInitializationType::RAW); |
| 372 | } |
| 373 | template <typename T> |
| 374 | autovector<T> newvectorzero (integer size) { |
| 375 | return autovector<T> (size, MelderArray::kInitializationType::ZERO); |
| 376 | } |
| 377 | template <typename T> |
| 378 | autovector<T> newvectorcopy (constvectorview<T> source) { |
| 379 | autovector<T> result = newvectorraw<T> (source.size); |
| 380 | for (integer i = 1; i <= source.size; i ++) |
| 381 | result [i] = source [i]; |
| 382 | return result; |
| 383 | } |
| 384 | template <typename T> |
| 385 | autovector<T> newvectorcopy (vectorview<T> source) { |
| 386 | return newvectorcopy (constvectorview<T> (source)); |
| 387 | } |
| 388 | |
| 389 | template <typename T> |
| 390 | struct matrix { |
| 391 | T *cells = nullptr; |
| 392 | integer nrow = 0, ncol = 0; |
| 393 | matrix () |
| 394 | = default; |
| 395 | explicit matrix (T *givenCells, integer givenNrow, integer givenNcol) |
| 396 | : cells (givenCells), nrow (givenNrow), ncol (givenNcol) { } |
| 397 | //matrix (matrix const& other) |
| 398 | // = default; |
| 399 | //matrix& operator= (matrix const& other) |
| 400 | // = default; |
| 401 | explicit matrix (vector<T> const& vec, integer givenNrow, integer givenNcol) |
| 402 | : matrix (vec.cells, givenNrow, givenNcol) |
| 403 | { |
| 404 | Melder_assert (givenNrow * givenNcol <= vec.size); |
| 405 | } |
| 406 | vector<T> operator[] (integer rowNumber) const { |
| 407 | return vector<T> (our cells + (rowNumber - 1) * our ncol, our ncol); |
| 408 | } |
| 409 | vector<T> row (integer rowNumber) const { |
| 410 | Melder_assert (rowNumber >= 1 && rowNumber <= our nrow); |
| 411 | Melder_assert (our cells); |
| 412 | return vector<T> (our cells + (rowNumber - 1) * our ncol, our ncol); |
| 413 | } |
| 414 | vectorview<T> column (integer columnNumber) const { |
| 415 | Melder_assert (columnNumber >= 1 && columnNumber <= our ncol); |
| 416 | return vectorview<T> (our cells + (columnNumber - 1), our nrow, our ncol); |
| 417 | } |
| 418 | vectorview<T> diagonal () const { |
| 419 | return vectorview<T> (our cells, std::min (our nrow, our ncol), our ncol + 1); |
| 420 | } |
| 421 | matrixview<T> horizontalBand (integer firstRow, integer lastRow) const { |
| 422 | const integer newNrow = lastRow - (firstRow - 1); |
| 423 | if (newNrow <= 0) |
| 424 | return matrixview<T> (); |
| 425 | Melder_assert (firstRow >= 1 && firstRow <= our nrow); |
| 426 | Melder_assert (lastRow >= 1 && lastRow <= our nrow); |
| 427 | return matrixview<T> (our cells + (firstRow - 1) * our ncol, newNrow, our ncol, our ncol, 1); |
| 428 | } |
| 429 | matrixview<T> verticalBand (integer firstColumn, integer lastColumn) const { |
| 430 | const integer newNcol = lastColumn - (firstColumn - 1); |
| 431 | if (newNcol <= 0) |
| 432 | return matrixview<T> (); |
| 433 | Melder_assert (firstColumn >= 1 && firstColumn <= our ncol); |
| 434 | Melder_assert (lastColumn >= 1 && lastColumn <= our ncol); |
| 435 | return matrixview<T> (our cells + (firstColumn - 1), our nrow, newNcol, our ncol, 1); |
| 436 | } |
| 437 | matrixview<T> part (integer firstRow, integer lastRow, integer firstColumn, integer lastColumn) const { |
| 438 | const integer newNrow = lastRow - (firstRow - 1), newNcol = lastColumn - (firstColumn - 1); |
| 439 | if (newNrow <= 0 || newNcol <= 0) |
| 440 | return matrixview<T> (); |
| 441 | Melder_assert (firstRow >= 1 && firstRow <= our nrow); |
| 442 | Melder_assert (lastRow >= 1 && lastRow <= our nrow); |
| 443 | Melder_assert (firstColumn >= 1 && firstColumn <= our ncol); |
| 444 | Melder_assert (lastColumn >= 1 && lastColumn <= our ncol); |
| 445 | return matrixview<T> ( |
| 446 | our cells + (firstRow - 1) * our ncol + (firstColumn - 1), |
| 447 | newNrow, newNcol, our ncol, 1 |
| 448 | ); |
| 449 | } |
| 450 | matrixview<T> transpose () const { |
| 451 | return matrixview<T> (our cells, our ncol, our nrow, 1, our ncol); |
| 452 | } |
| 453 | vector<T> asvector () const { |
| 454 | return vector<T> (our cells, our nrow * our ncol); |
| 455 | } |
| 456 | vector<T> asvector (integer size) const { |
| 457 | Melder_assert (size <= our nrow * our ncol); |
| 458 | return vector<T> (our cells, size); |
| 459 | } |
| 460 | }; |
| 461 | |
| 462 | template <typename T> |
| 463 | struct constmatrix { |
| 464 | const T *cells = nullptr; |
| 465 | integer nrow = 0, ncol = 0; |
| 466 | constmatrix () |
| 467 | = default; |
| 468 | explicit constmatrix (const T *givenCells, integer givenNrow, integer givenNcol) |
| 469 | : cells (givenCells), nrow (givenNrow), ncol (givenNcol) { } |
| 470 | constmatrix (matrix<T> const& other) |
| 471 | : constmatrix (other.cells, other.nrow, other.ncol) { } |
| 472 | explicit constmatrix (vector<T> const& vec, integer givenNrow, integer givenNcol) |
| 473 | : constmatrix (vec.cells, givenNrow, givenNcol) |
| 474 | { |
| 475 | Melder_assert (givenNrow * givenNcol <= vec.size); |
| 476 | } |
| 477 | constvector<T> operator[] (integer rowNumber) const { |
| 478 | return constvector<T> (our cells + (rowNumber - 1) * our ncol, our ncol); |
| 479 | } |
| 480 | constvector<T> row (integer rowNumber) const { |
| 481 | Melder_assert (rowNumber >= 1 && rowNumber <= our nrow); |
| 482 | Melder_assert (our cells); |
| 483 | return constvector<T> (our cells + (rowNumber - 1) * our ncol, our ncol); |
| 484 | } |
| 485 | constvectorview<T> column (integer columnNumber) const { |
| 486 | Melder_assert (columnNumber >= 1 && columnNumber <= our ncol); |
| 487 | return constvectorview<T> (our cells + (columnNumber - 1), our nrow, our ncol); |
| 488 | } |
| 489 | constvectorview<T> diagonal () const { |
| 490 | return constvectorview<T> (our cells, std::min (our nrow, our ncol), our ncol + 1); |
| 491 | } |
| 492 | constmatrixview<T> horizontalBand (integer firstRow, integer lastRow) const { |
| 493 | const integer newNrow = lastRow - (firstRow - 1); |
| 494 | if (newNrow <= 0) |
| 495 | return constmatrixview<T> (); |
| 496 | Melder_assert (firstRow >= 1 && firstRow <= our nrow); |
| 497 | Melder_assert (lastRow >= 1 && lastRow <= our nrow); |
| 498 | return constmatrixview<T> (our cells + (firstRow - 1) * our ncol, newNrow, our ncol, our ncol, 1); |
| 499 | } |
| 500 | constmatrixview<T> verticalBand (integer firstColumn, integer lastColumn) const { |
| 501 | const integer newNcol = lastColumn - (firstColumn - 1); |
| 502 | if (newNcol <= 0) |
| 503 | return constmatrixview<T> (); |
| 504 | Melder_assert (firstColumn >= 1 && firstColumn <= our ncol); |
| 505 | Melder_assert (lastColumn >= 1 && lastColumn <= our ncol); |
| 506 | return constmatrixview<T> (our cells + (firstColumn - 1), our nrow, newNcol, our ncol, 1); |
| 507 | } |
| 508 | constmatrixview<T> part (integer firstRow, integer lastRow, integer firstColumn, integer lastColumn) const { |
| 509 | const integer newNrow = lastRow - (firstRow - 1), newNcol = lastColumn - (firstColumn - 1); |
| 510 | if (newNrow <= 0 || newNcol <= 0) |
| 511 | return constmatrixview<T> (); |
| 512 | Melder_assert (firstRow >= 1 && firstRow <= our nrow); |
| 513 | Melder_assert (lastRow >= 1 && lastRow <= our nrow); |
| 514 | Melder_assert (firstColumn >= 1 && firstColumn <= our ncol); |
| 515 | Melder_assert (lastColumn >= 1 && lastColumn <= our ncol); |
| 516 | return constmatrixview<T> ( |
| 517 | our cells + (firstRow - 1) * our ncol + (firstColumn - 1), |
| 518 | newNrow, newNcol, our ncol, 1 |
| 519 | ); |
| 520 | } |
| 521 | constmatrixview<T> transpose () const { |
| 522 | return constmatrixview<T> (our cells, our ncol, our nrow, 1, our ncol); |
| 523 | } |
| 524 | constvector<T> asvector () const { |
| 525 | return constvector<T> (our cells, our nrow * our ncol); |
| 526 | } |
| 527 | constvector<T> asvector (integer size) const { |
| 528 | Melder_assert (size <= our nrow * our ncol); |
| 529 | return constvector<T> (our cells, size); |
| 530 | } |
| 531 | }; |
| 532 | |
| 533 | template <typename T> |
| 534 | struct matrixview { |
| 535 | T * firstCell = nullptr; |
| 536 | integer nrow = 0, ncol = 0; |
| 537 | /*mutable*/ integer rowStride = 0, colStride = 1; // mutable perhaps once an automatrix has strides |
| 538 | /* |
| 539 | Make sure that each of the following creates an appropriately initialized matrixview: |
| 540 | matrixview<double> matvu; // OK |
| 541 | auto matvu = matrixview<double>(); // OK |
| 542 | */ |
| 543 | matrixview () |
| 544 | = default; |
| 545 | /* |
| 546 | The following constructor is explicit, i.e., |
| 547 | it cannot be used as an implicit conversion from an initializer list, |
| 548 | as in any of the following: |
| 549 | matrixview<double> mat = { p, 10, 100, 100, 1 }; // WRONG |
| 550 | myFunction ({ p, 10, 100, 100, 1 }); // WRONG |
| 551 | whereas any of the following is fine: |
| 552 | matrixview<double> mat { p, 10, 100, 100, 1 }; // OK |
| 553 | matrixview<double> mat (p, 10, 100, 100, 1); // OK |
| 554 | auto mat = matrixview<double> { p, 10, 100, 100, 1 }; // OK |
| 555 | auto mat = matrixview<double> (p, 10, 100, 100, 1); // OK |
| 556 | myFunction (matrixview<double> { p, 10, 100, 100, 1 }); // OK |
| 557 | myFunction (matrixview<double> (p, 10, 100, 100, 1)); // OK |
| 558 | */ |
| 559 | explicit matrixview (T * firstCell_, integer nrow_, integer ncol_, integer rowStride_, integer colStride_) |
| 560 | : firstCell (firstCell_), nrow (nrow_), ncol (ncol_), rowStride (rowStride_), colStride (colStride_) { } |
| 561 | /* |
| 562 | The following constructor is implicit, i.e., |
| 563 | you can assign a matrix to a matrixview. |
| 564 | */ |
| 565 | matrixview (matrix<T> const& other) |
| 566 | : matrixview (other.cells, other.nrow, other.ncol, other.ncol, 1_integer) { } |
| 567 | explicit matrixview (vectorview<T> const& vec, integer givenNrow, integer givenNcol) : |
| 568 | matrixview (vec.cells, givenNrow, givenNcol, givenNcol * vec.stride, vec.stride) |
| 569 | { |
| 570 | Melder_assert (givenNrow * givenNcol <= vec.size); |
| 571 | } |
| 572 | vectorview<T> operator[] (integer rowNumber) const { |
| 573 | return vectorview<T> (our firstCell + (rowNumber - 1) * our rowStride, our ncol, our colStride); |
| 574 | } |
| 575 | vectorview<T> row (integer rowNumber) const { |
| 576 | Melder_assert (rowNumber >= 1 && rowNumber <= our nrow); |
| 577 | return vectorview<T> (our firstCell + (rowNumber - 1) * our rowStride, our ncol, our colStride); |
| 578 | } |
| 579 | vectorview<T> column (integer columnNumber) const { |
| 580 | Melder_assert (columnNumber >= 1 && columnNumber <= our ncol); |
| 581 | return vectorview<T> (our firstCell + (columnNumber - 1) * our colStride, our nrow, our rowStride); |
| 582 | } |
| 583 | vectorview<T> diagonal () const { |
| 584 | return vectorview<T> (our firstCell, std::min (our nrow, our ncol), our rowStride + our colStride); |
| 585 | } |
| 586 | matrixview<T> verticalBand (integer firstColumn, integer lastColumn) const { |
| 587 | const integer newNcol = lastColumn - (firstColumn - 1); |
| 588 | if (newNcol <= 0) |
| 589 | return matrixview<T> (); |
| 590 | Melder_assert (firstColumn >= 1 && firstColumn <= our ncol); |
| 591 | Melder_assert (lastColumn >= 1 && lastColumn <= our ncol); |
| 592 | return matrixview<T> (our firstCell + (firstColumn - 1) * our colStride, |
| 593 | our nrow, newNcol, our rowStride, our colStride); |
| 594 | } |
| 595 | matrixview<T> part (integer firstRow, integer lastRow, integer firstColumn, integer lastColumn) const { |
| 596 | const integer newNrow = lastRow - (firstRow - 1), newNcol = lastColumn - (firstColumn - 1); |
| 597 | if (newNrow <= 0 || newNcol <= 0) |
| 598 | return matrixview<T> (); |
| 599 | Melder_assert (firstRow >= 1 && firstRow <= our nrow); |
| 600 | Melder_assert (lastRow >= 1 && lastRow <= our nrow); |
| 601 | Melder_assert (firstColumn >= 1 && firstColumn <= our ncol); |
| 602 | Melder_assert (lastColumn >= 1 && lastColumn <= our ncol); |
| 603 | return matrixview<T> ( |
| 604 | our firstCell + (firstRow - 1) * our rowStride + (firstColumn - 1) * our colStride, |
| 605 | newNrow, newNcol, our rowStride, our colStride |
| 606 | ); |
| 607 | } |
| 608 | matrixview<T> transpose () const { |
| 609 | return matrixview<T> (our firstCell, our ncol, our nrow, our colStride, our rowStride); |
| 610 | } |
| 611 | }; |
| 612 | |
| 613 | template <typename T> |
| 614 | struct constmatrixview { |
| 615 | const T * firstCell = nullptr; |
| 616 | integer nrow = 0, ncol = 0; |
| 617 | integer rowStride = 0, colStride = 1; |
| 618 | constmatrixview () |
| 619 | = default; |
| 620 | explicit constmatrixview (const T *const firstCell_, integer const nrow_, integer const ncol_, integer const rowStride_, integer const colStride_) |
| 621 | : firstCell (firstCell_), nrow (nrow_), ncol (ncol_), rowStride (rowStride_), colStride (colStride_) { } |
| 622 | constmatrixview (const constmatrix<T>& other) |
| 623 | : constmatrixview (other.cells, other.nrow, other.ncol, other.ncol, 1_integer) { } |
| 624 | constmatrixview (const matrix<T>& other) // shortcut the otherwise double conversion |
| 625 | : constmatrixview (other.cells, other.nrow, other.ncol, other.ncol, 1_integer) { } |
| 626 | constmatrixview (matrixview<T> const& other) |
| 627 | : constmatrixview (other.firstCell, other.nrow, other.ncol, other.rowStride, other.colStride) { } |
| 628 | explicit constmatrixview (constvectorview<T> const& vec, integer givenNrow, integer givenNcol) : |
| 629 | constmatrixview (vec.cells, givenNrow, givenNcol, givenNcol * vec.stride, vec.stride) |
| 630 | { |
| 631 | Melder_assert (givenNrow * givenNcol <= vec.size); |
| 632 | } |
| 633 | constvectorview<T> operator[] (integer i) const { |
| 634 | return constvectorview<T> (our firstCell + (i - 1) * our rowStride, our ncol, our colStride); |
| 635 | } |
| 636 | constvectorview<T> row (integer rowNumber) const { |
| 637 | Melder_assert (rowNumber >= 1 && rowNumber <= our nrow); |
| 638 | return constvectorview<T> (our firstCell + (rowNumber - 1) * our rowStride, our ncol, our colStride); |
| 639 | } |
| 640 | constvectorview<T> column (integer columnNumber) const { |
| 641 | Melder_assert (columnNumber >= 1 && columnNumber <= our ncol); |
| 642 | return constvectorview<T> (our firstCell + (columnNumber - 1) * our colStride, our nrow, our rowStride); |
| 643 | } |
| 644 | constvectorview<T> diagonal () const { |
| 645 | return constvectorview<T> (our firstCell, std::min (our nrow, our ncol), our rowStride + our colStride); |
| 646 | } |
| 647 | constmatrixview<T> verticalBand (integer firstColumn, integer lastColumn) const { |
| 648 | const integer newNcol = lastColumn - (firstColumn - 1); |
| 649 | if (newNcol <= 0) |
| 650 | return constmatrixview<T> (); |
| 651 | Melder_assert (firstColumn >= 1 && firstColumn <= our ncol); |
| 652 | Melder_assert (lastColumn >= 1 && lastColumn <= our ncol); |
| 653 | return constmatrixview<T> (our firstCell + (firstColumn - 1) * our colStride, |
| 654 | our nrow, newNcol, our rowStride, our colStride); |
| 655 | } |
| 656 | constmatrixview<T> part (integer firstRow, integer lastRow, integer firstColumn, integer lastColumn) const { |
| 657 | const integer newNrow = lastRow - (firstRow - 1), newNcol = lastColumn - (firstColumn - 1); |
| 658 | if (newNrow <= 0 || newNcol <= 0) |
| 659 | return constmatrixview<T> (); |
| 660 | Melder_assert (firstRow >= 1 && firstRow <= our nrow); |
| 661 | Melder_assert (lastRow >= 1 && lastRow <= our nrow); |
| 662 | Melder_assert (firstColumn >= 1 && firstColumn <= our ncol); |
| 663 | Melder_assert (lastColumn >= 1 && lastColumn <= our ncol); |
| 664 | return constmatrixview<T> ( |
| 665 | our firstCell |
| 666 | + (firstRow - 1) * our rowStride |
| 667 | + (firstColumn - 1) * our colStride, |
| 668 | newNrow, newNcol, |
| 669 | our rowStride, our colStride |
| 670 | ); |
| 671 | } |
| 672 | constmatrixview<T> transpose () const { |
| 673 | return constmatrixview<T> (our firstCell, our ncol, our nrow, our colStride, our rowStride); |
| 674 | } |
| 675 | }; |
| 676 | |
| 677 | /* |
| 678 | An automatrix is the sole owner of its payload, which is a matrix. |
| 679 | When the automatrix ends its life (goes out of scope), |
| 680 | it should destroy its payload (if it has not sold it), |
| 681 | because keeping a payload alive when the owner is dead |
| 682 | would continue to use some of the computer's resources (namely, memory). |
| 683 | */ |
| 684 | template <typename T> |
| 685 | struct automatrix { |
| 686 | T *cells = nullptr; |
| 687 | integer nrow = 0, ncol = 0; |
| 688 | automatrix () // come into existence without a payload |
| 689 | = default; |
| 690 | explicit automatrix (integer givenNrow, integer givenNcol, MelderArray::kInitializationType initializationType) { // come into existence and manufacture a payload |
| 691 | Melder_assert (givenNrow >= 0); |
| 692 | Melder_assert (givenNcol >= 0); |
| 693 | our cells = MelderArray:: _alloc <T> (givenNrow * givenNcol, initializationType); |
| 694 | our nrow = givenNrow; |
| 695 | our ncol = givenNcol; |
| 696 | } |
| 697 | automatrix (std::initializer_list <std::initializer_list <T>> list) { |
| 698 | our nrow = uinteger_to_integer_a (list.size()); |
| 699 | Melder_assert (our nrow > 0); // empty matrices should be created with automatrix<T>() or automatrix<T> (0, 10) or so |
| 700 | our ncol = uinteger_to_integer_a (list.begin()->size()); |
| 701 | Melder_assert (our ncol > 0); // empty matrices should be created with automatrix<T>() or automatrix<T> (10, 0) or so |
| 702 | our cells = MelderArray:: _alloc <T> (our nrow * our ncol, MelderArray::kInitializationType::RAW); |
| 703 | T *p = our cells; |
| 704 | for (auto row : list) { |
| 705 | const integer numberOfColumnsInThisRow = uinteger_to_integer_a (row.size()); |
| 706 | Melder_assert (numberOfColumnsInThisRow == our ncol); // unfortunately, no support for static_assert here in C++17 |
| 707 | for (auto cell : row) |
| 708 | * (p ++) = cell; |
| 709 | } |
| 710 | } |
| 711 | ~automatrix () { // destroy the payload (if any) |
| 712 | if (our cells) |
| 713 | MelderArray:: _free (our cells, our nrow * our ncol); |
| 714 | } |
| 715 | matrix<T> get() const { return matrix<T> (our cells, our nrow, our ncol); } // let the public use the payload (they may change the values in the cells but not the cell pointer, nrow or ncol) |
| 716 | matrixview<T> all () const { |
| 717 | return matrixview<T> (our cells, our nrow, our ncol, our ncol, 1); |
| 718 | } |
| 719 | void adoptFromAmbiguousOwner (matrix<T> const& given) { // buy the payload from a non-automatrix |
| 720 | our reset(); |
| 721 | our cells = given.cells; |
| 722 | our nrow = given.nrow; |
| 723 | our ncol = given.ncol; |
| 724 | } |
| 725 | matrix<T> releaseToAmbiguousOwner () { // sell the payload to a non-automatrix |
| 726 | T *oldCells = our cells; |
| 727 | our cells = nullptr; // disown ourselves, preventing automatic destruction of the payload |
| 728 | return matrix<T> (oldCells, our nrow, our ncol); |
| 729 | } |
| 730 | /* |
| 731 | Disable copying via construction or assignment (which would violate unique ownership of the payload). |
| 732 | */ |
| 733 | automatrix (automatrix const& other) // disable copy constructor |
| 734 | = delete; |
| 735 | automatrix& operator= (automatrix const& other) // disable copy assignment |
| 736 | = delete; |
| 737 | /* |
| 738 | Enable moving of r-values (temporaries, implicitly) or l-values (for variables, via an explicit move()). |
| 739 | This implements buying a payload from another automatrix (which involves destroying our current payload). |
| 740 | */ |
| 741 | automatrix (automatrix&& other) noexcept : cells (other.cells), nrow (other.nrow), ncol (other.ncol) // enable move constructor |
| 742 | { |
| 743 | other.cells = nullptr; // disown source |
| 744 | other.nrow = 0; // to keep the source in a valid state |
| 745 | other.ncol = 0; // to keep the source in a valid state |
| 746 | } |
| 747 | automatrix& operator= (automatrix&& other) noexcept { // enable move assignment |
| 748 | if (other.cells != our cells) { |
| 749 | if (our cells) |
| 750 | MelderArray:: _free (our cells, our nrow * our ncol); |
| 751 | our cells = other.cells; |
| 752 | our nrow = other.nrow; |
| 753 | our ncol = other.ncol; |
| 754 | other.cells = nullptr; // disown source |
| 755 | other.nrow = 0; // to keep the source in a valid state |
| 756 | other.ncol = 0; // to keep the source in a valid state |
| 757 | } else if (! our cells) { |
| 758 | our nrow = other.nrow; // yes; if both cells are null, we still have to copy the shape, which could be { 0, 2 } or so |
| 759 | our ncol = other.ncol; |
| 760 | } |
| 761 | return *this; |
| 762 | } |
| 763 | vector<T> operator[] (integer rowNumber) const { |
| 764 | return vector<T> (our cells + (rowNumber - 1) * our ncol, our ncol); |
| 765 | } |
| 766 | vector<T> row (integer rowNumber) const { |
| 767 | Melder_assert (rowNumber >= 1 && rowNumber <= our nrow); |
| 768 | Melder_assert (our cells || our ncol == 0); |
| 769 | return vector<T> (our cells + (rowNumber - 1) * our ncol, our ncol); |
| 770 | } |
| 771 | vectorview<T> column (integer columnNumber) const { |
| 772 | Melder_assert (columnNumber >= 1 && columnNumber <= our ncol); |
| 773 | return vectorview<T> (our cells + (columnNumber - 1), our nrow, our ncol); |
| 774 | } |
| 775 | vectorview<T> diagonal () const { |
| 776 | return vectorview<T> (our cells, std::min (our nrow, our ncol), our ncol + 1); |
| 777 | } |
| 778 | matrixview<T> horizontalBand (integer firstRow, integer lastRow) const { |
| 779 | const integer newNrow = lastRow - (firstRow - 1); |
| 780 | if (newNrow <= 0) |
| 781 | return matrixview<T> (); |
| 782 | Melder_assert (firstRow >= 1 && firstRow <= our nrow); |
| 783 | Melder_assert (lastRow >= 1 && lastRow <= our nrow); |
| 784 | return matrixview<T> (our cells + (firstRow - 1) * our ncol, newNrow, our ncol, our ncol, 1); |
| 785 | } |
| 786 | matrixview<T> verticalBand (integer firstColumn, integer lastColumn) const { |
| 787 | const integer newNcol = lastColumn - (firstColumn - 1); |
| 788 | if (newNcol <= 0) |
| 789 | return matrixview<T> (); |
| 790 | Melder_assert (firstColumn >= 1 && firstColumn <= our ncol); |
| 791 | Melder_assert (lastColumn >= 1 && lastColumn <= our ncol); |
| 792 | return matrixview<T> (our cells + (firstColumn - 1), our nrow, newNcol, our ncol, 1); |
| 793 | } |
| 794 | matrixview<T> part (integer firstRow, integer lastRow, integer firstColumn, integer lastColumn) const { |
| 795 | const integer newNrow = lastRow - (firstRow - 1), newNcol = lastColumn - (firstColumn - 1); |
| 796 | if (newNrow <= 0 || newNcol <= 0) |
| 797 | return matrixview<T> (); |
| 798 | Melder_assert (firstRow >= 1 && firstRow <= our nrow); |
| 799 | Melder_assert (lastRow >= 1 && lastRow <= our nrow); |
| 800 | Melder_assert (firstColumn >= 1 && firstColumn <= our ncol); |
| 801 | Melder_assert (lastColumn >= 1 && lastColumn <= our ncol); |
| 802 | return matrixview<T> ( |
| 803 | our cells + (firstRow - 1) * our ncol + (firstColumn - 1), |
| 804 | newNrow, newNcol, our ncol, 1 |
| 805 | ); |
| 806 | } |
| 807 | matrixview<T> transpose () const { |
| 808 | return matrixview<T> (our cells, our ncol, our nrow, 1, our ncol); |
| 809 | } |
| 810 | void resize (integer newNrow, integer newNcol, MelderArray::kInitializationType initializationType = MelderArray::kInitializationType::ZERO) { |
| 811 | if (newNrow > our nrow || newNcol > our ncol) { |
| 812 | integer const newNumberOfCells = newNrow * newNcol; |
| 813 | T *newCells = MelderArray:: _alloc <T> (newNumberOfCells, initializationType); |
| 814 | integer const numberOfRowsToCopy = std::min (our nrow, newNrow); |
| 815 | integer const numberOfColumnsToCopy = std::min (our ncol, newNcol); |
| 816 | for (integer irow = 1; irow <= numberOfRowsToCopy; irow ++) |
| 817 | for (integer icol = 1; icol <= numberOfColumnsToCopy; icol ++) |
| 818 | newCells [(irow - 1) * newNcol + (icol - 1)] = std::move (our cells [(irow - 1) * our ncol + (icol - 1)]); |
| 819 | if (our cells) |
| 820 | MelderArray:: _free (our cells, our nrow * our ncol); |
| 821 | our cells = newCells; |
| 822 | } else if (newNcol == our ncol) { |
| 823 | // do nothing |
| 824 | } else { |
| 825 | /* |
| 826 | The cells of the first new row already have the correct values. |
| 827 | */ |
| 828 | for (integer irow = 2; irow <= newNrow; irow ++) |
| 829 | for (integer icol = 1; icol <= newNcol; icol ++) |
| 830 | our cells [(irow - 1) * newNcol + (icol - 1)] = std::move (our cells [(irow - 1) * our ncol + (icol - 1)]); |
| 831 | } |
| 832 | our nrow = newNrow; |
| 833 | our ncol = newNcol; |
| 834 | } |
| 835 | void reset () noexcept { // on behalf of ambiguous owners (otherwise this could be in autoMAT) |
| 836 | if (our cells) { |
| 837 | MelderArray:: _free (our cells, our nrow * our ncol); |
| 838 | our cells = nullptr; |
| 839 | } |
| 840 | our nrow = 0; |
| 841 | our ncol = 0; |
| 842 | } |
| 843 | automatrix&& move () noexcept { return static_cast <automatrix&&> (*this); } |
| 844 | }; |
| 845 | |
| 846 | template <typename T> |
| 847 | automatrix<T> newmatrixraw (integer nrow, integer ncol) { |
| 848 | return automatrix<T> (nrow, ncol, MelderArray::kInitializationType::RAW); |
| 849 | } |
| 850 | template <typename T> |
| 851 | automatrix<T> newmatrixzero (integer nrow, integer ncol) { |
| 852 | return automatrix<T> (nrow, ncol, MelderArray::kInitializationType::ZERO); |
| 853 | } |
| 854 | template <typename T> |
| 855 | void matrixcopy (matrixview<T> const& target, constmatrixview<T> const& source) { |
| 856 | Melder_assert (source.nrow == target.nrow && source.ncol == target.ncol); |
| 857 | for (integer irow = 1; irow <= source.nrow; irow ++) |
| 858 | for (integer icol = 1; icol <= source.ncol; icol ++) |
| 859 | target [irow] [icol] = source [irow] [icol]; |
| 860 | } |
| 861 | template <typename T> |
| 862 | void matrixcopy (matrixview<T> const& target, matrixview<T> const& source) { |
| 863 | matrixcopy (target, constmatrixview<T> (source)); |
| 864 | } |
| 865 | template <typename T> |
| 866 | automatrix<T> newmatrixcopy (constmatrixview<T> const& source) { |
| 867 | automatrix<T> result = newmatrixraw<T> (source.nrow, source.ncol); |
| 868 | matrixcopy (result.all(), source); |
| 869 | return result; |
| 870 | } |
| 871 | template <typename T> |
| 872 | automatrix<T> newmatrixcopy (matrixview<T> const& source) { |
| 873 | return newmatrixcopy<T> (constmatrixview<T> (source)); |
| 874 | } |
| 875 | |
| 876 | template <typename T> |
| 877 | void assertCell (constvector<T> const& x, integer elementNumber) { |
| 878 | Melder_assert (elementNumber >= 1 && elementNumber <= x.size); |
| 879 | } |
| 880 | template <typename T> |
| 881 | void assertRow (constmatrix<T> const& x, integer rowNumber) { |
| 882 | Melder_assert (rowNumber >= 1 && rowNumber <= x.nrow); |
| 883 | } |
| 884 | template <typename T> |
| 885 | void assertColumn (constmatrix<T> const& x, integer columnNumber) { |
| 886 | Melder_assert (columnNumber >= 1 && columnNumber <= x.ncol); |
| 887 | } |
| 888 | template <typename T> |
| 889 | void assertCell (constmatrix<T> const& x, integer rowNumber, integer columnNumber) { |
| 890 | assertRow (x, rowNumber); |
| 891 | assertColumn (x, columnNumber); |
| 892 | } |
| 893 | template <typename T> |
| 894 | automatrix<T> newmatrixpart (constmatrix<T> const& x, integer firstRow, integer lastRow, integer firstColumn, integer lastColumn) { |
| 895 | assertCell (x, firstRow, firstColumn); |
| 896 | assertCell (x, lastRow, lastColumn); |
| 897 | integer numberOfRows = lastRow - (firstRow - 1); |
| 898 | Melder_assert (numberOfRows >= 0); |
| 899 | integer numberOfColumns = lastColumn - (firstColumn - 1); |
| 900 | Melder_assert (numberOfColumns >= 0); |
| 901 | automatrix<T> result = newmatrixraw<T> (numberOfRows, numberOfColumns); |
| 902 | for (integer irow = 1; irow <= numberOfRows; irow ++) |
| 903 | for (integer icol = 1; icol <= numberOfColumns; icol ++) |
| 904 | result [irow] [icol] = x [firstRow - 1 + irow] [firstColumn - 1 + icol]; |
| 905 | return result; |
| 906 | } |
| 907 | template <typename T> |
| 908 | automatrix<T> newmatrixpart (matrix<T> const& x, integer firstRow, integer lastRow, integer firstColumn, integer lastColumn) { |
| 909 | newmatrixpart (constmatrix<T> (x), firstRow, lastRow, firstColumn, lastColumn); |
| 910 | } |
| 911 | |
| 912 | template <typename T> |
| 913 | struct autotensor3; |
| 914 | |
| 915 | template <typename T> |
| 916 | struct tensor3 { |
| 917 | T * cells = nullptr; |
| 918 | integer ndim1 = 0, ndim2 = 0, ndim3 = 0; |
| 919 | integer stride1 = 0, stride2 = 0, stride3 = 1; |
| 920 | tensor3 () |
| 921 | = default; |
| 922 | tensor3 (autotensor3<T> const& other) |
| 923 | = delete; |
| 924 | explicit tensor3 (T * cells_, |
| 925 | integer ndim1_, integer ndim2_, integer ndim3_, |
| 926 | integer stride1_, integer stride2_, integer stride3_ |
| 927 | ) : |
| 928 | cells (cells_), |
| 929 | ndim1 (ndim1_), ndim2 (ndim2_), ndim3 (ndim3_), |
| 930 | stride1 (stride1_), stride2 (stride2_), stride3 (stride3_) |
| 931 | { } |
| 932 | matrixview<T> operator[] (integer dim1) const { |
| 933 | return matrixview<T> (our cells + (dim1 - 1) * our stride1, our ndim2, our ndim3, our stride2, our stride3); |
| 934 | } |
| 935 | vectorview<T> line1 (integer dim2, integer dim3) const { |
| 936 | Melder_assert (dim2 >= 1 && dim2 <= our ndim2); |
| 937 | Melder_assert (dim3 >= 1 && dim3 <= our ndim3); |
| 938 | return vectorview<T> ( |
| 939 | our cells |
| 940 | + (dim2 - 1) * our stride2 |
| 941 | + (dim3 - 1) * our stride3, |
| 942 | our ndim1, |
| 943 | our stride1 |
| 944 | ); |
| 945 | } |
| 946 | vectorview<T> line2 (integer dim1, integer dim3) const { |
| 947 | Melder_assert (dim1 >= 1 && dim1 <= our ndim1); |
| 948 | Melder_assert (dim3 >= 1 && dim3 <= our ndim3); |
| 949 | return vectorview<T> ( |
| 950 | our cells |
| 951 | + (dim1 - 1) * our stride1 |
| 952 | + (dim3 - 1) * our stride3, |
| 953 | our ndim2, |
| 954 | our stride2 |
| 955 | ); |
| 956 | } |
| 957 | vectorview<T> line3 (integer dim1, integer dim2) const { |
| 958 | Melder_assert (dim1 >= 1 && dim1 <= our ndim1); |
| 959 | Melder_assert (dim2 >= 1 && dim2 <= our ndim2); |
| 960 | return vectorview<T> ( |
| 961 | our cells |
| 962 | + (dim1 - 1) * our stride1 |
| 963 | + (dim2 - 1) * our stride2, |
| 964 | our ndim3, |
| 965 | our stride3 |
| 966 | ); |
| 967 | } |
| 968 | matrixview<T> plane12 (integer dim3) const { |
| 969 | Melder_assert (dim3 >= 1 && dim3 <= our ndim3); |
| 970 | return matrixview<T> ( |
| 971 | our cells |
| 972 | + (dim3 - 1) * our stride3, |
| 973 | our ndim1, our ndim2, |
| 974 | our stride1, our stride2 |
| 975 | ); |
| 976 | } |
| 977 | matrixview<T> plane13 (integer dim2) const { |
| 978 | Melder_assert (dim2 >= 1 && dim2 <= our ndim2); |
| 979 | return matrixview<T> ( |
| 980 | our cells |
| 981 | + (dim2 - 1) * our stride2, |
| 982 | our ndim1, our ndim3, |
| 983 | our stride1, our stride3 |
| 984 | ); |
| 985 | } |
| 986 | matrixview<T> plane23 (integer dim1) const { |
| 987 | Melder_assert (dim1 >= 1 && dim1 <= our ndim1); |
| 988 | return matrixview<T> ( |
| 989 | our cells |
| 990 | + (dim1 - 1) * our stride1, |
| 991 | our ndim2, our ndim3, |
| 992 | our stride2, our stride3 |
| 993 | ); |
| 994 | } |
| 995 | tensor3<T> part ( |
| 996 | integer firstDim1, integer lastDim1, |
| 997 | integer firstDim2, integer lastDim2, |
| 998 | integer firstDim3, integer lastDim3 |
| 999 | ) const { |
| 1000 | const integer newNdim1 = lastDim1 - (firstDim1 - 1); |
| 1001 | const integer newNdim2 = lastDim2 - (firstDim2 - 1); |
| 1002 | const integer newNdim3 = lastDim3 - (firstDim3 - 1); |
| 1003 | if (newNdim1 <= 0 || newNdim2 <= 0 || newNdim3 <= 0) |
| 1004 | return tensor3<T> (); |
| 1005 | Melder_assert (firstDim1 >= 1 && firstDim1 <= our ndim1); |
| 1006 | Melder_assert (lastDim1 >= 1 && lastDim1 <= our ndim1); |
| 1007 | Melder_assert (firstDim2 >= 1 && firstDim2 <= our ndim2); |
| 1008 | Melder_assert (lastDim2 >= 1 && lastDim2 <= our ndim2); |
| 1009 | Melder_assert (firstDim3 >= 1 && firstDim3 <= our ndim3); |
| 1010 | Melder_assert (lastDim3 >= 1 && lastDim3 <= our ndim3); |
| 1011 | return tensor3<T> ( |
| 1012 | our cells |
| 1013 | + (firstDim1 - 1) * our stride1 |
| 1014 | + (firstDim2 - 1) * our stride2 |
| 1015 | + (firstDim3 - 1) * our stride3, |
| 1016 | newNdim1, newNdim2, newNdim3, |
| 1017 | our stride1, our stride2, our stride3 |
| 1018 | ); |
| 1019 | } |
| 1020 | }; |
| 1021 | |
| 1022 | template <typename T> |
| 1023 | struct consttensor3 { |
| 1024 | const T * cells = nullptr; |
| 1025 | integer ndim1 = 0, ndim2 = 0, ndim3 = 0; |
| 1026 | integer stride1 = 0, stride2 = 0, stride3 = 1; |
| 1027 | consttensor3 () |
| 1028 | = default; |
| 1029 | consttensor3 (const autotensor3<T>& other) |
| 1030 | = delete; |
| 1031 | explicit consttensor3 (const T * cells_, |
| 1032 | integer ndim1_, integer ndim2_, integer ndim3_, |
| 1033 | integer stride1_, integer stride2_, integer stride3_ |
| 1034 | ) : |
| 1035 | cells (cells_), |
| 1036 | ndim1 (ndim1_), ndim2 (ndim2_), ndim3 (ndim3_), |
| 1037 | stride1 (stride1_), stride2 (stride2_), stride3 (stride3_) |
| 1038 | { } |
| 1039 | consttensor3 (tensor3<T> ten) : |
| 1040 | cells (ten.cells), |
| 1041 | ndim1 (ten.ndim1), ndim2 (ten.ndim2), ndim3 (ten.ndim3), |
| 1042 | stride1 (ten.stride1), stride2 (ten.stride2), stride3 (ten.stride3) |
| 1043 | { } |
| 1044 | constmatrixview<T> operator[] (integer dim1) const { |
| 1045 | return constmatrixview<T> (our cells + (dim1 - 1) * our stride1, our ndim2, our ndim3, our stride2, our stride3); |
| 1046 | } |
| 1047 | constvectorview<T> line1 (integer dim2, integer dim3) const { |
| 1048 | Melder_assert (dim2 >= 1 && dim2 <= our ndim2); |
| 1049 | Melder_assert (dim3 >= 1 && dim3 <= our ndim3); |
| 1050 | return constvectorview<T> ( |
| 1051 | our cells |
| 1052 | + (dim2 - 1) * our stride2 |
| 1053 | + (dim3 - 1) * our stride3, |
| 1054 | our ndim1, |
| 1055 | our stride1 |
| 1056 | ); |
| 1057 | } |
| 1058 | constvectorview<T> line2 (integer dim1, integer dim3) const { |
| 1059 | Melder_assert (dim1 >= 1 && dim1 <= our ndim1); |
| 1060 | Melder_assert (dim3 >= 1 && dim3 <= our ndim3); |
| 1061 | return constvectorview<T> ( |
| 1062 | our cells |
| 1063 | + (dim1 - 1) * our stride1 |
| 1064 | + (dim3 - 1) * our stride3, |
| 1065 | our ndim2, |
| 1066 | our stride2 |
| 1067 | ); |
| 1068 | } |
| 1069 | constvectorview<T> line3 (integer dim1, integer dim2) const { |
| 1070 | Melder_assert (dim1 >= 1 && dim1 <= our ndim1); |
| 1071 | Melder_assert (dim2 >= 1 && dim2 <= our ndim2); |
| 1072 | return constvectorview<T> ( |
| 1073 | our cells |
| 1074 | + (dim1 - 1) * our stride1 |
| 1075 | + (dim2 - 1) * our stride2, |
| 1076 | our ndim3, |
| 1077 | our stride3 |
| 1078 | ); |
| 1079 | } |
| 1080 | constmatrixview<T> plane12 (integer dim3) const { |
| 1081 | Melder_assert (dim3 >= 1 && dim3 <= our ndim3); |
| 1082 | return constmatrixview<T> ( |
| 1083 | our cells |
| 1084 | + (dim3 - 1) * our stride3, |
| 1085 | our ndim1, our ndim2, |
| 1086 | our stride1, our stride2 |
| 1087 | ); |
| 1088 | } |
| 1089 | constmatrixview<T> plane13 (integer dim2) const { |
| 1090 | Melder_assert (dim2 >= 1 && dim2 <= our ndim2); |
| 1091 | return constmatrixview<T> ( |
| 1092 | our cells |
| 1093 | + (dim2 - 1) * our stride2, |
| 1094 | our ndim1, our ndim3, |
| 1095 | our stride1, our stride3 |
| 1096 | ); |
| 1097 | } |
| 1098 | constmatrixview<T> plane23 (integer dim1) const { |
| 1099 | Melder_assert (dim1 >= 1 && dim1 <= our ndim1); |
| 1100 | return constmatrixview<T> ( |
| 1101 | our cells |
| 1102 | + (dim1 - 1) * our stride1, |
| 1103 | our ndim2, our ndim3, |
| 1104 | our stride2, our stride3 |
| 1105 | ); |
| 1106 | } |
| 1107 | consttensor3<T> part ( |
| 1108 | integer firstDim1, integer lastDim1, |
| 1109 | integer firstDim2, integer lastDim2, |
| 1110 | integer firstDim3, integer lastDim3 |
| 1111 | ) const { |
| 1112 | const integer newNdim1 = lastDim1 - (firstDim1 - 1); |
| 1113 | const integer newNdim2 = lastDim2 - (firstDim2 - 1); |
| 1114 | const integer newNdim3 = lastDim3 - (firstDim3 - 1); |
| 1115 | if (newNdim1 <= 0 || newNdim2 <= 0 || newNdim3 <= 0) |
| 1116 | return consttensor3<T> (); |
| 1117 | Melder_assert (firstDim1 >= 1 && firstDim1 <= our ndim1); |
| 1118 | Melder_assert (lastDim1 >= 1 && lastDim1 <= our ndim1); |
| 1119 | Melder_assert (firstDim2 >= 1 && firstDim2 <= our ndim2); |
| 1120 | Melder_assert (lastDim2 >= 1 && lastDim2 <= our ndim2); |
| 1121 | Melder_assert (firstDim3 >= 1 && firstDim3 <= our ndim3); |
| 1122 | Melder_assert (lastDim3 >= 1 && lastDim3 <= our ndim3); |
| 1123 | return consttensor3<T> ( |
| 1124 | our cells |
| 1125 | + (firstDim1 - 1) * our stride1 |
| 1126 | + (firstDim2 - 1) * our stride2 |
| 1127 | + (firstDim3 - 1) * our stride3, |
| 1128 | newNdim1, newNdim2, newNdim3, |
| 1129 | our stride1, our stride2, our stride3 |
| 1130 | ); |
| 1131 | } |
| 1132 | }; |
| 1133 | |
| 1134 | template <typename T> |
| 1135 | struct autotensor3 : public tensor3<T> { |
| 1136 | autotensor3 () = default; // come into existence without a payload |
| 1137 | explicit autotensor3 (integer givenNdim1, integer givenNdim2, integer givenNdim3, MelderArray::kInitializationType initializationType) { // come into existence and manufacture a payload |
| 1138 | Melder_assert (givenNdim1 >= 0); |
| 1139 | Melder_assert (givenNdim2 >= 0); |
| 1140 | Melder_assert (givenNdim3 >= 0); |
| 1141 | our cells = MelderArray:: _alloc <T> (givenNdim3 * givenNdim2 * givenNdim1, initializationType); |
| 1142 | our ndim1 = givenNdim1; |
| 1143 | our ndim2 = givenNdim2; |
| 1144 | our ndim3 = givenNdim3; |
| 1145 | our stride3 = 1; |
| 1146 | our stride2 = givenNdim3; |
| 1147 | our stride1 = givenNdim3 * givenNdim2; |
| 1148 | } |
| 1149 | ~autotensor3 () { // destroy the payload (if any) |
| 1150 | if (our cells) |
| 1151 | MelderArray:: _free (our cells, our ndim1 * our ndim2 * our ndim3); |
| 1152 | } |
| 1153 | //tensor3<T> get () { return { our at, our nrow, our ncol }; } // let the public use the payload (they may change the values in the cells but not the structure) |
| 1154 | const tensor3<T>& get () const { return *this; } // let the public use the payload (they may change the values in the cells but not the structure) |
| 1155 | void adoptFromAmbiguousOwner (tensor3<T> const& given) { // buy the payload from a non-automatrix |
| 1156 | our reset(); |
| 1157 | our cells = given.cells; |
| 1158 | our ndim1 = given.ndim1; |
| 1159 | our ndim2 = given.ndim2; |
| 1160 | our ndim3 = given.ndim3; |
| 1161 | our stride1 = given.stride1; |
| 1162 | our stride2 = given.stride2; |
| 1163 | our stride3 = given.stride3; |
| 1164 | } |
| 1165 | tensor3<T> releaseToAmbiguousOwner () { // sell the payload to a non-automatrix |
| 1166 | T * oldCells = our cells; |
| 1167 | our cells = nullptr; // disown ourselves, preventing automatic destruction of the payload |
| 1168 | return tensor3<T> (oldCells, our ndim1, our ndim2, our ndim3, our stride1, our stride2, our stride3); |
| 1169 | } |
| 1170 | /* |
| 1171 | Disable copying via construction or assignment (which would violate unique ownership of the payload). |
| 1172 | */ |
| 1173 | autotensor3 (const autotensor3&) = delete; // disable copy constructor |
| 1174 | autotensor3& operator= (const autotensor3&) = delete; // disable copy assignment |
| 1175 | /* |
| 1176 | Enable moving of r-values (temporaries, implicitly) or l-values (for variables, via an explicit move()). |
| 1177 | This implements buying a payload from another automatrix (which involves destroying our current payload). |
| 1178 | */ |
| 1179 | autotensor3 (autotensor3&& other) noexcept : tensor3<T> (other.get()) { // enable move constructor |
| 1180 | other.cells = nullptr; // disown source |
| 1181 | other.ndim1 = 0; // to keep the source in a valid state |
| 1182 | other.ndim2 = 0; // to keep the source in a valid state |
| 1183 | other.ndim3 = 0; // to keep the source in a valid state |
| 1184 | } |
| 1185 | autotensor3& operator= (autotensor3&& other) noexcept { // enable move assignment |
| 1186 | if (other.cells != our cells) { |
| 1187 | if (our cells) |
| 1188 | MelderArray:: _free (our cells, our ndim1 * our ndim2 * our ndim3); |
| 1189 | our cells = other.cells; |
| 1190 | our ndim1 = other.ndim1; |
| 1191 | our ndim2 = other.ndim2; |
| 1192 | our ndim3 = other.ndim3; |
| 1193 | our stride1 = other.stride1; |
| 1194 | our stride2 = other.stride2; |
| 1195 | our stride3 = other.stride3; |
| 1196 | other.cells = nullptr; // disown source |
| 1197 | other.ndim1 = 0; // to keep the source in a valid state |
| 1198 | other.ndim2 = 0; // to keep the source in a valid state |
| 1199 | other.ndim3 = 0; // to keep the source in a valid state |
| 1200 | } else if (! our cells) { |
| 1201 | our ndim1 = other.ndim1; // me and other may or may not be the same object |
| 1202 | our ndim2 = other.ndim2; |
| 1203 | our ndim3 = other.ndim3; |
| 1204 | } |
| 1205 | return *this; |
| 1206 | } |
| 1207 | void reset () noexcept { // on behalf of ambiguous owners (otherwise this could be in autoMAT) |
| 1208 | if (our cells) { |
| 1209 | MelderArray:: _free (our cells, our ndim1 * our ndim2 * our ndim3); |
| 1210 | our cells = nullptr; |
| 1211 | } |
| 1212 | our ndim1 = 0; |
| 1213 | our ndim2 = 0; |
| 1214 | our ndim3 = 0; |
| 1215 | } |
| 1216 | autotensor3&& move () noexcept { return static_cast <autotensor3&&> (*this); } |
| 1217 | }; |
| 1218 | template <typename T> |
| 1219 | autotensor3<T> newtensor3raw (integer ndim1, integer ndim2, integer ndim3) { |
| 1220 | return autotensor3<T> (ndim1, ndim2, ndim3, MelderArray::kInitializationType::RAW); |
| 1221 | } |
| 1222 | template <typename T> |
| 1223 | autotensor3<T> newtensor3zero (integer ndim1, integer ndim2, integer ndim3) { |
| 1224 | return autotensor3<T> (ndim1, ndim2, ndim3, MelderArray::kInitializationType::ZERO); |
| 1225 | } |
| 1226 | template <typename T> |
| 1227 | void tensor3copy (tensor3<T> const& target, consttensor3<T> const& source) { |
| 1228 | Melder_assert (source.ndim1 == target.ndim1 && source.ndim2 == target.ndim2 && source.ndim3 == target.ndim3); |
| 1229 | for (integer idim1 = 1; idim1 <= source.ndim1; idim1 ++) |
| 1230 | for (integer idim2 = 1; idim2 <= source.ndim2; idim2 ++) |
| 1231 | for (integer idim3 = 1; idim3 <= source.ndim3; idim3 ++) |
| 1232 | target [idim1] [idim2] [idim3] = source [idim1] [idim2] [idim3]; |
| 1233 | } |
| 1234 | template <typename T> |
| 1235 | void tensor3copy (tensor3<T> const& target, tensor3<T> const& source) { |
| 1236 | tensor3copy (target, consttensor3<T> (source)); |
| 1237 | } |
| 1238 | template <typename T> |
| 1239 | autotensor3<T> newtensor3copy (consttensor3<T> const& source) { |
| 1240 | autotensor3<T> result = newtensor3raw<T> (source.ndim1, source.ndim2, source.ndim3); |
| 1241 | tensor3copy (result.get(), source); |
| 1242 | return result; |
| 1243 | } |
| 1244 | template <typename T> |
| 1245 | autotensor3<T> newtensor3copy (tensor3<T> const& source) { |
| 1246 | return newtensor3copy<T> (consttensor3<T> (source)); |
| 1247 | } |
| 1248 | template <typename T> |
| 1249 | void assertDim1 (consttensor3<T> const& x, integer dim1) { |
| 1250 | Melder_assert (dim1 >= 1 && dim1 <= x.ndim1); |
| 1251 | } |
| 1252 | template <typename T> |
| 1253 | void assertDim2 (consttensor3<T> const& x, integer dim2) { |
| 1254 | Melder_assert (dim2 >= 1 && dim2 <= x.ndim2); |
| 1255 | } |
| 1256 | template <typename T> |
| 1257 | void assertDim3 (consttensor3<T> const& x, integer dim3) { |
| 1258 | Melder_assert (dim3 >= 1 && dim3 <= x.ndim3); |
| 1259 | } |
| 1260 | template <typename T> |
| 1261 | void assertCell (consttensor3<T> const& x, integer dim1, integer dim2, integer dim3) { |
| 1262 | assertDim1 (x, dim1); |
| 1263 | assertDim2 (x, dim2); |
| 1264 | assertDim3 (x, dim3); |
| 1265 | } |
| 1266 | template <typename T> |
| 1267 | autotensor3<T> newtensor3part (consttensor3<T> const& x, |
| 1268 | integer firstDim1, integer lastDim1, |
| 1269 | integer firstDim2, integer lastDim2, |
| 1270 | integer firstDim3, integer lastDim3 |
| 1271 | ) { |
| 1272 | assertCell (x, firstDim1, firstDim2, firstDim3); |
| 1273 | assertCell (x, lastDim1, lastDim2, lastDim3); |
| 1274 | integer ndim1 = lastDim1 - (firstDim1 - 1); |
| 1275 | Melder_assert (ndim1 >= 0); |
| 1276 | integer ndim2 = lastDim2 - (firstDim2 - 1); |
| 1277 | Melder_assert (ndim2 >= 0); |
| 1278 | integer ndim3 = lastDim3 - (firstDim3 - 1); |
| 1279 | Melder_assert (ndim3 >= 0); |
| 1280 | autotensor3<T> result = newtensor3raw<T> (ndim1, ndim2, ndim3); |
| 1281 | for (integer idim1 = 1; idim1 <= ndim1; idim1 ++) |
| 1282 | for (integer idim2 = 1; idim2 <= ndim2; idim2 ++) |
| 1283 | for (integer idim3 = 1; idim3 <= ndim3; idim3 ++) |
| 1284 | result [idim1] [idim2] [idim3] = x [firstDim1 - 1 + idim1] [firstDim2 - 1 + idim2] [firstDim3 - 1 + idim3]; |
| 1285 | return result; |
| 1286 | } |
| 1287 | template <typename T> |
| 1288 | autotensor3<T> newtensor3part (tensor3<T> const& x, |
| 1289 | integer firstDim1, integer lastDim1, |
| 1290 | integer firstDim2, integer lastDim2, |
| 1291 | integer firstDim3, integer lastDim3 |
| 1292 | ) { |
| 1293 | return newtensor3part (consttensor3<T> (x), firstDim1, lastDim1, firstDim2, lastDim2, firstDim3, lastDim3); |
| 1294 | } |
| 1295 | |
| 1296 | /* |
| 1297 | instead of vector<double> we say VEC, because we want to have a one-to-one |
| 1298 | relation between VEC functions and the scripting language. |
| 1299 | For instance, we have raw_VEC and zero_VEC because Praat scripting has raw# and zero#. |
| 1300 | */ |
| 1301 | using VEC = vector <double>; |
| 1302 | using VECVU = vectorview <double>; |
| 1303 | using constVEC = constvector <double>; |
| 1304 | using constVECVU = constvectorview <double>; |
| 1305 | using autoVEC = autovector <double>; |
| 1306 | inline autoVEC raw_VEC (integer size) { |
| 1307 | return newvectorraw <double> (size); |
| 1308 | } |
| 1309 | inline autoVEC zero_VEC (integer size) { |
| 1310 | return newvectorzero <double> (size); |
| 1311 | } |
| 1312 | inline autoVEC copy_VEC (constVECVU const& source) { |
| 1313 | return newvectorcopy (source); |
| 1314 | } |
| 1315 | #define ARRAY_TO_VEC(doubleArray) constVEC (& doubleArray [0], sizeof (doubleArray) / sizeof (double)) |
| 1316 | |
| 1317 | /* |
| 1318 | And simply because we use vector<integer> so much as well, |
| 1319 | we have an abbreviation for that as well, namely INTVEC. |
| 1320 | But the scripting language has nothing that corresponds to INTVEC, |
| 1321 | so any numeric vector to be used by the scripting language |
| 1322 | should be a VEC, even if it contains integers. |
| 1323 | This is fine, as a double can contain an integer up to 54 bits. |
| 1324 | */ |
| 1325 | using INTVEC = vector <integer>; |
| 1326 | using INTVECVU = vectorview <integer>; |
| 1327 | using constINTVEC = constvector <integer>; |
| 1328 | using constINTVECVU = constvectorview <integer>; |
| 1329 | using autoINTVEC = autovector <integer>; |
| 1330 | inline autoINTVEC raw_INTVEC (integer size) { |
| 1331 | return newvectorraw <integer> (size); |
| 1332 | } |
| 1333 | inline autoINTVEC zero_INTVEC (integer size) { |
| 1334 | return newvectorzero <integer> (size); |
| 1335 | } |
| 1336 | inline autoINTVEC copy_INTVEC (constINTVECVU const& source) { |
| 1337 | return newvectorcopy (source); |
| 1338 | } |
| 1339 | #define ARRAY_TO_INTVEC(integerArray) constINTVEC (& integerArray [0], sizeof (integerArray) / sizeof (integer)) |
| 1340 | |
| 1341 | using BOOLVEC = vector <bool>; |
| 1342 | using BOOLVECVU = vectorview <bool>; |
| 1343 | using constBOOLVEC = constvector <bool>; |
| 1344 | using constBOOLVECVU = constvectorview <bool>; |
| 1345 | using autoBOOLVEC = autovector <bool>; |
| 1346 | inline autoBOOLVEC raw_BOOLVEC (integer size) { |
| 1347 | return newvectorraw <bool> (size); |
| 1348 | } |
| 1349 | inline autoBOOLVEC zero_BOOLVEC (integer size) { |
| 1350 | return newvectorzero <bool> (size); |
| 1351 | } |
| 1352 | inline autoBOOLVEC copy_BOOLVEC (constBOOLVECVU const& source) { |
| 1353 | return newvectorcopy (source); |
| 1354 | } |
| 1355 | #define ARRAY_TO_BOOLVEC(boolArray) constBOOLVEC (& boolArray [0], sizeof (boolArray) / sizeof (bool)) |
| 1356 | |
| 1357 | using BYTEVEC = vector <byte>; |
| 1358 | using BYTEVECVU = vectorview <byte>; |
| 1359 | using constBYTEVEC = constvector <byte>; |
| 1360 | using constBYTEVECVU = constvectorview <byte>; |
| 1361 | using autoBYTEVEC = autovector <byte>; |
| 1362 | inline autoBYTEVEC raw_BYTEVEC (integer size) { |
| 1363 | return newvectorraw <byte> (size); |
| 1364 | } |
| 1365 | inline autoBYTEVEC zero_BYTEVEC (integer size) { |
| 1366 | return newvectorzero <byte> (size); |
| 1367 | } |
| 1368 | inline autoBYTEVEC copy_BYTEVEC (constBYTEVECVU const& source) { |
| 1369 | return newvectorcopy (source); |
| 1370 | } |
| 1371 | #define ARRAY_TO_BYTEVEC(byteArray) constBYTEVEC (& byteArray [0], sizeof (byteArray) / sizeof (byte)) |
| 1372 | |
| 1373 | using COMPVEC = vector <dcomplex>; |
| 1374 | using COMPVECVU = vectorview <dcomplex>; |
| 1375 | using constCOMPVEC = constvector <dcomplex>; |
| 1376 | using constCOMPVECVU = constvectorview <dcomplex>; |
| 1377 | using autoCOMPVEC = autovector <dcomplex>; |
| 1378 | inline autoCOMPVEC raw_COMPVEC (integer size) { |
| 1379 | return newvectorraw <dcomplex> (size); |
| 1380 | } |
| 1381 | inline autoCOMPVEC zero_COMPVEC (integer size) { |
| 1382 | return newvectorzero <dcomplex> (size); |
| 1383 | } |
| 1384 | inline autoCOMPVEC copy_COMPVEC (constCOMPVECVU const& source) { |
| 1385 | return newvectorcopy (source); |
| 1386 | } |
| 1387 | #define ARRAY_TO_COMPVEC(dcomplexArray) constCOMPVEC (& dcomplexArray [0], sizeof (dcomplexArray) / sizeof (dcomplex)) |
| 1388 | |
| 1389 | using MAT = matrix <double>; |
| 1390 | using MATVU = matrixview <double>; |
| 1391 | using constMAT = constmatrix <double>; |
| 1392 | using constMATVU = constmatrixview <double>; |
| 1393 | using autoMAT = automatrix <double>; |
| 1394 | inline autoMAT raw_MAT (integer nrow, integer ncol) { |
| 1395 | return newmatrixraw <double> (nrow, ncol); |
| 1396 | } |
| 1397 | inline autoMAT zero_MAT (integer nrow, integer ncol) { |
| 1398 | return newmatrixzero <double> (nrow, ncol); |
| 1399 | } |
| 1400 | inline autoMAT copy_MAT (constMATVU const& source) { |
| 1401 | return newmatrixcopy (source); |
| 1402 | } |
| 1403 | inline autoMAT part_MAT (constMAT const& source, |
| 1404 | integer firstRow, integer lastRow, |
| 1405 | integer firstColumn, integer lastColumn |
| 1406 | ) { |
| 1407 | return newmatrixpart (source, firstRow, lastRow, firstColumn, lastColumn); |
| 1408 | } |
| 1409 | |
| 1410 | using TEN3 = tensor3 <double>; |
| 1411 | using constTEN3 = consttensor3 <double>; |
| 1412 | using autoTEN3 = autotensor3 <double>; |
| 1413 | inline autoTEN3 raw_TEN3 (integer ndim1, integer ndim2, integer ndim3) { |
| 1414 | return newtensor3raw <double> (ndim1, ndim2, ndim3); |
| 1415 | } |
| 1416 | inline autoTEN3 zero_TEN3 (integer ndim1, integer ndim2, integer ndim3) { |
| 1417 | return newtensor3zero <double> (ndim1, ndim2, ndim3); |
| 1418 | } |
| 1419 | inline autoTEN3 copy_TEN3 (constTEN3 source) { |
| 1420 | return newtensor3copy (source); |
| 1421 | } |
| 1422 | inline autoTEN3 part_TEN3 (const constTEN3& source, |
| 1423 | integer firstDim1, integer lastDim1, |
| 1424 | integer firstDim2, integer lastDim2, |
| 1425 | integer firstDim3, integer lastDim3 |
| 1426 | ) { |
| 1427 | return newtensor3part (source, firstDim1, lastDim1, firstDim2, lastDim2, firstDim3, lastDim3); |
| 1428 | } |
| 1429 | |
| 1430 | using INTMAT = matrix <integer>; |
| 1431 | using INTMATVU = matrixview <integer>; |
| 1432 | using constINTMAT = constmatrix <integer>; |
| 1433 | using constINTMATVU = constmatrixview <integer>; |
| 1434 | using autoINTMAT = automatrix <integer>; |
| 1435 | inline autoINTMAT raw_INTMAT (integer nrow, integer ncol) { |
| 1436 | return newmatrixraw <integer> (nrow, ncol); |
| 1437 | } |
| 1438 | inline autoINTMAT zero_INTMAT (integer nrow, integer ncol) { |
| 1439 | return newmatrixzero <integer> (nrow, ncol); |
| 1440 | } |
| 1441 | inline autoINTMAT copy_INTMAT (constINTMATVU source) { |
| 1442 | return newmatrixcopy (source); |
| 1443 | } |
| 1444 | |
| 1445 | using BOOLMAT = matrix <bool>; |
| 1446 | using BOOLMATVU = matrixview <bool>; |
| 1447 | using constBOOLMAT = constmatrix <bool>; |
| 1448 | using constBOOLMATVU = constmatrixview <bool>; |
| 1449 | using autoBOOLMAT = automatrix <bool>; |
| 1450 | inline autoBOOLMAT raw_BOOLMAT (integer nrow, integer ncol) { |
| 1451 | return newmatrixraw <bool> (nrow, ncol); |
| 1452 | } |
| 1453 | inline autoBOOLMAT zero_BOOLMAT (integer nrow, integer ncol) { |
| 1454 | return newmatrixzero <bool> (nrow, ncol); |
| 1455 | } |
| 1456 | inline autoBOOLMAT copy_BOOLMAT (constBOOLMATVU source) { |
| 1457 | return newmatrixcopy (source); |
| 1458 | } |
| 1459 | |
| 1460 | using BYTEMAT = matrix <byte>; |
| 1461 | using BYTEMATVU = matrixview <byte>; |
| 1462 | using constBYTEMAT = constmatrix <byte>; |
| 1463 | using constBYTEMATVU = constmatrixview <byte>; |
| 1464 | using autoBYTEMAT = automatrix <byte>; |
| 1465 | inline autoBYTEMAT raw_BYTEMAT (integer nrow, integer ncol) { |
| 1466 | return newmatrixraw <byte> (nrow, ncol); |
| 1467 | } |
| 1468 | inline autoBYTEMAT zero_BYTEMAT (integer nrow, integer ncol) { |
| 1469 | return newmatrixzero <byte> (nrow, ncol); |
| 1470 | } |
| 1471 | inline autoBYTEMAT copy_BYTEMAT (constBYTEMATVU source) { |
| 1472 | return newmatrixcopy (source); |
| 1473 | } |
| 1474 | |
| 1475 | conststring32 Melder_VEC (constVECVU const& value, bool horizontal = false); |
| 1476 | conststring32 Melder_fixed (constVECVU const& value, integer precision, bool horizontal = false); |
| 1477 | conststring32 Melder_MAT (constMATVU const& value); |
| 1478 | conststring32 Melder_fixed (constMATVU const& value, integer precision); |
| 1479 | |
| 1480 | inline void operator<<= (INTVECVU const& target, constINTVECVU const& source) { |
| 1481 | Melder_assert (target.size == source.size); |
| 1482 | for (integer i = 1; i <= target.size; i ++) |
| 1483 | target [i] = source [i]; |
| 1484 | } |
| 1485 | inline void operator<<= (BOOLVECVU const& target, constBOOLVECVU const& source) { |
| 1486 | Melder_assert (target.size == source.size); |
| 1487 | for (integer i = 1; i <= target.size; i ++) |
| 1488 | target [i] = source [i]; |
| 1489 | } |
| 1490 | inline void operator<<= (TEN3 const& target, constTEN3 const& source) { |
| 1491 | Melder_assert (target.ndim1 == source.ndim1); |
| 1492 | Melder_assert (target.ndim2 == source.ndim2); |
| 1493 | Melder_assert (target.ndim3 == source.ndim3); |
| 1494 | for (integer idim1 = 1; idim1 <= target.ndim1; idim1 ++) |
| 1495 | for (integer idim2 = 1; idim2 <= target.ndim2; idim2 ++) |
| 1496 | for (integer idim3 = 1; idim3 <= target.ndim3; idim3 ++) |
| 1497 | target [idim1] [idim2] [idim3] = source [idim1] [idim2] [idim3]; |
| 1498 | } |
| 1499 | |
| 1500 | /* End of file melder_tensor.h */ |
| 1501 | #endif |