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
|
// Copyright 2007 The Closure Library Authors. All Rights Reserved.
//
// 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.
/**
* @fileoverview Python style iteration utilities.
* @author arv@google.com (Erik Arvidsson)
*/
goog.provide('goog.iter');
goog.provide('goog.iter.Iterable');
goog.provide('goog.iter.Iterator');
goog.provide('goog.iter.StopIteration');
goog.require('goog.array');
goog.require('goog.asserts');
goog.require('goog.functions');
goog.require('goog.math');
/**
* @typedef {goog.iter.Iterator|{length:number}|{__iterator__}}
*/
goog.iter.Iterable;
/**
* Singleton Error object that is used to terminate iterations.
* @const {!Error}
*/
goog.iter.StopIteration = ('StopIteration' in goog.global) ?
// For script engines that support legacy iterators.
goog.global['StopIteration'] :
{message: 'StopIteration', stack: ''};
/**
* Class/interface for iterators. An iterator needs to implement a {@code next}
* method and it needs to throw a {@code goog.iter.StopIteration} when the
* iteration passes beyond the end. Iterators have no {@code hasNext} method.
* It is recommended to always use the helper functions to iterate over the
* iterator or in case you are only targeting JavaScript 1.7 for in loops.
* @constructor
* @template VALUE
*/
goog.iter.Iterator = function() {};
/**
* Returns the next value of the iteration. This will throw the object
* {@see goog.iter#StopIteration} when the iteration passes the end.
* @return {VALUE} Any object or value.
*/
goog.iter.Iterator.prototype.next = function() {
throw goog.iter.StopIteration;
};
/**
* Returns the {@code Iterator} object itself. This is used to implement
* the iterator protocol in JavaScript 1.7
* @param {boolean=} opt_keys Whether to return the keys or values. Default is
* to only return the values. This is being used by the for-in loop (true)
* and the for-each-in loop (false). Even though the param gives a hint
* about what the iterator will return there is no guarantee that it will
* return the keys when true is passed.
* @return {!goog.iter.Iterator<VALUE>} The object itself.
*/
goog.iter.Iterator.prototype.__iterator__ = function(opt_keys) {
return this;
};
/**
* Returns an iterator that knows how to iterate over the values in the object.
* @param {goog.iter.Iterator<VALUE>|goog.iter.Iterable} iterable If the
* object is an iterator it will be returned as is. If the object has an
* {@code __iterator__} method that will be called to get the value
* iterator. If the object is an array-like object we create an iterator
* for that.
* @return {!goog.iter.Iterator<VALUE>} An iterator that knows how to iterate
* over the values in {@code iterable}.
* @template VALUE
*/
goog.iter.toIterator = function(iterable) {
if (iterable instanceof goog.iter.Iterator) {
return iterable;
}
if (typeof iterable.__iterator__ == 'function') {
return iterable.__iterator__(false);
}
if (goog.isArrayLike(iterable)) {
var i = 0;
var newIter = new goog.iter.Iterator;
newIter.next = function() {
while (true) {
if (i >= iterable.length) {
throw goog.iter.StopIteration;
}
// Don't include deleted elements.
if (!(i in iterable)) {
i++;
continue;
}
return iterable[i++];
}
};
return newIter;
}
// TODO(arv): Should we fall back on goog.structs.getValues()?
throw new Error('Not implemented');
};
/**
* Calls a function for each element in the iterator with the element of the
* iterator passed as argument.
*
* @param {goog.iter.Iterator<VALUE>|goog.iter.Iterable} iterable The iterator
* to iterate over. If the iterable is an object {@code toIterator} will be
* called on it.
* @param {function(this:THIS,VALUE,?,!goog.iter.Iterator<VALUE>)} f
* The function to call for every element. This function takes 3 arguments
* (the element, undefined, and the iterator) and the return value is
* irrelevant. The reason for passing undefined as the second argument is
* so that the same function can be used in {@see goog.array#forEach} as
* well as others. The third parameter is of type "number" for
* arraylike objects, undefined, otherwise.
* @param {THIS=} opt_obj The object to be used as the value of 'this' within
* {@code f}.
* @template THIS, VALUE
*/
goog.iter.forEach = function(iterable, f, opt_obj) {
if (goog.isArrayLike(iterable)) {
try {
// NOTES: this passes the index number to the second parameter
// of the callback contrary to the documentation above.
goog.array.forEach(
/** @type {IArrayLike<?>} */ (iterable), f, opt_obj);
} catch (ex) {
if (ex !== goog.iter.StopIteration) {
throw ex;
}
}
} else {
iterable = goog.iter.toIterator(iterable);
try {
while (true) {
f.call(opt_obj, iterable.next(), undefined, iterable);
}
} catch (ex) {
if (ex !== goog.iter.StopIteration) {
throw ex;
}
}
}
};
/**
* Calls a function for every element in the iterator, and if the function
* returns true adds the element to a new iterator.
*
* @param {goog.iter.Iterator<VALUE>|goog.iter.Iterable} iterable The iterator
* to iterate over.
* @param {
* function(this:THIS,VALUE,undefined,!goog.iter.Iterator<VALUE>):boolean} f
* The function to call for every element. This function takes 3 arguments
* (the element, undefined, and the iterator) and should return a boolean.
* If the return value is true the element will be included in the returned
* iterator. If it is false the element is not included.
* @param {THIS=} opt_obj The object to be used as the value of 'this' within
* {@code f}.
* @return {!goog.iter.Iterator<VALUE>} A new iterator in which only elements
* that passed the test are present.
* @template THIS, VALUE
*/
goog.iter.filter = function(iterable, f, opt_obj) {
var iterator = goog.iter.toIterator(iterable);
var newIter = new goog.iter.Iterator;
newIter.next = function() {
while (true) {
var val = iterator.next();
if (f.call(opt_obj, val, undefined, iterator)) {
return val;
}
}
};
return newIter;
};
/**
* Calls a function for every element in the iterator, and if the function
* returns false adds the element to a new iterator.
*
* @param {goog.iter.Iterator<VALUE>|goog.iter.Iterable} iterable The iterator
* to iterate over.
* @param {
* function(this:THIS,VALUE,undefined,!goog.iter.Iterator<VALUE>):boolean} f
* The function to call for every element. This function takes 3 arguments
* (the element, undefined, and the iterator) and should return a boolean.
* If the return value is false the element will be included in the returned
* iterator. If it is true the element is not included.
* @param {THIS=} opt_obj The object to be used as the value of 'this' within
* {@code f}.
* @return {!goog.iter.Iterator<VALUE>} A new iterator in which only elements
* that did not pass the test are present.
* @template THIS, VALUE
*/
goog.iter.filterFalse = function(iterable, f, opt_obj) {
return goog.iter.filter(iterable, goog.functions.not(f), opt_obj);
};
/**
* Creates a new iterator that returns the values in a range. This function
* can take 1, 2 or 3 arguments:
* <pre>
* range(5) same as range(0, 5, 1)
* range(2, 5) same as range(2, 5, 1)
* </pre>
*
* @param {number} startOrStop The stop value if only one argument is provided.
* The start value if 2 or more arguments are provided. If only one
* argument is used the start value is 0.
* @param {number=} opt_stop The stop value. If left out then the first
* argument is used as the stop value.
* @param {number=} opt_step The number to increment with between each call to
* next. This can be negative.
* @return {!goog.iter.Iterator<number>} A new iterator that returns the values
* in the range.
*/
goog.iter.range = function(startOrStop, opt_stop, opt_step) {
var start = 0;
var stop = startOrStop;
var step = opt_step || 1;
if (arguments.length > 1) {
start = startOrStop;
stop = opt_stop;
}
if (step == 0) {
throw new Error('Range step argument must not be zero');
}
var newIter = new goog.iter.Iterator;
newIter.next = function() {
if (step > 0 && start >= stop || step < 0 && start <= stop) {
throw goog.iter.StopIteration;
}
var rv = start;
start += step;
return rv;
};
return newIter;
};
/**
* Joins the values in a iterator with a delimiter.
* @param {goog.iter.Iterator<VALUE>|goog.iter.Iterable} iterable The iterator
* to get the values from.
* @param {string} deliminator The text to put between the values.
* @return {string} The joined value string.
* @template VALUE
*/
goog.iter.join = function(iterable, deliminator) {
return goog.iter.toArray(iterable).join(deliminator);
};
/**
* For every element in the iterator call a function and return a new iterator
* with that value.
*
* @param {!goog.iter.Iterator<VALUE>|!goog.iter.Iterable} iterable The
* iterator to iterate over.
* @param {
* function(this:THIS,VALUE,undefined,!goog.iter.Iterator<VALUE>):RESULT} f
* The function to call for every element. This function takes 3 arguments
* (the element, undefined, and the iterator) and should return a new value.
* @param {THIS=} opt_obj The object to be used as the value of 'this' within
* {@code f}.
* @return {!goog.iter.Iterator<RESULT>} A new iterator that returns the
* results of applying the function to each element in the original
* iterator.
* @template THIS, VALUE, RESULT
*/
goog.iter.map = function(iterable, f, opt_obj) {
var iterator = goog.iter.toIterator(iterable);
var newIter = new goog.iter.Iterator;
newIter.next = function() {
var val = iterator.next();
return f.call(opt_obj, val, undefined, iterator);
};
return newIter;
};
/**
* Passes every element of an iterator into a function and accumulates the
* result.
*
* @param {goog.iter.Iterator<VALUE>|goog.iter.Iterable} iterable The iterator
* to iterate over.
* @param {function(this:THIS,VALUE,VALUE):VALUE} f The function to call for
* every element. This function takes 2 arguments (the function's previous
* result or the initial value, and the value of the current element).
* function(previousValue, currentElement) : newValue.
* @param {VALUE} val The initial value to pass into the function on the first
* call.
* @param {THIS=} opt_obj The object to be used as the value of 'this' within
* f.
* @return {VALUE} Result of evaluating f repeatedly across the values of
* the iterator.
* @template THIS, VALUE
*/
goog.iter.reduce = function(iterable, f, val, opt_obj) {
var rval = val;
goog.iter.forEach(
iterable, function(val) { rval = f.call(opt_obj, rval, val); });
return rval;
};
/**
* Goes through the values in the iterator. Calls f for each of these, and if
* any of them returns true, this returns true (without checking the rest). If
* all return false this will return false.
*
* @param {goog.iter.Iterator<VALUE>|goog.iter.Iterable} iterable The iterator
* object.
* @param {
* function(this:THIS,VALUE,undefined,!goog.iter.Iterator<VALUE>):boolean} f
* The function to call for every value. This function takes 3 arguments
* (the value, undefined, and the iterator) and should return a boolean.
* @param {THIS=} opt_obj The object to be used as the value of 'this' within
* {@code f}.
* @return {boolean} true if any value passes the test.
* @template THIS, VALUE
*/
goog.iter.some = function(iterable, f, opt_obj) {
iterable = goog.iter.toIterator(iterable);
try {
while (true) {
if (f.call(opt_obj, iterable.next(), undefined, iterable)) {
return true;
}
}
} catch (ex) {
if (ex !== goog.iter.StopIteration) {
throw ex;
}
}
return false;
};
/**
* Goes through the values in the iterator. Calls f for each of these and if any
* of them returns false this returns false (without checking the rest). If all
* return true this will return true.
*
* @param {goog.iter.Iterator<VALUE>|goog.iter.Iterable} iterable The iterator
* object.
* @param {
* function(this:THIS,VALUE,undefined,!goog.iter.Iterator<VALUE>):boolean} f
* The function to call for every value. This function takes 3 arguments
* (the value, undefined, and the iterator) and should return a boolean.
* @param {THIS=} opt_obj The object to be used as the value of 'this' within
* {@code f}.
* @return {boolean} true if every value passes the test.
* @template THIS, VALUE
*/
goog.iter.every = function(iterable, f, opt_obj) {
iterable = goog.iter.toIterator(iterable);
try {
while (true) {
if (!f.call(opt_obj, iterable.next(), undefined, iterable)) {
return false;
}
}
} catch (ex) {
if (ex !== goog.iter.StopIteration) {
throw ex;
}
}
return true;
};
/**
* Takes zero or more iterables and returns one iterator that will iterate over
* them in the order chained.
* @param {...!goog.iter.Iterator<VALUE>|!goog.iter.Iterable} var_args Any
* number of iterable objects.
* @return {!goog.iter.Iterator<VALUE>} Returns a new iterator that will
* iterate over all the given iterables' contents.
* @template VALUE
*/
goog.iter.chain = function(var_args) {
return goog.iter.chainFromIterable(arguments);
};
/**
* Takes a single iterable containing zero or more iterables and returns one
* iterator that will iterate over each one in the order given.
* @see https://goo.gl/5NRp5d
* @param {goog.iter.Iterable} iterable The iterable of iterables to chain.
* @return {!goog.iter.Iterator<VALUE>} Returns a new iterator that will
* iterate over all the contents of the iterables contained within
* {@code iterable}.
* @template VALUE
*/
goog.iter.chainFromIterable = function(iterable) {
var iterator = goog.iter.toIterator(iterable);
var iter = new goog.iter.Iterator();
var current = null;
iter.next = function() {
while (true) {
if (current == null) {
var it = iterator.next();
current = goog.iter.toIterator(it);
}
try {
return current.next();
} catch (ex) {
if (ex !== goog.iter.StopIteration) {
throw ex;
}
current = null;
}
}
};
return iter;
};
/**
* Builds a new iterator that iterates over the original, but skips elements as
* long as a supplied function returns true.
* @param {goog.iter.Iterator<VALUE>|goog.iter.Iterable} iterable The iterator
* object.
* @param {
* function(this:THIS,VALUE,undefined,!goog.iter.Iterator<VALUE>):boolean} f
* The function to call for every value. This function takes 3 arguments
* (the value, undefined, and the iterator) and should return a boolean.
* @param {THIS=} opt_obj The object to be used as the value of 'this' within
* {@code f}.
* @return {!goog.iter.Iterator<VALUE>} A new iterator that drops elements from
* the original iterator as long as {@code f} is true.
* @template THIS, VALUE
*/
goog.iter.dropWhile = function(iterable, f, opt_obj) {
var iterator = goog.iter.toIterator(iterable);
var newIter = new goog.iter.Iterator;
var dropping = true;
newIter.next = function() {
while (true) {
var val = iterator.next();
if (dropping && f.call(opt_obj, val, undefined, iterator)) {
continue;
} else {
dropping = false;
}
return val;
}
};
return newIter;
};
/**
* Builds a new iterator that iterates over the original, but only as long as a
* supplied function returns true.
* @param {goog.iter.Iterator<VALUE>|goog.iter.Iterable} iterable The iterator
* object.
* @param {
* function(this:THIS,VALUE,undefined,!goog.iter.Iterator<VALUE>):boolean} f
* The function to call for every value. This function takes 3 arguments
* (the value, undefined, and the iterator) and should return a boolean.
* @param {THIS=} opt_obj This is used as the 'this' object in f when called.
* @return {!goog.iter.Iterator<VALUE>} A new iterator that keeps elements in
* the original iterator as long as the function is true.
* @template THIS, VALUE
*/
goog.iter.takeWhile = function(iterable, f, opt_obj) {
var iterator = goog.iter.toIterator(iterable);
var iter = new goog.iter.Iterator();
iter.next = function() {
var val = iterator.next();
if (f.call(opt_obj, val, undefined, iterator)) {
return val;
}
throw goog.iter.StopIteration;
};
return iter;
};
/**
* Converts the iterator to an array
* @param {goog.iter.Iterator<VALUE>|goog.iter.Iterable} iterable The iterator
* to convert to an array.
* @return {!Array<VALUE>} An array of the elements the iterator iterates over.
* @template VALUE
*/
goog.iter.toArray = function(iterable) {
// Fast path for array-like.
if (goog.isArrayLike(iterable)) {
return goog.array.toArray(/** @type {!IArrayLike<?>} */ (iterable));
}
iterable = goog.iter.toIterator(iterable);
var array = [];
goog.iter.forEach(iterable, function(val) { array.push(val); });
return array;
};
/**
* Iterates over two iterables and returns true if they contain the same
* sequence of elements and have the same length.
* @param {!goog.iter.Iterator<VALUE>|!goog.iter.Iterable} iterable1 The first
* iterable object.
* @param {!goog.iter.Iterator<VALUE>|!goog.iter.Iterable} iterable2 The second
* iterable object.
* @param {function(VALUE,VALUE):boolean=} opt_equalsFn Optional comparison
* function.
* Should take two arguments to compare, and return true if the arguments
* are equal. Defaults to {@link goog.array.defaultCompareEquality} which
* compares the elements using the built-in '===' operator.
* @return {boolean} true if the iterables contain the same sequence of elements
* and have the same length.
* @template VALUE
*/
goog.iter.equals = function(iterable1, iterable2, opt_equalsFn) {
var fillValue = {};
var pairs = goog.iter.zipLongest(fillValue, iterable1, iterable2);
var equalsFn = opt_equalsFn || goog.array.defaultCompareEquality;
return goog.iter.every(
pairs, function(pair) { return equalsFn(pair[0], pair[1]); });
};
/**
* Advances the iterator to the next position, returning the given default value
* instead of throwing an exception if the iterator has no more entries.
* @param {goog.iter.Iterator<VALUE>|goog.iter.Iterable} iterable The iterable
* object.
* @param {VALUE} defaultValue The value to return if the iterator is empty.
* @return {VALUE} The next item in the iteration, or defaultValue if the
* iterator was empty.
* @template VALUE
*/
goog.iter.nextOrValue = function(iterable, defaultValue) {
try {
return goog.iter.toIterator(iterable).next();
} catch (e) {
if (e != goog.iter.StopIteration) {
throw e;
}
return defaultValue;
}
};
/**
* Cartesian product of zero or more sets. Gives an iterator that gives every
* combination of one element chosen from each set. For example,
* ([1, 2], [3, 4]) gives ([1, 3], [1, 4], [2, 3], [2, 4]).
* @see http://docs.python.org/library/itertools.html#itertools.product
* @param {...!IArrayLike<VALUE>} var_args Zero or more sets, as
* arrays.
* @return {!goog.iter.Iterator<!Array<VALUE>>} An iterator that gives each
* n-tuple (as an array).
* @template VALUE
*/
goog.iter.product = function(var_args) {
var someArrayEmpty =
goog.array.some(arguments, function(arr) { return !arr.length; });
// An empty set in a cartesian product gives an empty set.
if (someArrayEmpty || !arguments.length) {
return new goog.iter.Iterator();
}
var iter = new goog.iter.Iterator();
var arrays = arguments;
// The first indices are [0, 0, ...]
/** @type {?Array<number>} */
var indicies = goog.array.repeat(0, arrays.length);
iter.next = function() {
if (indicies) {
var retVal = goog.array.map(indicies, function(valueIndex, arrayIndex) {
return arrays[arrayIndex][valueIndex];
});
// Generate the next-largest indices for the next call.
// Increase the rightmost index. If it goes over, increase the next
// rightmost (like carry-over addition).
for (var i = indicies.length - 1; i >= 0; i--) {
// Assertion prevents compiler warning below.
goog.asserts.assert(indicies);
if (indicies[i] < arrays[i].length - 1) {
indicies[i]++;
break;
}
// We're at the last indices (the last element of every array), so
// the iteration is over on the next call.
if (i == 0) {
indicies = null;
break;
}
// Reset the index in this column and loop back to increment the
// next one.
indicies[i] = 0;
}
return retVal;
}
throw goog.iter.StopIteration;
};
return iter;
};
/**
* Create an iterator to cycle over the iterable's elements indefinitely.
* For example, ([1, 2, 3]) would return : 1, 2, 3, 1, 2, 3, ...
* @see: http://docs.python.org/library/itertools.html#itertools.cycle.
* @param {!goog.iter.Iterator<VALUE>|!goog.iter.Iterable} iterable The
* iterable object.
* @return {!goog.iter.Iterator<VALUE>} An iterator that iterates indefinitely
* over the values in {@code iterable}.
* @template VALUE
*/
goog.iter.cycle = function(iterable) {
var baseIterator = goog.iter.toIterator(iterable);
// We maintain a cache to store the iterable elements as we iterate
// over them. The cache is used to return elements once we have
// iterated over the iterable once.
var cache = [];
var cacheIndex = 0;
var iter = new goog.iter.Iterator();
// This flag is set after the iterable is iterated over once
var useCache = false;
iter.next = function() {
var returnElement = null;
// Pull elements off the original iterator if not using cache
if (!useCache) {
try {
// Return the element from the iterable
returnElement = baseIterator.next();
cache.push(returnElement);
return returnElement;
} catch (e) {
// If an exception other than StopIteration is thrown
// or if there are no elements to iterate over (the iterable was empty)
// throw an exception
if (e != goog.iter.StopIteration || goog.array.isEmpty(cache)) {
throw e;
}
// set useCache to true after we know that a 'StopIteration' exception
// was thrown and the cache is not empty (to handle the 'empty iterable'
// use case)
useCache = true;
}
}
returnElement = cache[cacheIndex];
cacheIndex = (cacheIndex + 1) % cache.length;
return returnElement;
};
return iter;
};
/**
* Creates an iterator that counts indefinitely from a starting value.
* @see http://docs.python.org/2/library/itertools.html#itertools.count
* @param {number=} opt_start The starting value. Default is 0.
* @param {number=} opt_step The number to increment with between each call to
* next. Negative and floating point numbers are allowed. Default is 1.
* @return {!goog.iter.Iterator<number>} A new iterator that returns the values
* in the series.
*/
goog.iter.count = function(opt_start, opt_step) {
var counter = opt_start || 0;
var step = goog.isDef(opt_step) ? opt_step : 1;
var iter = new goog.iter.Iterator();
iter.next = function() {
var returnValue = counter;
counter += step;
return returnValue;
};
return iter;
};
/**
* Creates an iterator that returns the same object or value repeatedly.
* @param {VALUE} value Any object or value to repeat.
* @return {!goog.iter.Iterator<VALUE>} A new iterator that returns the
* repeated value.
* @template VALUE
*/
goog.iter.repeat = function(value) {
var iter = new goog.iter.Iterator();
iter.next = goog.functions.constant(value);
return iter;
};
/**
* Creates an iterator that returns running totals from the numbers in
* {@code iterable}. For example, the array {@code [1, 2, 3, 4, 5]} yields
* {@code 1 -> 3 -> 6 -> 10 -> 15}.
* @see http://docs.python.org/3.2/library/itertools.html#itertools.accumulate
* @param {!goog.iter.Iterable} iterable The iterable of numbers to
* accumulate.
* @return {!goog.iter.Iterator<number>} A new iterator that returns the
* numbers in the series.
*/
goog.iter.accumulate = function(iterable) {
var iterator = goog.iter.toIterator(iterable);
var total = 0;
var iter = new goog.iter.Iterator();
iter.next = function() {
total += iterator.next();
return total;
};
return iter;
};
/**
* Creates an iterator that returns arrays containing the ith elements from the
* provided iterables. The returned arrays will be the same size as the number
* of iterables given in {@code var_args}. Once the shortest iterable is
* exhausted, subsequent calls to {@code next()} will throw
* {@code goog.iter.StopIteration}.
* @see http://docs.python.org/2/library/itertools.html#itertools.izip
* @param {...!goog.iter.Iterator<VALUE>|!goog.iter.Iterable} var_args Any
* number of iterable objects.
* @return {!goog.iter.Iterator<!Array<VALUE>>} A new iterator that returns
* arrays of elements from the provided iterables.
* @template VALUE
*/
goog.iter.zip = function(var_args) {
var args = arguments;
var iter = new goog.iter.Iterator();
if (args.length > 0) {
var iterators = goog.array.map(args, goog.iter.toIterator);
iter.next = function() {
var arr = goog.array.map(iterators, function(it) { return it.next(); });
return arr;
};
}
return iter;
};
/**
* Creates an iterator that returns arrays containing the ith elements from the
* provided iterables. The returned arrays will be the same size as the number
* of iterables given in {@code var_args}. Shorter iterables will be extended
* with {@code fillValue}. Once the longest iterable is exhausted, subsequent
* calls to {@code next()} will throw {@code goog.iter.StopIteration}.
* @see http://docs.python.org/2/library/itertools.html#itertools.izip_longest
* @param {VALUE} fillValue The object or value used to fill shorter iterables.
* @param {...!goog.iter.Iterator<VALUE>|!goog.iter.Iterable} var_args Any
* number of iterable objects.
* @return {!goog.iter.Iterator<!Array<VALUE>>} A new iterator that returns
* arrays of elements from the provided iterables.
* @template VALUE
*/
goog.iter.zipLongest = function(fillValue, var_args) {
var args = goog.array.slice(arguments, 1);
var iter = new goog.iter.Iterator();
if (args.length > 0) {
var iterators = goog.array.map(args, goog.iter.toIterator);
iter.next = function() {
var iteratorsHaveValues = false; // false when all iterators are empty.
var arr = goog.array.map(iterators, function(it) {
var returnValue;
try {
returnValue = it.next();
// Iterator had a value, so we've not exhausted the iterators.
// Set flag accordingly.
iteratorsHaveValues = true;
} catch (ex) {
if (ex !== goog.iter.StopIteration) {
throw ex;
}
returnValue = fillValue;
}
return returnValue;
});
if (!iteratorsHaveValues) {
throw goog.iter.StopIteration;
}
return arr;
};
}
return iter;
};
/**
* Creates an iterator that filters {@code iterable} based on a series of
* {@code selectors}. On each call to {@code next()}, one item is taken from
* both the {@code iterable} and {@code selectors} iterators. If the item from
* {@code selectors} evaluates to true, the item from {@code iterable} is given.
* Otherwise, it is skipped. Once either {@code iterable} or {@code selectors}
* is exhausted, subsequent calls to {@code next()} will throw
* {@code goog.iter.StopIteration}.
* @see http://docs.python.org/2/library/itertools.html#itertools.compress
* @param {!goog.iter.Iterator<VALUE>|!goog.iter.Iterable} iterable The
* iterable to filter.
* @param {!goog.iter.Iterator<VALUE>|!goog.iter.Iterable} selectors An
* iterable of items to be evaluated in a boolean context to determine if
* the corresponding element in {@code iterable} should be included in the
* result.
* @return {!goog.iter.Iterator<VALUE>} A new iterator that returns the
* filtered values.
* @template VALUE
*/
goog.iter.compress = function(iterable, selectors) {
var selectorIterator = goog.iter.toIterator(selectors);
return goog.iter.filter(
iterable, function() { return !!selectorIterator.next(); });
};
/**
* Implements the {@code goog.iter.groupBy} iterator.
* @param {!goog.iter.Iterator<VALUE>|!goog.iter.Iterable} iterable The
* iterable to group.
* @param {function(VALUE): KEY=} opt_keyFunc Optional function for
* determining the key value for each group in the {@code iterable}. Default
* is the identity function.
* @constructor
* @extends {goog.iter.Iterator<!Array<?>>}
* @template KEY, VALUE
* @private
*/
goog.iter.GroupByIterator_ = function(iterable, opt_keyFunc) {
/**
* The iterable to group, coerced to an iterator.
* @type {!goog.iter.Iterator}
*/
this.iterator = goog.iter.toIterator(iterable);
/**
* A function for determining the key value for each element in the iterable.
* If no function is provided, the identity function is used and returns the
* element unchanged.
* @type {function(VALUE): KEY}
*/
this.keyFunc = opt_keyFunc || goog.functions.identity;
/**
* The target key for determining the start of a group.
* @type {KEY}
*/
this.targetKey;
/**
* The current key visited during iteration.
* @type {KEY}
*/
this.currentKey;
/**
* The current value being added to the group.
* @type {VALUE}
*/
this.currentValue;
};
goog.inherits(goog.iter.GroupByIterator_, goog.iter.Iterator);
/** @override */
goog.iter.GroupByIterator_.prototype.next = function() {
while (this.currentKey == this.targetKey) {
this.currentValue = this.iterator.next(); // Exits on StopIteration
this.currentKey = this.keyFunc(this.currentValue);
}
this.targetKey = this.currentKey;
return [this.currentKey, this.groupItems_(this.targetKey)];
};
/**
* Performs the grouping of objects using the given key.
* @param {KEY} targetKey The target key object for the group.
* @return {!Array<VALUE>} An array of grouped objects.
* @private
*/
goog.iter.GroupByIterator_.prototype.groupItems_ = function(targetKey) {
var arr = [];
while (this.currentKey == targetKey) {
arr.push(this.currentValue);
try {
this.currentValue = this.iterator.next();
} catch (ex) {
if (ex !== goog.iter.StopIteration) {
throw ex;
}
break;
}
this.currentKey = this.keyFunc(this.currentValue);
}
return arr;
};
/**
* Creates an iterator that returns arrays containing elements from the
* {@code iterable} grouped by a key value. For iterables with repeated
* elements (i.e. sorted according to a particular key function), this function
* has a {@code uniq}-like effect. For example, grouping the array:
* {@code [A, B, B, C, C, A]} produces
* {@code [A, [A]], [B, [B, B]], [C, [C, C]], [A, [A]]}.
* @see http://docs.python.org/2/library/itertools.html#itertools.groupby
* @param {!goog.iter.Iterator<VALUE>|!goog.iter.Iterable} iterable The
* iterable to group.
* @param {function(VALUE): KEY=} opt_keyFunc Optional function for
* determining the key value for each group in the {@code iterable}. Default
* is the identity function.
* @return {!goog.iter.Iterator<!Array<?>>} A new iterator that returns
* arrays of consecutive key and groups.
* @template KEY, VALUE
*/
goog.iter.groupBy = function(iterable, opt_keyFunc) {
return new goog.iter.GroupByIterator_(iterable, opt_keyFunc);
};
/**
* Gives an iterator that gives the result of calling the given function
* <code>f</code> with the arguments taken from the next element from
* <code>iterable</code> (the elements are expected to also be iterables).
*
* Similar to {@see goog.iter#map} but allows the function to accept multiple
* arguments from the iterable.
*
* @param {!goog.iter.Iterable} iterable The iterable of
* iterables to iterate over.
* @param {function(this:THIS,...*):RESULT} f The function to call for every
* element. This function takes N+2 arguments, where N represents the
* number of items from the next element of the iterable. The two
* additional arguments passed to the function are undefined and the
* iterator itself. The function should return a new value.
* @param {THIS=} opt_obj The object to be used as the value of 'this' within
* {@code f}.
* @return {!goog.iter.Iterator<RESULT>} A new iterator that returns the
* results of applying the function to each element in the original
* iterator.
* @template THIS, RESULT
*/
goog.iter.starMap = function(iterable, f, opt_obj) {
var iterator = goog.iter.toIterator(iterable);
var iter = new goog.iter.Iterator();
iter.next = function() {
var args = goog.iter.toArray(iterator.next());
return f.apply(opt_obj, goog.array.concat(args, undefined, iterator));
};
return iter;
};
/**
* Returns an array of iterators each of which can iterate over the values in
* {@code iterable} without advancing the others.
* @see http://docs.python.org/2/library/itertools.html#itertools.tee
* @param {!goog.iter.Iterator<VALUE>|!goog.iter.Iterable} iterable The
* iterable to tee.
* @param {number=} opt_num The number of iterators to create. Default is 2.
* @return {!Array<goog.iter.Iterator<VALUE>>} An array of iterators.
* @template VALUE
*/
goog.iter.tee = function(iterable, opt_num) {
var iterator = goog.iter.toIterator(iterable);
var num = goog.isNumber(opt_num) ? opt_num : 2;
var buffers =
goog.array.map(goog.array.range(num), function() { return []; });
var addNextIteratorValueToBuffers = function() {
var val = iterator.next();
goog.array.forEach(buffers, function(buffer) { buffer.push(val); });
};
var createIterator = function(buffer) {
// Each tee'd iterator has an associated buffer (initially empty). When a
// tee'd iterator's buffer is empty, it calls
// addNextIteratorValueToBuffers(), adding the next value to all tee'd
// iterators' buffers, and then returns that value. This allows each
// iterator to be advanced independently.
var iter = new goog.iter.Iterator();
iter.next = function() {
if (goog.array.isEmpty(buffer)) {
addNextIteratorValueToBuffers();
}
goog.asserts.assert(!goog.array.isEmpty(buffer));
return buffer.shift();
};
return iter;
};
return goog.array.map(buffers, createIterator);
};
/**
* Creates an iterator that returns arrays containing a count and an element
* obtained from the given {@code iterable}.
* @see http://docs.python.org/2/library/functions.html#enumerate
* @param {!goog.iter.Iterator<VALUE>|!goog.iter.Iterable} iterable The
* iterable to enumerate.
* @param {number=} opt_start Optional starting value. Default is 0.
* @return {!goog.iter.Iterator<!Array<?>>} A new iterator containing
* count/item pairs.
* @template VALUE
*/
goog.iter.enumerate = function(iterable, opt_start) {
return goog.iter.zip(goog.iter.count(opt_start), iterable);
};
/**
* Creates an iterator that returns the first {@code limitSize} elements from an
* iterable. If this number is greater than the number of elements in the
* iterable, all the elements are returned.
* @see http://goo.gl/V0sihp Inspired by the limit iterator in Guava.
* @param {!goog.iter.Iterator<VALUE>|!goog.iter.Iterable} iterable The
* iterable to limit.
* @param {number} limitSize The maximum number of elements to return.
* @return {!goog.iter.Iterator<VALUE>} A new iterator containing
* {@code limitSize} elements.
* @template VALUE
*/
goog.iter.limit = function(iterable, limitSize) {
goog.asserts.assert(goog.math.isInt(limitSize) && limitSize >= 0);
var iterator = goog.iter.toIterator(iterable);
var iter = new goog.iter.Iterator();
var remaining = limitSize;
iter.next = function() {
if (remaining-- > 0) {
return iterator.next();
}
throw goog.iter.StopIteration;
};
return iter;
};
/**
* Creates an iterator that is advanced {@code count} steps ahead. Consumed
* values are silently discarded. If {@code count} is greater than the number
* of elements in {@code iterable}, an empty iterator is returned. Subsequent
* calls to {@code next()} will throw {@code goog.iter.StopIteration}.
* @param {!goog.iter.Iterator<VALUE>|!goog.iter.Iterable} iterable The
* iterable to consume.
* @param {number} count The number of elements to consume from the iterator.
* @return {!goog.iter.Iterator<VALUE>} An iterator advanced zero or more steps
* ahead.
* @template VALUE
*/
goog.iter.consume = function(iterable, count) {
goog.asserts.assert(goog.math.isInt(count) && count >= 0);
var iterator = goog.iter.toIterator(iterable);
while (count-- > 0) {
goog.iter.nextOrValue(iterator, null);
}
return iterator;
};
/**
* Creates an iterator that returns a range of elements from an iterable.
* Similar to {@see goog.array#slice} but does not support negative indexes.
* @param {!goog.iter.Iterator<VALUE>|!goog.iter.Iterable} iterable The
* iterable to slice.
* @param {number} start The index of the first element to return.
* @param {number=} opt_end The index after the last element to return. If
* defined, must be greater than or equal to {@code start}.
* @return {!goog.iter.Iterator<VALUE>} A new iterator containing a slice of
* the original.
* @template VALUE
*/
goog.iter.slice = function(iterable, start, opt_end) {
goog.asserts.assert(goog.math.isInt(start) && start >= 0);
var iterator = goog.iter.consume(iterable, start);
if (goog.isNumber(opt_end)) {
goog.asserts.assert(goog.math.isInt(opt_end) && opt_end >= start);
iterator = goog.iter.limit(iterator, opt_end - start /* limitSize */);
}
return iterator;
};
/**
* Checks an array for duplicate elements.
* @param {?IArrayLike<VALUE>} arr The array to check for
* duplicates.
* @return {boolean} True, if the array contains duplicates, false otherwise.
* @private
* @template VALUE
*/
// TODO(user): Consider moving this into goog.array as a public function.
goog.iter.hasDuplicates_ = function(arr) {
var deduped = [];
goog.array.removeDuplicates(arr, deduped);
return arr.length != deduped.length;
};
/**
* Creates an iterator that returns permutations of elements in
* {@code iterable}.
*
* Permutations are obtained by taking the Cartesian product of
* {@code opt_length} iterables and filtering out those with repeated
* elements. For example, the permutations of {@code [1,2,3]} are
* {@code [[1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], [3,2,1]]}.
* @see http://docs.python.org/2/library/itertools.html#itertools.permutations
* @param {!goog.iter.Iterator<VALUE>|!goog.iter.Iterable} iterable The
* iterable from which to generate permutations.
* @param {number=} opt_length Length of each permutation. If omitted, defaults
* to the length of {@code iterable}.
* @return {!goog.iter.Iterator<!Array<VALUE>>} A new iterator containing the
* permutations of {@code iterable}.
* @template VALUE
*/
goog.iter.permutations = function(iterable, opt_length) {
var elements = goog.iter.toArray(iterable);
var length = goog.isNumber(opt_length) ? opt_length : elements.length;
var sets = goog.array.repeat(elements, length);
var product = goog.iter.product.apply(undefined, sets);
return goog.iter.filter(
product, function(arr) { return !goog.iter.hasDuplicates_(arr); });
};
/**
* Creates an iterator that returns combinations of elements from
* {@code iterable}.
*
* Combinations are obtained by taking the {@see goog.iter#permutations} of
* {@code iterable} and filtering those whose elements appear in the order they
* are encountered in {@code iterable}. For example, the 3-length combinations
* of {@code [0,1,2,3]} are {@code [[0,1,2], [0,1,3], [0,2,3], [1,2,3]]}.
* @see http://docs.python.org/2/library/itertools.html#itertools.combinations
* @param {!goog.iter.Iterator<VALUE>|!goog.iter.Iterable} iterable The
* iterable from which to generate combinations.
* @param {number} length The length of each combination.
* @return {!goog.iter.Iterator<!Array<VALUE>>} A new iterator containing
* combinations from the {@code iterable}.
* @template VALUE
*/
goog.iter.combinations = function(iterable, length) {
var elements = goog.iter.toArray(iterable);
var indexes = goog.iter.range(elements.length);
var indexIterator = goog.iter.permutations(indexes, length);
// sortedIndexIterator will now give arrays of with the given length that
// indicate what indexes into "elements" should be returned on each iteration.
var sortedIndexIterator = goog.iter.filter(
indexIterator, function(arr) { return goog.array.isSorted(arr); });
var iter = new goog.iter.Iterator();
function getIndexFromElements(index) { return elements[index]; }
iter.next = function() {
return goog.array.map(sortedIndexIterator.next(), getIndexFromElements);
};
return iter;
};
/**
* Creates an iterator that returns combinations of elements from
* {@code iterable}, with repeated elements possible.
*
* Combinations are obtained by taking the Cartesian product of {@code length}
* iterables and filtering those whose elements appear in the order they are
* encountered in {@code iterable}. For example, the 2-length combinations of
* {@code [1,2,3]} are {@code [[1,1], [1,2], [1,3], [2,2], [2,3], [3,3]]}.
* @see https://goo.gl/C0yXe4
* @see https://goo.gl/djOCsk
* @param {!goog.iter.Iterator<VALUE>|!goog.iter.Iterable} iterable The
* iterable to combine.
* @param {number} length The length of each combination.
* @return {!goog.iter.Iterator<!Array<VALUE>>} A new iterator containing
* combinations from the {@code iterable}.
* @template VALUE
*/
goog.iter.combinationsWithReplacement = function(iterable, length) {
var elements = goog.iter.toArray(iterable);
var indexes = goog.array.range(elements.length);
var sets = goog.array.repeat(indexes, length);
var indexIterator = goog.iter.product.apply(undefined, sets);
// sortedIndexIterator will now give arrays of with the given length that
// indicate what indexes into "elements" should be returned on each iteration.
var sortedIndexIterator = goog.iter.filter(
indexIterator, function(arr) { return goog.array.isSorted(arr); });
var iter = new goog.iter.Iterator();
function getIndexFromElements(index) { return elements[index]; }
iter.next = function() {
return goog.array.map(
/** @type {!Array<number>} */
(sortedIndexIterator.next()), getIndexFromElements);
};
return iter;
};
|