| 1 | /* TableOfReal.cpp |
| 2 | * |
| 3 | * Copyright (C) 1992-2024,2026 Paul Boersma |
| 4 | * |
| 5 | * This code is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation; either version 3 of the License, or (at |
| 8 | * your option) any later version. |
| 9 | * |
| 10 | * This code is distributed in the hope that it will be useful, but |
| 11 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 13 | * See the GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this work. If not, see <http://www.gnu.org/licenses/>. |
| 17 | */ |
| 18 | |
| 19 | #include "TableOfReal.h" |
| 20 | #include "NUM2.h" |
| 21 | #include "Matrix.h" |
| 22 | #include "Formula.h" |
| 23 | |
| 24 | #include "oo_DESTROY.h" |
| 25 | #include "TableOfReal_def.h" |
| 26 | #include "oo_COPY.h" |
| 27 | #include "TableOfReal_def.h" |
| 28 | #include "oo_EQUAL.h" |
| 29 | #include "TableOfReal_def.h" |
| 30 | #include "oo_CAN_WRITE_AS_ENCODING.h" |
| 31 | #include "TableOfReal_def.h" |
| 32 | #include "oo_WRITE_BINARY.h" |
| 33 | #include "TableOfReal_def.h" |
| 34 | #include "oo_READ_BINARY.h" |
| 35 | #include "TableOfReal_def.h" |
| 36 | #include "oo_DESCRIPTION.h" |
| 37 | #include "TableOfReal_def.h" |
| 38 | |
| 39 | Thing_implement (TableOfReal, Daata, 0); |
| 40 | Thing_implement (TableOfRealList, Ordered, 0); |
| 41 | |
| 42 | static void fprintquotedstring (MelderFile file, conststring32 s) { |
| 43 | MelderFile_writeCharacter (file, U'\"'); |
| 44 | if (s) { char32 c; while ((c = *s ++) != U'\0') { MelderFile_writeCharacter (file, c); if (c == U'\"') MelderFile_writeCharacter (file, c); } } |
| 45 | MelderFile_writeCharacter (file, U'\"'); |
| 46 | } |
| 47 | |
| 48 | void structTableOfReal :: v1_writeText (MelderFile file) { |
| 49 | texputi32 (file, our numberOfColumns, U"numberOfColumns"); |
| 50 | MelderFile_write (file, U"\ncolumnLabels []: "); |
| 51 | if (our numberOfColumns < 1) MelderFile_write (file, U"(empty)"); |
| 52 | MelderFile_write (file, U"\n"); |
| 53 | for (integer i = 1; i <= our numberOfColumns; i ++) { |
| 54 | fprintquotedstring (file, our columnLabels [i].get()); |
| 55 | MelderFile_writeCharacter (file, U'\t'); |
| 56 | } |
| 57 | texputi32 (file, our numberOfRows, U"numberOfRows"); |
| 58 | for (integer i = 1; i <= our numberOfRows; i ++) { |
| 59 | MelderFile_write (file, U"\nrow [", i, U"]: "); |
| 60 | fprintquotedstring (file, our rowLabels [i].get()); |
| 61 | for (integer j = 1; j <= our numberOfColumns; j ++) { |
| 62 | double x = our data [i] [j]; |
| 63 | MelderFile_write (file, U"\t", x); |
| 64 | } |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | void structTableOfReal :: v1_readText (MelderReadText a_text, int /*formatVersion*/) { |
| 69 | our numberOfColumns = texgeti32 (a_text); |
| 70 | if (our numberOfColumns >= 1) { |
| 71 | our columnLabels = autoSTRVEC (our numberOfColumns); |
| 72 | for (integer i = 1; i <= our numberOfColumns; i ++) |
| 73 | our columnLabels [i] = texgetw16 (a_text); |
| 74 | } |
| 75 | our numberOfRows = texgeti32 (a_text); |
| 76 | if (our numberOfRows >= 1) { |
| 77 | our rowLabels = autoSTRVEC (our numberOfRows); |
| 78 | } |
| 79 | if (our numberOfRows >= 1 && our numberOfColumns >= 1) { |
| 80 | our data = zero_MAT (our numberOfRows, our numberOfColumns); |
| 81 | for (integer i = 1; i <= our numberOfRows; i ++) { |
| 82 | our rowLabels [i] = texgetw16 (a_text); |
| 83 | for (integer j = 1; j <= our numberOfColumns; j ++) |
| 84 | our data [i] [j] = texgetr64 (a_text); |
| 85 | } |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | void structTableOfReal :: v1_info () { |
| 90 | structDaata :: v1_info (); |
| 91 | MelderInfo_writeLine (U"Number of rows: ", our numberOfRows); |
| 92 | MelderInfo_writeLine (U"Number of columns: ", our numberOfColumns); |
| 93 | } |
| 94 | |
| 95 | conststring32 structTableOfReal :: v_getRowStr (const integer irow) const { |
| 96 | if (irow < 1 || irow > our numberOfRows) |
| 97 | return nullptr; |
| 98 | return our rowLabels [irow] ? our rowLabels [irow].get() : U""; |
| 99 | } |
| 100 | conststring32 structTableOfReal :: v_getColStr (const integer icol) const { |
| 101 | if (icol < 1 || icol > our numberOfColumns) |
| 102 | return nullptr; |
| 103 | return our columnLabels [icol] ? our columnLabels [icol].get() : U""; |
| 104 | } |
| 105 | double structTableOfReal :: v_getMatrix (const integer irow, const integer icol) const { |
| 106 | if (irow < 1 || irow > our numberOfRows) |
| 107 | return undefined; |
| 108 | if (icol < 1 || icol > our numberOfColumns) |
| 109 | return undefined; |
| 110 | return our data [irow] [icol]; |
| 111 | } |
| 112 | double structTableOfReal :: v_getRowIndex (const conststring32 rowLabel) const { |
| 113 | return TableOfReal_rowLabelToIndex (this, rowLabel); |
| 114 | } |
| 115 | double structTableOfReal :: v_getColIndex (const conststring32 columnLabel) const { |
| 116 | return TableOfReal_columnLabelToIndex (this, columnLabel); |
| 117 | } |
| 118 | |
| 119 | void TableOfReal_init (TableOfReal me, integer numberOfRows, integer numberOfColumns) { |
| 120 | if (numberOfRows < 1 || numberOfColumns < 1) |
| 121 | Melder_throw (U"Cannot create cell-less table."); |
| 122 | my numberOfRows = numberOfRows; |
| 123 | my numberOfColumns = numberOfColumns; |
| 124 | my rowLabels = autoSTRVEC (numberOfRows); |
| 125 | Melder_assert (my rowLabels.size == numberOfRows); // probably captured by test script |
| 126 | my columnLabels = autoSTRVEC (numberOfColumns); |
| 127 | my data = zero_MAT (my numberOfRows, my numberOfColumns); |
| 128 | } |
| 129 | |
| 130 | autoTableOfReal TableOfReal_create (integer numberOfRows, integer numberOfColumns) { |
| 131 | try { |
| 132 | autoTableOfReal me = Thing_new (TableOfReal); |
| 133 | TableOfReal_init (me.get(), numberOfRows, numberOfColumns); |
| 134 | return me; |
| 135 | } catch (MelderError) { |
| 136 | Melder_throw (U"TableOfReal not created."); |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | /***** QUERY *****/ |
| 141 | |
| 142 | integer TableOfReal_rowLabelToIndex (const constTableOfReal me, const conststring32 label) { |
| 143 | for (integer irow = 1; irow <= my numberOfRows; irow ++) |
| 144 | if (my rowLabels [irow] && str32equ (my rowLabels [irow].get(), label)) |
| 145 | return irow; |
| 146 | return 0; |
| 147 | } |
| 148 | |
| 149 | integer TableOfReal_columnLabelToIndex (const constTableOfReal me, const conststring32 label) { |
| 150 | for (integer icol = 1; icol <= my numberOfColumns; icol ++) |
| 151 | if (my columnLabels [icol] && str32equ (my columnLabels [icol].get(), label)) |
| 152 | return icol; |
| 153 | return 0; |
| 154 | } |
| 155 | |
| 156 | double TableOfReal_getColumnMean (TableOfReal me, integer columnNumber) { |
| 157 | if (columnNumber < 1 || columnNumber > my numberOfColumns) |
| 158 | return undefined; |
| 159 | return NUMmean (my data.column (columnNumber)); |
| 160 | } |
| 161 | |
| 162 | double TableOfReal_getColumnStdev (TableOfReal me, integer columnNumber) { |
| 163 | if (columnNumber < 1 || columnNumber > my numberOfColumns) |
| 164 | return undefined; |
| 165 | if (my numberOfRows < 2) |
| 166 | return undefined; |
| 167 | return NUMstdev (my data.column (columnNumber)); |
| 168 | } |
| 169 | |
| 170 | /***** MODIFY *****/ |
| 171 | |
| 172 | void TableOfReal_removeRow (TableOfReal me, integer rowNumber) { |
| 173 | try { |
| 174 | Melder_require (my numberOfRows > 1, |
| 175 | me, U" has only one row, and a TableOfReal without rows cannot exist."); |
| 176 | Melder_require (rowNumber > 0 && rowNumber <= my numberOfRows, |
| 177 | U"No row ", rowNumber, U"."); |
| 178 | autoMAT newData = raw_MAT (my numberOfRows - 1, my numberOfColumns); |
| 179 | for (integer icol = 1; icol <= my numberOfColumns; icol ++) { |
| 180 | for (integer irow = 1; irow < rowNumber; irow ++) |
| 181 | newData [irow] [icol] = my data [irow] [icol]; |
| 182 | for (integer irow = rowNumber; irow < my numberOfRows; irow ++) |
| 183 | newData [irow] [icol] = my data [irow + 1] [icol]; |
| 184 | } |
| 185 | /* |
| 186 | Change without error. |
| 187 | */ |
| 188 | for (integer irow = rowNumber; irow < my numberOfRows; irow ++) |
| 189 | my rowLabels [irow] = my rowLabels [irow + 1]. move(); |
| 190 | my rowLabels [my numberOfRows]. reset(); |
| 191 | my rowLabels.size -= 1; |
| 192 | my data = newData.move(); |
| 193 | my numberOfRows --; |
| 194 | } catch (MelderError) { |
| 195 | Melder_throw (me, U": row ", rowNumber, U" not removed."); |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | void TableOfReal_insertRow (TableOfReal me, integer rowNumber) { |
| 200 | try { |
| 201 | if (rowNumber < 1 || rowNumber > my numberOfRows + 1) |
| 202 | Melder_throw (U"Cannot create row ", rowNumber, U"."); |
| 203 | /* |
| 204 | Create without change. |
| 205 | */ |
| 206 | autoMAT newData = zero_MAT (my numberOfRows + 1, my numberOfColumns); |
| 207 | autoSTRVEC newRowLabels (my numberOfRows + 1); |
| 208 | for (integer irow = 1; irow < rowNumber; irow ++) { |
| 209 | newRowLabels [irow] = my rowLabels [irow]. move(); |
| 210 | for (integer icol = 1; icol <= my numberOfColumns; icol ++) |
| 211 | newData [irow] [icol] = my data [irow] [icol]; |
| 212 | } |
| 213 | for (integer irow = my numberOfRows + 1; irow > rowNumber; irow --) { |
| 214 | newRowLabels [irow] = my rowLabels [irow - 1]. move(); |
| 215 | for (integer icol = 1; icol <= my numberOfColumns; icol ++) |
| 216 | newData [irow] [icol] = my data [irow - 1] [icol]; |
| 217 | } |
| 218 | /* |
| 219 | Change without error. |
| 220 | */ |
| 221 | my rowLabels = std::move (newRowLabels); |
| 222 | my data = newData.move(); |
| 223 | my numberOfRows ++; |
| 224 | } catch (MelderError) { |
| 225 | Melder_throw (me, U": row at position ", rowNumber, U" not inserted."); |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | void TableOfReal_removeColumn (TableOfReal me, integer columnNumber) { |
| 230 | try { |
| 231 | if (my numberOfColumns == 1) |
| 232 | Melder_throw (U"Cannot remove the only column."); |
| 233 | if (columnNumber < 1 || columnNumber > my numberOfColumns) |
| 234 | Melder_throw (U"No column ", columnNumber, U"."); |
| 235 | /* |
| 236 | Create without change. |
| 237 | */ |
| 238 | autoMAT newData = raw_MAT (my numberOfRows, my numberOfColumns - 1); |
| 239 | for (integer irow = 1; irow <= my numberOfRows; irow ++) { |
| 240 | for (integer icol = 1; icol < columnNumber; icol ++) |
| 241 | newData [irow] [icol] = my data [irow] [icol]; |
| 242 | for (integer icol = columnNumber; icol < my numberOfColumns; icol ++) |
| 243 | newData [irow] [icol] = my data [irow] [icol + 1]; |
| 244 | } |
| 245 | /* |
| 246 | Change without error. |
| 247 | */ |
| 248 | for (integer icol = columnNumber; icol < my numberOfColumns; icol ++) |
| 249 | my columnLabels [icol] = my columnLabels [icol + 1]. move(); |
| 250 | my columnLabels [my numberOfColumns]. reset(); |
| 251 | my columnLabels.size -= 1; |
| 252 | my data = newData.move(); |
| 253 | my numberOfColumns --; |
| 254 | } catch (MelderError) { |
| 255 | Melder_throw (me, U": column ", columnNumber, U" not removed."); |
| 256 | } |
| 257 | } |
| 258 | |
| 259 | void TableOfReal_insertColumn (TableOfReal me, integer columnNumber) { |
| 260 | try { |
| 261 | if (columnNumber < 1 || columnNumber > my numberOfColumns + 1) |
| 262 | Melder_throw (U"Cannot create column ", columnNumber, U"."); |
| 263 | /* |
| 264 | Create without change. |
| 265 | */ |
| 266 | autoMAT newData = zero_MAT (my numberOfRows, my numberOfColumns + 1); |
| 267 | autoSTRVEC newColumnLabels (my numberOfColumns + 1); |
| 268 | for (integer j = 1; j < columnNumber; j ++) { |
| 269 | newColumnLabels [j] = my columnLabels [j]. move(); |
| 270 | for (integer i = 1; i <= my numberOfRows; i ++) |
| 271 | newData [i] [j] = my data [i] [j]; |
| 272 | } |
| 273 | for (integer j = my numberOfColumns + 1; j > columnNumber; j --) { |
| 274 | newColumnLabels [j] = my columnLabels [j - 1]. move(); |
| 275 | for (integer i = 1; i <= my numberOfRows; i ++) |
| 276 | newData [i] [j] = my data [i] [j - 1]; |
| 277 | } |
| 278 | /* |
| 279 | Change without error. |
| 280 | */ |
| 281 | my columnLabels = std::move (newColumnLabels); |
| 282 | my data = newData.move(); |
| 283 | my numberOfColumns ++; |
| 284 | } catch (MelderError) { |
| 285 | Melder_throw (me, U": column at position ", columnNumber, U" not inserted."); |
| 286 | } |
| 287 | } |
| 288 | |
| 289 | void TableOfReal_setRowLabel (TableOfReal me, integer rowNumber, conststring32 label) { |
| 290 | try { |
| 291 | if (rowNumber < 1 || rowNumber > my numberOfRows) return; |
| 292 | my rowLabels [rowNumber] = Melder_dup (label); |
| 293 | } catch (MelderError) { |
| 294 | Melder_throw (me, U": label of row ", rowNumber, U" not set."); |
| 295 | } |
| 296 | } |
| 297 | |
| 298 | void TableOfReal_setColumnLabel (TableOfReal me, integer columnNumber, conststring32 label) { |
| 299 | try { |
| 300 | if (columnNumber < 1 || columnNumber > my numberOfColumns) |
| 301 | return; |
| 302 | my columnLabels [columnNumber] = Melder_dup (label); |
| 303 | } catch (MelderError) { |
| 304 | Melder_throw (me, U": label of column ", columnNumber, U" not set."); |
| 305 | } |
| 306 | } |
| 307 | |
| 308 | void TableOfReal_formula (TableOfReal me, conststring32 expression, Interpreter interpreter, TableOfReal thee) { |
| 309 | try { |
| 310 | Formula_compile (interpreter, me, expression, kFormula_EXPRESSION_TYPE_NUMERIC, true); |
| 311 | Formula_Result result; |
| 312 | if (! thee) |
| 313 | thee = me; |
| 314 | for (integer irow = 1; irow <= my numberOfRows; irow ++) { |
| 315 | for (integer icol = 1; icol <= my numberOfColumns; icol ++) { |
| 316 | Formula_run (irow, icol, & result); |
| 317 | thy data [irow] [icol] = result. numericResult; |
| 318 | } |
| 319 | } |
| 320 | } catch (MelderError) { |
| 321 | Melder_throw (me, U": formula not completed."); |
| 322 | } |
| 323 | } |
| 324 | |
| 325 | /***** EXTRACT PART *****/ |
| 326 | |
| 327 | static void copyRowLabels (TableOfReal me, TableOfReal thee) { |
| 328 | Melder_assert (me != thee); |
| 329 | Melder_assert (my numberOfRows == thy numberOfRows); |
| 330 | for (integer irow = 1; irow <= my numberOfRows; irow ++) |
| 331 | thy rowLabels [irow] = Melder_dup (my rowLabels [irow].get()); |
| 332 | } |
| 333 | |
| 334 | static void copyColumnLabels (TableOfReal me, TableOfReal thee) { |
| 335 | Melder_assert (me != thee); |
| 336 | Melder_assert (my numberOfColumns == thy numberOfColumns); |
| 337 | for (integer icol = 1; icol <= my numberOfColumns; icol ++) |
| 338 | thy columnLabels [icol] = Melder_dup (my columnLabels [icol].get()); |
| 339 | } |
| 340 | |
| 341 | static void copyRow (TableOfReal me, integer myRow, TableOfReal thee, integer thyRow) { |
| 342 | Melder_assert (me != thee); |
| 343 | Melder_assert (my numberOfColumns == thy numberOfColumns); |
| 344 | thy rowLabels [thyRow] = Melder_dup (my rowLabels [myRow].get()); |
| 345 | for (integer icol = 1; icol <= my numberOfColumns; icol ++) |
| 346 | thy data [thyRow] [icol] = my data [myRow] [icol]; |
| 347 | } |
| 348 | |
| 349 | static void copyColumn (TableOfReal me, integer myCol, TableOfReal thee, integer thyCol) { |
| 350 | Melder_assert (me != thee); |
| 351 | Melder_assert (my numberOfRows == thy numberOfRows); |
| 352 | thy columnLabels [thyCol] = Melder_dup (my columnLabels [myCol].get()); |
| 353 | for (integer irow = 1; irow <= my numberOfRows; irow ++) |
| 354 | thy data [irow] [thyCol] = my data [irow] [myCol]; |
| 355 | } |
| 356 | |
| 357 | autoTableOfReal TableOfReal_extractRowsWhereColumn (TableOfReal me, integer column, kMelder_number which, double criterion) { |
| 358 | try { |
| 359 | if (column < 1 || column > my numberOfColumns) |
| 360 | Melder_throw (U"No such column: ", column, U"."); |
| 361 | integer n = 0; |
| 362 | for (integer irow = 1; irow <= my numberOfRows; irow ++) |
| 363 | if (Melder_numberMatchesCriterion (my data [irow] [column], which, criterion)) |
| 364 | n ++; |
| 365 | if (n == 0) |
| 366 | Melder_throw (U"No row matches this criterion."); |
| 367 | autoTableOfReal thee = TableOfReal_create (n, my numberOfColumns); |
| 368 | copyColumnLabels (me, thee.get()); |
| 369 | n = 0; |
| 370 | for (integer irow = 1; irow <= my numberOfRows; irow ++) |
| 371 | if (Melder_numberMatchesCriterion (my data [irow] [column], which, criterion)) |
| 372 | copyRow (me, irow, thee.get(), ++ n); |
| 373 | return thee; |
| 374 | } catch (MelderError) { |
| 375 | Melder_throw (me, U": rows not extracted."); |
| 376 | } |
| 377 | } |
| 378 | |
| 379 | autoTableOfReal TableOfReal_extractRowsWhoseLabel (TableOfReal me, kMelder_string which, conststring32 criterion) { |
| 380 | try { |
| 381 | integer n = 0; |
| 382 | for (integer irow = 1; irow <= my numberOfRows; irow ++) |
| 383 | if (Melder_stringMatchesCriterion (my rowLabels [irow].get(), which, criterion, true)) |
| 384 | n ++; |
| 385 | if (n == 0) |
| 386 | Melder_throw (U"No row matches this criterion."); |
| 387 | autoTableOfReal thee = TableOfReal_create (n, my numberOfColumns); |
| 388 | copyColumnLabels (me, thee.get()); |
| 389 | n = 0; |
| 390 | for (integer irow = 1; irow <= my numberOfRows; irow ++) |
| 391 | if (Melder_stringMatchesCriterion (my rowLabels [irow].get(), which, criterion, true)) |
| 392 | copyRow (me, irow, thee.get(), ++ n); |
| 393 | return thee; |
| 394 | } catch (MelderError) { |
| 395 | Melder_throw (me, U": rows not extracted."); |
| 396 | } |
| 397 | } |
| 398 | |
| 399 | autoTableOfReal TableOfReal_extractColumnsWhereRow (TableOfReal me, integer row, kMelder_number which, double criterion) { |
| 400 | try { |
| 401 | if (row < 1 || row > my numberOfRows) |
| 402 | Melder_throw (U"No such row: ", row, U"."); |
| 403 | integer n = 0; |
| 404 | for (integer icol = 1; icol <= my numberOfColumns; icol ++) |
| 405 | if (Melder_numberMatchesCriterion (my data [row] [icol], which, criterion)) |
| 406 | n ++; |
| 407 | if (n == 0) |
| 408 | Melder_throw (U"No column matches this criterion."); |
| 409 | |
| 410 | autoTableOfReal thee = TableOfReal_create (my numberOfRows, n); |
| 411 | copyRowLabels (me, thee.get()); |
| 412 | n = 0; |
| 413 | for (integer icol = 1; icol <= my numberOfColumns; icol ++) |
| 414 | if (Melder_numberMatchesCriterion (my data [row] [icol], which, criterion)) |
| 415 | copyColumn (me, icol, thee.get(), ++ n); |
| 416 | return thee; |
| 417 | } catch (MelderError) { |
| 418 | Melder_throw (me, U": columns not extracted."); |
| 419 | } |
| 420 | } |
| 421 | |
| 422 | autoTableOfReal TableOfReal_extractColumnsWhoseLabel (TableOfReal me, kMelder_string which, conststring32 criterion) { |
| 423 | try { |
| 424 | integer n = 0; |
| 425 | for (integer icol = 1; icol <= my numberOfColumns; icol ++) |
| 426 | if (Melder_stringMatchesCriterion (my columnLabels [icol].get(), which, criterion, true)) |
| 427 | n ++; |
| 428 | if (n == 0) |
| 429 | Melder_throw (U"No column matches this criterion."); |
| 430 | |
| 431 | autoTableOfReal thee = TableOfReal_create (my numberOfRows, n); |
| 432 | copyRowLabels (me, thee.get()); |
| 433 | n = 0; |
| 434 | for (integer icol = 1; icol <= my numberOfColumns; icol ++) |
| 435 | if (Melder_stringMatchesCriterion (my columnLabels [icol].get(), which, criterion, true)) |
| 436 | copyColumn (me, icol, thee.get(), ++ n); |
| 437 | return thee; |
| 438 | } catch (MelderError) { |
| 439 | Melder_throw (me, U": columns not extracted."); |
| 440 | } |
| 441 | } |
| 442 | |
| 443 | void TableOfReal_checkRowNumberWithinRange (TableOfReal me, integer rowNumber) { |
| 444 | Melder_require (rowNumber >= 1 && rowNumber <= my data.nrow, |
| 445 | me, U": row number (", rowNumber, U") should be between 1 and my number of rows (", my data.nrow, U")."); |
| 446 | } |
| 447 | void TableOfReal_checkColumnNumberWithinRange (TableOfReal me, integer columnNumber) { |
| 448 | Melder_require (columnNumber >= 1 && columnNumber <= my data.ncol, |
| 449 | me, U": column number (", columnNumber, U") should be between 1 and my number of rows (", my data.ncol, U")."); |
| 450 | } |
| 451 | |
| 452 | autoTableOfReal TableOfReal_extractRowsByNumber (TableOfReal me, constINTVECVU const& rowNumbers) { |
| 453 | try { |
| 454 | autoTableOfReal you = TableOfReal_create (rowNumbers.size, my numberOfColumns); |
| 455 | copyColumnLabels (me, you.get()); |
| 456 | for (integer ielement = 1; ielement <= rowNumbers.size; ielement ++) { |
| 457 | TableOfReal_checkRowNumberWithinRange (me, rowNumbers [ielement]); |
| 458 | copyRow (me, rowNumbers [ielement], you.get(), ielement); |
| 459 | } |
| 460 | return you; |
| 461 | } catch (MelderError) { |
| 462 | Melder_throw (me, U": rows not extracted."); |
| 463 | } |
| 464 | } |
| 465 | |
| 466 | autoTableOfReal TableOfReal_extractColumnsByNumber (TableOfReal me, constINTVECVU const& columnNumbers) { |
| 467 | try { |
| 468 | autoTableOfReal you = TableOfReal_create (my numberOfRows, columnNumbers.size); |
| 469 | copyRowLabels (me, you.get()); |
| 470 | for (integer ielement = 1; ielement <= columnNumbers.size; ielement ++) { |
| 471 | TableOfReal_checkColumnNumberWithinRange (me, columnNumbers [ielement]); |
| 472 | copyColumn (me, columnNumbers [ielement], you.get(), ielement); |
| 473 | } |
| 474 | return you; |
| 475 | } catch (MelderError) { |
| 476 | Melder_throw (me, U": columns not extracted."); |
| 477 | } |
| 478 | } |
| 479 | |
| 480 | autoTableOfReal TableOfReal_extractRowsWhere (TableOfReal me, conststring32 condition, Interpreter interpreter) { |
| 481 | try { |
| 482 | Formula_compile (interpreter, me, condition, kFormula_EXPRESSION_TYPE_NUMERIC, true); |
| 483 | Formula_Result result; |
| 484 | /* |
| 485 | Count the new number of rows. |
| 486 | */ |
| 487 | integer numberOfElements = 0; |
| 488 | for (integer irow = 1; irow <= my numberOfRows; irow ++) { |
| 489 | for (integer icol = 1; icol <= my numberOfColumns; icol ++) { |
| 490 | Formula_run (irow, icol, & result); |
| 491 | if (result. numericResult != 0.0) { |
| 492 | numberOfElements ++; |
| 493 | break; |
| 494 | } |
| 495 | } |
| 496 | } |
| 497 | if (numberOfElements < 1) |
| 498 | Melder_throw (U"No rows match this condition."); |
| 499 | |
| 500 | /* |
| 501 | Create room for the result. |
| 502 | */ |
| 503 | autoTableOfReal thee = TableOfReal_create (numberOfElements, my numberOfColumns); |
| 504 | copyColumnLabels (me, thee.get()); |
| 505 | /* |
| 506 | Store the result. |
| 507 | */ |
| 508 | numberOfElements = 0; |
| 509 | for (integer irow = 1; irow <= my numberOfRows; irow ++) { |
| 510 | for (integer icol = 1; icol <= my numberOfColumns; icol ++) { |
| 511 | Formula_run (irow, icol, & result); |
| 512 | if (result. numericResult != 0.0) { |
| 513 | copyRow (me, irow, thee.get(), ++ numberOfElements); |
| 514 | break; |
| 515 | } |
| 516 | } |
| 517 | } |
| 518 | return thee; |
| 519 | } catch (MelderError) { |
| 520 | Melder_throw (me, U": rows not extracted."); |
| 521 | } |
| 522 | } |
| 523 | |
| 524 | autoTableOfReal TableOfReal_extractColumnsWhere (TableOfReal me, conststring32 condition, Interpreter interpreter) { |
| 525 | try { |
| 526 | Formula_compile (interpreter, me, condition, kFormula_EXPRESSION_TYPE_NUMERIC, true); |
| 527 | Formula_Result result; |
| 528 | /* |
| 529 | Count the new number of columns. |
| 530 | */ |
| 531 | integer numberOfElements = 0; |
| 532 | for (integer icol = 1; icol <= my numberOfColumns; icol ++) { |
| 533 | for (integer irow = 1; irow <= my numberOfRows; irow ++) { |
| 534 | Formula_run (irow, icol, & result); |
| 535 | if (result. numericResult != 0.0) { |
| 536 | numberOfElements ++; |
| 537 | break; |
| 538 | } |
| 539 | } |
| 540 | } |
| 541 | if (numberOfElements < 1) |
| 542 | Melder_throw (U"No columns match this condition."); |
| 543 | |
| 544 | /* |
| 545 | Create room for the result. |
| 546 | */ |
| 547 | autoTableOfReal thee = TableOfReal_create (my numberOfRows, numberOfElements); |
| 548 | copyRowLabels (me, thee.get()); |
| 549 | /* |
| 550 | Store the result. |
| 551 | */ |
| 552 | numberOfElements = 0; |
| 553 | for (integer icol = 1; icol <= my numberOfColumns; icol ++) { |
| 554 | for (integer irow = 1; irow <= my numberOfRows; irow ++) { |
| 555 | Formula_run (irow, icol, & result); |
| 556 | if (result. numericResult != 0.0) { |
| 557 | copyColumn (me, icol, thee.get(), ++ numberOfElements); |
| 558 | break; |
| 559 | } |
| 560 | } |
| 561 | } |
| 562 | return thee; |
| 563 | } catch (MelderError) { |
| 564 | Melder_throw (me, U": columns not extracted."); |
| 565 | } |
| 566 | } |
| 567 | |
| 568 | /***** EXTRACT *****/ |
| 569 | |
| 570 | autoStrings TableOfReal_extractRowLabelsAsStrings (TableOfReal me) { |
| 571 | try { |
| 572 | autoStrings thee = Thing_new (Strings); |
| 573 | thy strings = autoSTRVEC (my numberOfRows); |
| 574 | thy numberOfStrings = my numberOfRows; |
| 575 | for (integer irow = 1; irow <= my numberOfRows; irow ++) { |
| 576 | thy strings [irow] = Melder_dup (my rowLabels [irow] ? my rowLabels [irow].get() : U""); |
| 577 | } |
| 578 | return thee; |
| 579 | } catch (MelderError) { |
| 580 | Melder_throw (me, U": row labels not extracted."); |
| 581 | } |
| 582 | } |
| 583 | |
| 584 | autoStrings TableOfReal_extractColumnLabelsAsStrings (TableOfReal me) { |
| 585 | try { |
| 586 | autoStrings thee = Thing_new (Strings); |
| 587 | thy strings = autoSTRVEC (my numberOfColumns); |
| 588 | thy numberOfStrings = my numberOfColumns; |
| 589 | for (integer icol = 1; icol <= my numberOfColumns; icol ++) { |
| 590 | thy strings [icol] = Melder_dup (my columnLabels [icol] ? my columnLabels [icol].get() : U""); |
| 591 | } |
| 592 | return thee; |
| 593 | } catch (MelderError) { |
| 594 | Melder_throw (me, U": column labels not extracted."); |
| 595 | } |
| 596 | } |
| 597 | |
| 598 | /***** DRAW *****/ |
| 599 | |
| 600 | static void NUMrationalize (double x, integer *numerator, integer *denominator) { |
| 601 | constexpr double epsilon = 1e-6; |
| 602 | *numerator = 1; |
| 603 | for (*denominator = 1; *denominator <= 100000; (*denominator) ++) { |
| 604 | double numerator_d = x * *denominator; |
| 605 | integer rounded = Melder_iround (numerator_d); |
| 606 | if (fabs (rounded - numerator_d) < epsilon) { |
| 607 | *numerator = rounded; |
| 608 | return; |
| 609 | } |
| 610 | } |
| 611 | *denominator = 0; /* Failure. */ |
| 612 | } |
| 613 | |
| 614 | static void print4 (char *buffer, double value, int iformat, int width, int precision) { |
| 615 | char formatString [40]; |
| 616 | if (iformat == 4) { |
| 617 | integer numerator, denominator; |
| 618 | NUMrationalize (value, & numerator, & denominator); |
| 619 | if (numerator == 0) |
| 620 | snprintf (buffer, 40, "0"); |
| 621 | else if (denominator > 1) |
| 622 | snprintf (buffer, 40, "%s/%s", Melder8_integer (numerator), Melder8_integer (denominator)); |
| 623 | else |
| 624 | snprintf (buffer, 40, "%.7g", value); |
| 625 | } else { |
| 626 | snprintf (formatString, 40, "%%%d.%d%c", width, precision, iformat == 1 ? 'f' : iformat == 2 ? 'e' : 'g'); |
| 627 | snprintf (buffer, 40, formatString, value); |
| 628 | } |
| 629 | } |
| 630 | |
| 631 | static void fixRows (TableOfReal me, integer *rowmin, integer *rowmax) { |
| 632 | if (*rowmax < *rowmin) { *rowmin = 1; *rowmax = my numberOfRows; } |
| 633 | else if (*rowmin < 1) *rowmin = 1; |
| 634 | else if (*rowmax > my numberOfRows) *rowmax = my numberOfRows; |
| 635 | } |
| 636 | static void fixColumns (TableOfReal me, integer *colmin, integer *colmax) { |
| 637 | if (*colmax < *colmin) { *colmin = 1; *colmax = my numberOfColumns; } |
| 638 | else if (*colmin < 1) *colmin = 1; |
| 639 | else if (*colmax > my numberOfColumns) *colmax = my numberOfColumns; |
| 640 | } |
| 641 | static double getMaxRowLabelWidth (TableOfReal me, Graphics graphics, integer rowmin, integer rowmax) { |
| 642 | double maxWidth = 0.0; |
| 643 | if (! my rowLabels) return 0.0; |
| 644 | fixRows (me, & rowmin, & rowmax); |
| 645 | for (integer irow = rowmin; irow <= rowmax; irow ++) if (my rowLabels [irow] && my rowLabels [irow] [0]) { |
| 646 | double textWidth = Graphics_textWidth_ps (graphics, my rowLabels [irow].get(), true); // SILIPA is bigger than XIPA |
| 647 | if (textWidth > maxWidth) maxWidth = textWidth; |
| 648 | } |
| 649 | return maxWidth; |
| 650 | } |
| 651 | static double getLeftMargin (Graphics graphics) { |
| 652 | return Graphics_dxMMtoWC (graphics, 1.0); |
| 653 | } |
| 654 | static double getLineSpacing (Graphics graphics) { |
| 655 | return Graphics_dyMMtoWC (graphics, 1.5 * Graphics_inqFontSize (graphics) * 25.4 / 72.0); |
| 656 | } |
| 657 | static double getMaxColumnLabelHeight (TableOfReal me, Graphics graphics, integer colmin, integer colmax) { |
| 658 | double maxHeight = 0.0, lineSpacing = getLineSpacing (graphics); |
| 659 | if (! my columnLabels) return 0.0; |
| 660 | fixRows (me, & colmin, & colmax); |
| 661 | for (integer icol = colmin; icol <= colmax; icol ++) if (my columnLabels [icol] && my columnLabels [icol] [0]) { |
| 662 | if (maxHeight == 0.0) maxHeight = lineSpacing; |
| 663 | } |
| 664 | return maxHeight; |
| 665 | } |
| 666 | |
| 667 | void TableOfReal_drawAsNumbers (TableOfReal me, Graphics graphics, integer rowmin, integer rowmax, int iformat, int precision) { |
| 668 | fixRows (me, & rowmin, & rowmax); |
| 669 | Graphics_setInner (graphics); |
| 670 | Graphics_setWindow (graphics, 0.5, my numberOfColumns + 0.5, 0.0, 1.0); |
| 671 | const double leftMargin = getLeftMargin (graphics); // not earlier! |
| 672 | const double lineSpacing = getLineSpacing (graphics); // not earlier! |
| 673 | const double maxTextWidth = getMaxRowLabelWidth (me, graphics, rowmin, rowmax); |
| 674 | const double maxTextHeight = getMaxColumnLabelHeight (me, graphics, 1, my numberOfColumns); |
| 675 | |
| 676 | Graphics_setTextAlignment (graphics, Graphics_CENTRE, Graphics_BOTTOM); |
| 677 | for (integer icol = 1; icol <= my numberOfColumns; icol ++) |
| 678 | if (my columnLabels && my columnLabels [icol] && my columnLabels [icol] [0]) |
| 679 | Graphics_text (graphics, icol, 1, my columnLabels [icol].get()); |
| 680 | for (integer irow = rowmin; irow <= rowmax; irow ++) { |
| 681 | const double y = 1.0 - lineSpacing * (irow - rowmin + 0.6); |
| 682 | Graphics_setTextAlignment (graphics, Graphics_RIGHT, Graphics_HALF); |
| 683 | if (my rowLabels && my rowLabels [irow] && my rowLabels [irow] [0]) |
| 684 | Graphics_text (graphics, 0.5 - leftMargin, y, my rowLabels [irow].get()); |
| 685 | Graphics_setTextAlignment (graphics, Graphics_CENTRE, Graphics_HALF); |
| 686 | for (integer icol = 1; icol <= my numberOfColumns; icol ++) { |
| 687 | char text [40]; |
| 688 | print4 (text, my data [irow] [icol], iformat, 0, precision); |
| 689 | Graphics_text (graphics, icol, y, Melder_peek8to32_u (text)); |
| 690 | } |
| 691 | } |
| 692 | if (maxTextHeight != 0.0) { |
| 693 | double left = 0.5; |
| 694 | if (maxTextWidth > 0.0) |
| 695 | left -= maxTextWidth + 2 * leftMargin; |
| 696 | Graphics_line (graphics, left, 1.0, my numberOfColumns + 0.5, 1.0); |
| 697 | } |
| 698 | Graphics_unsetInner (graphics); |
| 699 | } |
| 700 | |
| 701 | void TableOfReal_drawAsNumbers_if (TableOfReal me, Graphics graphics, integer rowmin, integer rowmax, int iformat, int precision, |
| 702 | conststring32 conditionFormula, Interpreter interpreter) |
| 703 | { |
| 704 | try { |
| 705 | autoMatrix original = TableOfReal_to_Matrix (me); |
| 706 | autoMatrix conditions = Data_copy (original.get()); |
| 707 | fixRows (me, & rowmin, & rowmax); |
| 708 | Graphics_setInner (graphics); |
| 709 | Graphics_setWindow (graphics, 0.5, my numberOfColumns + 0.5, 0.0, 1.0); |
| 710 | const double leftMargin = getLeftMargin (graphics); // not earlier! |
| 711 | const double lineSpacing = getLineSpacing (graphics); // not earlier! |
| 712 | const double maxTextWidth = getMaxRowLabelWidth (me, graphics, rowmin, rowmax); |
| 713 | const double maxTextHeight = getMaxColumnLabelHeight (me, graphics, 1, my numberOfColumns); |
| 714 | Matrix_formula (original.get(), conditionFormula, interpreter, conditions.get()); |
| 715 | |
| 716 | Graphics_setTextAlignment (graphics, Graphics_CENTRE, Graphics_BOTTOM); |
| 717 | for (integer icol = 1; icol <= my numberOfColumns; icol ++) |
| 718 | if (my columnLabels && my columnLabels [icol] && my columnLabels [icol] [0]) |
| 719 | Graphics_text (graphics, icol, 1, my columnLabels [icol].get()); |
| 720 | for (integer irow = rowmin; irow <= rowmax; irow ++) { |
| 721 | const double y = 1.0 - lineSpacing * (irow - rowmin + 0.6); |
| 722 | Graphics_setTextAlignment (graphics, Graphics_RIGHT, Graphics_HALF); |
| 723 | if (my rowLabels && my rowLabels [irow] && my rowLabels [irow] [0]) |
| 724 | Graphics_text (graphics, 0.5 - leftMargin, y, my rowLabels [irow].get()); |
| 725 | Graphics_setTextAlignment (graphics, Graphics_CENTRE, Graphics_HALF); |
| 726 | for (integer icol = 1; icol <= my numberOfColumns; icol ++) if (conditions -> z [irow] [icol] != 0.0) { |
| 727 | char text [40]; |
| 728 | print4 (text, my data [irow] [icol], iformat, 0, precision); |
| 729 | Graphics_text (graphics, icol, y, Melder_peek8to32_u (text)); |
| 730 | } |
| 731 | } |
| 732 | if (maxTextHeight != 0.0) { |
| 733 | double left = 0.5; |
| 734 | if (maxTextWidth > 0.0) |
| 735 | left -= maxTextWidth + 2 * leftMargin; |
| 736 | Graphics_line (graphics, left, 1.0, my numberOfColumns + 0.5, 1.0); |
| 737 | } |
| 738 | Graphics_unsetInner (graphics); |
| 739 | } catch (MelderError) { |
| 740 | Melder_throw (me, U": numbers not drawn."); |
| 741 | } |
| 742 | } |
| 743 | |
| 744 | void TableOfReal_drawVerticalLines (TableOfReal me, Graphics graphics, integer rowmin, integer rowmax) { |
| 745 | integer colmin = 1, colmax = my numberOfColumns; |
| 746 | fixRows (me, & rowmin, & rowmax); |
| 747 | Graphics_setInner (graphics); |
| 748 | Graphics_setWindow (graphics, colmin - 0.5, colmax + 0.5, 0, 1); |
| 749 | const double lineSpacing = getLineSpacing (graphics); // not earlier! |
| 750 | const double maxTextWidth = getMaxRowLabelWidth (me, graphics, rowmin, rowmax); |
| 751 | const double maxTextHeight = getMaxColumnLabelHeight (me, graphics, 1, my numberOfColumns); |
| 752 | |
| 753 | if (maxTextWidth > 0.0) |
| 754 | colmin -= 1; |
| 755 | for (integer col = colmin + 1; col <= colmax; col ++) |
| 756 | Graphics_line (graphics, col - 0.5, 1.0 + maxTextHeight, col - 0.5, 1.0 - lineSpacing * (rowmax - rowmin + 1)); |
| 757 | Graphics_unsetInner (graphics); |
| 758 | } |
| 759 | |
| 760 | void TableOfReal_drawLeftAndRightLines (TableOfReal me, Graphics graphics, integer rowmin, integer rowmax) { |
| 761 | integer colmin = 1, colmax = my numberOfColumns; |
| 762 | fixRows (me, & rowmin, & rowmax); |
| 763 | Graphics_setInner (graphics); |
| 764 | Graphics_setWindow (graphics, colmin - 0.5, colmax + 0.5, 0.0, 1.0); |
| 765 | const double lineSpacing = getLineSpacing (graphics); |
| 766 | const double maxTextWidth = getMaxRowLabelWidth (me, graphics, rowmin, rowmax); |
| 767 | const double maxTextHeight = getMaxColumnLabelHeight (me, graphics, 1, my numberOfColumns); |
| 768 | |
| 769 | double left = 0.5; |
| 770 | if (maxTextWidth > 0.0) |
| 771 | left -= maxTextWidth + 2.0 * lineSpacing; |
| 772 | const double right = colmax + 0.5; |
| 773 | const double top = 1.0 + maxTextHeight; |
| 774 | const double bottom = 1.0 - lineSpacing * (rowmax - rowmin + 1); |
| 775 | Graphics_line (graphics, left, top, left, bottom); |
| 776 | Graphics_line (graphics, right, top, right, bottom); |
| 777 | Graphics_unsetInner (graphics); |
| 778 | } |
| 779 | |
| 780 | void TableOfReal_drawHorizontalLines (TableOfReal me, Graphics graphics, integer rowmin, integer rowmax) { |
| 781 | integer colmin = 1, colmax = my numberOfColumns; |
| 782 | fixRows (me, & rowmin, & rowmax); |
| 783 | Graphics_setInner (graphics); |
| 784 | Graphics_setWindow (graphics, colmin - 0.5, colmax + 0.5, 0, 1); |
| 785 | const double lineSpacing = getLineSpacing (graphics); |
| 786 | const double maxTextWidth = getMaxRowLabelWidth (me, graphics, rowmin, rowmax); |
| 787 | const double maxTextHeight = getMaxColumnLabelHeight (me, graphics, 1, my numberOfColumns); |
| 788 | |
| 789 | double left = 0.5; |
| 790 | const double top = rowmin; |
| 791 | if (maxTextWidth > 0.0) |
| 792 | left -= maxTextWidth + 2.0 * lineSpacing; |
| 793 | if (maxTextHeight > 0.0) |
| 794 | rowmin -= 1; |
| 795 | const double right = colmax + 0.5; |
| 796 | for (integer irow = rowmin; irow < rowmax; irow ++) { |
| 797 | const double y = 1.0 - lineSpacing * (irow - top + 1); |
| 798 | Graphics_line (graphics, left, y, right, y); |
| 799 | } |
| 800 | Graphics_unsetInner (graphics); |
| 801 | } |
| 802 | |
| 803 | void TableOfReal_drawTopAndBottomLines (TableOfReal me, Graphics graphics, integer rowmin, integer rowmax) { |
| 804 | integer colmin = 1, colmax = my numberOfColumns; |
| 805 | fixRows (me, & rowmin, & rowmax); |
| 806 | Graphics_setInner (graphics); |
| 807 | Graphics_setWindow (graphics, colmin - 0.5, colmax + 0.5, 0.0, 1.0); |
| 808 | const double lineSpacing = getLineSpacing (graphics); |
| 809 | const double maxTextWidth = getMaxRowLabelWidth (me, graphics, rowmin, rowmax); |
| 810 | const double maxTextHeight = getMaxColumnLabelHeight (me, graphics, 1, my numberOfColumns); |
| 811 | |
| 812 | double left = 0.5; |
| 813 | if (maxTextWidth > 0.0) |
| 814 | left -= maxTextWidth + 2 * lineSpacing; |
| 815 | const double right = colmax + 0.5; |
| 816 | const double top = 1.0 + maxTextHeight; |
| 817 | const double bottom = 1.0 - lineSpacing * (rowmax - rowmin + 1); |
| 818 | Graphics_line (graphics, left, top, right, top); |
| 819 | Graphics_line (graphics, left, bottom, right, bottom); |
| 820 | Graphics_unsetInner (graphics); |
| 821 | } |
| 822 | |
| 823 | void TableOfReal_drawAsSquares (TableOfReal me, Graphics graphics, integer rowmin, integer rowmax, |
| 824 | integer colmin, integer colmax, bool garnish) |
| 825 | { |
| 826 | const double dx = 1.0, dy = 1.0; |
| 827 | MelderColour colour = Graphics_inqColour (graphics); |
| 828 | fixRows (me, & rowmin, & rowmax); |
| 829 | fixColumns (me, & colmin, & colmax); |
| 830 | |
| 831 | Graphics_setInner (graphics); |
| 832 | Graphics_setWindow (graphics, colmin - 0.5, colmax + 0.5, rowmin - 0.5, rowmax + 0.5); |
| 833 | //const double datamax = NUMabsoluteExtremum (my data); TODO |
| 834 | double datamax = my data [rowmin] [colmin]; |
| 835 | for (integer irow = 1; irow <= my numberOfRows; irow ++) |
| 836 | for (integer icol = 1; icol <= my numberOfColumns; icol ++) |
| 837 | if (fabs (my data [irow] [icol]) > datamax) |
| 838 | datamax = fabs (my data [irow] [icol]); |
| 839 | |
| 840 | for (integer irow = rowmin; irow <= rowmax; irow ++) { |
| 841 | double y = rowmax + rowmin - irow; |
| 842 | for (integer icol = colmin; icol <= colmax; icol ++) { |
| 843 | const double x = icol; |
| 844 | /* two neighbouring squares should not touch -> 0.95 */ |
| 845 | const double d = 0.95 * sqrt (fabs (my data [irow] [icol]) / datamax); |
| 846 | const double x1WC = x - d * dx / 2.0, x2WC = x + d * dx / 2.0; |
| 847 | const double y1WC = y - d * dy / 2.0, y2WC = y + d * dy / 2.0; |
| 848 | if (my data [irow] [icol] > 0.0) |
| 849 | Graphics_setColour (graphics, Melder_WHITE); |
| 850 | Graphics_fillRectangle (graphics, x1WC, x2WC, y1WC, y2WC); |
| 851 | Graphics_setColour (graphics, colour); |
| 852 | Graphics_rectangle (graphics, x1WC, x2WC , y1WC, y2WC); |
| 853 | } |
| 854 | } |
| 855 | Graphics_setGrey (graphics, 0.0); |
| 856 | Graphics_unsetInner (graphics); |
| 857 | if (garnish) { |
| 858 | for (integer irow = rowmin; irow <= rowmax; irow ++) if (my rowLabels [irow]) |
| 859 | Graphics_markLeft (graphics, rowmax + rowmin - irow, false, false, false, my rowLabels [irow].get()); |
| 860 | for (integer icol = colmin; icol <= colmax; icol ++) if (my columnLabels [icol]) |
| 861 | Graphics_markTop (graphics, icol, false, false, false, my columnLabels [icol].get()); |
| 862 | } |
| 863 | } |
| 864 | |
| 865 | autoTableOfReal TablesOfReal_append (TableOfReal me, TableOfReal thee) { |
| 866 | try { |
| 867 | if (thy numberOfColumns != my numberOfColumns) |
| 868 | Melder_throw (U"Numbers of columns are ", my numberOfColumns, U" and ", thy numberOfColumns, U" but should be equal."); |
| 869 | autoTableOfReal him = Thing_newFromClass (my classInfo).static_cast_move <structTableOfReal> (); |
| 870 | TableOfReal_init (him.get(), my numberOfRows + thy numberOfRows, my numberOfColumns); |
| 871 | /* Unsafe: new attributes not initialized. */ |
| 872 | for (integer icol = 1; icol <= my numberOfColumns; icol ++) |
| 873 | TableOfReal_setColumnLabel (him.get(), icol, my columnLabels [icol].get()); |
| 874 | for (integer irow = 1; irow <= my numberOfRows; irow ++) { |
| 875 | TableOfReal_setRowLabel (him.get(), irow, my rowLabels [irow].get()); |
| 876 | for (integer icol = 1; icol <= my numberOfColumns; icol ++) |
| 877 | his data [irow] [icol] = my data [irow] [icol]; |
| 878 | } |
| 879 | for (integer irow = 1; irow <= thy numberOfRows; irow ++) { |
| 880 | integer hisRow = irow + my numberOfRows; |
| 881 | TableOfReal_setRowLabel (him.get(), hisRow, thy rowLabels [irow].get()); |
| 882 | for (integer icol = 1; icol <= my numberOfColumns; icol ++) |
| 883 | his data [hisRow] [icol] = thy data [irow] [icol]; |
| 884 | } |
| 885 | return him; |
| 886 | } catch (MelderError) { |
| 887 | Melder_throw (U"TableOfReal objects not appended."); |
| 888 | } |
| 889 | } |
| 890 | |
| 891 | autoTableOfReal TablesOfReal_appendMany (OrderedOf<structTableOfReal>* me) { |
| 892 | try { |
| 893 | if (my size == 0) |
| 894 | Melder_throw (U"Cannot add zero tables."); |
| 895 | TableOfReal thee = my at [1]; |
| 896 | integer totalNumberOfRows = thy numberOfRows; |
| 897 | integer numberOfColumns = thy numberOfColumns; |
| 898 | for (integer itab = 2; itab <= my size; itab ++) { |
| 899 | thee = my at [itab]; |
| 900 | totalNumberOfRows += thy numberOfRows; |
| 901 | if (thy numberOfColumns != numberOfColumns) |
| 902 | Melder_throw (U"Numbers of columns do not match."); |
| 903 | } |
| 904 | autoTableOfReal him = Thing_newFromClass (thy classInfo).static_cast_move <structTableOfReal>(); |
| 905 | TableOfReal_init (him.get(), totalNumberOfRows, numberOfColumns); |
| 906 | /* Unsafe: new attributes not initialized. */ |
| 907 | for (integer icol = 1; icol <= numberOfColumns; icol ++) |
| 908 | TableOfReal_setColumnLabel (him.get(), icol, thy columnLabels [icol].get()); |
| 909 | totalNumberOfRows = 0; |
| 910 | for (integer itab = 1; itab <= my size; itab ++) { |
| 911 | thee = my at [itab]; |
| 912 | for (integer irow = 1; irow <= thy numberOfRows; irow ++) { |
| 913 | totalNumberOfRows ++; |
| 914 | TableOfReal_setRowLabel (him.get(), totalNumberOfRows, thy rowLabels [irow].get()); |
| 915 | for (integer icol = 1; icol <= numberOfColumns; icol ++) |
| 916 | his data [totalNumberOfRows] [icol] = thy data [irow] [icol]; |
| 917 | } |
| 918 | } |
| 919 | Melder_assert (totalNumberOfRows == his numberOfRows); |
| 920 | return him; |
| 921 | } catch (MelderError) { |
| 922 | Melder_throw (U"TableOfReal objects not appended."); |
| 923 | } |
| 924 | } |
| 925 | |
| 926 | static void TableOfReal_sort (TableOfReal me, bool useLabels, integer column1, integer column2) { |
| 927 | for (integer irow = 1; irow < my numberOfRows; irow ++) for (integer jrow = irow + 1; jrow <= my numberOfRows; jrow ++) { |
| 928 | if (useLabels) { |
| 929 | if (my rowLabels [irow]) { |
| 930 | if (my rowLabels [jrow]) { |
| 931 | int compare = str32cmp (my rowLabels [irow].get(), my rowLabels [jrow].get()); |
| 932 | if (compare < 0) |
| 933 | continue; |
| 934 | if (compare > 0) |
| 935 | goto swap; |
| 936 | } else { |
| 937 | goto swap; |
| 938 | } |
| 939 | } else if (my rowLabels [jrow]) continue; |
| 940 | } |
| 941 | /* |
| 942 | * If we arrive here, the two labels are equal or both null (or useLabels is `false`). |
| 943 | */ |
| 944 | if (column1 > 0 && column1 <= my numberOfColumns) { |
| 945 | if (my data [irow] [column1] < my data [jrow] [column1]) |
| 946 | continue; |
| 947 | if (my data [irow] [column1] > my data [jrow] [column1]) |
| 948 | goto swap; |
| 949 | } |
| 950 | if (column2 > 0 && column2 <= my numberOfColumns) { |
| 951 | if (my data [irow] [column2] < my data [jrow] [column2]) |
| 952 | continue; |
| 953 | if (my data [irow] [column2] > my data [jrow] [column2]) |
| 954 | goto swap; |
| 955 | } |
| 956 | /* |
| 957 | * If we arrive here, everything is equal. |
| 958 | */ |
| 959 | continue; |
| 960 | swap: |
| 961 | std::swap (my rowLabels [irow], my rowLabels [jrow]); |
| 962 | for (integer icol = 1; icol <= my numberOfColumns; icol ++) { |
| 963 | double tmpValue = my data [irow] [icol]; |
| 964 | my data [irow] [icol] = my data [jrow] [icol]; |
| 965 | my data [jrow] [icol] = tmpValue; |
| 966 | } |
| 967 | } |
| 968 | } |
| 969 | |
| 970 | void TableOfReal_sortByLabel (TableOfReal me, integer column1, integer column2) { |
| 971 | TableOfReal_sort (me, true, column1, column2); |
| 972 | } |
| 973 | |
| 974 | void TableOfReal_sortByColumn (TableOfReal me, integer column1, integer column2) { |
| 975 | TableOfReal_sort (me, false, column1, column2); |
| 976 | } |
| 977 | |
| 978 | autoTableOfReal Table_to_TableOfReal (Table me, integer labelColumn) { |
| 979 | try { |
| 980 | if (labelColumn < 1 || labelColumn > my numberOfColumns) |
| 981 | labelColumn = 0; |
| 982 | autoTableOfReal thee = TableOfReal_create (my rows.size, labelColumn ? my numberOfColumns - 1 : my numberOfColumns); |
| 983 | for (integer icol = 1; icol <= my numberOfColumns; icol ++) |
| 984 | Table_numericize_a (me, icol); |
| 985 | if (labelColumn) { |
| 986 | for (integer icol = 1; icol < labelColumn; icol ++) |
| 987 | TableOfReal_setColumnLabel (thee.get(), icol, my columnHeaders [icol]. label.get()); |
| 988 | for (integer icol = labelColumn + 1; icol <= my numberOfColumns; icol ++) |
| 989 | TableOfReal_setColumnLabel (thee.get(), icol - 1, my columnHeaders [icol]. label.get()); |
| 990 | for (integer irow = 1; irow <= my rows.size; irow ++) { |
| 991 | TableRow row = my rows.at [irow]; |
| 992 | char32 *string = row -> cells [labelColumn]. string.get(); |
| 993 | TableOfReal_setRowLabel (thee.get(), irow, string ? string : U""); |
| 994 | for (integer icol = 1; icol < labelColumn; icol ++) |
| 995 | thy data [irow] [icol] = row -> cells [icol]. number; // Optimization. |
| 996 | //thy data [irow] [icol] = Table_getNumericValue_a (me, irow, icol); |
| 997 | for (integer icol = labelColumn + 1; icol <= my numberOfColumns; icol ++) |
| 998 | thy data [irow] [icol - 1] = row -> cells [icol]. number; // Optimization. |
| 999 | //thy data [irow] [icol - 1] = Table_getNumericValue_a (me, irow, icol); |
| 1000 | } |
| 1001 | } else { |
| 1002 | for (integer icol = 1; icol <= my numberOfColumns; icol ++) { |
| 1003 | TableOfReal_setColumnLabel (thee.get(), icol, my columnHeaders [icol]. label.get()); |
| 1004 | } |
| 1005 | for (integer irow = 1; irow <= my rows.size; irow ++) { |
| 1006 | TableRow row = my rows.at [irow]; |
| 1007 | for (integer icol = 1; icol <= my numberOfColumns; icol ++) { |
| 1008 | thy data [irow] [icol] = row -> cells [icol]. number; // Optimization. |
| 1009 | //thy data [irow] [icol] = Table_getNumericValue_a (me, irow, icol); |
| 1010 | } |
| 1011 | } |
| 1012 | } |
| 1013 | return thee; |
| 1014 | } catch (MelderError) { |
| 1015 | Melder_throw (me, U": not converted to TableOfReal."); |
| 1016 | } |
| 1017 | } |
| 1018 | |
| 1019 | autoTable TableOfReal_to_Table (TableOfReal me, conststring32 nameOfFirstColumn) { |
| 1020 | try { |
| 1021 | autoTable thee = Table_createWithoutColumnNames (my numberOfRows, my numberOfColumns + 1); |
| 1022 | Table_renameColumn_e (thee.get(), 1, nameOfFirstColumn); |
| 1023 | for (integer icol = 1; icol <= my numberOfColumns; icol ++) { |
| 1024 | conststring32 columnLabel = my columnLabels [icol].get(); |
| 1025 | thy columnHeaders [icol + 1]. label = Melder_dup (columnLabel && columnLabel [0] ? columnLabel : U"?"); |
| 1026 | } |
| 1027 | for (integer irow = 1; irow <= thy rows.size; irow ++) { |
| 1028 | conststring32 stringValue = my rowLabels [irow].get(); |
| 1029 | const TableRow row = thy rows.at [irow]; |
| 1030 | row -> cells [1]. string = Melder_dup (stringValue && stringValue [0] ? stringValue : U"?"); |
| 1031 | for (integer icol = 1; icol <= my numberOfColumns; icol ++) { |
| 1032 | const double numericValue = my data [irow] [icol]; |
| 1033 | row -> cells [icol + 1]. string = Melder_dup (Melder_double (numericValue)); |
| 1034 | } |
| 1035 | } |
| 1036 | return thee; |
| 1037 | } catch (MelderError) { |
| 1038 | Melder_throw (me, U": not converted to Table."); |
| 1039 | } |
| 1040 | } |
| 1041 | |
| 1042 | void TableOfReal_writeToHeaderlessSpreadsheetFile (TableOfReal me, MelderFile file) { |
| 1043 | try { |
| 1044 | autoMelderString buffer; |
| 1045 | MelderString_copy (& buffer, U"rowLabel"); |
| 1046 | for (integer icol = 1; icol <= my numberOfColumns; icol ++) { |
| 1047 | MelderString_appendCharacter (& buffer, U'\t'); |
| 1048 | conststring32 s = my columnLabels [icol].get(); |
| 1049 | MelderString_append (& buffer, ( s && s [0] != U'\0' ? s : U"?" )); |
| 1050 | } |
| 1051 | MelderString_appendCharacter (& buffer, U'\n'); |
| 1052 | for (integer irow = 1; irow <= my numberOfRows; irow ++) { |
| 1053 | conststring32 s = my rowLabels [irow].get(); |
| 1054 | MelderString_append (& buffer, ( s && s [0] != U'\0' ? s : U"?" )); |
| 1055 | for (integer icol = 1; icol <= my numberOfColumns; icol ++) { |
| 1056 | MelderString_appendCharacter (& buffer, U'\t'); |
| 1057 | double x = my data [irow] [icol]; |
| 1058 | MelderString_append (& buffer, x); |
| 1059 | } |
| 1060 | MelderString_appendCharacter (& buffer, U'\n'); |
| 1061 | } |
| 1062 | MelderFile_writeText_e (file, buffer.string, Melder_getOutputEncoding ()); |
| 1063 | } catch (MelderError) { |
| 1064 | Melder_throw (me, U": not saved to tab-separated file."); |
| 1065 | } |
| 1066 | } |
| 1067 | |
| 1068 | autoTableOfReal TableOfReal_readFromHeaderlessSpreadsheetFile (MelderFile file) { |
| 1069 | try { |
| 1070 | autostring32 string = MelderFile_readText (file); |
| 1071 | |
| 1072 | /* |
| 1073 | Count columns. |
| 1074 | */ |
| 1075 | integer ncol = 0; |
| 1076 | char32 *p = & string [0]; |
| 1077 | for (;;) { |
| 1078 | char32 kar = *p++; |
| 1079 | if (kar == U'\n' || kar == U'\0') // TODO: Unicode |
| 1080 | break; |
| 1081 | if (kar == U' ' || kar == U'\t') |
| 1082 | continue; |
| 1083 | ncol ++; |
| 1084 | do { kar = *p++; } while (kar != U' ' && kar != U'\t' && kar != U'\n' && kar != U'\0'); |
| 1085 | if (kar == U'\n' || kar == U'\0') |
| 1086 | break; |
| 1087 | } |
| 1088 | ncol --; |
| 1089 | if (ncol < 1) |
| 1090 | Melder_throw (U"No columns."); |
| 1091 | |
| 1092 | /* |
| 1093 | Count elements. |
| 1094 | */ |
| 1095 | p = & string [0]; |
| 1096 | integer nelements = 0; |
| 1097 | for (;;) { |
| 1098 | char32 kar = *p++; |
| 1099 | if (kar == U'\0') |
| 1100 | break; |
| 1101 | if (kar == U' ' || kar == U'\t' || kar == U'\n') |
| 1102 | continue; |
| 1103 | nelements ++; |
| 1104 | do { kar = *p++; } while (kar != U' ' && kar != U'\t' && kar != U'\n' && kar != U'\0'); |
| 1105 | if (kar == U'\0') |
| 1106 | break; |
| 1107 | } |
| 1108 | |
| 1109 | /* |
| 1110 | Check if all columns are complete. |
| 1111 | */ |
| 1112 | if (nelements == 0 || nelements % (ncol + 1) != 0) |
| 1113 | Melder_throw (U"The number of elements (", nelements, U") is not a multiple of the number of columns plus 1 (", ncol + 1, U")."); |
| 1114 | |
| 1115 | /* |
| 1116 | Create empty table. |
| 1117 | */ |
| 1118 | integer nrow = nelements / (ncol + 1) - 1; |
| 1119 | autoTableOfReal me = TableOfReal_create (nrow, ncol); |
| 1120 | |
| 1121 | /* |
| 1122 | Read elements. |
| 1123 | */ |
| 1124 | autoMelderString buffer; |
| 1125 | p = & string [0]; |
| 1126 | while (*p == U' ' || *p == U'\t') { Melder_assert (*p != U'\0'); p ++; } |
| 1127 | while (*p != U' ' && *p != U'\t') { Melder_assert (*p != U'\0'); p ++; } // ignore the header of the zeroth column ("rowLabel" perhaps) |
| 1128 | for (integer icol = 1; icol <= ncol; icol ++) { |
| 1129 | while (*p == U' ' || *p == U'\t') { Melder_assert (*p != U'\0'); p ++; } |
| 1130 | MelderString_empty (& buffer); |
| 1131 | while (*p != U' ' && *p != U'\t' && *p != U'\n') { |
| 1132 | MelderString_appendCharacter (& buffer, *p); |
| 1133 | p ++; |
| 1134 | } |
| 1135 | TableOfReal_setColumnLabel (me.get(), icol, buffer.string); |
| 1136 | } |
| 1137 | for (integer irow = 1; irow <= nrow; irow ++) { |
| 1138 | while (*p == U' ' || *p == U'\t' || *p == U'\n') { Melder_assert (*p != U'\0'); p ++; } |
| 1139 | MelderString_empty (& buffer); |
| 1140 | while (*p != U' ' && *p != U'\t') { |
| 1141 | MelderString_appendCharacter (& buffer, *p); |
| 1142 | p ++; |
| 1143 | } |
| 1144 | TableOfReal_setRowLabel (me.get(), irow, buffer.string); |
| 1145 | for (integer icol = 1; icol <= ncol; icol ++) { |
| 1146 | while (*p == U' ' || *p == U'\t' || *p == U'\n') { Melder_assert (*p != U'\0'); p ++; } |
| 1147 | MelderString_empty (& buffer); |
| 1148 | while (*p != U' ' && *p != U'\t' && *p != U'\n' && *p != U'\0') { |
| 1149 | MelderString_appendCharacter (& buffer, *p); |
| 1150 | p ++; |
| 1151 | } |
| 1152 | my data [irow] [icol] = Melder_atof (buffer.string); // if cell contains a string, this will be 0 |
| 1153 | } |
| 1154 | } |
| 1155 | return me; |
| 1156 | } catch (MelderError) { |
| 1157 | Melder_throw (U"TableOfReal: tab-separated file ", file, U" not read."); |
| 1158 | } |
| 1159 | } |
| 1160 | |
| 1161 | /* End of file TableOfReal.cpp */ |