Repository navigation
Expand file tree
/
Copy pathArray.cs
More file actions
188 lines (155 loc) · 6.7 KB
/
Copy pathArray.cs
File metadata and controls
188 lines (155 loc) · 6.7 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
// 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;
using System.Collections;
using System.Numerics;
using System.Text;
using Microsoft.Scripting.Runtime;
using IronPython.Runtime;
using IronPython.Runtime.Operations;
using IronPython.Runtime.Types;
using System.Collections.Generic;
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");
}
nativeType.SetValue(_memHolder, 0, args);
}
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[[NotNull]Slice slice] {
get {
int start, stop, step;
int size = ((ArrayType)NativeType).Length;
SimpleType elemType = ((ArrayType)NativeType).ElementType as SimpleType;
slice.indices(size, out start, out stop, out step);
if ((step > 0 && start >= stop) || (step < 0 && start <= stop)) {
if (elemType != null && (elemType._type == SimpleTypeKind.WChar || elemType._type == SimpleTypeKind.Char)) {
return String.Empty;
}
return new PythonList();
}
int n = (int)(step > 0 ? (0L + stop - start + step - 1) / step : (0L + stop - start + step + 1) / step);
if (elemType != null && (elemType._type == SimpleTypeKind.WChar || elemType._type == SimpleTypeKind.Char)) {
int elmSize = ((INativeType)elemType).Size;
StringBuilder res = new StringBuilder(n);
for (int i = 0, index = start; i < n; i++, index += step) {
char c = elemType.ReadChar(_memHolder, checked(index * elmSize));
res.Append(c);
}
return res.ToString();
} else {
object[] ret = new object[n];
int ri = 0;
for (int i = 0, index = start; i < n; i++, index += step) {
ret[ri++] = this[index];
}
return new PythonList(ret);
}
}
set {
int start, stop, step;
int size = ((ArrayType)NativeType).Length;
slice.indices(size, out start, out stop, out step);
int n = (int)(step > 0 ? (0L + stop - start + step - 1) / step : (0L + stop - start + step + 1) / step);
IEnumerator ie = PythonOps.GetEnumerator(value);
for (int i = 0, index = start; i < n; 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() {
INativeType elemType = ElementType;
int dimensions = 1;
List<object> shape = new List<object>();
shape.Add((BigInteger)__len__());
while (elemType is ArrayType) {
dimensions++;
shape.Add((BigInteger)((ArrayType)elemType).Length);
elemType = ((ArrayType)elemType).ElementType;
}
return PythonTuple.MakeTuple(
NativeType.TypeFormat,
dimensions,
PythonTuple.Make(shape)
);
}
#region IBufferProtocol
public override int ItemCount {
[PythonHidden]
get {
return __len__();
}
}
public override BigInteger ItemSize {
[PythonHidden]
get {
var curType = this.NativeType;
while (curType is ArrayType) {
curType = ((ArrayType)curType).ElementType;
}
return curType.Size;
}
}
[PythonHidden]
public override IList<BigInteger> GetShape(int start, int? end) {
List<BigInteger> shape = new List<BigInteger>();
var curType = this.NativeType as ArrayType;
while (curType != null) {
shape.Add(curType.Length);
curType = curType.ElementType as ArrayType;
}
return shape;
}
#endregion
}
}
}
#endif