view src/clojure/lang/PersistentTreeSet.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.Comparator;
17 public class PersistentTreeSet extends APersistentSet implements IObj, Reversible, Sorted{
18 static public final PersistentTreeSet EMPTY = new PersistentTreeSet(null, PersistentTreeMap.EMPTY);
19 final IPersistentMap _meta;
22 static public PersistentTreeSet create(ISeq items){
23 PersistentTreeSet ret = EMPTY;
24 for(; items != null; items = items.next())
25 {
26 ret = (PersistentTreeSet) ret.cons(items.first());
27 }
28 return ret;
29 }
31 static public PersistentTreeSet create(Comparator comp, ISeq items){
32 PersistentTreeSet ret = new PersistentTreeSet(null, new PersistentTreeMap(null, comp));
33 for(; items != null; items = items.next())
34 {
35 ret = (PersistentTreeSet) ret.cons(items.first());
36 }
37 return ret;
38 }
40 PersistentTreeSet(IPersistentMap meta, IPersistentMap impl){
41 super(impl);
42 this._meta = meta;
43 }
45 public IPersistentSet disjoin(Object key) throws Exception{
46 if(contains(key))
47 return new PersistentTreeSet(meta(),impl.without(key));
48 return this;
49 }
51 public IPersistentSet cons(Object o){
52 if(contains(o))
53 return this;
54 return new PersistentTreeSet(meta(),impl.assoc(o,o));
55 }
57 public IPersistentCollection empty(){
58 return new PersistentTreeSet(meta(),(PersistentTreeMap)impl.empty());
59 }
61 public ISeq rseq() throws Exception{
62 return APersistentMap.KeySeq.create(((Reversible) impl).rseq());
63 }
65 public PersistentTreeSet withMeta(IPersistentMap meta){
66 return new PersistentTreeSet(meta, impl);
67 }
69 public Comparator comparator(){
70 return ((Sorted)impl).comparator();
71 }
73 public Object entryKey(Object entry){
74 return entry;
75 }
77 public ISeq seq(boolean ascending){
78 PersistentTreeMap m = (PersistentTreeMap) impl;
79 return RT.keys(m.seq(ascending));
80 }
82 public ISeq seqFrom(Object key, boolean ascending){
83 PersistentTreeMap m = (PersistentTreeMap) impl;
84 return RT.keys(m.seqFrom(key,ascending));
85 }
87 public IPersistentMap meta(){
88 return _meta;
89 }
90 }