Red black tree based set, a sorted persistent collection.

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

The comparator used for sorting the set is stored in the set’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-set :b :a :c)
;;=> #{:a :b :c}

(config (->sorted-set :b :a :c))
;;=> {:comparator #<DefaultComparator clojure.lang.RT$DefaultComparator@5d1d396d>}

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

(->sorted-set :b :a :C)
;;=> #{:C :a :b}

(->sorted-set-by custom-comparator :b :a :C)
;;=> #{:a :b :C}

(flip (->sorted-set :b :a :c))
;;=> #{:c :b :a}

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

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

(ascending? s)
;;=> true

(section s :a :z)
;;=> (:a :b :c :d :e :f :g :h :i :j :k :l :m :n :o :p :q :r :s :t :u :v :w :x :y)

empty-rbt-sorted-set

Available since version 1.0 (view source)

not referred automatically

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

  • (empty-rbt-sorted-set comparator)

Type signatures:
  • () → RbtSortedSet

  • (Function) → RbtSortedSet

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

rbt-sorted-set-factory

Available since version 1.0 (view source)

not referred automatically

VAR of type ICollectionFactory

A RBT sorted set 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 factory protocols.

New instances of RBT set can be created with collection and ->collection.