summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRich Hickey <richhickey@gmail.com>2006-08-11 16:30:07 +0000
committerRich Hickey <richhickey@gmail.com>2006-08-11 16:30:07 +0000
commita6aa6a69dd251e2a47fe6c1ef17c4e56c0dc5314 (patch)
tree575212baa2c20f1699dc9d58b07f7dfd6017a781
parent0addb40116e9a293c5f0a0f0040a9f4862964c92 (diff)
got rid of PersistentListMap
-rw-r--r--src/cli/runtime/PersistentHashtableMap.cs23
-rw-r--r--src/cli/runtime/PersistentListMap.cs363
-rw-r--r--src/cli/runtime/PersistentQueue.cs2
-rw-r--r--src/cli/runtime/PersistentTreeMap.cs10
-rw-r--r--src/jvm/clojure/lang/PersistentHashtableMap.java28
-rw-r--r--src/jvm/clojure/lang/PersistentListMap.java333
-rw-r--r--src/jvm/clojure/lang/PersistentTreeMap.java6
7 files changed, 36 insertions, 729 deletions
diff --git a/src/cli/runtime/PersistentHashtableMap.cs b/src/cli/runtime/PersistentHashtableMap.cs
index 76dd01f1..a14bcc2a 100644
--- a/src/cli/runtime/PersistentHashtableMap.cs
+++ b/src/cli/runtime/PersistentHashtableMap.cs
@@ -114,7 +114,7 @@ PersistentArray doPut(int i,Object key,Object val,PersistentArray array){
return array;
}
else
- newEntries = createListMap(key, val);
+ newEntries = createEntryMap(key, val);
//newEntries = createArrayMap(new Object[]{key, val});
return (PersistentArray)array.assocN(i, newEntries);
@@ -128,7 +128,7 @@ PersistentArray doAdd(int i,Object key,Object val,PersistentArray array) {
newEntries = entries.assocEx(key, val);
}
else
- newEntries = createListMap(key, val);
+ newEntries = createEntryMap(key, val);
return (PersistentArray)array.assocN(i, newEntries);
}
@@ -224,7 +224,7 @@ class Seq : ASeq{
internal class Iter : IEnumerator{
PersistentArray buckets;
int b;
- Object e;
+ ISeq e;
internal Iter(PersistentArray buckets){
this.buckets = buckets;
@@ -235,10 +235,10 @@ internal class Iter : IEnumerator{
e = null;
for(b = b+1;b<buckets.length();b++)
{
- Object a = buckets.nth(b);
- if(a != null && a != PersistentListMap.EMPTY)
+ IPersistentCollection a = (IPersistentCollection)buckets.nth(b);
+ if(a != null && a.seq() != null)
{
- e = a;
+ e = a.seq();
break;
}
}
@@ -248,12 +248,12 @@ internal class Iter : IEnumerator{
public object Current
{
- get { return e; }
+ get { return e.first(); }
}
public bool MoveNext()
{
- if (e == null || (e = ((PersistentListMap)e).next()) == PersistentListMap.EMPTY)
+ if (e == null || (e = e.rest()) == null)
nextBucket();
return e != null;
}
@@ -271,7 +271,7 @@ IPersistentMap entriesFor(Object key){
}
static int bucketFor(Object key, PersistentArray array) {
- return (key.GetHashCode() & 0x7fffffff) % array.length();
+ return (RT.hash(key) & 0x7fffffff) % array.length();
}
virtual internal IPersistentMap create(int capacity) {
@@ -293,8 +293,9 @@ virtual internal IPersistentMap create(int i, PersistentArray newArray, int grow
}
-virtual internal IPersistentMap createListMap(Object key, Object val){
- return PersistentListMap.create(key,val);
+virtual internal IPersistentMap createEntryMap(Object key, Object val){
+ return new MapEntry(key, val);
+//return PersistentListMap.create(key, val);
}
}
diff --git a/src/cli/runtime/PersistentListMap.cs b/src/cli/runtime/PersistentListMap.cs
deleted file mode 100644
index e632a67a..00000000
--- a/src/cli/runtime/PersistentListMap.cs
+++ /dev/null
@@ -1,363 +0,0 @@
-/**
- * Copyright (c) Rich Hickey. All rights reserved.
- * The use and distribution terms for this software are covered by the
- * Common Public License 1.0 (http://opensource.org/licenses/cpl.php)
- * which can be found in the file CPL.TXT at the root of this distribution.
- * By using this software in any fashion, you are agreeing to be bound by
- * the terms of this license.
- * You must not remove this notice, or any other, from this software.
- **/
-
-/* rich Jun 6, 2006 */
-
-using System;
-using System.Collections;
-
-namespace clojure.lang
-{
-/**
- * Implementation of persistent map on a linked list
-
- * Note that instances of this class are constant values
- * i.e. add/remove etc return new values
- *
- * Lookups/changes are linear, so only appropriate for _very_small_ maps
- * PersistentArrayMap is generally faster, but this class avoids the double allocation,
- * and so is better/faster as a bucket for hash tables
- *
- * null keys and values are ok, but you won't be able to distinguish a null value via get - use contains/find
- */
-public class PersistentListMap : APersistentMap, IMapEntry
-{
-
-static public PersistentListMap EMPTY = new PersistentListMap();
-
-static public PersistentListMap create(Object key, Object val){
- return new Tail(key, val,null);
-}
-
-
-public virtual Object key(){
- return null;
-}
-
-public virtual Object val(){
- return null;
-}
-
-internal virtual PersistentListMap next(){
- return this;
- }
-
-public Object peek()
- {
- return first();
- }
-
-public IPersistentList pop()
- {
- return rest();
- }
-
-override public int count(){
- return 0;
-}
-
-override public bool contains(Object key){
- return false;
-}
-
-override public IMapEntry find(Object key){
- return null;
-}
-
-override public IPersistentMap assocEx(Object key, Object val){
- return (IPersistentMap)assoc(key, val);
-}
-
-override public Associative assoc(Object key, Object val){
- return new Tail(key, val, _meta);
-}
-
-override public IPersistentMap without(Object key){
- return this;
-}
-
-override public Object get(Object key){
- return null;
-}
-
-virtual public Object first()
- {
- return null;
- }
-
-virtual public ISeq rest()
- {
- return null;
- }
-
-override public ISeq seq()
- {
- return null;
- }
-
-
-internal class Iter : IEnumerator{
- PersistentListMap e;
- bool first = true;
-
- internal Iter(PersistentListMap e)
- {
- this.e = e;
- }
-
-#region IEnumerator Members
-
-public object Current
- {
- get { return e; }
- }
-
-public bool MoveNext()
- {
- if(first)
- first = false;
- else
- e = e.next();
- return e.count() > 0;
- }
-
-public void Reset()
- {
- throw new Exception("The method or operation is not implemented.");
- }
-
-#endregion
- }
-
-override public IEnumerator GetEnumerator(){
- return new Iter(this);
-}
-
-internal class Tail : PersistentListMap {
- readonly Object _key;
- readonly Object _val;
-
- internal Tail(Object key,Object val,IPersistentMap meta){
- this._key = key;
- this._val = val;
- this._meta = meta;
- }
-
- override internal PersistentListMap next(){
- return EMPTY;
- }
-
- override public int count()
- {
- return 1;
- }
-
- override public Object get(Object key)
- {
- if(equalKey(key,_key))
- return _val;
- return null;
- }
-
- override public Object key()
- {
- return _key;
- }
-
- override public Object val()
- {
- return _val;
- }
-
- override public bool contains(Object key)
- {
- return equalKey(key,_key);
- }
-
- override public IMapEntry find(Object key)
- {
- if(equalKey(key,_key))
- return this;
- return null;
- }
-
- override public IPersistentMap assocEx(Object key, Object val)
- {
- if (equalKey(key, _key))
- {
- throw new Exception("Key already present");
- }
- return new Link(key, val, this,_meta);
- }
-
- override public Associative assoc(Object key, Object val)
- {
- if(equalKey(key,_key)) //replace
- {
- if(val == _val)
- return this;
- return new Tail(key,val,_meta);
- }
- return new Link(key,val,this,_meta);
- }
-
- override public IPersistentMap without(Object key){
- if(equalKey(key,_key))
- {
- if(_meta == null)
- return EMPTY;
- return (IPersistentMap)EMPTY.withMeta(_meta);
- }
- return this;
- }
-
- class Seq : ASeq{
- Tail t;
-
- public Seq(Tail t) {
- this.t = t;
- }
- override public Object first()
- {
- return t;
- }
-
- override public ISeq rest()
- {
- return null;
- }
- }
- override public ISeq seq()
- {
- return new Seq(this);
- }
-}
-
-internal class Link : PersistentListMap {
- readonly Object _key;
- readonly Object _val;
- readonly PersistentListMap _rest;
- readonly int _count;
-
- internal Link(Object key,Object val,PersistentListMap next,IPersistentMap meta){
- this._key = key;
- this._val = val;
- this._rest = next;
- this._meta = meta;
- this._count = 1 + next.count();
- }
-
- override public Object key(){
- return _key;
- }
-
- override public Object val(){
- return _val;
- }
-
- override internal PersistentListMap next(){
- return _rest;
- }
-
- override public int count(){
- return _count;
- }
-
- override public bool contains(Object key){
- return find(key) != null;
- }
-
- override public IMapEntry find(Object key){
- if(equalKey(key,_key))
- return this;
- return _rest.find(key);
- }
-
- override public IPersistentMap assocEx(Object key, Object val)
- {
- IMapEntry e = find(key);
- if(e != null)
- {
- throw new Exception("Key already present");
- }
- return new Link(key,val,this,_meta);
- }
-
- override public Associative assoc(Object key, Object val)
- {
- IMapEntry e = find(key);
- if(e != null)
- {
- if(e.val() == val)
- return this;
- return create(_key,_val,without(key));
- }
- return new Link(key,val,this,_meta);
- }
-
- override public IPersistentMap without(Object key)
- {
- if(equalKey(key,_key))
- {
- if(_rest._meta == _meta)
- return _rest;
- return (IPersistentMap)_rest.withMeta(_meta);
- }
- PersistentListMap r = (PersistentListMap)_rest.without(key);
- if(r == _rest) //not there
- return this;
- return create(_key,_val,r);
- }
-
- override public Object get(Object key){
- IMapEntry e = find(key);
- if(e != null)
- return e.val();
- return null;
- }
-
- class Seq : ASeq{
- Link lnk;
-
- public Seq(Link lnk) {
- this.lnk = lnk;
- }
-
- override public Object first() {
- return lnk;
- }
-
- override public ISeq rest() {
- return lnk._rest.seq();
- }
- }
-
- override public ISeq seq()
- {
- return new Seq(this);
- }
-
- PersistentListMap create(Object k, Object v, IPersistentMap r)
- {
- if(r.count() == 0)
- return new Tail(k,v,_meta);
- return new Link(k, v, (PersistentListMap)r,_meta);
- }
-
-}
-
-bool equalKey(Object k1,Object k2){
- if(k1 == null)
- return k2 == null;
- return k1.Equals(k2);
-}
-}
-
-}
diff --git a/src/cli/runtime/PersistentQueue.cs b/src/cli/runtime/PersistentQueue.cs
index 14d7c2f8..78635448 100644
--- a/src/cli/runtime/PersistentQueue.cs
+++ b/src/cli/runtime/PersistentQueue.cs
@@ -140,7 +140,7 @@ class Seq : ASeq {
}
}
-//*
+/*
public static void Main(String[] args) {
if (args.Length != 1)
{
diff --git a/src/cli/runtime/PersistentTreeMap.cs b/src/cli/runtime/PersistentTreeMap.cs
index 77be3e9f..743cb06d 100644
--- a/src/cli/runtime/PersistentTreeMap.cs
+++ b/src/cli/runtime/PersistentTreeMap.cs
@@ -789,7 +789,7 @@ public void Reset()
#endregion
}
- /*
+ //*
[STAThread]
static public void Main(String[] args){
if(args.Length != 1)
@@ -804,9 +804,9 @@ static public void Main(String[] args){
Array.Reverse(ints);
Console.WriteLine("Building set");
//IPersistentMap set = new PersistentTree();
- IPersistentMap set = new PersistentArrayMap();
+ //IPersistentMap set = new PersistentArrayMap();
//IPersistentMap set = new PersistentListMap();
- //IPersistentMap set = new PersistentHashtableMap(1001);
+ IPersistentMap set = new PersistentHashtableMap(1001);
//IPersistentMap set = new PersistentHybridMap(1001);
//for(int i = 0; i < ints.Length; i++)
// {
@@ -817,7 +817,7 @@ static public void Main(String[] args){
for (int i = 0; i < ints.Length; i++)
{
Object anInt = ints[i];
- set = set.put(anInt, anInt);
+ set = (IPersistentMap) set.assoc(anInt, anInt);
}
foreach(IMapEntry e in set)
@@ -831,7 +831,7 @@ static public void Main(String[] args){
for(int i = 0; i < ints.Length/2; i++)
{
Object anInt = ints[i];
- set = set.remove(anInt);
+ set = set.without(anInt);
}
Console.WriteLine("Time: " + (DateTime.Now - start));
diff --git a/src/jvm/clojure/lang/PersistentHashtableMap.java b/src/jvm/clojure/lang/PersistentHashtableMap.java
index cee7bb55..8c28c09a 100644
--- a/src/jvm/clojure/lang/PersistentHashtableMap.java
+++ b/src/jvm/clojure/lang/PersistentHashtableMap.java
@@ -106,7 +106,7 @@ PersistentArray doPut(int i,Object key,Object val,PersistentArray array){
return array;
}
else
- newEntries = createListMap(key, val);
+ newEntries = createEntryMap(key, val);
return array.assocN(i, newEntries);
}
@@ -119,7 +119,7 @@ PersistentArray doAdd(int i,Object key,Object val,PersistentArray array) throws
newEntries = entries.assocEx(key, val);
}
else
- newEntries = createListMap(key, val);
+ newEntries = createEntryMap(key, val);
return array.assocN(i, newEntries);
}
@@ -214,7 +214,7 @@ static class Seq extends ASeq{
static class Iter implements Iterator{
PersistentArray buckets;
int b;
- PersistentListMap e;
+ ISeq e;
Iter(PersistentArray buckets){
this.buckets = buckets;
@@ -226,10 +226,10 @@ static class Iter implements Iterator{
e = null;
for(b = b+1;b<buckets.length();b++)
{
- PersistentListMap a = (PersistentListMap) buckets.nth(b);
- if(a != null && a != PersistentListMap.EMPTY)
+ IPersistentCollection a = (IPersistentCollection) buckets.nth(b);
+ if(a != null && a.seq() != null)
{
- e = a;
+ e = a.seq();
break;
}
}
@@ -240,11 +240,11 @@ static class Iter implements Iterator{
}
public Object next() {
- PersistentListMap ret = e;
- e = e.next();
- if(e == PersistentListMap.EMPTY)
+ ISeq ret = e;
+ e = e.rest();
+ if(e == null)
nextBucket();
- return ret;
+ return ret.first();
}
public void remove() {
@@ -257,7 +257,7 @@ final IPersistentMap entriesFor(Object key){
}
static int bucketFor(Object key, PersistentArray array) {
- return (key.hashCode() & 0x7fffffff)%array.length();
+ return (RT.hash(key) & 0x7fffffff)%array.length();
}
IPersistentMap create(int capacity) {
@@ -279,8 +279,10 @@ IPersistentMap create(int i, PersistentArray newArray, int growAtCount){
}
-IPersistentMap createListMap(Object key, Object val){
- return PersistentListMap.create(key,val);
+IPersistentMap createEntryMap(Object key, Object val){
+ return new MapEntry(key,val);
+// return PersistentListMap.create(key,val);
+// return new PersistentArrayMap(key,val);
}
}
diff --git a/src/jvm/clojure/lang/PersistentListMap.java b/src/jvm/clojure/lang/PersistentListMap.java
deleted file mode 100644
index f32b25e6..00000000
--- a/src/jvm/clojure/lang/PersistentListMap.java
+++ /dev/null
@@ -1,333 +0,0 @@
-/**
- * Copyright (c) Rich Hickey. All rights reserved.
- * The use and distribution terms for this software are covered by the
- * Common Public License 1.0 (http://opensource.org/licenses/cpl.php)
- * which can be found in the file CPL.TXT at the root of this distribution.
- * By using this software in any fashion, you are agreeing to be bound by
- * the terms of this license.
- * You must not remove this notice, or any other, from this software.
- **/
-
-/* rich Jun 6, 2006 */
-
-package clojure.lang;
-
-import java.util.Iterator;
-
-/**
- * Implementation of persistent map on a linked list
-
- * Note that instances of this class are constant values
- * i.e. add/remove etc return new values
- *
- * Lookups/changes are linear, so only appropriate for _very_small_ maps
- * PersistentArrayMap is generally faster, but this class avoids the double allocation,
- * and so is better/faster as a bucket for hash tables
- *
- * null keys and values are ok, but you won't be able to distinguish a null value via get - use contains/find
- */
-public class PersistentListMap extends APersistentMap implements IMapEntry
-{
-
-static public PersistentListMap EMPTY = new PersistentListMap();
-
-static public PersistentListMap create(Object key, Object val){
- return new Tail(key, val,null);
-}
-
-public Object key(){
- return null;
-}
-
-public Object val(){
- return null;
-}
-
-PersistentListMap next(){
- return this;
- }
-
-public Object peek() {
- return first();
-}
-
-public IPersistentList pop() {
- return rest();
-}
-
-public int count(){
- return 0;
-}
-
-public boolean contains(Object key){
- return false;
-}
-
-public IMapEntry find(Object key){
- return null;
-}
-
-public IPersistentMap assocEx(Object key, Object val) throws Exception {
- return assoc(key, val);
-}
-
-public PersistentListMap assoc(Object key, Object val) {
- return new Tail(key, val,_meta);
-}
-
-public PersistentListMap without(Object key) {
- return this;
-}
-
-public Object get(Object key){
- return null;
-}
-
-public int capacity(){
- return 0;
-}
-
-public Object first() {
- return null;
-}
-
-public ISeq rest() {
- return null;
-}
-
-public ISeq seq() {
- return null;
-}
-
-static class Iter implements Iterator{
- PersistentListMap e;
-
- Iter(PersistentListMap e)
- {
- this.e = e;
- }
-
- public boolean hasNext(){
- return e.count() != 0;
- }
-
- public Object next(){
- PersistentListMap ret = e;
- e = e.next();
- return ret;
- }
-
- public void remove(){
- throw new UnsupportedOperationException();
- }
-}
-
-public Iterator iterator(){
- return new Iter(this);
-}
-
-static class Tail extends PersistentListMap {
- final Object _key;
- final Object _val;
-
- Tail(Object key,Object val, IPersistentMap meta){
- this._key = key;
- this._val = val;
- this._meta = meta;
- }
-
- PersistentListMap next(){
- return EMPTY;
- }
-
- public int count(){
- return 1;
- }
-
- public Object get(Object key){
- if(equalKey(key,_key))
- return _val;
- return null;
- }
-
- public Object key(){
- return _key;
- }
-
- public Object val(){
- return _val;
- }
-
- public boolean contains(Object key){
- return equalKey(key,_key);
- }
-
- public IMapEntry find(Object key){
- if(equalKey(key,_key))
- return this;
- return null;
- }
-
- public PersistentListMap assocEx(Object key, Object val) throws Exception {
- if(equalKey(key,_key)) //replace
- {
- throw new Exception("Key already present");
- }
- return new Link(key,val,this,_meta);
- }
-
- public PersistentListMap assoc(Object key, Object val){
- if(equalKey(key,_key)) //replace
- {
- if(val == _val)
- return this;
- return new Tail(key,val,_meta);
- }
- return new Link(key,val,this,_meta);
- }
-
- public PersistentListMap without(Object key) {
- if(equalKey(key,_key))
- {
- if(_meta == null)
- return EMPTY;
- return (PersistentListMap) EMPTY.withMeta(_meta);
- }
- return this;
- }
- static class Seq extends ASeq{
- Tail t;
-
- public Seq(Tail t) {
- this.t = t;
- }
-
- public Object first() {
- return t;
- }
-
- public ISeq rest() {
- return null;
- }
- }
-
- public ISeq seq() {
- return new Seq(this);
- }
-
-}
-
-static class Link extends PersistentListMap {
- final Object _key;
- final Object _val;
- final PersistentListMap _rest;
- final int _count;
-
- Link(Object key,Object val,PersistentListMap next,IPersistentMap meta){
- this._key = key;
- this._val = val;
- this._rest = next;
- this._meta = meta;
- this._count = 1 + next.count();
- }
-
- public Object key(){
- return _key;
- }
-
- public Object val(){
- return _val;
- }
-
- PersistentListMap next(){
- return _rest;
- }
-
- public int count(){
- return _count;
- }
-
- public boolean contains(Object key){
- return find(key) != null;
- }
-
- public IMapEntry find(Object key){
- if(equalKey(key,_key))
- return this;
- return _rest.find(key);
- }
-
- public PersistentListMap assocEx(Object key, Object val) throws Exception {
- IMapEntry e = find(key);
- if(e != null)
- {
- throw new Exception("Key already present");
- }
- return new Link(key,val,this,_meta);
- }
-
- public PersistentListMap assoc(Object key, Object val) {
- IMapEntry e = find(key);
- if(e != null)
- {
- if(e.val() == val)
- return this;
- return create(_key,_val,without(key));
- }
- return new Link(key,val,this,_meta);
- }
-
- public PersistentListMap without(Object key) {
- if(equalKey(key,_key))
- {
- if(_rest._meta == _meta)
- return _rest;
- return (PersistentListMap) _rest.withMeta(_meta);
- }
- PersistentListMap r = _rest.without(key);
- if(r == _rest) //not there
- return this;
- return create(_key,_val,r);
- }
-
- public Object get(Object key){
- IMapEntry e = find(key);
- if(e != null)
- return e.val();
- return null;
- }
-
- static class Seq extends ASeq{
- Link lnk;
-
- public Seq(Link lnk) {
- this.lnk = lnk;
- }
-
- public Object first() {
- return lnk;
- }
-
- public ISeq rest() {
- return lnk._rest.seq();
- }
- }
-
- public ISeq seq() {
- return new Seq(this);
- }
-
- PersistentListMap create(Object k,Object v,PersistentListMap r){
- if(r.count() == 0)
- return new Tail(k,v,_meta);
- return new Link(k, v, r,_meta);
- }
-
-}
-
-boolean equalKey(Object k1,Object k2){
- if(k1 == null)
- return k2 == null;
- return k1.equals(k2);
-}
-}
diff --git a/src/jvm/clojure/lang/PersistentTreeMap.java b/src/jvm/clojure/lang/PersistentTreeMap.java
index 31271f18..010d1063 100644
--- a/src/jvm/clojure/lang/PersistentTreeMap.java
+++ b/src/jvm/clojure/lang/PersistentTreeMap.java
@@ -743,14 +743,14 @@ static public void main(String args[]){
Collections.shuffle(Arrays.asList(ints));
//force the ListMap class loading now
try {
- PersistentListMap.EMPTY.assocEx(1, null).assocEx(2,null).assocEx(3,null);
+ //PersistentListMap.EMPTY.assocEx(1, null).assocEx(2,null).assocEx(3,null);
} catch (Exception e)
{
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
System.out.println("Building set");
- IPersistentMap set = new PersistentArrayMap();
- //IPersistentMap set = new PersistentHashtableMap(1001);
+ //IPersistentMap set = new PersistentArrayMap();
+ IPersistentMap set = new PersistentHashtableMap(1001);
//IPersistentMap set = new ListMap();
//IPersistentMap set = new ArrayMap();
//IPersistentMap set = new RBTree();