Mercurial > lasercutter
view src/clojure/lang/ArraySeq.java @ 10:ef7dbbd6452c
added clojure source goodness
author | Robert McIntyre <rlm@mit.edu> |
---|---|
date | Sat, 21 Aug 2010 06:25:44 -0400 |
parents | |
children |
line wrap: on
line source
1 /**2 * Copyright (c) Rich Hickey. All rights reserved.3 * The use and distribution terms for this software are covered by the4 * Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)5 * which can be found in the file epl-v10.html at the root of this distribution.6 * By using this software in any fashion, you are agreeing to be bound by7 * the terms of this license.8 * You must not remove this notice, or any other, from this software.9 **/11 /* rich Jun 19, 2006 */13 package clojure.lang;15 import java.lang.reflect.Array;17 public class ArraySeq extends ASeq implements IndexedSeq, IReduce{18 public final Object array;19 final int i;20 final Object[] oa;21 //ISeq _rest;23 static public ArraySeq create(){24 return null;25 }27 static public ArraySeq create(Object... array){28 if(array == null || array.length == 0)29 return null;30 return new ArraySeq(array, 0);31 }33 static ISeq createFromObject(Object array){34 if(array == null || Array.getLength(array) == 0)35 return null;36 Class aclass = array.getClass();37 if(aclass == int[].class)38 return new ArraySeq_int(null, (int[]) array, 0);39 if(aclass == float[].class)40 return new ArraySeq_float(null, (float[]) array, 0);41 if(aclass == double[].class)42 return new ArraySeq_double(null, (double[]) array, 0);43 if(aclass == long[].class)44 return new ArraySeq_long(null, (long[]) array, 0);45 if(aclass == byte[].class)46 return new ArraySeq_byte(null, (byte[]) array, 0);47 if(aclass == char[].class)48 return new ArraySeq_char(null, (char[]) array, 0);49 if(aclass == boolean[].class)50 return new ArraySeq_boolean(null, (boolean[]) array, 0);51 return new ArraySeq(array, 0);52 }54 ArraySeq(Object array, int i){55 this.array = array;56 this.i = i;57 this.oa = (Object[]) (array instanceof Object[] ? array : null);58 // this._rest = this;59 }61 ArraySeq(IPersistentMap meta, Object array, int i){62 super(meta);63 this.array = array;64 this.i = i;65 this.oa = (Object[]) (array instanceof Object[] ? array : null);66 }68 public Object first(){69 if(oa != null)70 return oa[i];71 return Reflector.prepRet(Array.get(array, i));72 }74 public ISeq next(){75 if(oa != null)76 {77 if(i + 1 < oa.length)78 return new ArraySeq(array, i + 1);79 }80 else81 {82 if(i + 1 < Array.getLength(array))83 return new ArraySeq(array, i + 1);84 }85 return null;86 }88 public int count(){89 if(oa != null)90 return oa.length - i;91 return Array.getLength(array) - i;92 }94 public int index(){95 return i;96 }98 public ArraySeq withMeta(IPersistentMap meta){99 return new ArraySeq(meta, array, i);100 }102 public Object reduce(IFn f) throws Exception{103 if(oa != null)104 {105 Object ret = oa[i];106 for(int x = i + 1; x < oa.length; x++)107 ret = f.invoke(ret, oa[x]);108 return ret;109 }111 Object ret = Reflector.prepRet(Array.get(array, i));112 for(int x = i + 1; x < Array.getLength(array); x++)113 ret = f.invoke(ret, Reflector.prepRet(Array.get(array, x)));114 return ret;115 }117 public Object reduce(IFn f, Object start) throws Exception{118 if(oa != null)119 {120 Object ret = f.invoke(start, oa[i]);121 for(int x = i + 1; x < oa.length; x++)122 ret = f.invoke(ret, oa[x]);123 return ret;124 }125 Object ret = f.invoke(start, Reflector.prepRet(Array.get(array, i)));126 for(int x = i + 1; x < Array.getLength(array); x++)127 ret = f.invoke(ret, Reflector.prepRet(Array.get(array, x)));128 return ret;129 }131 public int indexOf(Object o) {132 if (oa != null) {133 for (int j = i; j < oa.length; j++)134 if (Util.equals(o, oa[j])) return j - i;135 } else {136 int n = Array.getLength(array);137 for (int j = i; j < n; j++)138 if (Util.equals(o, Reflector.prepRet(Array.get(array, j)))) return j - i;139 }140 return -1;141 }143 public int lastIndexOf(Object o) {144 if (oa != null) {145 if (o == null) {146 for (int j = oa.length - 1 ; j >= i; j--)147 if (oa[j] == null) return j - i;148 } else {149 for (int j = oa.length - 1 ; j >= i; j--)150 if (o.equals(oa[j])) return j - i;151 }152 } else {153 if (o == null) {154 for (int j = Array.getLength(array) - 1 ; j >= i; j--)155 if (Reflector.prepRet(Array.get(array, j)) == null) return j - i;156 } else {157 for (int j = Array.getLength(array) - 1 ; j >= i; j--)158 if (o.equals(Reflector.prepRet(Array.get(array, j)))) return j - i;159 }160 }161 return -1;162 }164 //////////////////////////////////// specialized primitive versions ///////////////////////////////166 static public class ArraySeq_int extends ASeq implements IndexedSeq, IReduce{167 public final int[] array;168 final int i;170 ArraySeq_int(IPersistentMap meta, int[] array, int i){171 super(meta);172 this.array = array;173 this.i = i;174 }176 public Object first(){177 return array[i];178 }180 public ISeq next(){181 if(i + 1 < array.length)182 return new ArraySeq_int(meta(), array, i + 1);183 return null;184 }186 public int count(){187 return array.length - i;188 }190 public int index(){191 return i;192 }194 public ArraySeq_int withMeta(IPersistentMap meta){195 return new ArraySeq_int(meta, array, i);196 }198 public Object reduce(IFn f) throws Exception{199 Object ret = array[i];200 for(int x = i + 1; x < array.length; x++)201 ret = f.invoke(ret, array[x]);202 return ret;203 }205 public Object reduce(IFn f, Object start) throws Exception{206 Object ret = f.invoke(start, array[i]);207 for(int x = i + 1; x < array.length; x++)208 ret = f.invoke(ret, array[x]);209 return ret;210 }212 public int indexOf(Object o) {213 if (o instanceof Integer) {214 int k = ((Integer) o).intValue();215 for (int j = i; j < array.length; j++)216 if (k == array[j]) return j - i;217 }218 if (o == null) {219 return -1;220 }221 for (int j = i; j < array.length; j++)222 if (o.equals(array[j])) return j - i;223 return -1;224 }226 public int lastIndexOf(Object o) {227 if (o instanceof Integer) {228 int k = ((Integer) o).intValue();229 for (int j = array.length - 1; j >= i; j--)230 if (k == array[j]) return j - i;231 }232 if (o == null) {233 return -1;234 }235 for (int j = array.length - 1; j >= i; j--)236 if (o.equals(array[j])) return j - i;237 return -1;238 }239 }242 static public class ArraySeq_float extends ASeq implements IndexedSeq, IReduce{243 public final float[] array;244 final int i;246 ArraySeq_float(IPersistentMap meta, float[] array, int i){247 super(meta);248 this.array = array;249 this.i = i;250 }252 public Object first(){253 return array[i];254 }256 public ISeq next(){257 if(i + 1 < array.length)258 return new ArraySeq_float(meta(), array, i + 1);259 return null;260 }262 public int count(){263 return array.length - i;264 }266 public int index(){267 return i;268 }270 public ArraySeq_float withMeta(IPersistentMap meta){271 return new ArraySeq_float(meta, array, i);272 }274 public Object reduce(IFn f) throws Exception{275 Object ret = array[i];276 for(int x = i + 1; x < array.length; x++)277 ret = f.invoke(ret, array[x]);278 return ret;279 }281 public Object reduce(IFn f, Object start) throws Exception{282 Object ret = f.invoke(start, array[i]);283 for(int x = i + 1; x < array.length; x++)284 ret = f.invoke(ret, array[x]);285 return ret;286 }288 public int indexOf(Object o) {289 if (o instanceof Float) {290 float f = ((Float) o).floatValue();291 for (int j = i; j < array.length; j++)292 if (f == array[j]) return j - i;293 }294 if (o == null) {295 return -1;296 }297 for (int j = i; j < array.length; j++)298 if (o.equals(array[j])) return j - i;299 return -1;300 }302 public int lastIndexOf(Object o) {303 if (o instanceof Float) {304 float f = ((Float) o).floatValue();305 for (int j = array.length - 1; j >= i; j--)306 if (f == array[j]) return j - i;307 }308 if (o == null) {309 return -1;310 }311 for (int j = array.length - 1; j >= i; j--)312 if (o.equals(array[j])) return j - i;313 return -1;314 }315 }317 static public class ArraySeq_double extends ASeq implements IndexedSeq, IReduce{318 public final double[] array;319 final int i;321 ArraySeq_double(IPersistentMap meta, double[] array, int i){322 super(meta);323 this.array = array;324 this.i = i;325 }327 public Object first(){328 return array[i];329 }331 public ISeq next(){332 if(i + 1 < array.length)333 return new ArraySeq_double(meta(), array, i + 1);334 return null;335 }337 public int count(){338 return array.length - i;339 }341 public int index(){342 return i;343 }345 public ArraySeq_double withMeta(IPersistentMap meta){346 return new ArraySeq_double(meta, array, i);347 }349 public Object reduce(IFn f) throws Exception{350 Object ret = array[i];351 for(int x = i + 1; x < array.length; x++)352 ret = f.invoke(ret, array[x]);353 return ret;354 }356 public Object reduce(IFn f, Object start) throws Exception{357 Object ret = f.invoke(start, array[i]);358 for(int x = i + 1; x < array.length; x++)359 ret = f.invoke(ret, array[x]);360 return ret;361 }363 public int indexOf(Object o) {364 if (o instanceof Double) {365 double d = ((Double) o).doubleValue();366 for (int j = i; j < array.length; j++)367 if (d == array[j]) return j - i;368 }369 if (o == null) {370 return -1;371 }372 for (int j = i; j < array.length; j++)373 if (o.equals(array[j])) return j - i;374 return -1;375 }377 public int lastIndexOf(Object o) {378 if (o instanceof Double) {379 double d = ((Double) o).doubleValue();380 for (int j = array.length - 1; j >= i; j--)381 if (d == array[j]) return j - i;382 }383 if (o == null) {384 return -1;385 }386 for (int j = array.length - 1; j >= i; j--)387 if (o.equals(array[j])) return j - i;388 return -1;389 }390 }392 static public class ArraySeq_long extends ASeq implements IndexedSeq, IReduce{393 public final long[] array;394 final int i;396 ArraySeq_long(IPersistentMap meta, long[] array, int i){397 super(meta);398 this.array = array;399 this.i = i;400 }402 public Object first(){403 return array[i];404 }406 public ISeq next(){407 if(i + 1 < array.length)408 return new ArraySeq_long(meta(), array, i + 1);409 return null;410 }412 public int count(){413 return array.length - i;414 }416 public int index(){417 return i;418 }420 public ArraySeq_long withMeta(IPersistentMap meta){421 return new ArraySeq_long(meta, array, i);422 }424 public Object reduce(IFn f) throws Exception{425 Object ret = array[i];426 for(int x = i + 1; x < array.length; x++)427 ret = f.invoke(ret, array[x]);428 return ret;429 }431 public Object reduce(IFn f, Object start) throws Exception{432 Object ret = f.invoke(start, array[i]);433 for(int x = i + 1; x < array.length; x++)434 ret = f.invoke(ret, array[x]);435 return ret;436 }438 public int indexOf(Object o) {439 if (o instanceof Long) {440 long l = ((Long) o).longValue();441 for (int j = i; j < array.length; j++)442 if (l == array[j]) return j - i;443 }444 if (o == null) {445 return -1;446 }447 for (int j = i; j < array.length; j++)448 if (o.equals(array[j])) return j - i;449 return -1;450 }452 public int lastIndexOf(Object o) {453 if (o instanceof Long) {454 long l = ((Long) o).longValue();455 for (int j = array.length - 1; j >= i; j--)456 if (l == array[j]) return j - i;457 }458 if (o == null) {459 return -1;460 }461 for (int j = array.length - 1; j >= i; j--)462 if (o.equals(array[j])) return j - i;463 return -1;464 }465 }467 static public class ArraySeq_byte extends ASeq implements IndexedSeq, IReduce{468 public final byte[] array;469 final int i;471 ArraySeq_byte(IPersistentMap meta, byte[] array, int i){472 super(meta);473 this.array = array;474 this.i = i;475 }477 public Object first(){478 return array[i];479 }481 public ISeq next(){482 if(i + 1 < array.length)483 return new ArraySeq_byte(meta(), array, i + 1);484 return null;485 }487 public int count(){488 return array.length - i;489 }491 public int index(){492 return i;493 }495 public ArraySeq_byte withMeta(IPersistentMap meta){496 return new ArraySeq_byte(meta, array, i);497 }499 public Object reduce(IFn f) throws Exception{500 Object ret = array[i];501 for(int x = i + 1; x < array.length; x++)502 ret = f.invoke(ret, array[x]);503 return ret;504 }506 public Object reduce(IFn f, Object start) throws Exception{507 Object ret = f.invoke(start, array[i]);508 for(int x = i + 1; x < array.length; x++)509 ret = f.invoke(ret, array[x]);510 return ret;511 }513 public int indexOf(Object o) {514 if (o instanceof Byte) {515 byte b = ((Byte) o).byteValue();516 for (int j = i; j < array.length; j++)517 if (b == array[j]) return j - i;518 }519 if (o == null) {520 return -1;521 }522 for (int j = i; j < array.length; j++)523 if (o.equals(array[j])) return j - i;524 return -1;525 }527 public int lastIndexOf(Object o) {528 if (o instanceof Byte) {529 byte b = ((Byte) o).byteValue();530 for (int j = array.length - 1; j >= i; j--)531 if (b == array[j]) return j - i;532 }533 if (o == null) {534 return -1;535 }536 for (int j = array.length - 1; j >= i; j--)537 if (o.equals(array[j])) return j - i;538 return -1;539 }540 }542 static public class ArraySeq_char extends ASeq implements IndexedSeq, IReduce{543 public final char[] array;544 final int i;546 ArraySeq_char(IPersistentMap meta, char[] array, int i){547 super(meta);548 this.array = array;549 this.i = i;550 }552 public Object first(){553 return array[i];554 }556 public ISeq next(){557 if(i + 1 < array.length)558 return new ArraySeq_char(meta(), array, i + 1);559 return null;560 }562 public int count(){563 return array.length - i;564 }566 public int index(){567 return i;568 }570 public ArraySeq_char withMeta(IPersistentMap meta){571 return new ArraySeq_char(meta, array, i);572 }574 public Object reduce(IFn f) throws Exception{575 Object ret = array[i];576 for(int x = i + 1; x < array.length; x++)577 ret = f.invoke(ret, array[x]);578 return ret;579 }581 public Object reduce(IFn f, Object start) throws Exception{582 Object ret = f.invoke(start, array[i]);583 for(int x = i + 1; x < array.length; x++)584 ret = f.invoke(ret, array[x]);585 return ret;586 }588 public int indexOf(Object o) {589 if (o instanceof Character) {590 char c = ((Character) o).charValue();591 for (int j = i; j < array.length; j++)592 if (c == array[j]) return j - i;593 }594 if (o == null) {595 return -1;596 }597 for (int j = i; j < array.length; j++)598 if (o.equals(array[j])) return j - i;599 return -1;600 }602 public int lastIndexOf(Object o) {603 if (o instanceof Character) {604 char c = ((Character) o).charValue();605 for (int j = array.length - 1; j >= i; j--)606 if (c == array[j]) return j - i;607 }608 if (o == null) {609 return -1;610 }611 for (int j = array.length - 1; j >= i; j--)612 if (o.equals(array[j])) return j - i;613 return -1;614 }615 }617 static public class ArraySeq_boolean extends ASeq implements IndexedSeq, IReduce{618 public final boolean[] array;619 final int i;621 ArraySeq_boolean(IPersistentMap meta, boolean[] array, int i){622 super(meta);623 this.array = array;624 this.i = i;625 }627 public Object first(){628 return array[i];629 }631 public ISeq next(){632 if(i + 1 < array.length)633 return new ArraySeq_boolean(meta(), array, i + 1);634 return null;635 }637 public int count(){638 return array.length - i;639 }641 public int index(){642 return i;643 }645 public ArraySeq_boolean withMeta(IPersistentMap meta){646 return new ArraySeq_boolean(meta, array, i);647 }649 public Object reduce(IFn f) throws Exception{650 Object ret = array[i];651 for(int x = i + 1; x < array.length; x++)652 ret = f.invoke(ret, array[x]);653 return ret;654 }656 public Object reduce(IFn f, Object start) throws Exception{657 Object ret = f.invoke(start, array[i]);658 for(int x = i + 1; x < array.length; x++)659 ret = f.invoke(ret, array[x]);660 return ret;661 }663 public int indexOf(Object o) {664 if (o instanceof Boolean) {665 boolean b = ((Boolean) o).booleanValue();666 for (int j = i; j < array.length; j++)667 if (b == array[j]) return j - i;668 }669 if (o == null) {670 return -1;671 }672 for (int j = i; j < array.length; j++)673 if (o.equals(array[j])) return j - i;674 return -1;675 }677 public int lastIndexOf(Object o) {678 if (o instanceof Boolean) {679 boolean b = ((Boolean) o).booleanValue();680 for (int j = array.length - 1; j >= i; j--)681 if (b == array[j]) return j - i;682 }683 if (o == null) {684 return -1;685 }686 for (int j = array.length - 1; j >= i; j--)687 if (o.equals(array[j])) return j - i;688 return -1;689 }690 }692 }