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