U16Vector.java 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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.UShort;
  7. /** Simple adjustable-length vector of unsigned 16-bit integers (shorts). */
  8. public class U16Vector extends ShortVector<UShort>
  9. {
  10. public U16Vector() {
  11. data = empty;
  12. }
  13. public U16Vector(int size, short value) {
  14. short[] array = new short[size];
  15. data = array;
  16. if (value != 0) {
  17. while (--size >= 0)
  18. array[size] = value;
  19. }
  20. }
  21. public U16Vector(int size) {
  22. this(new short[size]);
  23. }
  24. /** Reuses the argument without making a copy. */
  25. public U16Vector(short[] data) {
  26. this.data = data;
  27. }
  28. /** Makes a copy of (part of) the argument array. */
  29. public U16Vector(short[] values, int offset, int length) {
  30. this(length);
  31. System.arraycopy(values, offset, data, 0, length);
  32. }
  33. public final int getIntRaw(int index) {
  34. return (int) data[index] & 0xffff;
  35. }
  36. public final UShort get(int index) {
  37. return UShort.valueOf(data[effectiveIndex(index)]);
  38. }
  39. public final UShort getRaw(int index) {
  40. return UShort.valueOf(data[index]);
  41. }
  42. @Override
  43. public final void setRaw(int index, UShort value) {
  44. data[index] = value.shortValue();
  45. }
  46. @Override
  47. protected U16Vector newInstance(int newLength) {
  48. return new U16Vector(newLength < 0 ? data : new short[newLength]);
  49. }
  50. public static U16Vector castOrNull(Object obj) {
  51. if (obj instanceof short[])
  52. return new U16Vector((short[]) obj);
  53. if (obj instanceof U16Vector)
  54. return (U16Vector) obj;
  55. return null;
  56. }
  57. public static U16Vector cast(Object value) {
  58. U16Vector vec = castOrNull(value);
  59. if (vec == null) {
  60. String msg;
  61. if (value == null)
  62. msg = "cannot convert null to U16Vector";
  63. else
  64. msg = "cannot convert a "+value.getClass().getName()+" to U16Vector";
  65. throw new ClassCastException(msg);
  66. }
  67. return vec;
  68. }
  69. public int getElementKind() { return INT_U16_VALUE; }
  70. public String getTag() { return "u16"; }
  71. public int compareTo(Object obj) {
  72. return compareToInt(this, (U16Vector) obj);
  73. }
  74. }