view src/clojure/lang/ArrayChunk.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 the
4 * 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 by
7 * the terms of this license.
8 * You must not remove this notice, or any other, from this software.
9 **/
11 /* rich May 24, 2009 */
13 package clojure.lang;
15 import java.io.Serializable;
17 public final class ArrayChunk implements IChunk, Serializable {
19 final Object[] array;
20 final int off;
21 final int end;
23 public ArrayChunk(Object[] array){
24 this(array, 0, array.length);
25 }
27 public ArrayChunk(Object[] array, int off){
28 this(array, off, array.length);
29 }
31 public ArrayChunk(Object[] array, int off, int end){
32 this.array = array;
33 this.off = off;
34 this.end = end;
35 }
37 public Object nth(int i){
38 return array[off + i];
39 }
41 public Object nth(int i, Object notFound){
42 if(i >= 0 && i < count())
43 return nth(i);
44 return notFound;
45 }
47 public int count(){
48 return end - off;
49 }
51 public IChunk dropFirst(){
52 if(off==end)
53 throw new IllegalStateException("dropFirst of empty chunk");
54 return new ArrayChunk(array, off + 1, end);
55 }
57 public Object reduce(IFn f, Object start) throws Exception{
58 Object ret = f.invoke(start, array[off]);
59 for(int x = off + 1; x < end; x++)
60 ret = f.invoke(ret, array[x]);
61 return ret;
62 }
63 }