forked from IronLanguages/ironpython3
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIterTools.cs
More file actions
1300 lines (1087 loc) · 47 KB
/
IterTools.cs
File metadata and controls
1300 lines (1087 loc) · 47 KB
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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the Apache 2.0 License.
// See the LICENSE file in the project root for more information.
using System;
using System.Collections;
using System.Collections.Generic;
using System.Numerics;
using System.Runtime.InteropServices;
using IronPython.Runtime;
using IronPython.Runtime.Binding;
using IronPython.Runtime.Exceptions;
using IronPython.Runtime.Operations;
using IronPython.Runtime.Types;
using Microsoft.Scripting;
using Microsoft.Scripting.Runtime;
using Microsoft.Scripting.Utils;
[assembly: PythonModule("itertools", typeof(IronPython.Modules.PythonIterTools))]
namespace IronPython.Modules {
public static class PythonIterTools {
public const string __doc__ = "Provides functions and classes for working with iterable objects.";
public static object tee(object iterable) {
return tee(iterable, 2);
}
public static object tee(object iterable, int n) {
if (n < 0) throw PythonOps.ValueError("n cannot be negative");
object[] res = new object[n];
if (!(iterable is TeeIterator)) {
IEnumerator iter = PythonOps.GetEnumerator(iterable);
PythonList dataList = new PythonList();
for (int i = 0; i < n; i++) {
res[i] = new TeeIterator(iter, dataList);
}
} else if (n != 0) {
// if you pass in a tee you get back the original tee
// and other iterators that share the same data.
TeeIterator ti = iterable as TeeIterator;
res[0] = ti;
for (int i = 1; i < n; i++) {
res[1] = new TeeIterator(ti._iter, ti._data);
}
}
return PythonTuple.MakeTuple(res);
}
/// <summary>
/// Base class used for iterator wrappers.
/// </summary>
[PythonType, PythonHidden]
public class IterBase : IEnumerator {
private IEnumerator _inner;
internal IEnumerator InnerEnumerator {
set { _inner = value; }
}
#region IEnumerator Members
object IEnumerator.Current {
get { return _inner.Current; }
}
bool IEnumerator.MoveNext() {
return _inner.MoveNext();
}
void IEnumerator.Reset() {
_inner.Reset();
}
public object __iter__() {
return this;
}
#endregion
}
[PythonType]
public class accumulate : IterBase {
private static readonly object Undefined = new object();
private readonly IEnumerator iterable;
private readonly object func;
private object total;
public accumulate(CodeContext/*!*/ context, object iterable, object func = null) {
this.iterable = PythonOps.GetEnumerator(iterable);
this.func = func;
total = Undefined;
InnerEnumerator = Accumulator(context, this.iterable, func);
}
public PythonTuple __reduce__() {
return PythonTuple.MakeTuple(
DynamicHelpers.GetPythonType(this),
PythonTuple.MakeTuple(iterable, func),
total == Undefined ? null : total
);
}
public void __setstate__(object state) {
total = state;
}
private IEnumerator<object> Accumulator(CodeContext/*!*/ context, IEnumerator iterable, object function) {
if (!MoveNextHelper(iterable)) {
yield break;
}
if (function == null) {
PythonContext pc = context.LanguageContext;
total = total == Undefined ? iterable.Current : pc.Add(total, iterable.Current);
yield return total;
while (MoveNextHelper(iterable)) {
total = pc.Add(total, iterable.Current);
yield return total;
}
} else {
total = total == Undefined ? iterable.Current : PythonCalls.Call(function, total, iterable.Current);
yield return total;
while (MoveNextHelper(iterable)) {
total = PythonCalls.Call(function, total, iterable.Current);
yield return total;
}
}
}
}
[PythonType]
public class chain : IterBase {
private IEnumerator ie;
private IEnumerator inner;
private chain() { }
public chain([NotNone] params object[] iterables) {
SetInnerEnumerator(PythonTuple.MakeTuple(iterables));
}
[ClassMethod]
public static chain from_iterable(CodeContext/*!*/ context, PythonType cls, object iterables) {
chain res;
if (cls == DynamicHelpers.GetPythonTypeFromType(typeof(chain))) {
res = new chain();
} else {
res = (chain)cls.CreateInstance(context);
}
res.SetInnerEnumerator(iterables);
return res;
}
public PythonTuple __reduce__() {
return PythonTuple.MakeTuple(
DynamicHelpers.GetPythonType(this),
PythonTuple.EMPTY,
inner == null ? PythonTuple.MakeTuple(ie) : PythonTuple.MakeTuple(ie, inner)
);
}
public void __setstate__(PythonTuple state) {
IEnumerator iter;
IEnumerator innerIter;
switch (state.Count) {
case 0: throw PythonOps.TypeError("function takes at least 1 argument (0 given)");
case 1:
iter = state[0] as IEnumerator ?? throw PythonOps.TypeError("Arguments must be iterators.");
innerIter = null;
break;
case 2:
iter = state[0] as IEnumerator ?? throw PythonOps.TypeError("Arguments must be iterators.");
innerIter = state[1] as IEnumerator ?? throw PythonOps.TypeError("Arguments must be iterators.");
break;
default:
throw PythonOps.TypeError("function takes at most 2 argument ({0} given)", state.Count);
}
ie = iter;
inner = innerIter;
InnerEnumerator = LazyYielder();
}
private void SetInnerEnumerator(object iterables) {
ie = PythonOps.GetEnumerator(iterables);
InnerEnumerator = LazyYielder();
}
private IEnumerator<object> LazyYielder() {
while (inner != null && inner.MoveNext()) {
yield return inner.Current;
}
while (ie.MoveNext()) {
inner = PythonOps.GetEnumerator(ie.Current);
while (inner.MoveNext()) {
yield return inner.Current;
}
}
}
}
[PythonType]
public class compress : IterBase {
private compress() { }
public compress(CodeContext/*!*/ context, [NotNone] object data, [NotNone] object selectors) {
EnsureIterator(context, data);
EnsureIterator(context, selectors);
InnerEnumerator = LazyYielder(data, selectors);
}
public PythonTuple __reduce__() {
// TODO
return PythonTuple.MakeTuple(
DynamicHelpers.GetPythonType(this),
PythonTuple.MakeTuple() // arguments
);
}
private static void EnsureIterator(CodeContext/*!*/ context, object iter) {
if (iter is IEnumerable || iter is IEnumerator ||
iter is IEnumerable<object> || iter is IEnumerator<object>) {
return;
}
if (iter == null ||
!PythonOps.HasAttr(context, iter, "__iter__") &&
!PythonOps.HasAttr(context, iter, "__getitem__")) {
throw PythonOps.TypeError("'{0}' object is not iterable", PythonOps.GetPythonTypeName(iter));
}
}
// (d for d, s in zip(data, selectors) if s)
private static IEnumerator<object> LazyYielder(object data, object selectors) {
IEnumerator de = PythonOps.GetEnumerator(data);
IEnumerator se = PythonOps.GetEnumerator(selectors);
while (de.MoveNext()) {
if (!se.MoveNext()) {
break;
}
if (PythonOps.IsTrue(se.Current)) {
yield return de.Current;
}
}
}
}
[PythonType]
public class count : IterBase, ICodeFormattable {
private int _curInt;
private object _step, _cur;
public count() {
_curInt = 0;
_step = 1;
InnerEnumerator = IntYielder(this, 0, 1);
}
public count(int start) {
_curInt = start;
_step = 1;
InnerEnumerator = IntYielder(this, start, 1);
}
public count(BigInteger start) {
_cur = start;
_step = 1;
InnerEnumerator = BigIntYielder(this, start, 1);
}
public count(int start = 0, int step = 1) {
_curInt = start;
_step = step;
InnerEnumerator = IntYielder(this, start, step);
}
public count([DefaultParameterValue(0)] int start, BigInteger step) {
_curInt = start;
_step = step;
InnerEnumerator = IntYielder(this, start, step);
}
public count(BigInteger start, int step) {
_cur = start;
_step = step;
InnerEnumerator = BigIntYielder(this, start, step);
}
public count(BigInteger start, BigInteger step) {
_cur = start;
_step = step;
InnerEnumerator = BigIntYielder(this, start, step);
}
public count(CodeContext/*!*/ context, [DefaultParameterValue(0)] object start, [DefaultParameterValue(1)] object step) {
EnsureNumeric(context, start);
EnsureNumeric(context, step);
_cur = start;
_step = step;
InnerEnumerator = ObjectYielder(context.LanguageContext, this, start, step);
}
private static void EnsureNumeric(CodeContext/*!*/ context, object num) {
if (num is int || num is double || num is BigInteger || num is Complex) {
return;
}
if (num == null ||
!PythonOps.HasAttr(context, num, "__int__") &&
!PythonOps.HasAttr(context, num, "__float__")) {
throw PythonOps.TypeError("a number is required");
}
}
private static IEnumerator<object> IntYielder(count c, int start, int step) {
int prev;
for (; ; ) {
prev = c._curInt;
try {
start = checked(start + step);
} catch (OverflowException) {
break;
}
c._curInt = start;
yield return prev;
}
BigInteger startBig = (BigInteger)start + step;
c._cur = startBig;
yield return prev;
for (startBig += step; ; startBig += step) {
object prevObj = c._cur;
c._cur = startBig;
yield return prevObj;
}
}
private static IEnumerator<object> IntYielder(count c, int start, BigInteger step) {
BigInteger startBig = (BigInteger)start + step;
c._cur = startBig;
yield return start;
for (startBig += step; ; startBig += step) {
object prevObj = c._cur;
c._cur = startBig;
yield return prevObj;
}
}
private static IEnumerator<BigInteger> BigIntYielder(count c, BigInteger start, int step) {
for (start += step; ; start += step) {
BigInteger prev = (BigInteger)c._cur;
c._cur = start;
yield return prev;
}
}
private static IEnumerator<BigInteger> BigIntYielder(count c, BigInteger start, BigInteger step) {
for (start += step; ; start += step) {
BigInteger prev = (BigInteger)c._cur;
c._cur = start;
yield return prev;
}
}
private static IEnumerator<object> ObjectYielder(PythonContext context, count c, object start, object step) {
start = context.Operation(PythonOperationKind.Add, start, step);
for (; ; start = context.Operation(PythonOperationKind.Add, start, step)) {
object prev = c._cur;
c._cur = start;
yield return prev;
}
}
public PythonTuple __reduce__() {
PythonTuple args;
if (StepIsOne()) {
args = PythonTuple.MakeTuple(_cur == null ? _curInt : _cur);
} else {
args = PythonTuple.MakeTuple(_cur == null ? _curInt : _cur, _step);
}
return PythonTuple.MakeTuple(DynamicHelpers.GetPythonType(this), args);
}
private bool StepIsOne() {
return _step switch {
int i => i == 1,
BigInteger bi => bi == BigInteger.One,
Extensible<BigInteger> ebi => ebi.Value == BigInteger.One,
_ => false
};
}
#region ICodeFormattable Members
public string __repr__(CodeContext/*!*/ context) {
object cur = _cur == null ? _curInt : _cur;
if (StepIsOne()) {
return string.Format("count({0})", PythonOps.Repr(context, cur));
}
return string.Format(
"count({0}, {1})",
PythonOps.Repr(context, cur),
PythonOps.Repr(context, _step)
);
}
#endregion
}
[PythonType]
public class cycle : IterBase {
public cycle(object iterable) {
InnerEnumerator = Yielder(PythonOps.GetEnumerator(iterable));
}
public PythonTuple __reduce__() {
// TODO
return PythonTuple.MakeTuple(
DynamicHelpers.GetPythonType(this),
PythonTuple.MakeTuple(), // arguments
null // state
);
}
public void __setstate__(object state) {
// TODO
}
private IEnumerator<object> Yielder(IEnumerator iter) {
PythonList result = new PythonList();
while (MoveNextHelper(iter)) {
result.AddNoLock(iter.Current);
yield return iter.Current;
}
if (result.__len__() != 0) {
for (; ; ) {
for (int i = 0; i < result.__len__(); i++) {
yield return result[i];
}
}
}
}
}
[PythonType]
public class dropwhile : IterBase {
private readonly CodeContext/*!*/ _context;
public dropwhile(CodeContext/*!*/ context, object predicate, object iterable) {
_context = context;
InnerEnumerator = Yielder(predicate, PythonOps.GetEnumerator(iterable));
}
public PythonTuple __reduce__() {
// TODO
return PythonTuple.MakeTuple(
DynamicHelpers.GetPythonType(this),
PythonTuple.MakeTuple(), // arguments
null // state
);
}
public void __setstate__(object state) {
// TODO
}
private IEnumerator<object> Yielder(object predicate, IEnumerator iter) {
PythonContext pc = _context.LanguageContext;
while (MoveNextHelper(iter)) {
if (!Converter.ConvertToBoolean(pc.CallSplat(predicate, iter.Current))) {
yield return iter.Current;
break;
}
}
while (MoveNextHelper(iter)) {
yield return iter.Current;
}
}
}
[PythonType]
public class groupby : IterBase {
private static readonly object _starterKey = new object();
private bool _fFinished = false;
private object _key;
private readonly CodeContext/*!*/ _context;
public groupby(CodeContext/*!*/ context, object iterable) {
InnerEnumerator = Yielder(PythonOps.GetEnumerator(iterable));
_context = context;
}
public groupby(CodeContext/*!*/ context, object iterable, object key) {
InnerEnumerator = Yielder(PythonOps.GetEnumerator(iterable));
_context = context;
if (key != null) {
_key = key;
}
}
public PythonTuple __reduce__() {
// TODO
return PythonTuple.MakeTuple(
DynamicHelpers.GetPythonType(this),
PythonTuple.MakeTuple(), // arguments
null // state
);
}
public void __setstate__(object state) {
// TODO
}
private IEnumerator<object> Yielder(IEnumerator iter) {
object curKey = _starterKey;
if (MoveNextHelper(iter)) {
curKey = GetKey(iter.Current);
yield return PythonTuple.MakeTuple(curKey, Grouper(iter, curKey));
while (!_fFinished) {
while (PythonContext.Equal(GetKey(iter.Current), curKey)) {
if (!MoveNextHelper(iter)) {
_fFinished = true;
yield break;
}
}
curKey = GetKey(iter.Current);
yield return PythonTuple.MakeTuple(curKey, Grouper(iter, curKey));
}
}
}
private IEnumerator<object> Grouper(IEnumerator iter, object curKey) {
while (PythonContext.Equal(GetKey(iter.Current), curKey)) {
yield return iter.Current;
if (!MoveNextHelper(iter)) {
_fFinished = true;
yield break;
}
}
}
private object GetKey(object val) {
if (_key == null) return val;
return _context.LanguageContext.CallSplat(_key, val);
}
}
[PythonType]
public class filterfalse : IterBase {
private readonly CodeContext/*!*/ _context;
public filterfalse(CodeContext/*!*/ context, object predicate, object iterable) {
_context = context;
InnerEnumerator = Yielder(predicate, PythonOps.GetEnumerator(iterable));
}
public PythonTuple __reduce__() {
// TODO
return PythonTuple.MakeTuple(
DynamicHelpers.GetPythonType(this),
PythonTuple.MakeTuple() // arguments
);
}
private IEnumerator<object> Yielder(object predicate, IEnumerator iter) {
while (MoveNextHelper(iter)) {
if (ShouldYield(predicate, iter.Current)) {
yield return iter.Current;
}
}
}
private bool ShouldYield(object predicate, object current) {
if (predicate == null) return !PythonOps.IsTrue(current);
return !Converter.ConvertToBoolean(
_context.LanguageContext.CallSplat(predicate, current)
);
}
}
[PythonType]
public class islice : IterBase {
public islice(object iterable, object stop)
: this(iterable, 0, stop, 1) {
}
public islice(object iterable, object start, object stop)
: this(iterable, start, stop, 1) {
}
public islice(object iterable, object start, object stop, object step) {
int startInt = 0, stopInt = -1;
if (start != null && !Converter.TryConvertToInt32(start, out startInt) || startInt < 0)
throw PythonOps.ValueError("start argument must be non-negative integer, ({0})", start);
if (stop != null) {
if (!Converter.TryConvertToInt32(stop, out stopInt) || stopInt < 0)
throw PythonOps.ValueError("stop argument must be non-negative integer ({0})", stop);
}
int stepInt = 1;
if (step != null && !Converter.TryConvertToInt32(step, out stepInt) || stepInt <= 0) {
throw PythonOps.ValueError("step must be 1 or greater for islice");
}
InnerEnumerator = Yielder(PythonOps.GetEnumerator(iterable), startInt, stopInt, stepInt);
}
public PythonTuple __reduce__() {
// TODO
return PythonTuple.MakeTuple(
DynamicHelpers.GetPythonType(this),
PythonTuple.MakeTuple(), // arguments
null // state
);
}
public void __setstate__(object state) {
// TODO
}
private IEnumerator<object> Yielder(IEnumerator iter, int start, int stop, int step) {
if (!MoveNextHelper(iter)) yield break;
int cur = 0;
while (cur < start) {
if (!MoveNextHelper(iter)) yield break;
cur++;
}
while (cur < stop || stop == -1) {
yield return iter.Current;
if ((cur + step) < 0) yield break; // early out if we'll overflow.
for (int i = 0; i < step; i++) {
if ((stop != -1 && ++cur >= stop) || !MoveNextHelper(iter)) {
yield break;
}
}
}
}
}
[PythonType]
public class zip_longest : IEnumerator {
private readonly IEnumerator[]/*!*/ _iters;
private readonly object _fill;
private PythonTuple _current;
public zip_longest([NotNone] params object[] iterables) {
_iters = new IEnumerator[iterables.Length];
for (int i = 0; i < iterables.Length; i++) {
_iters[i] = PythonOps.GetEnumerator(iterables[i]);
}
}
public zip_longest([ParamDictionary] IDictionary<object, object> paramDict, [NotNone] params object[] iterables) {
object fill;
if (paramDict.TryGetValue("fillvalue", out fill)) {
_fill = fill;
if (paramDict.Count != 1) {
paramDict.Remove("fillvalue");
throw UnexpectedKeywordArgument(paramDict);
}
} else if (paramDict.Count != 0) {
throw UnexpectedKeywordArgument(paramDict);
}
_iters = new IEnumerator[iterables.Length];
for (int i = 0; i < iterables.Length; i++) {
_iters[i] = PythonOps.GetEnumerator(iterables[i]);
}
}
public PythonTuple __reduce__() {
// TODO
return PythonTuple.MakeTuple(
DynamicHelpers.GetPythonType(this),
PythonTuple.MakeTuple(), // arguments
null // state
);
}
public void __setstate__(object state) {
// TODO
}
#region IEnumerator Members
object IEnumerator.Current {
get {
return _current;
}
}
bool IEnumerator.MoveNext() {
if (_iters.Length == 0) return false;
object[] current = new object[_iters.Length];
bool gotValue = false;
for (int i = 0; i < _iters.Length; i++) {
if (!MoveNextHelper(_iters[i])) {
current[i] = _fill;
} else {
// values need to be extraced and saved as we move incase
// the user passed the same iterable multiple times.
gotValue = true;
current[i] = _iters[i].Current;
}
}
if (gotValue) {
_current = PythonTuple.MakeTuple(current);
return true;
}
return false;
}
void IEnumerator.Reset() {
throw new NotImplementedException("The method or operation is not implemented.");
}
public object __iter__() {
return this;
}
#endregion
}
private static Exception UnexpectedKeywordArgument(IDictionary<object, object> paramDict) {
foreach (object name in paramDict.Keys) {
return PythonOps.TypeError("got unexpected keyword argument {0}", name);
}
throw new InvalidOperationException();
}
[PythonType]
public class product : IterBase {
private PythonTuple[] tuples;
public product(CodeContext context, [NotNone] params object[] iterables) {
tuples = ArrayUtils.ConvertAll(iterables, x => new PythonTuple(context, PythonOps.GetEnumerator(x)));
InnerEnumerator = Yielder(tuples);
}
public product(CodeContext context, [ParamDictionary] IDictionary<object, object> paramDict, [NotNone] params object[] iterables) {
object repeat;
int iRepeat = 1;
if (paramDict.TryGetValue("repeat", out repeat)) {
if (repeat is int) {
iRepeat = (int)repeat;
} else {
throw PythonOps.TypeError("an integer is required");
}
if (paramDict.Count != 1) {
paramDict.Remove("repeat");
throw UnexpectedKeywordArgument(paramDict);
}
} else if (paramDict.Count != 0) {
throw UnexpectedKeywordArgument(paramDict);
}
tuples = new PythonTuple[iterables.Length * iRepeat];
for (int i = 0; i < iRepeat; i++) {
for (int j = 0; j < iterables.Length; j++) {
tuples[i * iterables.Length + j] = new PythonTuple(context, iterables[j]);
}
}
InnerEnumerator = Yielder(tuples);
}
public PythonTuple __reduce__() {
return PythonTuple.MakeTuple(
DynamicHelpers.GetPythonType(this),
PythonTuple.MakeTuple(tuples), // arguments
null // TODO: state
);
}
public void __setstate__(object state) {
// TODO
}
private IEnumerator<object> Yielder(PythonTuple[] iterables) {
if (iterables.Length > 0) {
IEnumerator[] enums = new IEnumerator[iterables.Length];
enums[0] = iterables[0].GetEnumerator();
int curDepth = 0;
do {
if (enums[curDepth].MoveNext()) {
if (curDepth == enums.Length - 1) {
// create a new array so we don't mutate previous tuples
object[] final = new object[enums.Length];
for (int j = 0; j < enums.Length; j++) {
final[j] = enums[j].Current;
}
yield return PythonTuple.MakeTuple(final);
} else {
// going to the next depth, get a new enumerator
curDepth++;
enums[curDepth] = iterables[curDepth].GetEnumerator();
}
} else {
// current depth exhausted, go to the previous iterator
curDepth--;
}
} while (curDepth != -1);
} else {
yield return PythonTuple.EMPTY;
}
tuples = new PythonTuple[1] { PythonTuple.EMPTY };
}
}
[PythonType]
public class combinations : IterBase {
private readonly PythonList _data;
public combinations(CodeContext context, object iterable, object r) {
_data = new PythonList(context, iterable);
InnerEnumerator = Yielder(GetR(r, _data));
}
public PythonTuple __reduce__() {
// TODO
return PythonTuple.MakeTuple(
DynamicHelpers.GetPythonType(this),
PythonTuple.MakeTuple(), // arguments
null // state
);
}
public void __setstate__(object state) {
// TODO
}
private IEnumerator<object> Yielder(int r) {
IEnumerator[] enums = new IEnumerator[r];
if (r > 0) {
enums[0] = _data.GetEnumerator();
int curDepth = 0;
int[] curIndices = new int[enums.Length];
do {
if (enums[curDepth].MoveNext()) {
curIndices[curDepth]++;
bool shouldSkip = false;
for (int i = 0; i < curDepth; i++) {
if (curIndices[i] >= curIndices[curDepth]) {
// skip if we've already seen this index or a higher
// index elsewhere
shouldSkip = true;
break;
}
}
if (!shouldSkip) {
if (curDepth == enums.Length - 1) {
// create a new array so we don't mutate previous tuples
object[] final = new object[r];
for (int j = 0; j < enums.Length; j++) {
final[j] = enums[j].Current;
}
yield return PythonTuple.MakeTuple(final);
} else {
// going to the next depth, get a new enumerator
curDepth++;
enums[curDepth] = _data.GetEnumerator();
curIndices[curDepth] = 0;
}
}
} else {
// current depth exhausted, go to the previous iterator
curDepth--;
}
} while (curDepth != -1);
} else {
yield return PythonTuple.EMPTY;
}
}
}
[PythonType]
public class combinations_with_replacement : IterBase {
private readonly PythonList _data;
public combinations_with_replacement(CodeContext context, object iterable, object r) {
_data = new PythonList(context, iterable);
InnerEnumerator = Yielder(GetR(r, _data));
}
public PythonTuple __reduce__() {
// TODO
return PythonTuple.MakeTuple(
DynamicHelpers.GetPythonType(this),
PythonTuple.MakeTuple(), // arguments
null // state
);
}
public void __setstate__(object state) {
// TODO
}
private IEnumerator<object> Yielder(int r) {
IEnumerator[] enums = new IEnumerator[r];
if (r > 0) {
enums[0] = _data.GetEnumerator();
int curDepth = 0;
int[] curIndices = new int[enums.Length];
do {
if (enums[curDepth].MoveNext()) {
curIndices[curDepth]++;
bool shouldSkip = false;
for (int i = 0; i < curDepth; i++) {
if (curIndices[i] > curIndices[curDepth]) {
// skip if we've already seen a higher index elsewhere
shouldSkip = true;
break;
}
}
if (!shouldSkip) {
if (curDepth == enums.Length - 1) {
// create a new array so we don't mutate previous tuples
object[] final = new object[r];
for (int j = 0; j < enums.Length; j++) {
final[j] = enums[j].Current;
}
yield return PythonTuple.MakeTuple(final);
} else {
// going to the next depth, get a new enumerator
curDepth++;
enums[curDepth] = _data.GetEnumerator();
curIndices[curDepth] = 0;
}
}
} else {
// current depth exhausted, go to the previous iterator
curDepth--;
}
} while (curDepth != -1);
} else {
yield return PythonTuple.EMPTY;
}
}
}
[PythonType]
public class permutations : IterBase {
private readonly PythonList _data;
public permutations(CodeContext context, object iterable) {
_data = new PythonList(context, iterable);
InnerEnumerator = Yielder(_data.Count);
}
public permutations(CodeContext context, object iterable, object r) {
_data = new PythonList(context, iterable);
InnerEnumerator = Yielder(GetR(r, _data));
}
public PythonTuple __reduce__() {
// TODO
return PythonTuple.MakeTuple(
DynamicHelpers.GetPythonType(this),