U64Vector.java 3.0 KB

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