F32Vector.java 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. // This file is generated from PrimVector.template. DO NOT EDIT!
  2. // Copyright (c) 2001, 2002, 2015 Per M.A. Bothner and Brainfood Inc.
  3. // This is free software; for terms and warranty disclaimer see ./COPYING.
  4. package gnu.lists;
  5. import java.io.*;
  6. /** Simple adjustable-length vector of 32-bit floats. */
  7. public class F32Vector extends SimpleVector<Float>
  8. implements Comparable, GVector<Float>
  9. {
  10. float[] data;
  11. protected static float[] empty = new float[0];
  12. public F32Vector() {
  13. data = empty;
  14. }
  15. public F32Vector(int size, float value) {
  16. float[] array = new float[size];
  17. data = array;
  18. if (value != 0) {
  19. while (--size >= 0)
  20. array[size] = value;
  21. }
  22. }
  23. public F32Vector(int size) {
  24. this(new float[size]);
  25. }
  26. /** Reuses the argument without making a copy. */
  27. public F32Vector(float[] data) {
  28. this.data = data;
  29. }
  30. /** Makes a copy of (part of) the argument array. */
  31. public F32Vector(float[] values, int offset, int length) {
  32. this(length);
  33. System.arraycopy(values, offset, data, 0, length);
  34. }
  35. /** Get the allocated length of the data buffer. */
  36. public int getBufferLength() {
  37. return data.length;
  38. }
  39. public void copyBuffer(int length) {
  40. int oldLength = data.length;
  41. if (length == -1)
  42. length = oldLength;
  43. if (oldLength != length) {
  44. float[] tmp = new float[length];
  45. System.arraycopy(data, 0, tmp, 0,
  46. oldLength < length ? oldLength : length);
  47. data = tmp;
  48. }
  49. }
  50. public float[] getBuffer() { return data; }
  51. protected void setBuffer(Object buffer) { data = (float[]) buffer; }
  52. public final float getFloat(int index) {
  53. return data[effectiveIndex(index)];
  54. }
  55. public final float getFloatRaw(int index) {
  56. return data[index];
  57. }
  58. public final Float get(int index) {
  59. return Float.valueOf(data[effectiveIndex(index)]);
  60. }
  61. public final Float getRaw(int index) {
  62. return Float.valueOf(data[index]);
  63. }
  64. public final void setFloat(int index, float value) {
  65. checkCanWrite(); // FIXME maybe inline and fold into following
  66. data[effectiveIndex(index)] = value;
  67. }
  68. public final void setFloatRaw(int index, float value) {
  69. data[index] = value;
  70. }
  71. @Override
  72. public final void setRaw(int index, Float value) {
  73. data[index] = value.floatValue();
  74. }
  75. public void add(float v) {
  76. int sz = size();
  77. addSpace(sz, 1);
  78. setFloat(sz, v);
  79. }
  80. protected void clearBuffer(int start, int count) {
  81. float[] d = data;
  82. while (--count >= 0)
  83. d[start++] = 0;
  84. }
  85. @Override
  86. protected F32Vector newInstance(int newLength) {
  87. return new F32Vector(newLength < 0 ? data : new float[newLength]);
  88. }
  89. public static F32Vector castOrNull(Object obj) {
  90. if (obj instanceof float[])
  91. return new F32Vector((float[]) obj);
  92. if (obj instanceof F32Vector)
  93. return (F32Vector) obj;
  94. return null;
  95. }
  96. public static F32Vector cast(Object value) {
  97. F32Vector vec = castOrNull(value);
  98. if (vec == null) {
  99. String msg;
  100. if (value == null)
  101. msg = "cannot convert null to F32Vector";
  102. else
  103. msg = "cannot convert a "+value.getClass().getName()+" to F32Vector";
  104. throw new ClassCastException(msg);
  105. }
  106. return vec;
  107. }
  108. public int getElementKind() { return FLOAT_VALUE; }
  109. public String getTag() { return "f32"; }
  110. public void consumePosRange(int iposStart, int iposEnd, Consumer out) {
  111. if (out.ignoring())
  112. return;
  113. int i = nextIndex(iposStart);
  114. int end = nextIndex(iposEnd);
  115. for (; i < end; i++)
  116. out.writeFloat(getFloat(i));
  117. }
  118. public int compareTo(Object obj) {
  119. F32Vector vec2 = (F32Vector) obj;
  120. float[] arr1 = data;
  121. float[] arr2 = vec2.data;
  122. int n1 = size();
  123. int n2 = vec2.size();
  124. int n = n1 > n2 ? n2 : n1;
  125. for (int i = 0; i < n; i++) {
  126. float v1 = arr1[effectiveIndex(i)];
  127. float v2 = arr2[effectiveIndex(i)];
  128. if (v1 != v2)
  129. return v1 > v2 ? 1 : -1;
  130. }
  131. return n1 - n2;
  132. }
  133. }