forked from IronLanguages/ironpython3
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArray.cs
More file actions
189 lines (160 loc) · 7.06 KB
/
Array.cs
File metadata and controls
189 lines (160 loc) · 7.06 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
// 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.
#if FEATURE_CTYPES
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Numerics;
using System.Text;
using IronPython.Runtime;
using IronPython.Runtime.Operations;
using IronPython.Runtime.Types;
namespace IronPython.Modules {
/// <summary>
/// Provides support for interop with native code from Python code.
/// </summary>
public static partial class CTypes {
[PythonType("Array")]
public abstract class _Array : CData {
public void __init__(params object[] args) {
INativeType nativeType = NativeType;
MemHolder = new MemoryHolder(nativeType.Size);
if (args.Length > ((ArrayType)nativeType).Length) {
throw PythonOps.IndexError("too many arguments");
}
INativeType elementType = ElementType;
for (var i = 0; i < args.Length; i++) {
elementType.SetValue(MemHolder, checked(i * elementType.Size), args[i]);
}
}
public object this[int index] {
get {
index = PythonOps.FixIndex(index, ((ArrayType)NativeType).Length);
INativeType elementType = ElementType;
return elementType.GetValue(
MemHolder,
this,
checked(index * elementType.Size),
false
);
}
set {
index = PythonOps.FixIndex(index, ((ArrayType)NativeType).Length);
INativeType elementType = ElementType;
object keepAlive = elementType.SetValue(
MemHolder,
checked(index * elementType.Size),
value
);
if (keepAlive != null) {
MemHolder.AddObject(index.ToString(), keepAlive);
}
}
}
public object this[[NotNone] Slice slice] {
get {
int start, stop, step, count;
int size = ((ArrayType)NativeType).Length;
SimpleType elemType = ((ArrayType)NativeType).ElementType as SimpleType;
slice.GetIndicesAndCount(size, out start, out stop, out step, out count);
if ((step > 0 && start >= stop) || (step < 0 && start <= stop)) {
if (elemType != null) {
if (elemType._type == SimpleTypeKind.Char) return Bytes.Empty;
if (elemType._type == SimpleTypeKind.WChar) return string.Empty;
}
return new PythonList();
}
if (elemType != null) {
if (elemType._type == SimpleTypeKind.Char) {
Debug.Assert(((INativeType)elemType).Size == 1);
if (step == 1) {
return MemHolder.ReadBytes(start, count);
} else {
var buffer = new byte[count];
for (int i = 0, index = start; i < count; i++, index += step) {
buffer[i] = MemHolder.ReadByte(index);
}
return Bytes.Make(buffer);
}
}
if (elemType._type == SimpleTypeKind.WChar) {
int elmSize = ((INativeType)elemType).Size;
if (step == 1) {
return MemHolder.ReadUnicodeString(checked(start * elmSize), count);
} else {
var res = new StringBuilder(count);
for (int i = 0, index = start; i < count; i++, index += step) {
res.Append((char)MemHolder.ReadInt16(checked(index * elmSize)));
}
return res.ToString();
}
}
}
object[] ret = new object[count];
for (int i = 0, index = start; i < count; i++, index += step) {
ret[i] = this[index];
}
return PythonList.FromArrayNoCopy(ret);
}
set {
int start, stop, step, count;
int size = ((ArrayType)NativeType).Length;
slice.GetIndicesAndCount(size, out start, out stop, out step, out count);
IEnumerator ie = PythonOps.GetEnumerator(value);
for (int i = 0, index = start; i < count; i++, index += step) {
if (!ie.MoveNext()) {
throw PythonOps.ValueError("sequence not long enough");
}
this[index] = ie.Current;
}
if (ie.MoveNext()) {
throw PythonOps.ValueError("not all values consumed while slicing");
}
}
}
public int __len__() {
return ((ArrayType)NativeType).Length;
}
private INativeType ElementType {
get {
return ((ArrayType)NativeType).ElementType;
}
}
internal override PythonTuple GetBufferInfo() {
var shape = Shape;
return PythonTuple.MakeTuple(
NativeType.TypeFormat,
shape.Count,
PythonTuple.Make(shape)
);
}
#region IBufferProtocol
[PythonHidden]
public override int ItemSize {
get {
var curType = NativeType;
while (curType is ArrayType arrayType) {
curType = arrayType.ElementType;
}
return curType.Size;
}
}
[PythonHidden]
public override IReadOnlyList<int> Shape {
get {
List<int> shape = new List<int>();
var curType = NativeType;
while (curType is ArrayType arrayType) {
shape.Add(arrayType.Length);
curType = arrayType.ElementType;
}
return shape;
}
}
// TODO: if this.NativeType.ElementType is ArrayType, suboffsets need to be reported
#endregion
}
}
}
#endif