Sequences.java 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. package gnu.lists;
  2. import gnu.text.Char;
  3. import gnu.math.ULong;
  4. import java.util.*;
  5. import java.lang.reflect.Array;
  6. public class Sequences {
  7. public static List asSequenceOrNull(Object value) {
  8. if (value instanceof List)
  9. return (List) value;
  10. if (value instanceof CharSequence) { // FIXME?
  11. return new IString(value.toString());
  12. }
  13. if (value instanceof Object[])
  14. return new FVector((Object[]) value);
  15. SimpleVector vec = null;
  16. if (value.getClass().isArray()) {
  17. if (value instanceof long[])
  18. vec = new S64Vector((long[]) value);
  19. else if (value instanceof int[])
  20. vec = new S32Vector((int[]) value);
  21. else if (value instanceof short[])
  22. vec = new S16Vector((short[]) value);
  23. else if (value instanceof byte[])
  24. vec = new S8Vector((byte[]) value);
  25. else if (value instanceof double[])
  26. vec = new F64Vector((double[]) value);
  27. else if (value instanceof float[])
  28. vec = new F32Vector((float[]) value);
  29. else if (value instanceof boolean[])
  30. vec = new BitVector((boolean[]) value);
  31. else if (value instanceof char[])
  32. vec = new CharVector((char[]) value);
  33. if (vec != null) {
  34. vec.info |= SimpleVector.SHARED_FLAG;
  35. return vec;
  36. }
  37. }
  38. return null;
  39. }
  40. public static IntSequence asIntSequenceOrNull(Object value) {
  41. List lst = asSequenceOrNull(value);
  42. if (lst == null)
  43. return null;
  44. if (lst instanceof IntSequence)
  45. return (IntSequence) lst;
  46. int len = lst.size();
  47. int[] arr = new int[len];
  48. int i = 0;
  49. for (Object el : lst) {
  50. arr[i++] = ((Number) el).intValue();
  51. }
  52. return new S32Vector(arr);
  53. }
  54. public static List coerceToSequence(Object value) {
  55. List lst = asSequenceOrNull(value);
  56. if (lst == null) {
  57. String msg;
  58. if (value == null)
  59. msg = "null is not a sequence";
  60. else
  61. msg = "cannot cast a "+value.getClass().getName()+" to a sequence";
  62. throw new ClassCastException(msg);
  63. }
  64. return lst;
  65. }
  66. public static Object getAt(List seq, int index) {
  67. if (index < 0)
  68. index = seq.size() + index;
  69. return seq.get(index);
  70. }
  71. public static int getSize(Object values) {
  72. if (values instanceof Object[])
  73. return ((Object[]) values).length;
  74. else if (values instanceof CharSequence)
  75. return Strings.sizeInCodePoints((CharSequence) values);
  76. else if (values instanceof List<?>)
  77. return ((List<?>) values).size();
  78. else if (values.getClass().isArray())
  79. return Array.getLength(values);
  80. else
  81. throw new ClassCastException("value is neither List or array");
  82. }
  83. /** Get an Iterator for a "sequence-like" object.
  84. * This handles Iterables, CharSequences, and Java arrays.
  85. * A CharSequences is treated as a sequence of (20-bit) code-points,
  86. * not 16-bit char values.
  87. */
  88. public static Iterator getIterator(Object object) {
  89. if (object instanceof CharSequence)
  90. return new CharacterIterator((CharSequence) object);
  91. if (! (object instanceof Iterable)) {
  92. List list = asSequenceOrNull(object);
  93. if (list != null)
  94. return list.iterator();
  95. }
  96. // Causes ClassCastException if neither Iterable or otherwise handled.
  97. return ((Iterable) object).iterator();
  98. }
  99. /** Iterator subclass to iterate of CharSequences.
  100. * A CharSequences is treated as a sequence of (20-bit) code-points,
  101. * not 16-bit char values.
  102. */
  103. public static class CharacterIterator implements Iterator<Char> {
  104. CharSequence cseq;
  105. int len;
  106. int pos;
  107. public CharacterIterator(CharSequence cseq) {
  108. this.cseq = cseq;
  109. this.len = cseq.length();
  110. }
  111. public boolean hasNext() { return pos < len; }
  112. public Char next() {
  113. if (pos >= len)
  114. throw new NoSuchElementException();
  115. int ch1 = cseq.charAt(pos++);
  116. if (ch1 >= 0xD800 && ch1 <= 0xDBFF && pos < len) {
  117. int ch2 = cseq.charAt(pos);
  118. if (ch2 >= 0xDC00 && ch2 <= 0xDFFF) {
  119. ch1 = ((ch1 - 0xD800) << 10)
  120. + (ch2 - 0xDC00)
  121. + 0x10000;
  122. pos++;
  123. }
  124. }
  125. return Char.make(ch1);
  126. }
  127. /* #ifndef JAVA8 */
  128. // public void remove() {
  129. // throw new UnsupportedOperationException("remove");
  130. // }
  131. /* #endif */
  132. }
  133. public static Object subList(Object base, int fromIndex, int toIndex) {
  134. List<?> lbase = coerceToSequence(base);
  135. if (toIndex == -1)
  136. toIndex = lbase.size();
  137. return lbase.subList(fromIndex, toIndex);
  138. }
  139. public static List indirectIndexed(List lst, IntSequence indexes) {
  140. /*
  141. if (lst instanceof SimpleVector)
  142. return ((SimpleVector) lst).select(indexes);
  143. */
  144. return new IndirectIndexedSeq(lst, indexes);
  145. }
  146. public static Object drop(Object base, int count) {
  147. if (count >= 0)
  148. return subList(base, count, -1);
  149. else
  150. return subList(base, 0, -count);
  151. }
  152. public static Object drop(Object base, int fromStart, int fromEnd) {
  153. List<?> lbase = coerceToSequence(base);
  154. return subList(base, fromStart, lbase.size() - fromEnd);
  155. }
  156. /** Do a logical substring operation with sharing.
  157. * Requires base.isVerySimple() || base.isSubRange().
  158. * Note also that if base is an FString, the indexes count 16-bit code units.
  159. */
  160. public static SimpleVector copySimple(SimpleVector base, int start, int end,
  161. boolean writable) {
  162. SimpleVector nvec = base.newInstance(-1);
  163. long flags = SimpleVector.SUBRANGE_FLAG;
  164. if (! writable)
  165. flags |= SimpleVector.READ_ONLY_FLAG;
  166. int baseStart, baseSize;
  167. if (base.isVerySimple()) {
  168. baseStart = 0;
  169. baseSize = base.size();
  170. } else {
  171. baseStart = base.getOffsetBits();
  172. baseSize = base.getSizeBits();
  173. }
  174. int off = baseStart + start;
  175. if (start < 0 || start > end || end > baseSize)
  176. throw new IndexOutOfBoundsException();
  177. // buffer[offset <: offset+size]
  178. nvec.setInfoField(end - start, off, flags);
  179. base.info |= SimpleVector.COPY_ON_WRITE;
  180. return nvec;
  181. }
  182. public static SimpleVector copy(SimpleVector base, int start, int end,
  183. boolean writable) {
  184. if (base.isVerySimple() || base.isSubRange()) {
  185. return copySimple(base, start, end, writable);
  186. }
  187. return copy(base, new Range.IntRange(start, 1, end - start), writable);
  188. }
  189. public static SimpleVector copy(List base, Range.IntRange range,
  190. boolean writable) {
  191. if (base instanceof SimpleVector && range.getStepInt() == 1) {
  192. SimpleVector svec = (SimpleVector) base;
  193. if (svec.isVerySimple() || svec.isSubRange()) {
  194. int start = range.getStartInt();
  195. int bsize = base.size();
  196. int end = range.isUnbounded() ? bsize
  197. : start + range.getSize();
  198. if (start < 0 || end > bsize)
  199. throw new IndexOutOfBoundsException();
  200. return copy(svec, start, end, writable);
  201. }
  202. }
  203. return Arrays.flattenCopy(new IndirectIndexedSeq(base, range),
  204. writable);
  205. }
  206. private static Object bufferForCopy(Object obj) {
  207. for (;;) {
  208. if (obj instanceof SimpleVector)
  209. return ((SimpleVector) obj).getBuffer();
  210. if (obj instanceof TransformedArray)
  211. obj = ((TransformedArray) obj).base;
  212. else
  213. return null;
  214. }
  215. }
  216. public static boolean copyInPlaceIsSafe(Object src, Object dst) {
  217. Object s = bufferForCopy(src);
  218. Object d = bufferForCopy(dst);
  219. return s != d && s != null && d != null;
  220. }
  221. public static void replace(List lst, int fromStart, int fromEnd,
  222. List values) {
  223. if (lst instanceof SimpleVector && values instanceof SimpleVector) {
  224. SimpleVector svec = (SimpleVector) values;
  225. SimpleVector dvec = (SimpleVector) lst;
  226. if (svec.getTag() == dvec.getTag()) {
  227. int srcLength = svec.size();
  228. int dstLength = fromEnd - fromStart;
  229. int grow = srcLength - dstLength;
  230. if (grow > 0)
  231. dvec.addSpace(fromEnd, grow);
  232. Object dbuffer = dvec.getBuffer();
  233. Object sbuffer = svec.getBuffer();
  234. int sstart;
  235. int dstart = dvec.getSegment(fromStart, srcLength);
  236. if (dstart >= 0
  237. && (sstart = svec.getSegmentReadOnly(0, srcLength)) >= 0) {
  238. System.arraycopy(sbuffer, sstart,
  239. dbuffer, dstart, srcLength);
  240. } else {
  241. int srcStart = 0;
  242. boolean copied = dbuffer == sbuffer;
  243. if (copied)
  244. sbuffer = svec.toDataArray();
  245. while (srcLength > 0) {
  246. int step = srcLength;
  247. long dresult = dvec.getSegment(fromStart);
  248. int dwhere = (int) dresult;
  249. int dsize = (int) (dresult >> 32);
  250. if (dsize < step)
  251. step = dsize;
  252. int swhere;
  253. if (copied) {
  254. swhere = srcStart;
  255. } else {
  256. long sresult = svec.getSegment(srcStart);
  257. swhere = (int) sresult;
  258. int ssize = (int) (sresult >> 32);
  259. if (ssize < step)
  260. step = ssize;
  261. }
  262. if (step == 0)
  263. throw new Error("zero step in replace loop!");
  264. System.arraycopy(sbuffer, swhere, dbuffer, dwhere,
  265. step);
  266. srcLength -= step;
  267. srcStart += step;
  268. fromStart += step;
  269. }
  270. }
  271. if (grow < 0)
  272. dvec.delete(fromEnd+grow, fromEnd);
  273. return;
  274. }
  275. }
  276. int oldSize = fromEnd - fromStart;
  277. int newSize = values.size();
  278. // Making a copy is unfortunate, but it is hard to handle overlap
  279. // in the general case. We do at least optimize SimpleVectors above.
  280. Object[] varray = values.toArray();
  281. int i = 0;
  282. for (Object el : varray) {
  283. if (i < oldSize)
  284. lst.set(fromStart+i, el);
  285. else
  286. lst.add(fromStart+i, el);
  287. i++;
  288. }
  289. if (i < oldSize) {
  290. if (lst instanceof AbstractSequence) {
  291. AbstractSequence alst = (AbstractSequence) lst;
  292. alst.removePos(alst.createPos(fromStart+i, false), oldSize - i);
  293. } else {
  294. while (i < oldSize) {
  295. lst.remove(fromStart+i);
  296. oldSize--;
  297. }
  298. }
  299. }
  300. }
  301. public static void writeUInt(int value, Consumer out) {
  302. if (value >= 0)
  303. out.writeInt(value);
  304. else
  305. out.writeLong((long) value & 0xffffffffl);
  306. }
  307. public static void writeULong(long value, Consumer out) {
  308. if (value >= 0)
  309. out.writeLong(value);
  310. else
  311. out.writeObject(ULong.valueOf(value));
  312. }
  313. }