Red black tree based map, a sorted persistent collection.

RBT sorted maps are persistent maps represented by a self-balancing binary search tree. Besides being sorted, they are counted and have support for efficient unpacked reduce, efficient sectioning, flipping and reversions.

The comparator used for sorting the map is stored in the map’s configuration and is accessible with config function.

Except for very special cases, it is idiomatic to use functions defined in coll.default rather than ones in this namespace.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
(ns foo.bar
  (:api dunaj)
  (:require [dunaj.string :refer [lower-case]]))

(->sorted-map :b 1 :a 2 :c 3)
;;=> {:a 2, :b 1, :c 3}

(config (->sorted-map :b 1 :a 2 :c 3))
;;=> {:comparator #<DefaultComparator [email protected]>}

(defn custom-comparator :- Integer
  "Returns a case insensitive comparator for named objects."
  [x :- INamed, y :- INamed]
  (compare (lower-case (name x)) (lower-case (name y))))
;;=> #'foo.bar/custom-comparator

(->sorted-map :b 1 :a 2 :C 3)
;;=> {:C 3, :a 2, :b 1}

(->sorted-map-by custom-comparator :b 1 :a 2 :C 3)
;;=> {:a 2, :b 1, :C 3}

(flip (->sorted-map :b 1 :a 2 :c 3))
;;=> {:c 3, :b 1, :a 2}

(def v (range 128))
;;=> #'foo.bar/v

(def m (sorted-zipmap (map (comp keyword ->str char) v) v))
;;=> #'foo.bar/m

(ascending? m)
;;=> true

(section m :a :z)
;;=> ([:a 97] [:b 98] [:c 99] [:d 100] [:e 101] [:f 102] [:g 103] [:h 104] [:i 105] [:j 106] [:k 107] [:l 108] [:m 109] [:n 110] [:o 111] [:p 112] [:q 113] [:r 114] [:s 115] [:t 116] [:u 117] [:v 118] [:w 119] [:x 120] [:y 121])

empty-rbt-sorted-map

Available since version 1.0 (view source)

not referred automatically

Usage:
  • (empty-rbt-sorted-map)

  • (empty-rbt-sorted-map comparator)

Type signatures:
  • () → RbtSortedMap

  • (Function) → RbtSortedMap

Returns an empty RBT map with comparator. Will compare using natural total ordering (see IComparable) if no comparator is given.

rbt-sorted-map-factory

Available since version 1.0 (view source)

not referred automatically

VAR of type (U ICollectionFactory IConvolutionFactory)

A RBT sorted map factory instance. Factory has following configuration options:

  • comparator - a comparator used for sorting items. nil represents a natural ordering, implemented with IComparable protocol.

This factory implements ICollectionFactory and IConvolutionFactory factory protocols.

New instances of RBT map can be created with collection, ->collection, convolution and ->convolution.