S64Vector.java 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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 signed 64-bit integers (longs). */
  7. public class S64Vector extends LongVector<Long>
  8. {
  9. public S64Vector() {
  10. data = empty;
  11. }
  12. public S64Vector(int size, long value) {
  13. long[] array = new long[size];
  14. data = array;
  15. if (value != 0) {
  16. while (--size >= 0)
  17. array[size] = value;
  18. }
  19. }
  20. public S64Vector(int size) {
  21. this(new long[size]);
  22. }
  23. /** Reuses the argument without making a copy. */
  24. public S64Vector(long[] data) {
  25. this.data = data;
  26. }
  27. /** Makes a copy of (part of) the argument array. */
  28. public S64Vector(long[] values, int offset, int length) {
  29. this(length);
  30. System.arraycopy(values, offset, data, 0, length);
  31. }
  32. public final Long get(int index) {
  33. return Long.valueOf(data[effectiveIndex(index)]);
  34. }
  35. public final Long getRaw(int index) {
  36. return Long.valueOf(data[index]);
  37. }
  38. @Override
  39. public final void setRaw(int index, Long value) {
  40. data[index] = value.longValue();
  41. }
  42. @Override
  43. protected S64Vector newInstance(int newLength) {
  44. return new S64Vector(newLength < 0 ? data : new long[newLength]);
  45. }
  46. public static S64Vector castOrNull(Object obj) {
  47. if (obj instanceof long[])
  48. return new S64Vector((long[]) obj);
  49. if (obj instanceof S64Vector)
  50. return (S64Vector) obj;
  51. return null;
  52. }
  53. public static S64Vector cast(Object value) {
  54. S64Vector vec = castOrNull(value);
  55. if (vec == null) {
  56. String msg;
  57. if (value == null)
  58. msg = "cannot convert null to S64Vector";
  59. else
  60. msg = "cannot convert a "+value.getClass().getName()+" to S64Vector";
  61. throw new ClassCastException(msg);
  62. }
  63. return vec;
  64. }
  65. public int getElementKind() { return INT_S64_VALUE; }
  66. public String getTag() { return "s64"; }
  67. public void consumePosRange(int iposStart, int iposEnd, Consumer out) {
  68. if (out.ignoring())
  69. return;
  70. int i = nextIndex(iposStart);
  71. int end = nextIndex(iposEnd);
  72. for (; i < end; i++)
  73. out.writeLong(getLong(i));
  74. }
  75. public int compareTo(Object obj) {
  76. S64Vector vec2 = (S64Vector) obj;
  77. long[] arr1 = data;
  78. long[] arr2 = vec2.data;
  79. int n1 = size();
  80. int n2 = vec2.size();
  81. int n = n1 > n2 ? n2 : n1;
  82. for (int i = 0; i < n; i++) {
  83. long v1 = arr1[effectiveIndex(i)];
  84. long v2 = arr2[effectiveIndex(i)];
  85. if (v1 != v2)
  86. return v1 > v2 ? 1 : -1;
  87. }
  88. return n1 - n2;
  89. }
  90. }