summaryrefslogtreecommitdiff
path: root/src/cli/runtime/Symbol.cs
blob: 58bc30fe01469012464dfaedfa7961f055d3c172 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
/**
 *   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 Mar 25, 2006 11:42:47 AM */

using System;
using System.Collections;

namespace clojure.lang
{
public class Symbol : Obj, IComparable{

static public readonly Hashtable table = new Hashtable(1001);
static public readonly Hashtable hashes = new Hashtable(1001);
static readonly Random rand = new Random(42);


public readonly String name;
int hash = 0;

override public String ToString()
	{
	return name;
	}

public static Symbol intern(String name)
	{
	lock(table)
		{
		Symbol sym = (Symbol) table[name];
		if(sym == null)
		    {
		    if(name[0] == ':')
		        sym = new Keyword(name);
            else if (name[0] == '.')
                sym = new Accessor(name);
            else
		        sym = new Symbol(name);
		    if(table[name] != null) //defend against recursive static init
				return (Symbol)table[name];
			table.Add(name, sym);
			}
		return sym;
		}
	}
/**
 * Used by Namespace.intern()
 * @param name
 * @param ns
 */
internal Symbol(String name)
	{
	this.name = 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

override public Obj withMeta(IPersistentMap meta) {
    this._meta = meta;
    return this;
}

    }
}