Persistent Red Black Tree Note that instances of this class are constant values i.e. add/remove etc return new values See Okasaki, Kahrs, Larsen et al
| 24 | */ |
| 25 | |
| 26 | public class PersistentTreeMap extends APersistentMap implements IObj, Reversible, Sorted, IKVReduce{ |
| 27 | |
| 28 | public final Comparator comp; |
| 29 | public final Node tree; |
| 30 | public final int _count; |
| 31 | final IPersistentMap _meta; |
| 32 | |
| 33 | final static public PersistentTreeMap EMPTY = new PersistentTreeMap(); |
| 34 | |
| 35 | static public IPersistentMap create(Map other){ |
| 36 | IPersistentMap ret = EMPTY; |
| 37 | for(Object o : other.entrySet()) |
| 38 | { |
| 39 | Map.Entry e = (Entry) o; |
| 40 | ret = ret.assoc(e.getKey(), e.getValue()); |
| 41 | } |
| 42 | return ret; |
| 43 | } |
| 44 | |
| 45 | public PersistentTreeMap(){ |
| 46 | this(RT.DEFAULT_COMPARATOR); |
| 47 | } |
| 48 | |
| 49 | public PersistentTreeMap withMeta(IPersistentMap meta){ |
| 50 | if(_meta == meta) |
| 51 | return this; |
| 52 | return new PersistentTreeMap(meta, comp, tree, _count); |
| 53 | } |
| 54 | |
| 55 | private PersistentTreeMap(Comparator comp){ |
| 56 | this(null, comp); |
| 57 | } |
| 58 | |
| 59 | |
| 60 | public PersistentTreeMap(IPersistentMap meta, Comparator comp){ |
| 61 | this.comp = comp; |
| 62 | this._meta = meta; |
| 63 | tree = null; |
| 64 | _count = 0; |
| 65 | } |
| 66 | |
| 67 | PersistentTreeMap(IPersistentMap meta, Comparator comp, Node tree, int _count){ |
| 68 | this._meta = meta; |
| 69 | this.comp = comp; |
| 70 | this.tree = tree; |
| 71 | this._count = _count; |
| 72 | } |
| 73 | |
| 74 | static public PersistentTreeMap create(ISeq items){ |
| 75 | IPersistentMap ret = EMPTY; |
| 76 | for(; items != null; items = items.next().next()) |
| 77 | { |
| 78 | if(items.next() == null) |
| 79 | throw new IllegalArgumentException(String.format("No value supplied for key: %s", items.first())); |
| 80 | ret = ret.assoc(items.first(), RT.second(items)); |
| 81 | } |
| 82 | return (PersistentTreeMap) ret; |
| 83 | } |
nothing calls this directly
no outgoing calls
no test coverage detected