Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 | 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 104x 104x 104x 104x 104x 104x 104x 104x 104x 104x 104x 282x 282x 104x 104x 104x 104x 104x 15x 15x 15x 104x 104x 104x 104x 104x 104x 104x 104x 104x 104x 104x 104x 104x 132x 132x 132x 132x 132x 132x 132x 132x 12x 132x 80x 80x 80x 80x 80x 80x 120x 40x 40x 40x 40x 40x 40x 132x 132x 132x 132x 132x 361x 361x 361x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 361x 361x 361x 361x 361x 361x 361x 361x 361x 12x 12x 361x 132x 132x 132x 132x 132x 132x 132x 132x 132x 361x 361x 361x 361x 361x 361x 361x 361x 361x 361x 132x 132x 132x 132x 132x 132x 132x 132x 132x 3x 3x 3x 3x 13x 13x 3x 3x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 24x 24x 24x 10x 10x 24x 9x 9x 24x 13x 13x 1x 1x 13x 24x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 24x 24x 24x 24x 10x 10x 24x 9x 9x 24x 13x 13x 13x 1x 1x 13x 24x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 24x 24x 24x 10x 10x 24x 9x 9x 24x 13x 13x 1x 1x 13x 4x 24x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 22x 22x 10x 10x 22x 9x 9x 22x 8x 8x 22x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 6x 6x 6x 6x 6x 7x 7x 7x 7x 7x 6x 6x 20x 20x 6x 6x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 67x 67x 67x 67x 67x 67x 30x 30x 67x 1x 1x 36x 67x 18x 18x 67x 2x 2x 1x 1x 2x 67x 2x 67x 16x 16x 9x 9x 16x 3x 3x 1x 1x 16x 1x 1x 16x 9x 9x 67x 12x 12x 12x 9x 67x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 7x 7x 7x 7x 7x 7x 7x 7x 7x 1x 1x 7x 6x 6x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 23x 23x 7x 7x 7x 7x 7x 7x 13x 13x 13x 13x 13x 7x 7x 7x 23x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 33x 33x 33x 33x 7x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 12x 12x 12x 11x 11x 1x 12x 2x 2x 1x 12x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 13x 13x 13x 13x 13x 13x 9x 9x 13x 2x 2x 2x 2x 13x 3x 13x 1x 1x 4x 13x 8x 8x 8x 8x 4x 4x 8x 4x 4x 13x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 16x 16x 10x 10x 6x 6x 6x 16x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 26x 26x 10x 10x 16x 26x 10x 9x 9x 1x 26x 6x 6x 7x 26x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 13x 13x 13x 11x 11x 2x 13x 3x 3x 3x 3x 1x 1x 3x 2x 2x 13x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 29x 29x 10x 10x 29x 10x 10x 29x 3x 3x 29x 4x 4x 5x 5x 5x 29x 132x 104x 104x 104x 104x 104x 104x 104x 104x 104x 13x 13x 104x 104x 104x 104x 104x 104x 104x 104x 104x 104x 104x 104x 104x 104x 104x 104x 104x 104x 104x 104x 104x 104x 104x 104x 104x 104x 104x 104x 104x 104x 104x 104x 104x 104x 104x 104x 104x 104x 104x 104x 104x 104x 104x 104x 104x 104x 104x 104x 104x 104x 104x 104x 104x 104x 104x 104x 104x 12x 12x 12x 12x 12x | /**
* @license Apache-2.0
*
* Copyright (c) 2018 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/* eslint-disable max-len, max-lines */
'use strict';
// MODULES //
var isStringArray = require( '@stdlib/assert/is-string-array' ).primitives;
var isEmptyArrayLikeObject = require( '@stdlib/assert/is-empty-array-like-object' );
var isString = require( '@stdlib/assert/is-string' ).isPrimitive;
var isArrayBuffer = require( '@stdlib/assert/is-arraybuffer' );
var isFunction = require( '@stdlib/assert/is-function' );
var isInteger = require( '@stdlib/assert/is-integer' ).isPrimitive;
var isObject = require( '@stdlib/assert/is-object' );
var isCollection = require( '@stdlib/assert/is-collection' );
var hasOwnProp = require( '@stdlib/assert/has-own-property' );
var hasIteratorSymbolSupport = require( '@stdlib/assert/has-iterator-symbol-support' );
var propertiesIn = require( '@stdlib/utils/properties-in' );
var copy = require( '@stdlib/array/base/copy' );
var typedarray = require( '@stdlib/array/typed' );
var Int8Array = require( '@stdlib/array/int8' );
var getDType = require( '@stdlib/array/dtype' );
var defineProperty = require( '@stdlib/utils/define-property' );
var setReadOnly = require( '@stdlib/utils/define-nonenumerable-read-only-property' );
var setNonEnumerableProperty = require( '@stdlib/utils/define-nonenumerable-property' );
var setNonEnumerableReadOnlyAccessor = require( '@stdlib/utils/define-nonenumerable-read-only-accessor' ); // eslint-disable-line id-length
var setNonEnumerableReadWriteAccessor = require( '@stdlib/utils/define-nonenumerable-read-write-accessor' ); // eslint-disable-line id-length
var floor = require( '@stdlib/math/base/special/floor' );
var ITERATOR_SYMBOL = require( '@stdlib/symbol/iterator' );
var format = require( '@stdlib/string/format' );
var contains = require( './contains.js' );
var hasDistinctElements = require( './has_distinct_elements.js' );
var validate = require( './validate.js' );
var ascending = require( './ascending.js' );
var fromIterator = require( './from_iterator.js' );
var fromIteratorMap = require( './from_iterator_map.js' );
// VARIABLES //
var RESERVED_PROPS = propertiesIn( new Int8Array( 0 ) );
var HAS_ITERATOR_SYMBOL = hasIteratorSymbolSupport();
// MAIN //
/**
* Returns a named typed tuple factory.
*
* @param {StringArray} names - field (property) names
* @param {Options} [options] - options
* @param {string} [options.dtype="float64"] - default data type
* @param {string} [options.name="tuple"] - tuple name
* @throws {TypeError} must provide an array of strings
* @throws {TypeError} must provide distinct field names
* @throws {Error} cannot provide a reserved field (property) name
* @throws {TypeError} must provide valid options
* @throws {Error} must provide a recognized data type
* @returns {Function} factory function
*
* @example
* var point = factory( [ 'x', 'y' ] );
*
* var p = point( [ 1.0, -1.0 ] );
*
* var x = p[ 0 ];
* // returns 1.0
*
* x = p.x;
* // returns 1.0
*
* var y = p[ 1 ];
* // returns -1.0
*
* y = p.y;
* // returns -1.0
*/
function factory( names, options ) { // eslint-disable-line max-lines-per-function, stdlib/jsdoc-require-throws-tags
var nfields;
var fields;
var opts;
var err;
var i;
if ( !isStringArray( names ) && !isEmptyArrayLikeObject( names ) ) {
throw new TypeError( format( 'invalid argument. Must provide an array of strings. Value: `%s`.', names ) );
}
if ( !hasDistinctElements( names ) ) {
throw new TypeError( format( 'invalid argument. Field names must be distinct. Value: `%s`.', names ) );
}
fields = copy( names );
nfields = fields.length;
for ( i = 0; i < nfields; i++ ) {
if ( contains( RESERVED_PROPS, fields[ i ] ) ) {
throw new Error( format( 'invalid argument. Provided field name is reserved. Name: `%s`.', fields[ i ] ) );
}
}
opts = {
'dtype': 'float64',
'name': 'tuple'
};
if ( arguments.length > 1 ) {
err = validate( opts, options );
if ( err ) {
throw err;
}
}
/**
* Returns a named typed tuple.
*
* @private
* @param {(TypedArray|ArrayLikeObject|ArrayBuffer|Iterable)} [arg] - a typed array, array-like object, buffer, or an iterable
* @param {NonNegativeInteger} [byteOffset=0] - byte offset
* @param {string} [dtype] - data type
* @throws {TypeError} must provide a recognized data type
* @throws {RangeError} arguments must be compatible with tuple length
* @returns {TypedArray} named typed tuple
*/
function namedtypedtuple() { // eslint-disable-line max-lines-per-function
var indices;
var dtype;
var nargs;
var tuple;
var i;
nargs = arguments.length;
if ( nargs <= 0 ) {
tuple = typedarray( nfields, opts.dtype );
} else if ( nargs === 1 ) {
if ( isString( arguments[ 0 ] ) ) {
// Arguments: [ dtype ]
tuple = typedarray( nfields, arguments[ 0 ] );
} else if ( isArrayBuffer( arguments[ 0 ] ) ) {
// Arguments: [ ArrayBuffer ]
tuple = typedarray( arguments[ 0 ], 0, nfields, opts.dtype );
} else {
// Arguments: [ TypedArray|ArrayLikeObject|Iterable ]
tuple = typedarray( arguments[ 0 ], opts.dtype );
}
} else if ( nargs === 2 ) {
if ( isArrayBuffer( arguments[ 0 ] ) ) {
if ( isString( arguments[ 1 ] ) ) {
// Arguments: [ ArrayBuffer, dtype ]
tuple = typedarray( arguments[ 0 ], 0, nfields, arguments[ 1 ] );
} else {
// Arguments: [ ArrayBuffer, byteOffset ]
tuple = typedarray( arguments[ 0 ], arguments[ 1 ], nfields, opts.dtype );
}
} else {
// Arguments: [ TypedArray|ArrayLikeObject|Iterable, dtype ]
tuple = typedarray( arguments[ 0 ], arguments[ 1 ] );
}
} else {
// Arguments: [ ArrayBuffer, byteOffset, dtype ]
tuple = typedarray( arguments[ 0 ], arguments[ 1 ], nfields, arguments[ 2 ] );
}
if ( tuple.length !== nfields ) {
throw new RangeError( format( 'invalid arguments. Arguments are incompatible with the number of tuple fields. Number of fields: `%u`. Number of data elements: `%u`.', nfields, tuple.length ) );
}
dtype = getDType( tuple );
indices = []; // indirect index look-up table
for ( i = 0; i < nfields; i++ ) {
indices.push( i );
setNonEnumerableReadWriteAccessor( tuple, fields[ i ], getter( i ), setter( i ) );
}
setNonEnumerableProperty( tuple, 'name', opts.name );
setNonEnumerableReadOnlyAccessor( tuple, 'fields', getFields );
setNonEnumerableReadOnlyAccessor( tuple, 'orderedFields', orderedFields );
// Note: keep in alphabetical order
setNonEnumerableProperty( tuple, 'entries', entries );
setNonEnumerableProperty( tuple, 'every', every );
setNonEnumerableProperty( tuple, 'fieldOf', fieldOf );
setNonEnumerableProperty( tuple, 'filter', filter );
setNonEnumerableProperty( tuple, 'find', find );
setNonEnumerableProperty( tuple, 'findField', findField );
setNonEnumerableProperty( tuple, 'findIndex', findIndex );
setNonEnumerableProperty( tuple, 'findLast', findLast );
setNonEnumerableProperty( tuple, 'findLastField', findLastField );
setNonEnumerableProperty( tuple, 'findLastIndex', findLastIndex );
setNonEnumerableProperty( tuple, 'forEach', forEach );
setNonEnumerableProperty( tuple, 'ind2key', ind2key );
setNonEnumerableProperty( tuple, 'key2ind', key2ind );
setNonEnumerableProperty( tuple, 'keys', keys );
setNonEnumerableProperty( tuple, 'lastFieldOf', lastFieldOf );
setNonEnumerableProperty( tuple, 'map', map );
setNonEnumerableProperty( tuple, 'reduce', reduce );
setNonEnumerableProperty( tuple, 'reduceRight', reduceRight );
setNonEnumerableProperty( tuple, 'reverse', reverse );
setNonEnumerableProperty( tuple, 'slice', slice );
setNonEnumerableProperty( tuple, 'some', some );
setNonEnumerableProperty( tuple, 'sort', sort );
setNonEnumerableProperty( tuple, 'subtuple', subtuple );
setNonEnumerableProperty( tuple, 'toJSON', toJSON );
setNonEnumerableProperty( tuple, 'toLocaleString', toLocaleString );
setNonEnumerableProperty( tuple, 'toReversed', toReversed );
setNonEnumerableProperty( tuple, 'toSorted', toSorted );
setNonEnumerableProperty( tuple, 'toString', toString );
setNonEnumerableProperty( tuple, 'with', copyWith );
return tuple;
/**
* Returns an accessor to retrieve a tuple value.
*
* @private
* @param {NonNegativeInteger} i - tuple index
* @returns {Function} accessor
*/
function getter( i ) {
return get;
/**
* Returns a tuple value.
*
* @private
* @returns {number} tuple value
*/
function get() {
return tuple[ indices.indexOf( i ) ];
}
}
/**
* Returns an accessor to set a tuple value.
*
* @private
* @param {NonNegativeInteger} i - tuple index
* @returns {Function} accessor
*/
function setter( i ) {
return set;
/**
* Sets a tuple value.
*
* @private
* @param {number} v - value to set
*/
function set( v ) {
tuple[ indices.indexOf( i ) ] = v;
}
}
/**
* Returns the list of tuple fields in index order.
*
* @private
* @memberof tuple
* @returns {StringArray} tuple fields
*/
function orderedFields() {
var out;
var i;
out = fields.slice();
for ( i = 0; i < nfields; i++ ) {
out[ i ] = fields[ indices[i] ];
}
return out;
}
// Note: keep functions which follow in alphabetical order
/**
* Returns an iterator for iterating over tuple key-value pairs.
*
* @private
* @memberof tuple
* @throws {TypeError} `this` must be the host tuple
* @returns {Iterator} iterator
*/
function entries() {
var self;
var iter;
var FLG;
var i;
self = this; // eslint-disable-line no-invalid-this
if ( self !== tuple ) {
throw new TypeError( 'invalid invocation. `this` is not host tuple.' );
}
// Initialize the iteration index:
i = -1;
// Create an iterator protocol-compliant object:
iter = {};
defineProperty( iter, 'next', {
'configurable': false,
'enumerable': false,
'writable': false,
'value': next
});
defineProperty( iter, 'return', {
'configurable': false,
'enumerable': false,
'writable': false,
'value': end
});
if ( HAS_ITERATOR_SYMBOL ) {
defineProperty( iter, ITERATOR_SYMBOL, {
'configurable': false,
'enumerable': false,
'writable': false,
'value': factory
});
}
return iter;
/**
* Returns an iterator protocol-compliant object containing the next iterated value.
*
* @private
* @returns {Object} iterator protocol-compliant object
*/
function next() {
i += 1;
if ( FLG || i >= nfields ) {
return {
'done': true
};
}
return {
'value': [ i, fields[ indices[ i ] ], tuple[ i ] ],
'done': false
};
}
/**
* Finishes an iterator.
*
* @private
* @param {*} [value] - value to return
* @returns {Object} iterator protocol-compliant object
*/
function end( value ) {
FLG = true;
if ( arguments.length ) {
return {
'value': value,
'done': true
};
}
return {
'done': true
};
}
/**
* Returns a new iterator.
*
* @private
* @returns {Iterator} iterator
*/
function factory() {
return self.entries();
}
}
/**
* Tests whether all tuple elements pass a test implemented by a predicate function.
*
* @private
* @memberof tuple
* @param {Function} predicate - predicate function
* @param {*} [thisArg] - execution context
* @throws {TypeError} `this` must be the host tuple
* @throws {TypeError} first argument must be a function
* @returns {boolean} boolean indicating if all elements pass
*/
function every( predicate, thisArg ) {
var bool;
var i;
if ( this !== tuple ) { // eslint-disable-line no-invalid-this
throw new TypeError( 'invalid invocation. `this` is not host tuple.' );
}
if ( !isFunction( predicate ) ) {
throw new TypeError( format( 'invalid argument. First argument must be a function. Value: `%s`.', predicate ) );
}
for ( i = 0; i < nfields; i++ ) {
bool = predicate.call( thisArg, tuple[ i ], i, fields[ indices[i] ], tuple );
if ( !bool ) {
return false;
}
}
return true;
}
/**
* Returns the field of the first tuple element strictly equal to a search element.
*
* ## Notes
*
* - The function does not distinguish between signed and unsigned zero.
* - If unable to locate a search element, the function returns `undefined`.
*
* @private
* @memberof tuple
* @param {*} searchElement - search element
* @param {integer} [fromIndex=0] - tuple index from which to begin searching
* @throws {TypeError} `this` must be the host tuple
* @throws {TypeError} second argument must be an integer
* @returns {(string|void)} tuple field name or `undefined`
*/
function fieldOf( searchElement ) {
var i;
if ( this !== tuple ) { // eslint-disable-line no-invalid-this
throw new TypeError( 'invalid invocation. `this` is not host tuple.' );
}
if ( arguments.length > 1 ) {
i = arguments[ 1 ];
if ( !isInteger( i ) ) {
throw new TypeError( format( 'invalid argument. Second argument must be an integer. Value: `%s`.', i ) );
}
if ( i >= nfields ) {
return;
}
if ( i < 0 ) {
i = nfields + i;
if ( i < 0 ) {
i = 0;
}
}
} else {
i = 0;
}
for ( ; i < nfields; i++ ) {
if ( tuple[ i ] === searchElement ) {
return fields[ indices[ i ] ];
}
}
}
/**
* Creates a new tuple which includes those elements for which a predicate function returns a truthy value.
*
* ## Notes
*
* - The returned tuple has the same data type as the host tuple.
* - If a predicate function does not return a truthy value for any tuple element, the function returns `null`.
*
* @private
* @memberof tuple
* @param {Function} predicate - filter (predicate) function
* @param {*} [thisArg] - execution context
* @throws {TypeError} `this` must be the host tuple
* @throws {TypeError} first argument must be a function
* @returns {(TypedArray|null)} new tuple
*/
function filter( predicate, thisArg ) {
var bool;
var tmp;
var f;
var i;
if ( this !== tuple ) { // eslint-disable-line no-invalid-this
throw new TypeError( 'invalid invocation. `this` is not host tuple.' );
}
if ( !isFunction( predicate ) ) {
throw new TypeError( format( 'invalid argument. First argument must be a function. Value: `%s`.', predicate ) );
}
tmp = [];
f = [];
for ( i = 0; i < nfields; i++ ) {
bool = predicate.call( thisArg, tuple[ i ], i, fields[ indices[i] ], tuple );
if ( bool ) {
f.push( fields[ indices[i] ] );
tmp.push( tuple[ i ] );
}
}
if ( f.length === nfields ) {
return namedtypedtuple( tmp, dtype );
}
if ( f.length ) {
return factory( f, opts )( tmp );
}
return null;
}
/**
* Returns the first tuple element for which a provided predicate function returns a truthy value.
*
* @private
* @memberof tuple
* @param {Function} predicate - predicate function
* @param {*} [thisArg] - execution context
* @throws {TypeError} `this` must be the host tuple
* @throws {TypeError} first argument must be a function
* @returns {(number|void)} tuple element
*/
function find( predicate, thisArg ) {
var bool;
var i;
if ( this !== tuple ) { // eslint-disable-line no-invalid-this
throw new TypeError( 'invalid invocation. `this` is not host tuple.' );
}
if ( !isFunction( predicate ) ) {
throw new TypeError( format( 'invalid argument. First argument must be a function. Value: `%s`.', predicate ) );
}
for ( i = 0; i < nfields; i++ ) {
bool = predicate.call( thisArg, tuple[ i ], i, fields[ indices[i] ], tuple );
if ( bool ) {
return tuple[ i ];
}
}
}
/**
* Returns the field of the first tuple element for which a provided predicate function returns a truthy value.
*
* ## Notes
*
* - If the predicate function never returns a truthy value, the function returns `undefined`.
*
* @private
* @memberof tuple
* @param {Function} predicate - predicate function
* @param {*} [thisArg] - execution context
* @throws {TypeError} `this` must be the host tuple
* @throws {TypeError} first argument must be a function
* @returns {(string|void)} tuple field name or `undefined`
*/
function findField( predicate, thisArg ) {
var bool;
var f;
var i;
if ( this !== tuple ) { // eslint-disable-line no-invalid-this
throw new TypeError( 'invalid invocation. `this` is not host tuple.' );
}
if ( !isFunction( predicate ) ) {
throw new TypeError( format( 'invalid argument. First argument must be a function. Value: `%s`.', predicate ) );
}
for ( i = 0; i < nfields; i++ ) {
f = fields[ indices[ i ] ];
bool = predicate.call( thisArg, tuple[ i ], i, f, tuple );
if ( bool ) {
return f;
}
}
}
/**
* Returns the index of the first tuple element for which a provided predicate function returns a truthy value.
*
* ## Notes
*
* - If the predicate function never returns a truthy value, the function returns `-1`.
*
* @private
* @memberof tuple
* @param {Function} predicate - predicate function
* @param {*} [thisArg] - execution context
* @throws {TypeError} `this` must be the host tuple
* @throws {TypeError} first argument must be a function
* @returns {integer} tuple index or `-1`
*/
function findIndex( predicate, thisArg ) {
var bool;
var i;
if ( this !== tuple ) { // eslint-disable-line no-invalid-this
throw new TypeError( 'invalid invocation. `this` is not host tuple.' );
}
if ( !isFunction( predicate ) ) {
throw new TypeError( format( 'invalid argument. First argument must be a function. Value: `%s`.', predicate ) );
}
for ( i = 0; i < nfields; i++ ) {
bool = predicate.call( thisArg, tuple[ i ], i, fields[ indices[i] ], tuple );
if ( bool ) {
return i;
}
}
return -1;
}
/**
* Returns the last tuple element for which a provided predicate function returns a truthy value.
*
* ## Notes
*
* - The method iterates from right to left.
*
* @private
* @memberof tuple
* @param {Function} predicate - predicate function
* @param {*} [thisArg] - execution context
* @throws {TypeError} `this` must be the host tuple
* @throws {TypeError} first argument must be a function
* @returns {(number|void)} tuple element
*/
function findLast( predicate, thisArg ) {
var bool;
var i;
if ( this !== tuple ) { // eslint-disable-line no-invalid-this
throw new TypeError( 'invalid invocation. `this` is not host tuple.' );
}
if ( !isFunction( predicate ) ) {
throw new TypeError( format( 'invalid argument. First argument must be a function. Value: `%s`.', predicate ) );
}
for ( i = nfields - 1; i >= 0; i-- ) {
bool = predicate.call( thisArg, tuple[ i ], i, fields[ indices[i] ], tuple );
if ( bool ) {
return tuple[ i ];
}
}
}
/**
* Returns the field of the last tuple element for which a provided predicate function returns a truthy value.
*
* ## Notes
*
* - The method iterates from right to left.
* - If the predicate function never returns a truthy value, the function returns `undefined`.
*
* @private
* @memberof tuple
* @param {Function} predicate - predicate function
* @param {*} [thisArg] - execution context
* @throws {TypeError} `this` must be the host tuple
* @throws {TypeError} first argument must be a function
* @returns {(string|void)} tuple field name or `undefined`
*/
function findLastField( predicate, thisArg ) {
var bool;
var f;
var i;
if ( this !== tuple ) { // eslint-disable-line no-invalid-this
throw new TypeError( 'invalid invocation. `this` is not host tuple.' );
}
if ( !isFunction( predicate ) ) {
throw new TypeError( format( 'invalid argument. First argument must be a function. Value: `%s`.', predicate ) );
}
for ( i = nfields - 1; i >= 0; i-- ) {
f = fields[ indices[ i ] ];
bool = predicate.call( thisArg, tuple[ i ], i, f, tuple );
if ( bool ) {
return f;
}
}
}
/**
* Returns the index of the last tuple element for which a provided predicate function returns a truthy value.
*
* ## Notes
*
* - The method iterates from right to left.
* - If the predicate function never returns a truthy value, the function returns `-1`.
*
* @private
* @memberof tuple
* @param {Function} predicate - predicate function
* @param {*} [thisArg] - execution context
* @throws {TypeError} `this` must be the host tuple
* @throws {TypeError} first argument must be a function
* @returns {integer} tuple index or `-1`
*/
function findLastIndex( predicate, thisArg ) {
var bool;
var i;
if ( this !== tuple ) { // eslint-disable-line no-invalid-this
throw new TypeError( 'invalid invocation. `this` is not host tuple.' );
}
if ( !isFunction( predicate ) ) {
throw new TypeError( format( 'invalid argument. First argument must be a function. Value: `%s`.', predicate ) );
}
for ( i = nfields - 1; i >= 0; i-- ) {
bool = predicate.call( thisArg, tuple[ i ], i, fields[ indices[i] ], tuple );
if ( bool ) {
return i;
}
}
return -1;
}
/**
* Invokes a callback for each tuple element.
*
* @private
* @memberof tuple
* @param {Function} fcn - function to invoke
* @param {*} [thisArg] - execution context
* @throws {TypeError} `this` must be the host tuple
* @throws {TypeError} first argument must be a function
*/
function forEach( fcn, thisArg ) {
var i;
if ( this !== tuple ) { // eslint-disable-line no-invalid-this
throw new TypeError( 'invalid invocation. `this` is not host tuple.' );
}
if ( !isFunction( fcn ) ) {
throw new TypeError( format( 'invalid argument. First argument must be a function. Value: `%s`.', fcn ) );
}
for ( i = 0; i < nfields; i++ ) {
fcn.call( thisArg, tuple[ i ], i, fields[ indices[i] ], tuple );
}
}
/**
* Converts a tuple index to a field name.
*
* ## Notes
*
* - If provided an out-of-bounds index, the function returns `undefined`.
* - If provided a negative tuple index, the function resolves the index relative to the last tuple element.
*
* @private
* @memberof tuple
* @param {integer} ind - tuple index
* @throws {TypeError} `this` must be the host tuple
* @throws {TypeError} must provide an integer
* @returns {(string|void)} field name or undefined
*/
function ind2key( ind ) {
if ( this !== tuple ) { // eslint-disable-line no-invalid-this
throw new TypeError( 'invalid invocation. `this` is not host tuple.' );
}
if ( !isInteger( ind ) ) {
throw new TypeError( format( 'invalid argument. Must provide an integer. Value: `%s`.', ind ) );
}
if ( ind < 0 ) {
ind = nfields + ind;
}
if ( ind < 0 || ind >= nfields ) {
return;
}
return fields[ indices[ ind ] ];
}
/**
* Converts a field name to a tuple index.
*
* ## Notes
*
* - If provided an unknown field name, the function returns `-1`.
*
* @private
* @memberof tuple
* @param {string} key - field name
* @throws {TypeError} `this` must be the host tuple
* @throws {TypeError} first argument must be a string
* @returns {integer} tuple index
*/
function key2ind( key ) {
var i;
if ( this !== tuple ) { // eslint-disable-line no-invalid-this
throw new TypeError( 'invalid invocation. `this` is not host tuple.' );
}
if ( !isString( key ) ) {
throw new TypeError( format( 'invalid argument. First argument must be a string. Value: `%s`.', key ) );
}
for ( i = 0; i < nfields; i++ ) {
if ( fields[ indices[i] ] === key ) {
return i;
}
}
return -1;
}
/**
* Returns an iterator for iterating over tuple keys.
*
* @private
* @memberof tuple
* @throws {TypeError} `this` must be the host tuple
* @returns {Iterator} iterator
*/
function keys() {
var self;
var iter;
var FLG;
var i;
self = this; // eslint-disable-line no-invalid-this
if ( self !== tuple ) {
throw new TypeError( 'invalid invocation. `this` is not host tuple.' );
}
// Initialize the iteration index:
i = -1;
// Create an iterator protocol-compliant object:
iter = {};
defineProperty( iter, 'next', {
'configurable': false,
'enumerable': false,
'writable': false,
'value': next
});
defineProperty( iter, 'return', {
'configurable': false,
'enumerable': false,
'writable': false,
'value': end
});
if ( HAS_ITERATOR_SYMBOL ) {
defineProperty( iter, ITERATOR_SYMBOL, {
'configurable': false,
'enumerable': false,
'writable': false,
'value': factory
});
}
return iter;
/**
* Returns an iterator protocol-compliant object containing the next iterated value.
*
* @private
* @returns {Object} iterator protocol-compliant object
*/
function next() {
i += 1;
if ( FLG || i >= nfields ) {
return {
'done': true
};
}
return {
'value': [ i, fields[ indices[ i ] ] ],
'done': false
};
}
/**
* Finishes an iterator.
*
* @private
* @param {*} [value] - value to return
* @returns {Object} iterator protocol-compliant object
*/
function end( value ) {
FLG = true;
if ( arguments.length ) {
return {
'value': value,
'done': true
};
}
return {
'done': true
};
}
/**
* Returns a new iterator.
*
* @private
* @returns {Iterator} iterator
*/
function factory() {
return self.keys();
}
}
/**
* Returns the field of the last tuple element strictly equal to a search element, iterating from right to left.
*
* ## Notes
*
* - The function does not distinguish between signed and unsigned zero.
* - If unable to locate a search element, the function returns `undefined`.
*
* @private
* @memberof tuple
* @param {*} searchElement - search element
* @param {integer} [fromIndex=-1] - tuple index from which to begin searching
* @throws {TypeError} `this` must be the host tuple
* @throws {TypeError} second argument must be an integer
* @returns {(string|void)} tuple field name or `undefined`
*/
function lastFieldOf( searchElement ) {
var i;
if ( this !== tuple ) { // eslint-disable-line no-invalid-this
throw new TypeError( 'invalid invocation. `this` is not host tuple.' );
}
if ( arguments.length > 1 ) {
i = arguments[ 1 ];
if ( !isInteger( i ) ) {
throw new TypeError( format( 'invalid argument. Second argument must be an integer. Value: `%s`.', i ) );
}
if ( i >= nfields ) {
i = nfields - 1;
} else if ( i < 0 ) {
i = nfields + i;
if ( i < 0 ) {
return;
}
}
} else {
i = nfields - 1;
}
for ( ; i >= 0; i-- ) {
if ( tuple[ i ] === searchElement ) {
return fields[ indices[ i ] ];
}
}
}
/**
* Maps each tuple element to an element in a new tuple.
*
* ## Notes
*
* - The returned tuple has the same data type as the host tuple.
*
* @private
* @memberof tuple
* @param {Function} fcn - map function
* @param {*} [thisArg] - execution context
* @throws {TypeError} `this` must be the host tuple
* @throws {TypeError} first argument must be a function
* @returns {TypedArray} new tuple
*/
function map( fcn, thisArg ) {
var out;
var i;
if ( this !== tuple ) { // eslint-disable-line no-invalid-this
throw new TypeError( 'invalid invocation. `this` is not host tuple.' );
}
if ( !isFunction( fcn ) ) {
throw new TypeError( format( 'invalid argument. First argument must be a function. Value: `%s`.', fcn ) );
}
out = namedtypedtuple( dtype );
for ( i = 0; i < nfields; i++ ) {
out[ i ] = fcn.call( thisArg, tuple[ i ], i, fields[ indices[i] ], tuple );
}
return out;
}
/**
* Applies a function against an accumulator and each element in a tuple and returns the accumulated result.
*
* @private
* @memberof tuple
* @param {Function} fcn - reduction function
* @param {*} [initial] - initial value
* @throws {TypeError} `this` must be the host tuple
* @throws {TypeError} first argument must be a function
* @returns {*} accumulated result
*/
function reduce( fcn ) {
var acc;
var i;
if ( this !== tuple ) { // eslint-disable-line no-invalid-this
throw new TypeError( 'invalid invocation. `this` is not host tuple.' );
}
if ( !isFunction( fcn ) ) {
throw new TypeError( format( 'invalid argument. First argument must be a function. Value: `%s`.', fcn ) );
}
if ( arguments.length > 1 ) {
acc = arguments[ 1 ];
i = 0;
} else {
acc = tuple[ 0 ];
i = 1;
}
for ( ; i < nfields; i++ ) {
acc = fcn( acc, tuple[ i ], i, fields[ indices[i] ], tuple );
}
return acc;
}
/**
* Applies a function against an accumulator and each element in a tuple and returns the accumulated result, iterating from right to left.
*
* @private
* @memberof tuple
* @param {Function} fcn - reduction function
* @param {*} [initial] - initial value
* @throws {TypeError} `this` must be the host tuple
* @throws {TypeError} first argument must be a function
* @returns {*} accumulated result
*/
function reduceRight( fcn ) {
var acc;
var i;
if ( this !== tuple ) { // eslint-disable-line no-invalid-this
throw new TypeError( 'invalid invocation. `this` is not host tuple.' );
}
if ( !isFunction( fcn ) ) {
throw new TypeError( format( 'invalid argument. First argument must be a function. Value: `%s`.', fcn ) );
}
if ( arguments.length > 1 ) {
acc = arguments[ 1 ];
i = nfields - 1;
} else {
acc = tuple[ nfields-1 ];
i = nfields - 2;
}
for ( ; i >= 0; i-- ) {
acc = fcn( acc, tuple[ i ], i, fields[ indices[i] ], tuple );
}
return acc;
}
/**
* Reverses a tuple **in-place**.
*
* @private
* @memberof tuple
* @throws {TypeError} `this` must be the host tuple
* @returns {TypedArray} reversed tuple
*/
function reverse() {
var tmp;
var i;
var j;
if ( this !== tuple ) { // eslint-disable-line no-invalid-this
throw new TypeError( 'invalid invocation. `this` is not host tuple.' );
}
for ( i = 0; i < floor( nfields/2 ); i++ ) {
j = nfields - i - 1;
tmp = tuple[ i ];
tuple[ i ] = tuple[ j ];
tuple[ j ] = tmp;
}
// Because the indices are bounded [0,nfields), we can use simple arithmetic to "reverse" index values in-place...
for ( i = 0; i < nfields; i++ ) {
indices[ i ] = nfields - indices[ i ] - 1;
}
return tuple;
}
/**
* Copies elements to a new tuple with the same underlying data type as the host tuple.
*
* @private
* @memberof tuple
* @param {integer} [begin=0] - start element index (inclusive)
* @param {integer} [end=tuple.length] - end element index (exclusive)
* @throws {TypeError} `this` must be the host tuple
* @throws {TypeError} first argument must be an integer
* @throws {TypeError} second argument must be an integer
* @returns {TypedArray} new tuple
*/
function slice( begin, end ) {
var tmp;
var f;
var i;
var j;
if ( this !== tuple ) { // eslint-disable-line no-invalid-this
throw new TypeError( 'invalid invocation. `this` is not host tuple.' );
}
if ( arguments.length === 0 ) {
return namedtypedtuple( tuple, dtype );
}
i = begin;
if ( !isInteger( i ) ) {
throw new TypeError( format( 'invalid argument. First argument must be an integer. Value: `%s`.', begin ) );
}
if ( i < 0 ) {
i = nfields + i;
if ( i < 0 ) {
i = 0;
}
}
if ( arguments.length === 1 ) {
j = nfields;
} else {
j = end;
if ( !isInteger( j ) ) {
throw new TypeError( format( 'invalid argument. Second argument must be an integer. Value: `%s`.', end ) );
}
if ( j < 0 ) {
j = nfields + j;
if ( j < 0 ) {
j = 0;
}
} else if ( j > nfields ) {
j = nfields;
}
}
f = [];
tmp = [];
for ( ; i < j; i++ ) {
f.push( fields[ indices[i] ] );
tmp.push( tuple[ i ] );
}
return factory( f, opts )( tmp, dtype );
}
/**
* Tests whether at least one tuple element passes a test implemented by a predicate function.
*
* @private
* @memberof tuple
* @param {Function} predicate - predicate function
* @param {*} [thisArg] - execution context
* @throws {TypeError} `this` must be the host tuple
* @throws {TypeError} first argument must be a function
* @returns {boolean} boolean indicating if at least one element passes
*/
function some( predicate, thisArg ) {
var bool;
var i;
if ( this !== tuple ) { // eslint-disable-line no-invalid-this
throw new TypeError( 'invalid invocation. `this` is not host tuple.' );
}
if ( !isFunction( predicate ) ) {
throw new TypeError( format( 'invalid argument. First argument must be a function. Value: `%s`.', predicate ) );
}
for ( i = 0; i < nfields; i++ ) {
bool = predicate.call( thisArg, tuple[ i ], i, fields[ indices[i] ], tuple );
if ( bool ) {
return true;
}
}
return false;
}
/**
* Sorts a tuple in-place.
*
* ## Notes
*
* - The comparison function is provided two tuple elements, `a` and `b`, per invocation, and its return value determines the sort order as follows:
*
* - If the comparison function returns a value **less** than zero, then the function sorts `a` to an index lower than `b` (i.e., `a` should come **before** `b`).
* - If the comparison function returns a value **greater** than zero, then the function sorts `a` to an index higher than `b` (i.e., `b` should come **before** `a`).
* - If the comparison function returns **zero**, then the relative order of `a` and `b` _should_ remain unchanged.
*
* - Invoking this method does **not** affect tuple field assignments.
*
* @private
* @memberof tuple
* @param {Function} [compareFunction] - function which specifies the sort order
* @throws {TypeError} `this` must be the host tuple
* @throws {TypeError} first argument must be a function
* @returns {TypedArray} sorted tuple
*/
function sort( compareFunction ) {
var clbk;
var idx;
var tmp;
var i;
var j;
var k;
var v;
if ( this !== tuple ) { // eslint-disable-line no-invalid-this
throw new TypeError( 'invalid invocation. `this` is not host tuple.' );
}
if ( arguments.length ) {
if ( !isFunction( compareFunction ) ) {
throw new TypeError( format( 'invalid argument. First argument must be a function. Value: `%s`.', compareFunction ) );
}
clbk = compareFunction;
} else {
clbk = ascending;
}
// Create a copy of the list of indices so that we can access the original sort order when invoking the comparison function:
idx = indices.slice();
// Sort the list of indices:
indices.sort( wrapper );
// Create a temporary indices array which we'll reorder as we rearrange the tuple elements:
tmp = indices.slice();
// Rearrange tuple elements according to the rearranged indices (note: every "move" moves a tuple element to its desired position with runtime complexity O(N))...
for ( i = 0; i < nfields; i++ ) {
// Check if we need to move a tuple element:
if ( tmp[ i ] !== i ) {
v = tuple[ i ];
j = i;
k = tmp[ j ];
// Follow "cycles", stopping once we are back at index `i`...
while ( k !== i ) {
tuple[ j ] = tuple[ k ];
tmp[ j ] = j;
j = k;
k = tmp[ j ];
}
tuple[ j ] = v;
tmp[ j ] = j;
}
}
return tuple;
/**
* Wraps a comparison function to allow sorting the internal indices array rather than the tuple directly.
*
* @private
* @param {NonNegativeInteger} ia - first index
* @param {NonNegativeInteger} ib - second index
* @returns {*} value specifying the sort order
*/
function wrapper( ia, ib ) {
var a = tuple[ idx[ ia ] ];
var b = tuple[ idx[ ib ] ];
return clbk( a, b );
}
}
/**
* Creates a new tuple over the same underlying `ArrayBuffer` and with the same underlying data type as the host tuple.
*
* @private
* @memberof tuple
* @param {integer} [begin=0] - start element index (inclusive)
* @param {integer} [end=tuple.length] - end element index (exclusive)
* @throws {TypeError} `this` must be the host tuple
* @throws {TypeError} first argument must be an integer
* @throws {TypeError} second argument must be an integer
* @returns {TypedArray} new tuple
*/
function subtuple( begin, end ) {
var f;
var i;
var j;
var k;
if ( this !== tuple ) { // eslint-disable-line no-invalid-this
throw new TypeError( 'invalid invocation. `this` is not host tuple.' );
}
if ( arguments.length === 0 ) {
return namedtypedtuple( tuple.buffer, tuple.byteOffset, dtype );
}
i = begin;
if ( !isInteger( i ) ) {
throw new TypeError( format( 'invalid argument. First argument must be an integer. Value: `%s`.', begin ) );
}
if ( i < 0 ) {
i = nfields + i;
if ( i < 0 ) {
i = 0;
}
}
if ( arguments.length === 1 ) {
j = nfields;
} else {
j = end;
if ( !isInteger( j ) ) {
throw new TypeError( format( 'invalid argument. Second argument must be an integer. Value: `%s`.', end ) );
}
if ( j < 0 ) {
j = nfields + j;
if ( j < 0 ) {
j = 0;
}
} else if ( j > nfields ) {
j = nfields;
}
}
if ( j <= i ) {
return factory( [], opts )( tuple.buffer, tuple.byteOffset, dtype );
}
f = [];
for ( k = i; k < j; k++ ) {
f.push( fields[ indices[k] ] );
}
return factory( f, opts )( tuple.buffer, tuple.byteOffset+(i*tuple.BYTES_PER_ELEMENT), dtype );
}
/**
* Serializes a tuple as JSON.
*
* @private
* @memberof tuple
* @throws {TypeError} `this` must be the host tuple
* @returns {JSON} tuple JSON representation
*/
function toJSON() {
var out;
var i;
if ( this !== tuple ) { // eslint-disable-line no-invalid-this
throw new TypeError( 'invalid invocation. `this` is not host tuple.' );
}
out = {};
for ( i = 0; i < nfields; i++ ) {
out[ fields[i] ] = tuple[ indices[i] ];
}
return out;
}
/**
* Serializes a tuple as a locale-specific string.
*
* @private
* @memberof tuple
* @param {(string|Array<string>)} [locales] - locale identifier(s)
* @param {Object} [options] - configuration options
* @throws {TypeError} `this` must be the host tuple
* @throws {TypeError} first argument must be a string or an array of strings
* @throws {TypeError} options argument must be an object
* @returns {string} string representation
*/
function toLocaleString( locales, options ) {
var loc;
var out;
var o;
var i;
if ( this !== tuple ) { // eslint-disable-line no-invalid-this
throw new TypeError( 'invalid invocation. `this` is not host tuple.' );
}
if ( arguments.length === 0 ) {
loc = [];
} else if ( isString( locales ) || isStringArray( locales ) ) {
loc = locales;
} else {
throw new TypeError( format( 'invalid argument. First argument must be a string or an array of strings. Value: `%s`.', locales ) );
}
if ( arguments.length < 2 ) {
o = {};
} else if ( isObject( options ) ) {
o = options;
} else {
throw new TypeError( format( 'invalid argument. Options argument must be an object. Value: `%s`.', options ) );
}
out = opts.name.toLocaleString( loc, o ) + '(';
for ( i = 0; i < nfields; i++ ) {
out += fields[ i ].toLocaleString( loc, o );
out += '=';
out += tuple[ indices[ i ] ].toLocaleString( loc, o );
if ( i < nfields-1 ) {
out += ', ';
}
}
out += ')';
return out;
}
/**
* Returns a new tuple with elements in reversed order.
*
* ## Notes
*
* - Unlike `reverse`, this method does **not** mutate the host tuple.
*
* @private
* @memberof tuple
* @throws {TypeError} `this` must be the host tuple
* @returns {TypedArray} new reversed tuple
*/
function toReversed() {
var out;
if ( this !== tuple ) { // eslint-disable-line no-invalid-this
throw new TypeError( 'invalid invocation. `this` is not host tuple.' );
}
out = namedtypedtuple( tuple, dtype );
out.reverse();
return out;
}
/**
* Returns a new tuple with elements sorted in ascending order.
*
* ## Notes
*
* - The comparison function is provided two tuple elements, `a` and `b`, per invocation, and its return value determines the sort order as follows:
*
* - If the comparison function returns a value **less** than zero, then the function sorts `a` to an index lower than `b` (i.e., `a` should come **before** `b`).
* - If the comparison function returns a value **greater** than zero, then the function sorts `a` to an index higher than `b` (i.e., `b` should come **before** `a`).
* - If the comparison function returns **zero**, then the relative order of `a` and `b` _should_ remain unchanged.
*
* - Unlike `sort`, this method does **not** mutate the host tuple.
*
* @private
* @memberof tuple
* @param {Function} [compareFunction] - function which specifies the sort order
* @throws {TypeError} `this` must be the host tuple
* @throws {TypeError} first argument must be a function
* @returns {TypedArray} new sorted tuple
*/
function toSorted( compareFunction ) {
var out;
if ( this !== tuple ) { // eslint-disable-line no-invalid-this
throw new TypeError( 'invalid invocation. `this` is not host tuple.' );
}
out = namedtypedtuple( tuple, dtype );
if ( arguments.length ) {
if ( !isFunction( compareFunction ) ) {
throw new TypeError( format( 'invalid argument. First argument must be a function. Value: `%s`.', compareFunction ) );
}
out.sort( compareFunction );
} else {
out.sort();
}
return out;
}
/**
* Serializes a tuple as a string.
*
* @private
* @memberof tuple
* @throws {TypeError} `this` must be the host tuple
* @returns {string} tuple string representation
*/
function toString() {
var out;
var i;
if ( this !== tuple ) { // eslint-disable-line no-invalid-this
throw new TypeError( 'invalid invocation. `this` is not host tuple.' );
}
out = opts.name + '(';
for ( i = 0; i < nfields; i++ ) {
out += fields[ i ];
out += '=';
out += tuple[ indices[ i ] ];
if ( i < nfields-1 ) {
out += ', ';
}
}
out += ')';
return out;
}
/**
* Returns a new tuple with the element at the specified index replaced with a provided value.
*
* @private
* @memberof tuple
* @param {integer} index - element index
* @param {number} value - new value
* @throws {TypeError} `this` must be the host tuple
* @throws {TypeError} first argument must be an integer
* @throws {RangeError} index argument must be in bounds
* @returns {TypedArray} new tuple
*/
function copyWith( index, value ) {
var out;
if ( this !== tuple ) { // eslint-disable-line no-invalid-this
throw new TypeError( 'invalid invocation. `this` is not host tuple.' );
}
if ( !isInteger( index ) ) {
throw new TypeError( format( 'invalid argument. First argument must be an integer. Value: `%s`.', index ) );
}
if ( index < 0 ) {
index = nfields + index;
}
if ( index < 0 || index >= nfields ) {
throw new RangeError( format( 'invalid argument. Index argument is out-of-bounds. Value: `%u`.', index ) );
}
out = namedtypedtuple( tuple, dtype );
out[ index ] = value;
return out;
}
}
/**
* Returns the list of tuple fields.
*
* @private
* @memberof tuple
* @returns {StringArray} tuple fields
*/
function getFields() {
return fields.slice();
}
// Note: keep the following methods in alphabetical order...
/**
* Creates a new tuple from an array-like object or an iterable.
*
* @private
* @name from
* @memberof namedtypedtuple
* @type {Function}
* @param {(ArrayLikeObject|Iterable)} src - array-like object or iterable
* @param {Function} [clbk] - callback to invoke for each source element
* @param {*} [thisArg] - callback execution context
* @throws {TypeError} `this` must be the host tuple factory
* @throws {TypeError} first argument must be an array-like object or an iterable
* @throws {RangeError} source must be compatible with tuple length
* @throws {TypeError} second argument must be a function
* @returns {TypedArray} new tuple
*/
setReadOnly( namedtypedtuple, 'from', function from( src ) { // eslint-disable-line no-restricted-syntax
var thisArg;
var nargs;
var tuple;
var clbk;
var tmp;
var it;
var i;
if ( this !== namedtypedtuple ) { // eslint-disable-line no-invalid-this
throw new TypeError( 'invalid invocation. `this` is not the host tuple factory.' );
}
nargs = arguments.length;
if ( nargs > 1 ) {
clbk = arguments[ 1 ];
if ( !isFunction( clbk ) ) {
throw new TypeError( format( 'invalid argument. Second argument must be a function. Value: `%s`.', clbk ) );
}
if ( nargs > 2 ) {
thisArg = arguments[ 2 ];
}
}
if ( isCollection( src ) ) {
if ( src.length !== nfields ) {
throw new RangeError( format( 'invalid argument. Source is incompatible with the number of tuple fields. Number of fields: `%u`. Source length: `%u`.', nfields, src.length ) );
}
tuple = namedtypedtuple( nfields, opts.dtype );
if ( clbk ) {
for ( i = 0; i < nfields; i++ ) {
tuple[ i ] = clbk.call( thisArg, src[ i ], i, fields[ i ] );
}
} else {
for ( i = 0; i < nfields; i++ ) {
tuple[ i ] = src[ i ];
}
}
} else if ( isObject( src ) && HAS_ITERATOR_SYMBOL && isFunction( src[ ITERATOR_SYMBOL ] ) ) {
it = src[ ITERATOR_SYMBOL ]();
if ( !isFunction( it.next ) ) {
throw new TypeError( format( 'invalid argument. First argument must be an array-like object or an iterable. Value: `%s`.', src ) );
}
if ( clbk ) {
tmp = fromIteratorMap( fields, it, clbk, thisArg );
} else {
tmp = fromIterator( it );
}
tuple = namedtypedtuple( tmp, opts.dtype );
} else {
throw new TypeError( format( 'invalid argument. First argument must be an array-like object or an iterable. Value: `%s`.', src ) );
}
return tuple;
});
/**
* Creates a new tuple from an object containing tuple fields.
*
* @private
* @name fromObject
* @memberof namedtypedtuple
* @type {Function}
* @param {Object} obj - source object
* @param {Function} [clbk] - callback to invoke for each source object tuple field
* @param {*} [thisArg] - callback execution context
* @throws {TypeError} `this` must be the host tuple factory
* @throws {TypeError} first argument must be an object
* @throws {TypeError} second argument must be a function
* @returns {TypedArray} new tuple
*/
setReadOnly( namedtypedtuple, 'fromObject', function fromObject( obj ) { // eslint-disable-line no-restricted-syntax
var thisArg;
var nargs;
var tuple;
var clbk;
var f;
var i;
if ( this !== namedtypedtuple ) { // eslint-disable-line no-invalid-this
throw new TypeError( 'invalid invocation. `this` is not the host tuple factory.' );
}
if ( obj === null || typeof obj !== 'object' ) {
throw new TypeError( format( 'invalid argument. First argument must be an object. Value: `%s`.', obj ) );
}
nargs = arguments.length;
if ( nargs > 1 ) {
clbk = arguments[ 1 ];
if ( !isFunction( clbk ) ) {
throw new TypeError( format( 'invalid argument. Second argument must be a function. Value: `%s`.', clbk ) );
}
if ( nargs > 2 ) {
thisArg = arguments[ 2 ];
}
}
tuple = namedtypedtuple( nfields, opts.dtype );
if ( clbk ) {
for ( i = 0; i < nfields; i++ ) {
f = fields[ i ];
if ( hasOwnProp( obj, f ) ) {
tuple[ i ] = clbk.call( thisArg, obj[ f ], f );
}
}
} else {
for ( i = 0; i < nfields; i++ ) {
f = fields[ i ];
if ( hasOwnProp( obj, f ) ) {
tuple[ i ] = obj[ f ];
}
}
}
return tuple;
});
/**
* Creates a new tuple from a variable number of arguments.
*
* @private
* @name of
* @memberof namedtypedtuple
* @type {Function}
* @param {...number} element - tuple elements
* @throws {TypeError} `this` must be the host tuple factory
* @throws {RangeError} incompatible number of arguments
* @returns {TypedArray} new tuple
*/
setReadOnly( namedtypedtuple, 'of', function of() { // eslint-disable-line no-restricted-syntax
var args;
var i;
if ( this !== namedtypedtuple ) { // eslint-disable-line no-invalid-this
throw new TypeError( 'invalid invocation. `this` is not the host tuple factory.' );
}
if ( arguments.length !== nfields ) {
throw new RangeError( format( 'invalid invocation. Number of arguments is incompatible with the number of tuple fields. Number of fields: `%u`. Number of arguments: `%u`.', nfields, arguments.length ) );
}
args = [];
for ( i = 0; i < arguments.length; i++ ) {
args.push( arguments[ i ] );
}
return namedtypedtuple( args );
});
return namedtypedtuple;
}
// EXPORTS //
module.exports = factory;
|