Permalink
Browse files

cache Cycle head

link back to it on loop, create an actual cycle in memory
  • Loading branch information...
Mike Blume
Mike Blume committed Mar 23, 2015
1 parent 4e2a8c8 commit 22ffd779b75cfe6277c1553565d2396bcea713cf
Showing with 18 additions and 6 deletions.
  1. +18 −6 src/jvm/clojure/lang/Cycle.java
@@ -17,22 +17,33 @@
private final ISeq all; // never null
private final ISeq current; // never null
private volatile ISeq _next; // cached
private final Cycle _head;
private Cycle(ISeq all, ISeq current){
private Cycle(ISeq all, ISeq current, Cycle head){
this.all = all;
this.current = current;
this._head = head;
}
private Cycle(IPersistentMap meta, ISeq all, ISeq current){
private Cycle(IPersistentMap meta, ISeq all, ISeq current, Cycle head){
super(meta);
this.all = all;
this.current = current;
this._head = head;
}
public static ISeq create(ISeq vals){
if(vals == null)
return PersistentList.EMPTY;
return new Cycle(vals, vals);
return new Cycle(vals, vals, null);
}
private Cycle head() {
if (_head != null)
return _head;
if (current == all)
return this;
return null;
}
public Object first(){
@@ -42,16 +53,17 @@ public Object first(){
public ISeq next(){
if(_next == null) {
ISeq next = current.next();
Cycle head = head();
if (next != null)
_next = new Cycle(all, next);
_next = new Cycle(all, next, head);
else
_next = new Cycle(all, all);
_next = head == null ? new Cycle(all, all, null) : head;
}
return _next;
}
public Cycle withMeta(IPersistentMap meta){
return new Cycle(meta, all, current);
return new Cycle(meta, all, current, null);
}
public Object reduce(IFn f){

0 comments on commit 22ffd77

Please sign in to comment.