F64Vector.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 64-bit doubles. */
  7. public class F64Vector extends SimpleVector<Double>
  8. implements Comparable, GVector<Double>
  9. {
  10. double[] data;
  11. protected static double[] empty = new double[0];
  12. public F64Vector() {
  13. data = empty;
  14. }
  15. public F64Vector(int size, double value) {
  16. double[] array = new double[size];
  17. data = array;
  18. if (value != 0) {
  19. while (--size >= 0)
  20. array[size] = value;
  21. }
  22. }
  23. public F64Vector(int size) {
  24. this(new double[size]);
  25. }
  26. /** Reuses the argument without making a copy. */
  27. public F64Vector(double[] data) {
  28. this.data = data;
  29. }
  30. /** Makes a copy of (part of) the argument array. */
  31. public F64Vector(double[] 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. double[] tmp = new double[length];
  45. System.arraycopy(data, 0, tmp, 0,
  46. oldLength < length ? oldLength : length);
  47. data = tmp;
  48. }
  49. }
  50. public double[] getBuffer() { return data; }
  51. protected void setBuffer(Object buffer) { data = (double[]) buffer; }
  52. public final double getDouble(int index) {
  53. return data[effectiveIndex(index)];
  54. }
  55. public final double getDoubleRaw(int index) {
  56. return data[index];
  57. }
  58. public final Double get(int index) {
  59. return Double.valueOf(data[effectiveIndex(index)]);
  60. }
  61. public final Double getRaw(int index) {
  62. return Double.valueOf(data[index]);
  63. }
  64. public final void setDouble(int index, double value) {
  65. checkCanWrite(); // FIXME maybe inline and fold into following
  66. data[effectiveIndex(index)] = value;
  67. }
  68. public final void setDoubleRaw(int index, double value) {
  69. data[index] = value;
  70. }
  71. @Override
  72. public final void setRaw(int index, Double value) {
  73. data[index] = value.doubleValue();
  74. }
  75. public void add(double v) {
  76. int sz = size();
  77. addSpace(sz, 1);
  78. setDouble(sz, v);
  79. }
  80. protected void clearBuffer(int start, int count) {
  81. double[] d = data;
  82. while (--count >= 0)
  83. d[start++] = 0;
  84. }
  85. @Override
  86. protected F64Vector newInstance(int newLength) {
  87. return new F64Vector(newLength < 0 ? data : new double[newLength]);
  88. }
  89. public static F64Vector castOrNull(Object obj) {
  90. if (obj instanceof double[])
  91. return new F64Vector((double[]) obj);
  92. if (obj instanceof F64Vector)
  93. return (F64Vector) obj;
  94. return null;
  95. }
  96. public static F64Vector cast(Object value) {
  97. F64Vector vec = castOrNull(value);
  98. if (vec == null) {
  99. String msg;
  100. if (value == null)
  101. msg = "cannot convert null to F64Vector";
  102. else
  103. msg = "cannot convert a "+value.getClass().getName()+" to F64Vector";
  104. throw new ClassCastException(msg);
  105. }
  106. return vec;
  107. }
  108. public int getElementKind() { return DOUBLE_VALUE; }
  109. public String getTag() { return "f64"; }
  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.writeDouble(getDouble(i));
  117. }
  118. public int compareTo(Object obj) {
  119. F64Vector vec2 = (F64Vector) obj;
  120. double[] arr1 = data;
  121. double[] 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. double v1 = arr1[effectiveIndex(i)];
  127. double v2 = arr2[effectiveIndex(i)];
  128. if (v1 != v2)
  129. return v1 > v2 ? 1 : -1;
  130. }
  131. return n1 - n2;
  132. }
  133. }