Sell book works!
This commit is contained in:
1 parent
0d126be10c
commit
1fa719ffe3
6 files changed
+239
-9
No files matched your search
@@ -0,0 +1,120 @@
|
||||
package book;
|
||||
|
||||
import clojure.lang.*;
|
||||
import java.util.*;
|
||||
|
||||
public class PersistentSortedList extends ASeq implements IPersistentList, IReduce, List, Counted{
|
||||
final private PersistentTreeMap impl;
|
||||
final private int _count;
|
||||
static public final PersistentSortedList EMPTY = new PersistentSortedList(null, PersistentTreeMap.EMPTY, 0);
|
||||
|
||||
public static PersistentSortedList create(ISeq items){
|
||||
PersistentSortedList ret = EMPTY;
|
||||
for(; items != null; items = items.next()){
|
||||
ret = ret.cons(items.first());
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
public static PersistentSortedList create(Comparator comp, ISeq items){
|
||||
PersistentSortedList ret = new PersistentSortedList(null, new PersistentTreeMap(null, comp), 0);
|
||||
for(; items != null; items = items.next()){
|
||||
ret = ret.cons(items.first());
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
PersistentSortedList(IPersistentMap meta, PersistentTreeMap impl, int count){
|
||||
super(meta);
|
||||
this.impl = impl;
|
||||
this._count = count;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int count(){
|
||||
return _count;
|
||||
}
|
||||
|
||||
@Override
|
||||
public PersistentSortedList cons(final Object o){
|
||||
final IPersistentCollection f = (IPersistentCollection) impl.get(o);
|
||||
IPersistentCollection newList = f == null ? new PersistentList(o) : f.cons(o);
|
||||
return new PersistentSortedList(meta(), impl.assoc(o, newList), _count + 1);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object peek() {
|
||||
if(_count == 0){
|
||||
return null;
|
||||
}
|
||||
final PersistentTreeMap.NodeIterator ite = this.impl.iterator();
|
||||
final AMapEntry node = (AMapEntry) ite.next();
|
||||
return ((IPersistentCollection) node.val()).seq().first();
|
||||
}
|
||||
|
||||
@Override
|
||||
public IPersistentStack pop() {
|
||||
if(_count == 0){
|
||||
throw new IllegalStateException("Can't pop empty list");
|
||||
}
|
||||
final PersistentTreeMap.NodeIterator ite = this.impl.iterator();
|
||||
final AMapEntry node = (AMapEntry) ite.next();
|
||||
if(((IPersistentCollection) node.val()).count() == 1){
|
||||
final PersistentTreeMap nextImpl = impl.without(node.getKey());
|
||||
return new PersistentSortedList(meta(), nextImpl, _count - 1);
|
||||
}
|
||||
final PersistentTreeMap nextImpl = impl.assoc(node.getKey(), ((IPersistentStack) node.getValue()).pop());
|
||||
return new PersistentSortedList(meta(), nextImpl, _count -1);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object reduce(IFn f) {
|
||||
Object ret = first();
|
||||
for(ISeq s = next(); s != null; s = s.next()) {
|
||||
ret = f.invoke(ret, s.first());
|
||||
if (RT.isReduced(ret)) return ((IDeref)ret).deref();
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object reduce(IFn f, Object start) {
|
||||
Object ret = f.invoke(start, first());
|
||||
for(ISeq s = next(); s != null; s = s.next()) {
|
||||
if (RT.isReduced(ret)) return ((IDeref)ret).deref();
|
||||
ret = f.invoke(ret, s.first());
|
||||
}
|
||||
if (RT.isReduced(ret)) return ((IDeref)ret).deref();
|
||||
return ret;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object first() {
|
||||
return peek();
|
||||
}
|
||||
|
||||
@Override
|
||||
public PersistentSortedList next() {
|
||||
if(_count == 0){
|
||||
return null;
|
||||
}
|
||||
final PersistentTreeMap.NodeIterator ite = this.impl.iterator();
|
||||
final AMapEntry node = (AMapEntry) ite.next();
|
||||
final ISeq nextValue = ((IPersistentCollection) node.val()).seq().next();
|
||||
if(nextValue == null){
|
||||
final PersistentTreeMap nextImpl = impl.without(node.getKey());
|
||||
return impl.count() > 1 ? new PersistentSortedList(meta(), nextImpl, _count - 1) : null;
|
||||
}
|
||||
final PersistentTreeMap nextImpl = impl.assoc(node.getKey(), nextValue);
|
||||
return new PersistentSortedList(meta(), nextImpl, _count - 1);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Obj withMeta(IPersistentMap meta) {
|
||||
if(meta != meta()){
|
||||
return new PersistentSortedList(meta, impl, _count);
|
||||
}
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user