view src/clojure/lang/PersistentHashSet.java @ 10:ef7dbbd6452c

added clojure source goodness
author Robert McIntyre <rlm@mit.edu>
date Sat, 21 Aug 2010 06:25:44 -0400
parents
children
line wrap: on
line source
1 /**
2 * Copyright (c) Rich Hickey. All rights reserved.
3 * The use and distribution terms for this software are covered by the
4 * Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
5 * which can be found in the file epl-v10.html at the root of this distribution.
6 * By using this software in any fashion, you are agreeing to be bound by
7 * the terms of this license.
8 * You must not remove this notice, or any other, from this software.
9 **/
11 /* rich Mar 3, 2008 */
13 package clojure.lang;
15 import java.util.List;
17 public class PersistentHashSet extends APersistentSet implements IObj, IEditableCollection {
19 static public final PersistentHashSet EMPTY = new PersistentHashSet(null, PersistentHashMap.EMPTY);
21 final IPersistentMap _meta;
23 public static PersistentHashSet create(Object... init){
24 PersistentHashSet ret = EMPTY;
25 for(int i = 0; i < init.length; i++)
26 {
27 ret = (PersistentHashSet) ret.cons(init[i]);
28 }
29 return ret;
30 }
32 public static PersistentHashSet create(List init){
33 PersistentHashSet ret = EMPTY;
34 for(Object key : init)
35 {
36 ret = (PersistentHashSet) ret.cons(key);
37 }
38 return ret;
39 }
41 static public PersistentHashSet create(ISeq items){
42 PersistentHashSet ret = EMPTY;
43 for(; items != null; items = items.next())
44 {
45 ret = (PersistentHashSet) ret.cons(items.first());
46 }
47 return ret;
48 }
50 public static PersistentHashSet createWithCheck(Object... init){
51 PersistentHashSet ret = EMPTY;
52 for(int i = 0; i < init.length; i++)
53 {
54 ret = (PersistentHashSet) ret.cons(init[i]);
55 if(ret.count() != i + 1)
56 throw new IllegalArgumentException("Duplicate key: " + init[i]);
57 }
58 return ret;
59 }
61 public static PersistentHashSet createWithCheck(List init){
62 PersistentHashSet ret = EMPTY;
63 int i=0;
64 for(Object key : init)
65 {
66 ret = (PersistentHashSet) ret.cons(key);
67 if(ret.count() != i + 1)
68 throw new IllegalArgumentException("Duplicate key: " + key);
69 ++i;
70 }
71 return ret;
72 }
74 static public PersistentHashSet createWithCheck(ISeq items){
75 PersistentHashSet ret = EMPTY;
76 for(int i=0; items != null; items = items.next(), ++i)
77 {
78 ret = (PersistentHashSet) ret.cons(items.first());
79 if(ret.count() != i + 1)
80 throw new IllegalArgumentException("Duplicate key: " + items.first());
81 }
82 return ret;
83 }
85 PersistentHashSet(IPersistentMap meta, IPersistentMap impl){
86 super(impl);
87 this._meta = meta;
88 }
90 public IPersistentSet disjoin(Object key) throws Exception{
91 if(contains(key))
92 return new PersistentHashSet(meta(),impl.without(key));
93 return this;
94 }
96 public IPersistentSet cons(Object o){
97 if(contains(o))
98 return this;
99 return new PersistentHashSet(meta(),impl.assoc(o,o));
100 }
102 public IPersistentCollection empty(){
103 return EMPTY.withMeta(meta());
104 }
106 public PersistentHashSet withMeta(IPersistentMap meta){
107 return new PersistentHashSet(meta, impl);
108 }
110 public ITransientCollection asTransient() {
111 return new TransientHashSet(((PersistentHashMap) impl).asTransient());
112 }
114 public IPersistentMap meta(){
115 return _meta;
116 }
118 static final class TransientHashSet extends ATransientSet {
119 TransientHashSet(ITransientMap impl) {
120 super(impl);
121 }
123 public IPersistentCollection persistent() {
124 return new PersistentHashSet(null, impl.persistent());
125 }
126 }
128 }