-
Notifications
You must be signed in to change notification settings - Fork 255
Expand file tree
/
Copy pathUint8Array.cs
More file actions
90 lines (73 loc) · 2.42 KB
/
Uint8Array.cs
File metadata and controls
90 lines (73 loc) · 2.42 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
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
namespace AlphaTab.Core.EcmaScript
{
public class Uint8Array : IEnumerable<byte>, IEnumerable<double>
{
private ArraySegment<byte> _data;
public double Length => _data.Count;
public double ByteOffset => _data.Offset;
public ArrayBuffer Buffer => new ArrayBuffer(_data);
public ArraySegment<byte> Data => _data;
public Uint8Array(IList<double> data)
{
_data = new ArraySegment<byte>(data.Select(d => (byte)d).ToArray());
}
public Uint8Array() : this(System.Array.Empty<byte>())
{
}
public Uint8Array(byte[] data)
{
_data = new ArraySegment<byte>(data);
}
private Uint8Array(ArraySegment<byte> data)
{
_data = data;
}
public Uint8Array(double size)
: this(new byte[(int)size])
{
}
public Uint8Array(IEnumerable<int> values)
: this(values.Select(d => (byte)d).ToArray())
{
}
public double this[double index]
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => _data.Array![_data.Offset + (int)index];
[MethodImpl(MethodImplOptions.AggressiveInlining)]
set => _data.Array![_data.Offset + (int)index] = (byte)value;
}
public Uint8Array Subarray(double begin, double end)
{
return new Uint8Array(new ArraySegment<byte>(_data.Array!, _data.Offset + (int)begin,
(int)(end - begin)));
}
public void Set(Uint8Array subarray, double pos)
{
var buffer = subarray.Buffer.Raw;
System.Buffer.BlockCopy(buffer.Array!, buffer.Offset, _data.Array!,
_data.Offset + (int)pos, buffer.Count);
}
public static implicit operator Uint8Array(byte[] v)
{
return new Uint8Array(v);
}
IEnumerator<double> IEnumerable<double>.GetEnumerator()
{
return _data.Select(d => (double)d).GetEnumerator();
}
public IEnumerator<byte> GetEnumerator()
{
return ((IEnumerable<byte>)_data).GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
}
}