/
FloatBuffer.hx
118 lines (95 loc) · 2.63 KB
/
FloatBuffer.hx
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
package hxd;
import hxd.impl.TypedArray;
private typedef InnerData = #if js Float32Expand #else Array<hxd.impl.Float32> #end
#if js
private abstract Float32Expand({ pos : Int, array : hxd.impl.TypedArray.Float32Array }) {
public var length(get, set) : Int;
public function new(length) {
this = { pos : length, array : new Float32Array(new ArrayBuffer(length<<2)) };
}
inline function get_length() return this.pos;
inline function set_length(v:Int) {
if( length != v ) {
var newArray = new Float32Array(v);
newArray.set(this.array);
this.array = newArray;
}
this.pos = v;
return v;
}
public inline function push(v:Float) {
if( this.pos == this.array.length ) {
var newSize = this.array.length << 1;
if( newSize < 128 ) newSize = 128;
var newArray = new Float32Array(newSize);
newArray.set(this.array);
this.array = newArray;
}
this.array[this.pos++] = v;
}
@:arrayAccess inline function get(index) return this.array[index];
@:arrayAccess inline function set(index,v:Float) return this.array[index] = v;
@:to inline function toF32Array() return this.array;
@:to inline function toArray() return [for( i in 0...this.pos ) this.array[i]];
}
#end
private class InnerIterator {
var b : InnerData;
var len : Int;
var pos : Int;
public inline function new( b : InnerData ) {
this.b = b;
this.len = this.b.length;
this.pos = 0;
}
public inline function hasNext() {
return pos < len;
}
public inline function next() {
return b[pos++];
}
}
abstract FloatBuffer(InnerData) {
public var length(get, never) : Int;
public inline function new(length = 0) {
#if js
this = new InnerData(length);
#else
this = new InnerData();
if( length > 0 ) grow(length);
#end
}
public inline function push( v : hxd.impl.Float32 ) {
this.push(v);
}
public inline function grow( v : Int ) {
#if js
for( i in this.length...v )
this.push(0.);
#else
if( v > this.length ) this[v - 1] = 0.;
#end
}
public inline function resize( v : Int ) {
#if js
this.length = v;
#else
if( this.length > v ) this.splice(v, this.length - v) else grow(v);
#end
}
@:arrayAccess inline function arrayRead(key:Int) : hxd.impl.Float32 {
return this[key];
}
@:arrayAccess inline function arrayWrite(key:Int, value : hxd.impl.Float32) : hxd.impl.Float32 {
return this[key] = value;
}
public inline function getNative() : InnerData {
return this;
}
public inline function iterator() {
return new InnerIterator(this);
}
inline function get_length() : Int {
return this.length;
}
}