forked from IronLanguages/ironpython3
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPythonTuple.cs
More file actions
704 lines (549 loc) · 21.8 KB
/
PythonTuple.cs
File metadata and controls
704 lines (549 loc) · 21.8 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
// 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 MSAst = System.Linq.Expressions;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Numerics;
using System.Text;
using Microsoft.Scripting;
using Microsoft.Scripting.Runtime;
using Microsoft.Scripting.Utils;
using IronPython.Compiler.Ast;
using IronPython.Runtime.Exceptions;
using IronPython.Runtime.Operations;
using IronPython.Runtime.Types;
using Utils = Microsoft.Scripting.Ast.Utils;
namespace IronPython.Runtime {
using Ast = MSAst.Expression;
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1710:IdentifiersShouldHaveCorrectSuffix")]
[PythonType("tuple"), Serializable, DebuggerTypeProxy(typeof(CollectionDebugProxy)), DebuggerDisplay("tuple, {Count} items")]
public class PythonTuple : IList, IList<object>, ICodeFormattable, IExpressionSerializable, IStructuralEquatable, IStructuralComparable, IReadOnlyList<object> {
internal readonly object[] _data;
internal static readonly PythonTuple EMPTY = new PythonTuple();
public PythonTuple(object o) {
_data = MakeItems(o);
}
protected PythonTuple(object[] items) {
_data = items;
}
public PythonTuple() {
_data = ArrayUtils.EmptyObjects;
}
internal PythonTuple(PythonTuple other, object o) {
_data = other.Expand(o);
}
#region Python Constructors
// Tuples are immutable so their initialization happens in __new__
// They also explicitly implement __new__ so they can perform the
// appropriate caching.
public static PythonTuple __new__(CodeContext context, PythonType cls) {
if (cls == TypeCache.PythonTuple) {
return EMPTY;
} else {
if (!(cls.CreateInstance(context) is PythonTuple tupObj)) throw PythonOps.TypeError("{0} is not a subclass of tuple", cls);
return tupObj;
}
}
public static PythonTuple __new__(CodeContext context, PythonType cls, object sequence) {
if (sequence == null) throw PythonOps.TypeError("iteration over a non-sequence");
if (cls == TypeCache.PythonTuple) {
if (sequence.GetType() == typeof(PythonTuple)) return (PythonTuple)sequence;
return new PythonTuple(MakeItems(sequence));
} else {
if (!(cls.CreateInstance(context, sequence) is PythonTuple tupObj)) throw PythonOps.TypeError("{0} is not a subclass of tuple", cls);
return tupObj;
}
}
#endregion
#region Python 2.6 Methods
public int index(object obj, object start) {
return index(obj, Converter.ConvertToIndex(start), _data.Length);
}
public int index(object obj, int start = 0) {
return index(obj, start, _data.Length);
}
public int index(object obj, object start, object end) {
return index(obj, Converter.ConvertToIndex(start), Converter.ConvertToIndex(end));
}
public int index(object obj, int start, int end) {
start = PythonOps.FixSliceIndex(start, _data.Length);
end = PythonOps.FixSliceIndex(end, _data.Length);
for (int i = start; i < end; i++) {
if (PythonOps.IsOrEqualsRetBool(obj, _data[i])) {
return i;
}
}
throw PythonOps.ValueError("tuple.index(x): x not in list");
}
public int count(object obj) {
int cnt = 0;
foreach (object elem in _data) {
if (PythonOps.IsOrEqualsRetBool(obj, elem)) {
cnt++;
}
}
return cnt;
}
#endregion
internal static PythonTuple Make(object o) {
if (o is PythonTuple) return (PythonTuple)o;
return new PythonTuple(MakeItems(o));
}
internal static PythonTuple MakeTuple(params object[] items) {
if (items.Length == 0) return EMPTY;
return new PythonTuple(items);
}
private static object[] MakeItems(object o) {
var t = o.GetType();
// Only use fast paths if we have an exact tuple/list, otherwise use iter
if (t == typeof(PythonTuple)) {
return ((PythonTuple)o)._data;
} else if (t == typeof(PythonList)) {
return ((PythonList)o).GetObjectArray();
} else if (o is string) {
string s = (string)o;
object[] res = new object[s.Length];
for (int i = 0; i < res.Length; i++) {
res[i] = ScriptingRuntimeHelpers.CharToString(s[i]);
}
return res;
} else if (o is object[] arr) {
return ArrayOps.CopyArray(arr, arr.Length);
} else {
PerfTrack.NoteEvent(PerfTrack.Categories.OverAllocate, "TupleOA: " + PythonTypeOps.GetName(o));
List<object> l = new List<object>();
IEnumerator i = PythonOps.GetEnumerator(o);
while (i.MoveNext()) {
l.Add(i.Current);
}
return l.ToArray();
}
}
/// <summary>
/// Return a copy of this tuple's data array.
/// </summary>
internal object[] ToArray() {
return ArrayOps.CopyArray(_data, _data.Length);
}
#region ISequence Members
public virtual int __len__() {
return _data.Length;
}
public virtual object this[int index] {
get {
return _data[PythonOps.FixIndex(index, _data.Length)];
}
}
public virtual object this[object index] {
get {
return this[Converter.ConvertToIndex(index)];
}
}
public virtual object this[BigInteger index] {
get {
return this[(int)index];
}
}
public virtual object this[Slice slice] {
get {
int start, stop, step;
slice.indices(_data.Length, out start, out stop, out step);
if (start == 0 && stop == _data.Length && step == 1 &&
this.GetType() == typeof(PythonTuple)) {
return this;
}
return MakeTuple(ArrayOps.GetSlice(_data, start, stop, step));
}
}
#endregion
#region binary operators
public static PythonTuple operator +([NotNull]PythonTuple x, object y) {
if (y is PythonTuple t) return x + t;
throw PythonOps.TypeError($"can only concatenate tuple (not \"{PythonTypeOps.GetName(y)}\") to tuple");
}
public static PythonTuple operator +([NotNull]PythonTuple x, [NotNull]PythonTuple y) {
return MakeTuple(ArrayOps.Add(x._data, x._data.Length, y._data, y._data.Length));
}
private static PythonTuple MultiplyWorker(PythonTuple self, int count) {
if (count <= 0) {
return EMPTY;
} else if (count == 1 && self.GetType() == typeof(PythonTuple)) {
return self;
}
return MakeTuple(ArrayOps.Multiply(self._data, self._data.Length, count));
}
public static PythonTuple operator *(PythonTuple x, int n) {
return MultiplyWorker(x, n);
}
public static PythonTuple operator *(int n, PythonTuple x) {
return MultiplyWorker(x, n);
}
public static object operator *([NotNull]PythonTuple self, [NotNull]Index count) {
return PythonOps.MultiplySequence<PythonTuple>(MultiplyWorker, self, count, true);
}
public static object operator *([NotNull]Index count, [NotNull]PythonTuple self) {
return PythonOps.MultiplySequence<PythonTuple>(MultiplyWorker, self, count, false);
}
public static object operator *([NotNull]PythonTuple self, object count) {
int index;
if (Converter.TryConvertToIndex(count, out index)) {
return self * index;
}
throw PythonOps.TypeErrorForUnIndexableObject(count);
}
public static object operator *(object count, [NotNull]PythonTuple self) {
int index;
if (Converter.TryConvertToIndex(count, out index)) {
return index * self;
}
throw PythonOps.TypeErrorForUnIndexableObject(count);
}
#endregion
#region ICollection Members
bool ICollection.IsSynchronized {
get { return false; }
}
public int Count {
[PythonHidden]
get { return _data.Length; }
}
[PythonHidden]
public void CopyTo(Array array, int index) {
Array.Copy(_data, 0, array, index, _data.Length);
}
object ICollection.SyncRoot {
get {
return this;
}
}
#endregion
public virtual IEnumerator __iter__() {
return new TupleEnumerator(this);
}
#region IEnumerable Members
[PythonHidden]
public IEnumerator GetEnumerator() {
return __iter__();
}
#endregion
private object[] Expand(object value) {
object[] args;
int length = _data.Length;
if (value == null)
args = new object[length];
else
args = new object[length + 1];
for (int i = 0; i < length; i++) {
args[i] = _data[i];
}
if (value != null) {
args[length] = value;
}
return args;
}
public object __getnewargs__() {
// Call "new Tuple()" to force result to be a Tuple (otherwise, it could possibly be a Tuple subclass)
return PythonTuple.MakeTuple(new PythonTuple(this));
}
#region IEnumerable<object> Members
IEnumerator<object> IEnumerable<object>.GetEnumerator() {
return new TupleEnumerator(this);
}
#endregion
#region IList<object> Members
[PythonHidden]
public int IndexOf(object item) {
for (int i = 0; i < Count; i++) {
if (PythonOps.IsOrEqualsRetBool(this[i], item)) return i;
}
return -1;
}
void IList<object>.Insert(int index, object item) {
throw new InvalidOperationException("Tuple is readonly");
}
void IList<object>.RemoveAt(int index) {
throw new InvalidOperationException("Tuple is readonly");
}
object IList<object>.this[int index] {
get {
return this[index];
}
set {
throw new InvalidOperationException("Tuple is readonly");
}
}
#endregion
#region ICollection<object> Members
void ICollection<object>.Add(object item) {
throw new InvalidOperationException("Tuple is readonly");
}
void ICollection<object>.Clear() {
throw new InvalidOperationException("Tuple is readonly");
}
[PythonHidden]
public bool Contains(object item) {
for (int i = 0; i < _data.Length; i++) {
if (PythonOps.IsOrEqualsRetBool(_data[i], item)) {
return true;
}
}
return false;
}
[PythonHidden]
public void CopyTo(object[] array, int arrayIndex) {
for (int i = 0; i < Count; i++) {
array[arrayIndex + i] = this[i];
}
}
bool ICollection<object>.IsReadOnly {
get { return true; }
}
bool ICollection<object>.Remove(object item) {
throw new InvalidOperationException("Tuple is readonly");
}
#endregion
#region Rich Comparison Members
internal int CompareTo(PythonTuple other) {
return PythonOps.CompareArrays(_data, _data.Length, other._data, other._data.Length);
}
public static bool operator >([NotNull]PythonTuple self, [NotNull]PythonTuple other) {
return self.CompareTo(other) > 0;
}
public static bool operator <([NotNull]PythonTuple self, [NotNull]PythonTuple other) {
return self.CompareTo(other) < 0;
}
public static bool operator >=([NotNull]PythonTuple self, [NotNull]PythonTuple other) {
return self.CompareTo(other) >= 0;
}
public static bool operator <=([NotNull]PythonTuple self, [NotNull]PythonTuple other) {
return self.CompareTo(other) <= 0;
}
#endregion
#region IStructuralComparable Members
int IStructuralComparable.CompareTo(object obj, IComparer comparer) {
if (!(obj is PythonTuple other)) {
throw new ValueErrorException("expected tuple");
}
return PythonOps.CompareArrays(_data, _data.Length, other._data, other._data.Length, comparer);
}
#endregion
public override bool Equals(object obj) {
if (!Object.ReferenceEquals(this, obj)) {
if (!(obj is PythonTuple other) || _data.Length != other._data.Length) {
return false;
}
for (int i = 0; i < _data.Length; i++) {
object obj1 = this[i], obj2 = other[i];
if (Object.ReferenceEquals(obj1, obj2)) {
continue;
} else if (obj1 != null) {
if (!obj1.Equals(obj2)) {
return false;
}
} else {
return false;
}
}
}
return true;
}
public override int GetHashCode() {
int hash1 = 6551;
int hash2 = hash1;
for (int i = 0; i < _data.Length; i += 2) {
hash1 = ((hash1 << 27) + ((hash2 + 1) << 1) + (hash1 >> 5)) ^ (_data[i] == null ? NoneTypeOps.NoneHashCode : _data[i].GetHashCode());
if (i == _data.Length - 1) {
break;
}
hash2 = ((hash2 << 5) + ((hash1 - 1) >> 1) + (hash2 >> 27)) ^ (_data[i + 1] == null ? NoneTypeOps.NoneHashCode : _data[i + 1].GetHashCode());
}
return hash1 + (hash2 * 1566083941);
}
private int GetHashCode(HashDelegate dlg) {
int hash1 = 6551;
int hash2 = hash1;
for (int i = 0; i < _data.Length; i += 2) {
hash1 = ((hash1 << 27) + ((hash2 + 1) << 1) + (hash1 >> 5)) ^ dlg(_data[i], ref dlg);
if (i == _data.Length - 1) {
break;
}
hash2 = ((hash2 << 5) + ((hash1 - 1) >> 1) + (hash2 >> 27)) ^ dlg(_data[i + 1], ref dlg);
}
return hash1 + (hash2 * 1566083941);
}
private int GetHashCode(IEqualityComparer comparer) {
int hash1 = 6551;
int hash2 = hash1;
for (int i = 0; i < _data.Length; i += 2) {
hash1 = ((hash1 << 27) + ((hash2 + 1) << 1) + (hash1 >> 5)) ^ comparer.GetHashCode(_data[i]);
if (i == _data.Length - 1) {
break;
}
hash2 = ((hash2 << 5) + ((hash1 - 1) >> 1) + (hash2 >> 27)) ^ comparer.GetHashCode(_data[i + 1]);
}
return hash1 + (hash2 * 1566083941);
}
public override string ToString() {
return __repr__(DefaultContext.Default);
}
#region IStructuralEquatable Members
int IStructuralEquatable.GetHashCode(IEqualityComparer comparer) {
// Optimization for when comparer is IronPython's default IEqualityComparer
if (comparer is PythonContext.PythonEqualityComparer pythonComparer) {
return GetHashCode(pythonComparer.Context.InitialHasher);
}
return GetHashCode(comparer);
}
bool IStructuralEquatable.Equals(object other, IEqualityComparer comparer) {
if (!Object.ReferenceEquals(other, this)) {
if (!(other is PythonTuple l) || _data.Length != l._data.Length) {
return false;
}
for (int i = 0; i < _data.Length; i++) {
object obj1 = _data[i], obj2 = l._data[i];
if (Object.ReferenceEquals(obj1, obj2)) {
continue;
} else if (!comparer.Equals(obj1, obj2)) {
return false;
}
}
}
return true;
}
#endregion
#region ICodeFormattable Members
public virtual string/*!*/ __repr__(CodeContext/*!*/ context) {
StringBuilder buf = new StringBuilder();
buf.Append("(");
for (int i = 0; i < _data.Length; i++) {
if (i > 0) buf.Append(", ");
buf.Append(PythonOps.Repr(context, _data[i]));
}
if (_data.Length == 1) buf.Append(",");
buf.Append(")");
return buf.ToString();
}
#endregion
#region IList Members
int IList.Add(object value) {
throw new InvalidOperationException("Tuple is readonly");
}
void IList.Clear() {
throw new InvalidOperationException("Tuple is readonly");
}
void IList.Insert(int index, object value) {
throw new InvalidOperationException("Tuple is readonly");
}
bool IList.IsFixedSize {
get { return true; }
}
bool IList.IsReadOnly {
get { return true; }
}
void IList.Remove(object value) {
throw new InvalidOperationException("Tuple is readonly");
}
void IList.RemoveAt(int index) {
throw new InvalidOperationException("Tuple is readonly");
}
object IList.this[int index] {
get {
return this[index];
}
set {
throw new InvalidOperationException("Tuple is readonly");
}
}
#endregion
#region IExpressionSerializable Members
public MSAst.Expression CreateExpression() {
Ast[] items = new Ast[Count];
for (int i = 0; i < items.Length; i++) {
items[i] = Expression.Convert(Utils.Constant(this[i]), typeof(object));
}
return Ast.Call(
AstMethods.MakeTuple,
Ast.NewArrayInit(
typeof(object),
items
)
);
}
#endregion
}
/// <summary>
/// public class to get optimized
/// </summary>
[PythonType("tuple_iterator")]
public sealed class TupleEnumerator : IEnumerable, IEnumerator, IEnumerator<object> {
private int _curIndex;
private PythonTuple _tuple;
public TupleEnumerator(PythonTuple t) {
_tuple = t;
_curIndex = -1;
}
#region IEnumerator Members
public object Current {
get {
// access _data directly because this is what CPython does:
// class T(tuple):
// def __getitem__(self): return None
//
// for x in T((1,2)): print x
// prints 1 and 2
return _tuple._data[_curIndex];
}
}
public bool MoveNext() {
if ((_curIndex + 1) >= _tuple.Count) {
return false;
}
_curIndex++;
return true;
}
public void Reset() {
_curIndex = -1;
}
#endregion
#region IDisposable Members
public void Dispose() {
GC.SuppressFinalize(this);
}
#endregion
#region IEnumerable Members
public IEnumerator GetEnumerator() {
return this;
}
#endregion
public PythonTuple __reduce__(CodeContext/*!*/ context) {
object iter;
context.TryLookupBuiltin("iter", out iter);
return PythonTuple.MakeTuple(iter, PythonTuple.MakeTuple(_tuple), _curIndex + 1);
}
public void __setstate__(int position) {
if (position < 0) position = 0;
else if (position > _tuple.Count) position = _tuple.Count;
_curIndex = position - 1;
}
public int __length_hint__() {
return _tuple.__len__() - _curIndex - 1;
}
}
internal static class TupleExtensions {
public static PythonTuple ToPythonTuple<T1>(this Tuple<T1> value) {
return PythonTuple.MakeTuple(value.Item1);
}
public static PythonTuple ToPythonTuple<T1, T2>(this Tuple<T1, T2> value) {
return PythonTuple.MakeTuple(value.Item1, value.Item2);
}
public static PythonTuple ToPythonTuple<T1, T2, T3>(this Tuple<T1, T2, T3> value) {
return PythonTuple.MakeTuple(value.Item1, value.Item2, value.Item3);
}
public static PythonTuple ToPythonTuple<T1, T2, T3, T4>(this Tuple<T1, T2, T3, T4> value) {
return PythonTuple.MakeTuple(value.Item1, value.Item2, value.Item3, value.Item4);
}
}
}