S16Vector.java 2.4 KB

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