first draft of the stock exchange
This commit is contained in:
1 parent
b1ba21f391
commit
c099924a89
7 files changed
+296
-24
No files matched your search
@@ -3,10 +3,18 @@ package book;
|
||||
import clojure.lang.*;
|
||||
import java.util.*;
|
||||
|
||||
public class PersistentSortedList extends ASeq implements IPersistentList, IReduce, List, Counted{
|
||||
//IPersistentVector, Iterable,
|
||||
// List,
|
||||
// RandomAccess, Comparable,
|
||||
// Serializable, IHashEq
|
||||
|
||||
public class PersistentSortedList extends Obj implements Iterable, RandomAccess, Comparable,
|
||||
IHashEq, IPersistentList, IReduce, ISeq,
|
||||
List, Counted{
|
||||
final private PersistentTreeMap impl;
|
||||
final private int _count;
|
||||
static public final PersistentSortedList EMPTY = new PersistentSortedList(null, PersistentTreeMap.EMPTY, 0);
|
||||
private int _hasheq = 0;
|
||||
|
||||
public static PersistentSortedList create(ISeq items){
|
||||
PersistentSortedList ret = EMPTY;
|
||||
@@ -42,6 +50,24 @@ public class PersistentSortedList extends ASeq implements IPersistentList, IRedu
|
||||
return new PersistentSortedList(meta(), impl.assoc(o, newList), _count + 1);
|
||||
}
|
||||
|
||||
@Override
|
||||
public IPersistentCollection empty() {
|
||||
return PersistentSortedList.EMPTY;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equiv(Object obj) {
|
||||
if(!(obj instanceof Sequential || obj instanceof List))
|
||||
return false;
|
||||
ISeq ms = RT.seq(obj);
|
||||
for(ISeq s = seq(); s != null; s = s.next(), ms = ms.next()){
|
||||
if(ms == null || !Util.equiv(s.first(), ms.first())){
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return ms == null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object peek() {
|
||||
if(_count == 0){
|
||||
@@ -70,7 +96,7 @@ public class PersistentSortedList extends ASeq implements IPersistentList, IRedu
|
||||
@Override
|
||||
public Object reduce(IFn f) {
|
||||
Object ret = first();
|
||||
for(ISeq s = next(); s != null; s = s.next()) {
|
||||
for(ISeq s = (ISeq) next(); s != null; s = s.next()) {
|
||||
ret = f.invoke(ret, s.first());
|
||||
if (RT.isReduced(ret)) return ((IDeref)ret).deref();
|
||||
}
|
||||
@@ -80,7 +106,7 @@ public class PersistentSortedList extends ASeq implements IPersistentList, IRedu
|
||||
@Override
|
||||
public Object reduce(IFn f, Object start) {
|
||||
Object ret = f.invoke(start, first());
|
||||
for(ISeq s = next(); s != null; s = s.next()) {
|
||||
for(PersistentSortedList s = next(); s != null; s = s.next()) {
|
||||
if (RT.isReduced(ret)) return ((IDeref)ret).deref();
|
||||
ret = f.invoke(ret, s.first());
|
||||
}
|
||||
@@ -88,12 +114,10 @@ public class PersistentSortedList extends ASeq implements IPersistentList, IRedu
|
||||
return ret;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object first() {
|
||||
return peek();
|
||||
}
|
||||
|
||||
@Override
|
||||
public PersistentSortedList next() {
|
||||
if(_count == 0){
|
||||
return null;
|
||||
@@ -109,6 +133,18 @@ public class PersistentSortedList extends ASeq implements IPersistentList, IRedu
|
||||
return new PersistentSortedList(meta(), nextImpl, _count - 1);
|
||||
}
|
||||
|
||||
// Things that i did not want to implement.
|
||||
|
||||
private List reify(){
|
||||
return Collections.unmodifiableList(new ArrayList(this));
|
||||
}
|
||||
|
||||
@Override
|
||||
public ISeq more() {
|
||||
ISeq s = next();
|
||||
return s == null ? PersistentSortedList.EMPTY : s;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Obj withMeta(IPersistentMap meta) {
|
||||
if(meta != meta()){
|
||||
@@ -116,5 +152,151 @@ public class PersistentSortedList extends ASeq implements IPersistentList, IRedu
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hasheq() {
|
||||
if(_hasheq == 0){
|
||||
_hasheq = Murmur3.hashOrdered(this);
|
||||
}
|
||||
return _hasheq;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ISeq seq() {
|
||||
return count() == 0 ? null : (ISeq) this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int compareTo(Object o) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int size() {
|
||||
return count();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isEmpty() {
|
||||
return seq() == null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean contains(Object o) {
|
||||
return impl.containsKey(o);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object[] toArray() {
|
||||
return RT.seqToArray(seq());
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean add(Object o) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean remove(Object o) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean addAll(Collection collection) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean addAll(int i, Collection collection) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void clear() {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object get(int index) {
|
||||
return RT.nth(this, index);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object set(int i, Object o) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void add(int i, Object o) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object remove(int i) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int indexOf(Object o) {
|
||||
// TODO: make this function fast by using the underlying sorted set. This function should be constant.
|
||||
ISeq s = seq();
|
||||
for(int i = 0; s != null; s = s.next(), i++) {
|
||||
if(Util.equiv(s.first(), o)){
|
||||
return i;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int lastIndexOf(Object o) {
|
||||
// TODO: make this function fast by using the underlying sorted set. This function should be constant.
|
||||
return reify().lastIndexOf(o);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ListIterator listIterator() {
|
||||
return reify().listIterator();
|
||||
}
|
||||
|
||||
@Override
|
||||
public ListIterator listIterator(int index) {
|
||||
return reify().listIterator(index);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List subList(int fromIndex, int toIndex) {
|
||||
return reify().subList(fromIndex, toIndex);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean retainAll(Collection collection) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean removeAll(Collection collection) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean containsAll(Collection collection) {
|
||||
for(Object o : collection) {
|
||||
if(!contains(o)){
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object[] toArray(Object[] objects) {
|
||||
return RT.seqToPassedArray(seq(), objects);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Iterator iterator() {
|
||||
return new SeqIterator(this);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user