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