SHOW:
|
|
- or go back to the newest paste.
1 | public class DynamicFloatArray | |
2 | { | |
3 | private int length; | |
4 | private int currentIndex; | |
5 | private int currentArrayIndex; | |
6 | private float[] currentArray; | |
7 | private ArrayList<float[]> arrays; | |
8 | ||
9 | public DynamicFloatArray() | |
10 | { | |
11 | this(16); | |
12 | } | |
13 | ||
14 | public DynamicFloatArray(int baseCapacity) | |
15 | { | |
16 | this.arrays = new ArrayList<float[]>(); | |
17 | this.arrays.add(new float[baseCapacity]); | |
18 | this.reset(); | |
19 | } | |
20 | ||
21 | public synchronized DynamicFloatArray adds(float value) | |
22 | { | |
23 | return this.add(value); | |
24 | } | |
25 | ||
26 | public DynamicFloatArray add(float value) | |
27 | { | |
28 | this.length++; | |
29 | this.currentIndex++; | |
30 | if (this.currentIndex >= this.currentArray.length) | |
31 | { | |
32 | this.currentIndex = 0; | |
33 | this.currentArrayIndex++; | |
34 | if (this.currentArrayIndex < this.arrays.size()) | |
35 | { | |
36 | this.currentArray = this.arrays.get(this.currentArrayIndex); | |
37 | } | |
38 | else | |
39 | { | |
40 | - | int newCapacity = this.currentArray.length + (this.currentArray.length >> 1); |
40 | + | int newCapacity = this.currentArray.length + (this.currentArray.length >> 1); |
41 | this.currentArray = new float[newCapacity]; | |
42 | this.arrays.add(this.currentArray); | |
43 | } | |
44 | } | |
45 | ||
46 | this.currentArray[this.currentIndex] = value; | |
47 | return this; | |
48 | } | |
49 | ||
50 | public final DynamicFloatArray reset() | |
51 | { | |
52 | this.length = 0; | |
53 | this.currentIndex = -1; | |
54 | this.currentArrayIndex = 0; | |
55 | this.currentArray = this.arrays.get(0); | |
56 | return this; | |
57 | } | |
58 | ||
59 | public DynamicFloatArray clear() | |
60 | { | |
61 | this.reset(); | |
62 | this.arrays.clear(); | |
63 | this.arrays.add(this.currentArray); | |
64 | return this; | |
65 | } | |
66 | ||
67 | public FloatBuffer toFloatBuffer() | |
68 | { | |
69 | return this.toFloatBuffer(null); | |
70 | } | |
71 | ||
72 | public FloatBuffer toFloatBuffer(FloatBuffer result) | |
73 | { | |
74 | if (result == null || result.remaining() < this.length) | |
75 | { | |
76 | result = BufferUtils.createFloatBuffer(this.length); | |
77 | } | |
78 | ||
79 | int currIdx = 0; | |
80 | int currArrIdx = 0; | |
81 | float[] currArr; | |
82 | ||
83 | while (currArrIdx < this.currentArrayIndex) | |
84 | { | |
85 | currArr = this.arrays.get(currArrIdx); | |
86 | result.put(currArr); | |
87 | currIdx += currArr.length; | |
88 | currArrIdx++; | |
89 | } | |
90 | ||
91 | currArr = this.arrays.get(currArrIdx); | |
92 | int remaining = this.length - currIdx; | |
93 | result.put(currArr, 0, remaining); | |
94 | ||
95 | return result; | |
96 | } | |
97 | ||
98 | public int getLength() | |
99 | { | |
100 | return this.length; | |
101 | } | |
102 | } |