summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRich Hickey <richhickey@gmail.com>2006-05-23 17:41:46 +0000
committerRich Hickey <richhickey@gmail.com>2006-05-23 17:41:46 +0000
commit7b16ded965dfa76689d0235d4f1cf999a3f1c8a5 (patch)
tree342ed9cd2e62050bbe32f67e6355ec10e41f965f
parent9fbd270d682ff99d1cf7574481b9dae1f0f40f71 (diff)
added hash and compare
-rw-r--r--src/cli/runtime/Symbol.cs42
-rw-r--r--src/org/clojure/runtime/Symbol.java59
2 files changed, 79 insertions, 22 deletions
diff --git a/src/cli/runtime/Symbol.cs b/src/cli/runtime/Symbol.cs
index 2900a93f..532b5f39 100644
--- a/src/cli/runtime/Symbol.cs
+++ b/src/cli/runtime/Symbol.cs
@@ -11,16 +11,19 @@
/* rich Mar 25, 2006 11:42:47 AM */
using System;
-using System.Collections.Specialized;
+using System.Collections;
namespace org.clojure.runtime
{
-public class Symbol : AMap{
+public class Symbol : AMap, IComparable{
-static public HybridDictionary table = new HybridDictionary();
+static public readonly Hashtable table = new Hashtable(1001);
+static public readonly Hashtable hashes = new Hashtable(1001);
+static readonly Random rand = new Random(42);
-public String name; //const is not equivalent to Java final with init elsewhere
+public readonly String name;
+int hash = 0;
public String toString()
{
@@ -39,5 +42,34 @@ internal Symbol(String name)
}
-}
+public override int GetHashCode()
+ {
+ if(hash == 0)
+ {
+ lock (hashes)
+ {
+ while (hash == 0)
+ {
+ int h = rand.Next();
+ if (h != 0 && !hashes.ContainsKey(h))
+ {
+ hash = h;
+ hashes.Add(h,null);
+ }
+ }
+ }
+ }
+ return hash;
+ }
+
+
+#region IComparable Members
+
+public int CompareTo(object obj)
+ {
+ return GetHashCode() - ((Symbol)obj).GetHashCode();
+ }
+
+#endregion
+ }
}
diff --git a/src/org/clojure/runtime/Symbol.java b/src/org/clojure/runtime/Symbol.java
index d5fd12f1..8cb79e18 100644
--- a/src/org/clojure/runtime/Symbol.java
+++ b/src/org/clojure/runtime/Symbol.java
@@ -13,39 +13,64 @@
package org.clojure.runtime;
import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Random;
-public class Symbol extends AMap{
+public class Symbol extends AMap implements Comparable{
final public static HashMap table = new HashMap();
+final public static HashSet hashes = new HashSet();
+final static Random rand = new Random(42);
public final String name;
+int hash = 0;
public String toString()
- {
- return name;
- }
+ {
+ return name;
+ }
public static Symbol intern(String name)
- {
- synchronized(table)
- {
- Symbol sym = (Symbol) table.get(name);
- if(sym == null)
- table.put(name, sym = new Symbol(name));
- return sym;
- }
- }
+ {
+ synchronized(table)
+ {
+ Symbol sym = (Symbol) table.get(name);
+ if(sym == null)
+ table.put(name, sym = new Symbol(name));
+ return sym;
+ }
+ }
/**
* Used by intern()
* @param name
*/
Symbol(String name)
- {
- this.name = name;
- }
-
+ {
+ this.name = name;
+ }
+ public int hashCode(){
+ if(hash == 0)
+ {
+ synchronized (hashes)
+ {
+ while (hash == 0)
+ {
+ Integer h = new Integer(rand.nextInt());
+ if (h.intValue() != 0 && !hashes.contains(h))
+ {
+ hash = h.intValue();
+ hashes.add(h);
+ }
+ }
+ }
+ }
+ return hash;
+ }
+public int compareTo(Object o) {
+ return hashCode() - ((Symbol)o).hashCode();
+}
}