FVector.java 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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 objects. */
  7. public class FVector<E> extends SimpleVector<E>
  8. implements Consumable, Comparable, GVector<E>
  9. {
  10. Object[] data;
  11. protected static Object[] empty = new Object[0];
  12. public FVector() {
  13. data = empty;
  14. }
  15. public FVector(int size, Object value) {
  16. Object[] array = new Object[size];
  17. data = array;
  18. if (value != null) {
  19. while (--size >= 0)
  20. array[size] = value;
  21. }
  22. }
  23. public FVector(int size) {
  24. this(new Object[size]);
  25. }
  26. /** Reuses the argument without making a copy. */
  27. public FVector(Object[] data) {
  28. this.data = data;
  29. }
  30. /** Makes a copy of (part of) the argument array. */
  31. public FVector(Object[] values, int offset, int length) {
  32. this(length);
  33. System.arraycopy(values, offset, data, 0, length);
  34. }
  35. public FVector(java.util.List seq) {
  36. this.data = new Object[seq.size()];
  37. int i = 0;
  38. for (java.util.Iterator<? extends E> it = seq.iterator(); it.hasNext(); )
  39. data[i++] = it.next();
  40. }
  41. public static FVector make(Object... data) {
  42. return new FVector(data);
  43. }
  44. public static <E> FVector<E> makeConstant(E... data) {
  45. FVector<E> vec = new FVector(data);
  46. vec.setReadOnly();
  47. return vec;
  48. }
  49. public void replaceAll(E[] data) {
  50. this.data = data;
  51. this.info = VERY_SIMPLE_FLAG;
  52. }
  53. public void copyFrom (int index, GVector<E> src, int start, int end) {
  54. int count = end-start;
  55. int sz = size();
  56. int src_sz = src.size();
  57. if (count < 0 || index+count > sz || end > src_sz)
  58. throw new ArrayIndexOutOfBoundsException();
  59. int sseg, dseg;
  60. FVector<E> fsrc;
  61. if (src instanceof FVector
  62. && (sseg = (fsrc = (FVector)src).getSegmentReadOnly(start, count)) >= 0
  63. && (dseg = getSegment(index, count)) >= 0) {
  64. System.arraycopy(fsrc.data, sseg, data, dseg, count);
  65. } else {
  66. for (int i = 0; i < count; i++)
  67. set(index+i, src.get(start+i));
  68. }
  69. }
  70. /** Get the allocated length of the data buffer. */
  71. public int getBufferLength() {
  72. return data.length;
  73. }
  74. public void copyBuffer(int length) {
  75. int oldLength = data.length;
  76. if (length == -1)
  77. length = oldLength;
  78. if (oldLength != length) {
  79. Object[] tmp = new Object[length];
  80. System.arraycopy(data, 0, tmp, 0,
  81. oldLength < length ? oldLength : length);
  82. data = tmp;
  83. }
  84. }
  85. public Object[] getBuffer() { return data; }
  86. protected void setBuffer(Object buffer) { data = (Object[]) buffer; }
  87. public final E get(int index) {
  88. return (E) data[effectiveIndex(index)];
  89. }
  90. public final E getRaw(int index) {
  91. return (E) data[index];
  92. }
  93. @Override
  94. public final void setRaw(int index, Object value) {
  95. data[index] = value;
  96. }
  97. protected void clearBuffer(int start, int count) {
  98. Object[] d = data;
  99. while (--count >= 0)
  100. d[start++] = null;
  101. }
  102. @Override
  103. protected FVector<E> newInstance(int newLength) {
  104. return new FVector<E>(newLength < 0 ? data : new Object[newLength]);
  105. }
  106. public static FVector castOrNull(Object obj) {
  107. if (obj instanceof Object[])
  108. return new FVector((Object[]) obj);
  109. if (obj instanceof FVector)
  110. return (FVector) obj;
  111. return null;
  112. }
  113. public static FVector cast(Object value) {
  114. FVector vec = castOrNull(value);
  115. if (vec == null) {
  116. String msg;
  117. if (value == null)
  118. msg = "cannot convert null to FVector<E>";
  119. else
  120. msg = "cannot convert a "+value.getClass().getName()+" to FVector<E>";
  121. throw new ClassCastException(msg);
  122. }
  123. return vec;
  124. }
  125. public final void fill(int start, int end, E new_value) {
  126. if (isVerySimple())
  127. java.util.Arrays.fill(data, start, end, new_value);
  128. else
  129. super.fill(start, end, new_value);
  130. }
  131. public void consumePosRange(int iposStart, int iposEnd, Consumer out) {
  132. if (out.ignoring())
  133. return;
  134. int i = nextIndex(iposStart);
  135. int end = nextIndex(iposEnd);
  136. for (; i < end; i++)
  137. out.writeObject(get(i));
  138. }
  139. public void consume(Consumer out) {
  140. out.startElement("#vector");
  141. int len = size();
  142. for (int i = 0; i < len; i++)
  143. out.writeObject(get(i));
  144. out.endElement();
  145. }
  146. public boolean equals(Object obj) {
  147. if (obj == null || !(obj instanceof FVector))
  148. return false;
  149. FVector obj_vec = (FVector) obj;
  150. int n = size();
  151. if (obj_vec.data == null || obj_vec.size() != n)
  152. return false;
  153. Object[] this_data = data;
  154. Object[] obj_data = obj_vec.data;
  155. for (int i = 0; i < n; i++) {
  156. if (! (this_data[effectiveIndex(i)].equals(obj_data[obj_vec.effectiveIndex(i)])))
  157. return false;
  158. }
  159. return true;
  160. }
  161. public int compareTo(Object obj) {
  162. FVector vec2 = (FVector) obj;
  163. Object[] arr1 = data;
  164. Object[] arr2 = vec2.data;
  165. int n1 = size();
  166. int n2 = vec2.size();
  167. int n = n1 > n2 ? n2 : n1;
  168. for (int i = 0; i < n; i++) {
  169. Object v1 = arr1[effectiveIndex(i)];
  170. Object v2 = arr2[effectiveIndex(i)];
  171. if (v1 != v2)
  172. {int d = ((Comparable) v1).compareTo((Comparable) v2); if (d != 0) return d; };
  173. }
  174. return n1 - n2;
  175. }
  176. }