Liking cljdoc? Tell your friends :D

ordered-collections.core

Public API for ordered-collections.

Public API for ordered-collections.
raw docstring

aggregateclj

(aggregate coll)

Return the aggregate over the entire segment tree. O(1).

Return the aggregate over the entire segment tree. O(1).
sourceraw docstring

assoc-coalescingclj

(assoc-coalescing rm rng val)

Insert range with coalescing. Adjacent ranges with the same value are automatically merged. Equivalent to Guava's putCoalescing.

Use this instead of assoc when you want adjacent same-value ranges to be merged into a single range.

Example: (-> (range-map) (assoc-coalescing [0 100] :a) (assoc-coalescing [100 200] :a)) ;; => single range [0 200) :a

Insert range with coalescing. Adjacent ranges with the same value
are automatically merged. Equivalent to Guava's putCoalescing.

Use this instead of assoc when you want adjacent same-value ranges
to be merged into a single range.

Example:
  (-> (range-map)
      (assoc-coalescing [0 100] :a)
      (assoc-coalescing [100 200] :a))
  ;; => single range [0 200) :a
sourceraw docstring

assoc-newclj

(assoc-new m k v)

Associate key with value only if key is not already present. Returns the new collection with the key added, or the original collection unchanged if the key already exists.

Example: (assoc-new m :new-key :value) ; => {... :new-key :value} (assoc-new m :existing-key :v) ; => m (unchanged)

Associate key with value only if key is not already present.
Returns the new collection with the key added, or the original
collection unchanged if the key already exists.

Example:
  (assoc-new m :new-key :value)  ; => {... :new-key :value}
  (assoc-new m :existing-key :v) ; => m (unchanged)
sourceraw docstring

compare-byclj

(compare-by pred)

Given a predicate that defines a total order (e.g., <), return a java.util.Comparator. Example: (compare-by <) returns a comparator for ascending order.

Given a predicate that defines a total order (e.g., <), return a java.util.Comparator.
Example: (compare-by <) returns a comparator for ascending order.
sourceraw docstring

differenceclj

(difference this that)

Return a set that is s1 without elements in s2.

For ordered-sets: Uses Adams' divide-and-conquer algorithm with parallel execution for large sets. In the current benchmark suite, roughly 5-24x faster than clojure.set/difference on the tested workloads.

Complexity: O(m log(n/m + 1)) where m <= n

Examples: (difference (ordered-set [1 2 3]) (ordered-set [2])) ; #{1 3}

Return a set that is s1 without elements in s2.

For ordered-sets: Uses Adams' divide-and-conquer algorithm with parallel
execution for large sets. In the current benchmark suite, roughly 5-24x
faster than clojure.set/difference on the tested workloads.

Complexity: O(m log(n/m + 1)) where m <= n

Examples:
  (difference (ordered-set [1 2 3]) (ordered-set [2]))  ; #{1 3}
sourceraw docstring

disj-allclj

(disj-all ms x)

Remove all occurrences of x from a multiset. (disj-all ms x) => new-ms

Remove all occurrences of x from a multiset.
(disj-all ms x) => new-ms
sourceraw docstring

disj-oneclj

(disj-one ms x)

Remove one occurrence of x from a multiset. (disj-one ms x) => new-ms

Remove one occurrence of x from a multiset.
(disj-one ms x) => new-ms
sourceraw docstring

disjoint?clj

(disjoint? this that)

True if s1 and s2 share no elements. Short-circuits on the first common element found.

Examples: (disjoint? (ordered-set [1 2 3]) (ordered-set [4 5 6])) ; true (disjoint? (ordered-set [1 2 3]) (ordered-set [3 4 5])) ; false

True if s1 and s2 share no elements.
Short-circuits on the first common element found.

Examples:
  (disjoint? (ordered-set [1 2 3]) (ordered-set [4 5 6]))  ; true
  (disjoint? (ordered-set [1 2 3]) (ordered-set [3 4 5]))  ; false
sourceraw docstring

distinct-elementsclj

(distinct-elements ms)

Return a lazy seq of distinct elements in sorted order. (distinct-elements ms) => seq

Return a lazy seq of distinct elements in sorted order.
(distinct-elements ms) => seq
sourceraw docstring

double-compareclj

Specialized java.util.Comparator for Double keys. Uses Double/compare directly for faster numeric comparisons.

Specialized java.util.Comparator for Double keys.
Uses Double/compare directly for faster numeric comparisons.
sourceraw docstring

double-ordered-mapclj

(double-ordered-map)
(double-ordered-map coll)

Create an ordered map optimized for Double keys. Uses primitive double storage and specialized Double.compare for faster comparisons.

Create an ordered map optimized for Double keys.
Uses primitive double storage and specialized Double.compare for faster comparisons.
sourceraw docstring

double-ordered-setclj

(double-ordered-set)
(double-ordered-set coll)

Create an ordered set optimized for Double keys. Uses primitive double storage and specialized Double.compare for faster comparisons.

Create an ordered set optimized for Double keys.
Uses primitive double storage and specialized Double.compare for faster comparisons.
sourceraw docstring

element-frequenciesclj

(element-frequencies ms)

Return a map of {element -> count} for all elements. (element-frequencies ms) => map

Return a map of {element -> count} for all elements.
(element-frequencies ms) => map
sourceraw docstring

fuzzy-exact-contains?clj

(fuzzy-exact-contains? coll k)

Check if the fuzzy collection contains exactly the given element/key. Unlike regular lookup, this does not do fuzzy matching.

Check if the fuzzy collection contains exactly the given element/key.
Unlike regular lookup, this does not do fuzzy matching.
sourceraw docstring

fuzzy-exact-getclj

(fuzzy-exact-get coll k)
(fuzzy-exact-get coll k not-found)

Get the value for exactly the given key (no fuzzy matching). Only for fuzzy-map.

Get the value for exactly the given key (no fuzzy matching).
Only for fuzzy-map.
sourceraw docstring

fuzzy-mapclj

(fuzzy-map coll
           &
           {:keys [tiebreak distance]
            :or {tiebreak :< distance fuzzy-set/numeric-distance}})

Create a fuzzy map that returns the value for the closest key.

When looking up a key, returns the value for the key in the map that is closest to the query. For numeric keys, distance is |query - key|.

Options: :tiebreak - :< (prefer smaller, default) or :> (prefer larger) when equidistant :distance - custom distance function (fn [a b] -> number)

Examples: (def fm (fuzzy-map {0 :zero 10 :ten 100 :hundred})) (fm 7) ; => :ten (closest key to 7 is 10) (fm 42) ; => :ten (closest key to 42 is 10 or 100)

;; With tiebreak (def fm (fuzzy-map {0 :zero 10 :ten 100 :hundred} :tiebreak :>)) (fm 55) ; => :hundred (prefer larger when equidistant)

The collection should be a map or sequence of [key value] pairs.

Create a fuzzy map that returns the value for the closest key.

When looking up a key, returns the value for the key in the map that is
closest to the query. For numeric keys, distance is |query - key|.

Options:
  :tiebreak - :< (prefer smaller, default) or :> (prefer larger) when equidistant
  :distance - custom distance function (fn [a b] -> number)

Examples:
  (def fm (fuzzy-map {0 :zero 10 :ten 100 :hundred}))
  (fm 7)   ; => :ten (closest key to 7 is 10)
  (fm 42)  ; => :ten (closest key to 42 is 10 or 100)

  ;; With tiebreak
  (def fm (fuzzy-map {0 :zero 10 :ten 100 :hundred} :tiebreak :>))
  (fm 55)  ; => :hundred (prefer larger when equidistant)

The collection should be a map or sequence of [key value] pairs.
sourceraw docstring

fuzzy-map-byclj

(fuzzy-map-by comparator
              coll
              &
              {:keys [tiebreak distance]
               :or {tiebreak :< distance fuzzy-set/numeric-distance}})

Create a fuzzy map with a custom comparator.

Example: (fuzzy-map-by > {1 :a 5 :b 10 :c}) ; reverse key order

Create a fuzzy map with a custom comparator.

Example:
  (fuzzy-map-by > {1 :a 5 :b 10 :c})  ; reverse key order
sourceraw docstring

fuzzy-nearestclj

(fuzzy-nearest coll query)

Find the nearest element/entry and its distance. For fuzzy-set: (fuzzy-nearest fs query) => [element distance] For fuzzy-map: (fuzzy-nearest fm query) => [key value distance]

Find the nearest element/entry and its distance.
For fuzzy-set: (fuzzy-nearest fs query) => [element distance]
For fuzzy-map: (fuzzy-nearest fm query) => [key value distance]
sourceraw docstring

fuzzy-setclj

(fuzzy-set coll
           &
           {:keys [tiebreak distance]
            :or {tiebreak :< distance fuzzy-set/numeric-distance}})

Create a fuzzy set that returns the closest element to a query.

When looking up a value, returns the element in the set that is closest to the query. For numeric keys, distance is |query - element|.

Options: :tiebreak - :< (prefer smaller, default) or :> (prefer larger) when equidistant :distance - custom distance function (fn [a b] -> number)

Examples: (def fs (fuzzy-set [1 5 10 20])) (fs 7) ; => 5 (closest to 7) (fs 15) ; => 10 or 20 depending on tiebreak

;; With tiebreak (def fs (fuzzy-set [1 5 10 20] :tiebreak :>)) (fs 15) ; => 20 (prefer larger when equidistant)

;; With custom distance (def fs (fuzzy-set ["apple" "banana" "cherry"] :distance (fn [a b] (Math/abs (- (count a) (count b)))))) (fs "pear") ; => closest by string length

Create a fuzzy set that returns the closest element to a query.

When looking up a value, returns the element in the set that is closest
to the query. For numeric keys, distance is |query - element|.

Options:
  :tiebreak - :< (prefer smaller, default) or :> (prefer larger) when equidistant
  :distance - custom distance function (fn [a b] -> number)

Examples:
  (def fs (fuzzy-set [1 5 10 20]))
  (fs 7)   ; => 5 (closest to 7)
  (fs 15)  ; => 10 or 20 depending on tiebreak

  ;; With tiebreak
  (def fs (fuzzy-set [1 5 10 20] :tiebreak :>))
  (fs 15)  ; => 20 (prefer larger when equidistant)

  ;; With custom distance
  (def fs (fuzzy-set ["apple" "banana" "cherry"]
            :distance (fn [a b] (Math/abs (- (count a) (count b))))))
  (fs "pear")  ; => closest by string length
sourceraw docstring

fuzzy-set-byclj

(fuzzy-set-by comparator
              coll
              &
              {:keys [tiebreak distance]
               :or {tiebreak :< distance fuzzy-set/numeric-distance}})

Create a fuzzy set with a custom comparator.

Example: (fuzzy-set-by > [1 5 10 20]) ; reverse order

Create a fuzzy set with a custom comparator.

Example:
  (fuzzy-set-by > [1 5 10 20])  ; reverse order
sourceraw docstring

gapsclj

(gaps rm)

Return a seq of [lo hi) ranges that have no mapping in a range-map.

Return a seq of [lo hi) ranges that have no mapping in a range-map.
sourceraw docstring

general-compareclj

General-purpose java.util.Comparator that provides a deterministic total order over all values, including types that clojure.core/compare does not order (such as Namespace and Var).

Use with ordered-set-with / ordered-map-with: (ordered-set-with general-compare (all-ns)) (ordered-map-with general-compare [[#'clojure.core/map :map]])

Expect roughly 20% slower lookups compared to the default comparator on Comparable types. Set algebra overhead is smaller and masked by parallelism at scale.

General-purpose java.util.Comparator that provides a deterministic total
order over all values, including types that clojure.core/compare does not
order (such as Namespace and Var).

Use with ordered-set-with / ordered-map-with:
  (ordered-set-with general-compare (all-ns))
  (ordered-map-with general-compare [[#'clojure.core/map :map]])

Expect roughly 20% slower lookups compared to the default comparator on
Comparable types. Set algebra overhead is smaller and masked by parallelism
at scale.
sourceraw docstring

get-entryclj

(get-entry rm point)

Return [range value] for the range containing point x, or nil. Equivalent to Guava's getEntry(K).

Example: (get-entry rm 50) ;; => [[0 100] :a]

Return [range value] for the range containing point x, or nil.
Equivalent to Guava's getEntry(K).

Example:
  (get-entry rm 50) ;; => [[0 100] :a]
sourceraw docstring

intersectionclj

(intersection this that)

Return a set that is the intersection of the input sets.

For ordered-sets: Uses Adams' divide-and-conquer algorithm with parallel execution for large sets. In the current benchmark suite, roughly 4-17x faster than clojure.set/intersection on the tested workloads.

Complexity: O(m log(n/m + 1)) where m <= n

Examples: (intersection (ordered-set [1 2 3]) (ordered-set [2 3 4])) ; #{2 3}

Return a set that is the intersection of the input sets.

For ordered-sets: Uses Adams' divide-and-conquer algorithm with parallel
execution for large sets. In the current benchmark suite, roughly 4-17x
faster than clojure.set/intersection on the tested workloads.

Complexity: O(m log(n/m + 1)) where m <= n

Examples:
  (intersection (ordered-set [1 2 3]) (ordered-set [2 3 4]))  ; #{2 3}
sourceraw docstring

interval-mapclj

(interval-map)
(interval-map coll)

Create an interval map from [interval value] entries. Intervals are [lo hi] pairs. Supports O(log n + k) overlap queries.

Query by invoking as a function: (imap point) — entries whose interval contains point (imap [lo hi]) — entries overlapping the range

Examples: (interval-map [[[1 5] :a] [[3 8] :b]]) (def imap (interval-map [[[0 10] "x"] [[5 20] "y"]])) (imap 7) ; => [[[0 10] "x"] [[5 20] "y"]] (imap [11 15]) ; => [[[5 20] "y"]]

Create an interval map from [interval value] entries.
Intervals are [lo hi] pairs. Supports O(log n + k) overlap queries.

Query by invoking as a function:
  (imap point)       — entries whose interval contains point
  (imap [lo hi])     — entries overlapping the range

Examples:
  (interval-map [[[1 5] :a] [[3 8] :b]])
  (def imap (interval-map [[[0 10] "x"] [[5 20] "y"]]))
  (imap 7)         ; => [[[0 10] "x"] [[5 20] "y"]]
  (imap [11 15])   ; => [[[5 20] "y"]]
sourceraw docstring

interval-setclj

(interval-set)
(interval-set coll)

Create an interval set from intervals or points. Intervals are [lo hi] pairs; bare values become [x x] point intervals. Supports O(log n + k) overlap queries.

Query by invoking as a function: (iset point) — intervals containing point (iset [lo hi]) — intervals overlapping the range

Examples: (interval-set [[1 3] [2 4] [5 9]]) (def iset (interval-set [[0 10] [5 20]])) (iset 7) ; => [[0 10] [5 20]] (iset [11 15]) ; => [[5 20]]

Create an interval set from intervals or points.
Intervals are [lo hi] pairs; bare values become [x x] point intervals.
Supports O(log n + k) overlap queries.

Query by invoking as a function:
  (iset point)       — intervals containing point
  (iset [lo hi])     — intervals overlapping the range

Examples:
  (interval-set [[1 3] [2 4] [5 9]])
  (def iset (interval-set [[0 10] [5 20]]))
  (iset 7)         ; => [[0 10] [5 20]]
  (iset [11 15])   ; => [[5 20]]
sourceraw docstring

long-compareclj

Specialized java.util.Comparator for Long keys. Uses Long/compare directly for ~15-25% faster comparisons than default.

Specialized java.util.Comparator for Long keys.
Uses Long/compare directly for ~15-25% faster comparisons than default.
sourceraw docstring

long-ordered-mapclj

(long-ordered-map)
(long-ordered-map coll)

Create an ordered map optimized for Long keys. Uses primitive long storage and specialized Long.compare for maximum performance. Typically 15-25% faster than ordered-map for numeric workloads.

Create an ordered map optimized for Long keys.
Uses primitive long storage and specialized Long.compare for maximum performance.
Typically 15-25% faster than ordered-map for numeric workloads.
sourceraw docstring

long-ordered-setclj

(long-ordered-set)
(long-ordered-set coll)

Create an ordered set optimized for Long keys. Uses primitive long storage and specialized Long.compare for maximum performance. Typically 15-25% faster than ordered-set for numeric workloads.

Create an ordered set optimized for Long keys.
Uses primitive long storage and specialized Long.compare for maximum performance.
Typically 15-25% faster than ordered-set for numeric workloads.
sourceraw docstring

max-treeclj

(max-tree coll)

Create a segment tree for range maximum queries.

Create a segment tree for range maximum queries.
sourceraw docstring

medianclj

(median coll)

Return the median element. O(log n). Works on any collection implementing PRanked.

Return the median element. O(log n).
Works on any collection implementing PRanked.
sourceraw docstring

min-treeclj

(min-tree coll)

Create a segment tree for range minimum queries.

Create a segment tree for range minimum queries.
sourceraw docstring

multiplicityclj

(multiplicity ms x)

Return the number of occurrences of x in a multiset. (multiplicity ms x) => count

Return the number of occurrences of x in a multiset.
(multiplicity ms x) => count
sourceraw docstring

nearestclj

(nearest coll test k)

Find the nearest element to key k satisfying the given test.

Tests: :< - greatest element less than k (predecessor) :<= - greatest element less than or equal to k (floor) :>= - least element greater than or equal to k (ceiling) :> - least element greater than k (successor)

Returns the element (for sets) or [key value] (for maps), or nil if none.

Complexity: O(log n)

Example: (nearest (ordered-set [1 3 5 7 9]) :< 6) ;=> 5

(nearest (ordered-set [1 3 5 7 9]) :>= 6) ;=> 7

(nearest (ordered-map [[1 :a] [3 :b] [5 :c]]) :<= 4) ;=> [3 :b]

Find the nearest element to key k satisfying the given test.

Tests:
  :<  - greatest element less than k (predecessor)
  :<= - greatest element less than or equal to k (floor)
  :>= - least element greater than or equal to k (ceiling)
  :>  - least element greater than k (successor)

Returns the element (for sets) or [key value] (for maps), or nil if none.

Complexity: O(log n)

Example:
  (nearest (ordered-set [1 3 5 7 9]) :< 6)
  ;=> 5

  (nearest (ordered-set [1 3 5 7 9]) :>= 6)
  ;=> 7

  (nearest (ordered-map [[1 :a] [3 :b] [5 :c]]) :<= 4)
  ;=> [3 :b]
sourceraw docstring

ordered-mapclj

(ordered-map)
(ordered-map coll)

Create a persistent sorted map backed by a weight-balanced binary tree.

Drop-in replacement for clojure.core/sorted-map with these enhancements:

  • O(log n) first/last via java.util.SortedMap (vs O(n) for sorted-map)
  • O(log n) nth positional access
  • Parallel r/fold (2.3x faster than sorted-map)
  • Fast merge-with via ordered-merge-with

Keys are sorted by clojure.core/compare. For custom ordering, use ordered-map-by. For numeric keys, use long-ordered-map.

Examples: (ordered-map) ; empty map (ordered-map [[3 :c] [1 :a] [2 :b]]) ; {1 :a, 2 :b, 3 :c} (ordered-map {3 :c, 1 :a, 2 :b}) ; {1 :a, 2 :b, 3 :c} (first (ordered-map (zipmap (range 1e6) (range)))) ; [0 0], in O(log n)

Memory: ~88 bytes/entry (vs ~85 for sorted-map, ~4% overhead)

Create a persistent sorted map backed by a weight-balanced binary tree.

Drop-in replacement for clojure.core/sorted-map with these enhancements:
- O(log n) first/last via java.util.SortedMap (vs O(n) for sorted-map)
- O(log n) nth positional access
- Parallel r/fold (2.3x faster than sorted-map)
- Fast merge-with via ordered-merge-with

Keys are sorted by clojure.core/compare. For custom ordering,
use ordered-map-by. For numeric keys, use long-ordered-map.

Examples:
  (ordered-map)                          ; empty map
  (ordered-map [[3 :c] [1 :a] [2 :b]])   ; {1 :a, 2 :b, 3 :c}
  (ordered-map {3 :c, 1 :a, 2 :b})       ; {1 :a, 2 :b, 3 :c}
  (first (ordered-map (zipmap (range 1e6) (range))))  ; [0 0], in O(log n)

Memory: ~88 bytes/entry (vs ~85 for sorted-map, ~4% overhead)
sourceraw docstring

ordered-map-byclj

(ordered-map-by pred coll)

Create an ordered map with custom key ordering via a predicate.

The predicate should define a total order (like < or >).

Examples: (ordered-map-by > [[1 :a] [2 :b]]) ; descending keys: {2 :b, 1 :a}

Create an ordered map with custom key ordering via a predicate.

The predicate should define a total order (like < or >).

Examples:
  (ordered-map-by > [[1 :a] [2 :b]])  ; descending keys: {2 :b, 1 :a}
sourceraw docstring

ordered-map-withclj

(ordered-map-with comparator)
(ordered-map-with comparator coll)

Create an ordered map with a custom java.util.Comparator. For best performance, use a Comparator rather than a predicate.

Examples: ;; Using a pre-built comparator (ordered-map-with long-compare [[1 :a] [2 :b]])

;; Using compare-by with a predicate (slightly slower) (ordered-map-with (compare-by >) {1 :a 2 :b}) ; descending key order

Create an ordered map with a custom java.util.Comparator.
For best performance, use a Comparator rather than a predicate.

Examples:
  ;; Using a pre-built comparator
  (ordered-map-with long-compare [[1 :a] [2 :b]])

  ;; Using compare-by with a predicate (slightly slower)
  (ordered-map-with (compare-by >) {1 :a 2 :b})  ; descending key order
sourceraw docstring

ordered-merge-withclj

(ordered-merge-with f & maps)

Merge ordered maps with a function to resolve conflicts. When the same key appears in multiple maps, (f key val-in-result val-in-latter) is called. Uses the same conservative fork-join threshold as ordered-set algebra for large compatible ordered-maps.

Examples: (ordered-merge-with (fn [k a b] (+ a b)) m1 m2) (ordered-merge-with (fn [k a b] b) m1 m2 m3) ; last-wins

Merge ordered maps with a function to resolve conflicts.
When the same key appears in multiple maps, (f key val-in-result val-in-latter) is called.
Uses the same conservative fork-join threshold as ordered-set algebra for
large compatible ordered-maps.

Examples:
  (ordered-merge-with (fn [k a b] (+ a b)) m1 m2)
  (ordered-merge-with (fn [k a b] b) m1 m2 m3)  ; last-wins
sourceraw docstring

ordered-multisetclj

(ordered-multiset)
(ordered-multiset coll)

Create an ordered multiset (sorted bag) from a collection. Unlike ordered-set, allows duplicate elements.

Supports O(log n) add/remove, nth access, and parallel fold.

Examples: (ordered-multiset) ; empty multiset (ordered-multiset [3 1 4 1 5 9 2 6 5 3 5]) ;; => #OrderedMultiset[1 1 2 3 3 4 5 5 5 6 9]

Create an ordered multiset (sorted bag) from a collection.
Unlike ordered-set, allows duplicate elements.

Supports O(log n) add/remove, nth access, and parallel fold.

Examples:
  (ordered-multiset)                                ; empty multiset
  (ordered-multiset [3 1 4 1 5 9 2 6 5 3 5])
  ;; => #OrderedMultiset[1 1 2 3 3 4 5 5 5 6 9]
sourceraw docstring

ordered-multiset-byclj

(ordered-multiset-by pred coll)

Create an ordered multiset with custom ordering via a predicate.

Example: (ordered-multiset-by > [3 1 4 1 5])

Create an ordered multiset with custom ordering via a predicate.

Example:
  (ordered-multiset-by > [3 1 4 1 5])
sourceraw docstring

ordered-multiset-withclj

(ordered-multiset-with comparator)
(ordered-multiset-with comparator coll)

Create an ordered multiset with a custom java.util.Comparator.

Example: (ordered-multiset-with long-compare [3 1 4 1 5])

Create an ordered multiset with a custom java.util.Comparator.

Example:
  (ordered-multiset-with long-compare [3 1 4 1 5])
sourceraw docstring

ordered-setclj

(ordered-set)
(ordered-set coll)

Create a persistent sorted set backed by a weight-balanced binary tree.

Drop-in replacement for clojure.core/sorted-set with these enhancements:

  • O(log n) first/last via java.util.SortedSet (vs O(n) for sorted-set)
  • O(log n) nth positional access
  • Parallel r/fold (2.3x faster than sorted-set)
  • 7-9x faster set operations (union, intersection, difference)

Elements are sorted by clojure.core/compare. For custom ordering, use ordered-set-by. For numeric keys, use long-ordered-set.

Examples: (ordered-set) ; empty set (ordered-set [3 1 4 1 5 9]) ; #{1 3 4 5 9} (first (ordered-set (range 1e6))) ; 0, in O(log n) (nth (ordered-set (range 100)) 50) ; 50, in O(log n)

Memory: ~64 bytes/element (vs ~61 for sorted-set, ~6% overhead)

Create a persistent sorted set backed by a weight-balanced binary tree.

Drop-in replacement for clojure.core/sorted-set with these enhancements:
- O(log n) first/last via java.util.SortedSet (vs O(n) for sorted-set)
- O(log n) nth positional access
- Parallel r/fold (2.3x faster than sorted-set)
- 7-9x faster set operations (union, intersection, difference)

Elements are sorted by clojure.core/compare. For custom ordering,
use ordered-set-by. For numeric keys, use long-ordered-set.

Examples:
  (ordered-set)                      ; empty set
  (ordered-set [3 1 4 1 5 9])        ; #{1 3 4 5 9}
  (first (ordered-set (range 1e6)))  ; 0, in O(log n)
  (nth (ordered-set (range 100)) 50) ; 50, in O(log n)

Memory: ~64 bytes/element (vs ~61 for sorted-set, ~6% overhead)
sourceraw docstring

ordered-set-byclj

(ordered-set-by pred coll)

Create an ordered set with custom ordering via a predicate.

The predicate should define a total order (like < or >).

Examples: (ordered-set-by > [1 2 3]) ; descending: #{3 2 1} (ordered-set-by #(compare (count %1) (count %2)) ["a" "bb" "ccc"])

Create an ordered set with custom ordering via a predicate.

The predicate should define a total order (like < or >).

Examples:
  (ordered-set-by > [1 2 3])  ; descending: #{3 2 1}
  (ordered-set-by #(compare (count %1) (count %2)) ["a" "bb" "ccc"])
sourceraw docstring

ordered-set-withclj

(ordered-set-with comparator)
(ordered-set-with comparator coll)

Create an ordered set with a custom java.util.Comparator. For best performance, use a Comparator rather than a predicate.

Examples: ;; Using a pre-built comparator (ordered-set-with long-compare [1 2 3])

;; Using compare-by with a predicate (slightly slower) (ordered-set-with (compare-by >) [1 2 3]) ; descending order

Create an ordered set with a custom java.util.Comparator.
For best performance, use a Comparator rather than a predicate.

Examples:
  ;; Using a pre-built comparator
  (ordered-set-with long-compare [1 2 3])

  ;; Using compare-by with a predicate (slightly slower)
  (ordered-set-with (compare-by >) [1 2 3])  ; descending order
sourceraw docstring

overlappingclj

(overlapping coll interval)

Return all intervals overlapping the given point or interval. Works with interval-set and interval-map.

For interval-set: returns seq of intervals For interval-map: returns seq of [interval value] entries

Example: (overlapping iset 5) ; intervals containing point 5 (overlapping iset [3 7]) ; intervals overlapping range [3,7] (overlapping imap 5) ; entries for intervals containing 5

Return all intervals overlapping the given point or interval.
Works with interval-set and interval-map.

For interval-set: returns seq of intervals
For interval-map: returns seq of [interval value] entries

Example:
  (overlapping iset 5)           ; intervals containing point 5
  (overlapping iset [3 7])       ; intervals overlapping range [3,7]
  (overlapping imap 5)           ; entries for intervals containing 5
sourceraw docstring

peek-maxclj

(peek-max pq)

Return [priority value] of the last element in queue order. (peek-max pq) => [priority value] or nil

Return [priority value] of the last element in queue order.
(peek-max pq) => [priority value] or nil
sourceraw docstring

peek-max-valclj

(peek-max-val pq)

Return just the value of the last element in queue order. (peek-max-val pq) => value or nil

Return just the value of the last element in queue order.
(peek-max-val pq) => value or nil
sourceraw docstring

peek-minclj

(peek-min pq)

Return [priority value] of the first element in queue order. (peek-min pq) => [priority value] or nil

Return [priority value] of the first element in queue order.
(peek-min pq) => [priority value] or nil
sourceraw docstring

peek-min-valclj

(peek-min-val pq)

Return just the value (not priority) of the first element in queue order. (peek-min-val pq) => value or nil

Note: (peek-min pq) returns [priority value].

Return just the value (not priority) of the first element in queue order.
(peek-min-val pq) => value or nil

Note: (peek-min pq) returns [priority value].
sourceraw docstring

percentileclj

(percentile coll pct)

Return element at given percentile (0-100). O(log n). Works on any collection implementing PRanked.

Return element at given percentile (0-100). O(log n).
Works on any collection implementing PRanked.
sourceraw docstring

pop-maxclj

(pop-max pq)

Remove the last element in queue order. Returns the queue unchanged if empty. (pop-max pq) => new-pq

Remove the last element in queue order.
Returns the queue unchanged if empty.
(pop-max pq) => new-pq
sourceraw docstring

pop-minclj

(pop-min pq)

Remove the first element in queue order. Returns the queue unchanged if empty. (pop-min pq) => new-pq

Remove the first element in queue order.
Returns the queue unchanged if empty.
(pop-min pq) => new-pq
sourceraw docstring

priority-queueclj

(priority-queue)
(priority-queue pairs)

Create a persistent priority queue from [priority value] pairs.

Supports O(log n) push/peek/pop operations, plus parallel fold. Priorities are ordered by clojure.core/compare (natural ordering). For custom ordering, use priority-queue-by or priority-queue-with.

Examples: (priority-queue) (priority-queue [[1 :urgent] [5 :low] [3 :medium]]) (priority-queue [["beta" :b] ["alpha" :a]])

Create a persistent priority queue from [priority value] pairs.

Supports O(log n) push/peek/pop operations, plus parallel fold.
Priorities are ordered by clojure.core/compare (natural ordering).
For custom ordering, use priority-queue-by or priority-queue-with.

Examples:
  (priority-queue)
  (priority-queue [[1 :urgent] [5 :low] [3 :medium]])
  (priority-queue [["beta" :b] ["alpha" :a]])
sourceraw docstring

priority-queue-byclj

(priority-queue-by pred pairs)

Create a priority queue with custom ordering via a predicate.

Examples: (priority-queue-by > [[1 :a] [3 :c] [2 :b]]) ; max-heap (priority-queue-by > []) ; empty max-heap

Create a priority queue with custom ordering via a predicate.

Examples:
  (priority-queue-by > [[1 :a] [3 :c] [2 :b]])  ; max-heap
  (priority-queue-by > [])                         ; empty max-heap
sourceraw docstring

priority-queue-withclj

(priority-queue-with comparator)
(priority-queue-with comparator pairs)

Create a priority queue with a custom java.util.Comparator for priorities.

Examples: (priority-queue-with long-compare [[1 :a] [2 :b]]) (priority-queue-with string-compare)

Create a priority queue with a custom java.util.Comparator for priorities.

Examples:
  (priority-queue-with long-compare [[1 :a] [2 :b]])
  (priority-queue-with string-compare)
sourceraw docstring

pushclj

(push pq priority value)

Add an element to a priority queue with the given priority. (push pq priority value) => new-pq

Example: (push pq 1 :urgent)

Add an element to a priority queue with the given priority.
(push pq priority value) => new-pq

Example:
  (push pq 1 :urgent)
sourceraw docstring

push-allclj

(push-all pq pairs)

Add multiple [priority value] pairs to a priority queue. (push-all pq [[p1 v1] [p2 v2]]) => new-pq

Add multiple [priority value] pairs to a priority queue.
(push-all pq [[p1 v1] [p2 v2]]) => new-pq
sourceraw docstring

queryclj

(query coll lo hi)

Query the aggregate over key range [lo, hi] inclusive. O(log n).

Example: (def st (segment-tree + 0 {0 10, 1 20, 2 30, 3 40})) (query st 0 3) ; => 100 (query st 1 2) ; => 50

Query the aggregate over key range [lo, hi] inclusive. O(log n).

Example:
  (def st (segment-tree + 0 {0 10, 1 20, 2 30, 3 40}))
  (query st 0 3)  ; => 100
  (query st 1 2)  ; => 50
sourceraw docstring

range-mapclj

(range-map)
(range-map coll)

Create a map from non-overlapping ranges to values.

Unlike interval-map, ranges never overlap. Inserting a range removes any overlapping portions of existing ranges.

Ranges are half-open: [lo, hi) includes lo but excludes hi.

Example: (def rm (range-map {[0 10] :a [20 30] :b})) (rm 5) ; => :a (rm 15) ; => nil (gap) (assoc rm [5 25] :c) ; splits existing ranges

Create a map from non-overlapping ranges to values.

Unlike interval-map, ranges never overlap. Inserting a range removes
any overlapping portions of existing ranges.

Ranges are half-open: [lo, hi) includes lo but excludes hi.

Example:
  (def rm (range-map {[0 10] :a [20 30] :b}))
  (rm 5)            ; => :a
  (rm 15)           ; => nil (gap)
  (assoc rm [5 25] :c)  ; splits existing ranges
sourceraw docstring

range-removeclj

(range-remove rm rng)

Remove all mappings in the given range [lo hi). Any overlapping ranges are trimmed; ranges fully contained are removed. Equivalent to Guava's remove(Range).

Example: (range-remove rm [25 75]) ;; [0 100]:a becomes [0 25):a and [75 100):a

Remove all mappings in the given range [lo hi).
Any overlapping ranges are trimmed; ranges fully contained are removed.
Equivalent to Guava's remove(Range).

Example:
  (range-remove rm [25 75])
  ;; [0 100]:a becomes [0 25):a and [75 100):a
sourceraw docstring

rangesclj

(ranges rm)

Return seq of [range value] pairs from a range-map.

Return seq of [range value] pairs from a range-map.
sourceraw docstring

rankclj

(rank coll x)

Return the 0-based index of element x, or nil if not present. O(log n). Works on any collection implementing PRanked (ordered-set, ordered-map, etc.).

Return the 0-based index of element x, or nil if not present. O(log n).
Works on any collection implementing PRanked (ordered-set, ordered-map, etc.).
sourceraw docstring

ropeclj

(rope)
(rope coll)

Create a persistent rope from a collection.

A rope is a chunked, tree-backed persistent sequence optimized for structural editing: O(log n) concat, split, splice, insert, and remove. Use a rope when you need to repeatedly edit the middle of a large sequence. Use a vector for random-access-heavy workloads.

Supports nth, get, assoc, conj, peek, pop, seq, rseq, reduce, r/fold, compare, java.util.List, and all standard Clojure sequence operations.

Examples: (rope [1 2 3 4 5]) (rope (range 100000)) (nth (rope (range 1000)) 500) ;=> 500

Create a persistent rope from a collection.

A rope is a chunked, tree-backed persistent sequence optimized for
structural editing: O(log n) concat, split, splice, insert, and remove.
Use a rope when you need to repeatedly edit the middle of a large
sequence. Use a vector for random-access-heavy workloads.

Supports nth, get, assoc, conj, peek, pop, seq, rseq, reduce,
r/fold, compare, java.util.List, and all standard Clojure sequence
operations.

Examples:
  (rope [1 2 3 4 5])
  (rope (range 100000))
  (nth (rope (range 1000)) 500)  ;=> 500
sourceraw docstring

rope-chunk-countclj

(rope-chunk-count v)

Return the number of chunks in the rope.

Return the number of chunks in the rope.
sourceraw docstring

rope-chunksclj

(rope-chunks r)

Return a seq of the rope's internal chunk vectors.

Return a seq of the rope's internal chunk vectors.
sourceraw docstring

rope-chunks-reverseclj

(rope-chunks-reverse v)

Return a reverse seq of the rope's internal chunk vectors.

Return a reverse seq of the rope's internal chunk vectors.
sourceraw docstring

rope-concatclj

(rope-concat x)
(rope-concat left right)
(rope-concat left right & more)

Concatenate ropes or rope-coercible collections. Two arguments: O(log n) binary tree join. Three or more: O(total chunks) bulk construction.

Examples: (rope-concat (rope [1 2 3]) (rope [4 5 6])) ;=> #ordered/rope [1 2 3 4 5 6] (rope-concat (rope [1 2]) (rope [3 4]) (rope [5 6])) ;=> #ordered/rope [1 2 3 4 5 6]

Concatenate ropes or rope-coercible collections.
Two arguments: O(log n) binary tree join.
Three or more: O(total chunks) bulk construction.

Examples:
  (rope-concat (rope [1 2 3]) (rope [4 5 6]))
  ;=> #ordered/rope [1 2 3 4 5 6]
  (rope-concat (rope [1 2]) (rope [3 4]) (rope [5 6]))
  ;=> #ordered/rope [1 2 3 4 5 6]
sourceraw docstring

rope-insertclj

(rope-insert r i coll)

Insert elements at index i. O(log n).

Examples: (rope-insert (rope [0 1 2 3]) 2 [:a :b]) ;=> #ordered/rope [0 1 :a :b 2 3]

Insert elements at index i. O(log n).

Examples:
  (rope-insert (rope [0 1 2 3]) 2 [:a :b])
  ;=> #ordered/rope [0 1 :a :b 2 3]
sourceraw docstring

rope-removeclj

(rope-remove r start end)

Remove elements in range [start, end). O(log n).

Examples: (rope-remove (rope (range 10)) 3 7) ;=> #ordered/rope [0 1 2 7 8 9]

Remove elements in range [start, end). O(log n).

Examples:
  (rope-remove (rope (range 10)) 3 7)
  ;=> #ordered/rope [0 1 2 7 8 9]
sourceraw docstring

rope-spliceclj

(rope-splice r start end coll)

Replace elements in range [start, end) with new content. O(log n).

Examples: (rope-splice (rope (range 10)) 2 5 [:x :y]) ;=> #ordered/rope [0 1 :x :y 5 6 7 8 9]

Replace elements in range [start, end) with new content. O(log n).

Examples:
  (rope-splice (rope (range 10)) 2 5 [:x :y])
  ;=> #ordered/rope [0 1 :x :y 5 6 7 8 9]
sourceraw docstring

rope-splitclj

(rope-split r i)

Split a rope at element index i, returning [left right]. O(log n).

Examples: (rope-split (rope (range 10)) 4) ;=> [#ordered/rope [0 1 2 3] #ordered/rope [4 5 6 7 8 9]]

Split a rope at element index i, returning [left right]. O(log n).

Examples:
  (rope-split (rope (range 10)) 4)
  ;=> [#ordered/rope [0 1 2 3] #ordered/rope [4 5 6 7 8 9]]
sourceraw docstring

rope-strclj

(rope-str r)

Efficiently convert a rope of characters/strings to a String via StringBuilder. Much faster than (apply str r) for large ropes.

Examples: (rope-str (rope (seq "hello world"))) ;=> "hello world"

Efficiently convert a rope of characters/strings to a String via
StringBuilder. Much faster than (apply str r) for large ropes.

 Examples:
   (rope-str (rope (seq "hello world")))
   ;=> "hello world"
sourceraw docstring

rope-subclj

(rope-sub r start end)

Extract a subrange [start, end) as a Rope. O(log n). The result shares structure with the original.

Examples: (rope-sub (rope (range 100)) 20 30) ;=> #ordered/rope [20 21 22 23 24 25 26 27 28 29]

Extract a subrange [start, end) as a Rope. O(log n).
The result shares structure with the original.

Examples:
  (rope-sub (rope (range 100)) 20 30)
  ;=> #ordered/rope [20 21 22 23 24 25 26 27 28 29]
sourceraw docstring

segment-treeclj

(segment-tree op identity)
(segment-tree op identity coll)

Create a segment tree for O(log n) range aggregate queries.

Arguments: op - associative operation (+, min, max, etc.) identity - identity element (0 for +, Long/MAX_VALUE for min) coll - map or seq of [key value] pairs

Example: (def st (segment-tree + 0 {0 10, 1 20, 2 30, 3 40})) (query st 1 3) ; => 90 (sum of indices 1,2,3)

Create a segment tree for O(log n) range aggregate queries.

Arguments:
  op       - associative operation (+, min, max, etc.)
  identity - identity element (0 for +, Long/MAX_VALUE for min)
  coll     - map or seq of [key value] pairs

Example:
  (def st (segment-tree + 0 {0 10, 1 20, 2 30, 3 40}))
  (query st 1 3)  ; => 90 (sum of indices 1,2,3)
sourceraw docstring

segment-tree-byclj

(segment-tree-by pred op identity coll)

Create a segment tree with a custom ordering predicate.

Create a segment tree with a custom ordering predicate.
sourceraw docstring

segment-tree-withclj

(segment-tree-with comparator op identity)
(segment-tree-with comparator op identity coll)

Create a segment tree with a custom java.util.Comparator.

Create a segment tree with a custom java.util.Comparator.
sourceraw docstring

sliceclj

(slice coll start end)

Return elements from index start (inclusive) to end (exclusive). O(log n + k). Works on any collection implementing PRanked.

Return elements from index start (inclusive) to end (exclusive). O(log n + k).
Works on any collection implementing PRanked.
sourceraw docstring

spanclj

(span coll)

Return [min max] covering all elements, or nil if empty. Works with interval-set, interval-map, and range-map.

Examples: (span (interval-set [[1 5] [3 8] [10 15]])) ; => [1 15] (span (range-map [[[100 200] :a] [[500 600] :b]])) ; => [100 600]

Return [min max] covering all elements, or nil if empty.
Works with interval-set, interval-map, and range-map.

Examples:
  (span (interval-set [[1 5] [3 8] [10 15]]))          ; => [1 15]
  (span (range-map [[[100 200] :a] [[500 600] :b]]))   ; => [100 600]
sourceraw docstring

split-atclj

(split-at i coll)

Split collection at index i, returning [left right].

  • left: collection of the first i elements (indices 0 to i-1)
  • right: collection of remaining elements (indices i to n-1)

Complexity: O(log n)

Compatible with clojure.core/split-at and clojure.data.avl/split-at.

Example: (split-at 2 (ordered-set [1 2 3 4 5])) ;=> [#{1 2} #{3 4 5}]

Split collection at index i, returning [left right].

- left:  collection of the first i elements (indices 0 to i-1)
- right: collection of remaining elements (indices i to n-1)

Complexity: O(log n)

Compatible with clojure.core/split-at and clojure.data.avl/split-at.

Example:
  (split-at 2 (ordered-set [1 2 3 4 5]))
  ;=> [#{1 2} #{3 4 5}]
sourceraw docstring

split-keyclj

(split-key k coll)

Split collection at key k, returning [left entry right].

  • left: collection of elements less than k
  • entry: the element/entry at k, or nil if not present (for sets: the key itself; for maps: [key value])
  • right: collection of elements greater than k

Complexity: O(log n)

Compatible with clojure.data.avl/split-key.

Example: (split-key 3 (ordered-set [1 2 3 4 5])) ;=> [#{1 2} 3 #{4 5}]

(split-key 2 (ordered-map [[1 :a] [2 :b] [3 :c]])) ;=> [{1 :a} [2 :b] {3 :c}]

Split collection at key k, returning [left entry right].

- left:  collection of elements less than k
- entry: the element/entry at k, or nil if not present
         (for sets: the key itself; for maps: [key value])
- right: collection of elements greater than k

Complexity: O(log n)

Compatible with clojure.data.avl/split-key.

Example:
  (split-key 3 (ordered-set [1 2 3 4 5]))
  ;=> [#{1 2} 3 #{4 5}]

  (split-key 2 (ordered-map [[1 :a] [2 :b] [3 :c]]))
  ;=> [{1 :a} [2 :b] {3 :c}]
sourceraw docstring

string-compareclj

Specialized java.util.Comparator for String keys. Uses String.compareTo directly for faster string comparisons.

Specialized java.util.Comparator for String keys.
Uses String.compareTo directly for faster string comparisons.
sourceraw docstring

string-ordered-mapclj

(string-ordered-map)
(string-ordered-map coll)

Create an ordered map optimized for String keys. Uses String.compareTo directly for faster string comparisons.

Create an ordered map optimized for String keys.
Uses String.compareTo directly for faster string comparisons.
sourceraw docstring

string-ordered-setclj

(string-ordered-set)
(string-ordered-set coll)

Create an ordered set optimized for String keys. Uses String.compareTo directly for faster string comparisons.

Create an ordered set optimized for String keys.
Uses String.compareTo directly for faster string comparisons.
sourceraw docstring

subrangeclj

(subrange coll test key)
(subrange coll start-test start-key end-test end-key)

Return a subcollection comprising elements in the given range.

Arguments: (subrange coll test key) - elements satisfying test relative to key (subrange coll start-test start-key end-test end-key)

Tests: :< :<= :> :>=

Complexity: O(log n) to construct the subrange

Example: (subrange (ordered-set (range 10)) :>= 3 :< 7) ;=> #{3 4 5 6}

(subrange (ordered-set (range 10)) :> 5) ;=> #{6 7 8 9}

Return a subcollection comprising elements in the given range.

Arguments:
  (subrange coll test key)           - elements satisfying test relative to key
  (subrange coll start-test start-key end-test end-key)

Tests: :< :<= :> :>=

Complexity: O(log n) to construct the subrange

Example:
  (subrange (ordered-set (range 10)) :>= 3 :< 7)
  ;=> #{3 4 5 6}

  (subrange (ordered-set (range 10)) :> 5)
  ;=> #{6 7 8 9}
sourceraw docstring

subset?clj

(subset? this that)

True if s1 is a subset of s2 (every element of s1 is in s2).

Examples: (subset? (ordered-set [1 2]) (ordered-set [1 2 3])) ; true (subset? (ordered-set [1 4]) (ordered-set [1 2 3])) ; false

True if s1 is a subset of s2 (every element of s1 is in s2).

Examples:
  (subset? (ordered-set [1 2]) (ordered-set [1 2 3]))  ; true
  (subset? (ordered-set [1 4]) (ordered-set [1 2 3]))  ; false
sourceraw docstring

sum-treeclj

(sum-tree coll)

Create a segment tree for range sums.

Create a segment tree for range sums.
sourceraw docstring

superset?clj

(superset? this that)

True if s1 is a superset of s2 (s1 contains every element of s2).

Examples: (superset? (ordered-set [1 2 3]) (ordered-set [1 2])) ; true

True if s1 is a superset of s2 (s1 contains every element of s2).

Examples:
  (superset? (ordered-set [1 2 3]) (ordered-set [1 2]))  ; true
sourceraw docstring

unionclj

(union this that)

Return a set that is the union of the input sets.

For ordered-sets: Uses Adams' divide-and-conquer algorithm with parallel execution for large sets. In the current benchmark suite, roughly 4-20x faster than clojure.set/union on the tested workloads.

Complexity: O(m log(n/m + 1)) where m <= n

Examples: (union (ordered-set [1 2]) (ordered-set [2 3])) ; #{1 2 3} (union s1 s2 s3) ; multiple sets

Return a set that is the union of the input sets.

For ordered-sets: Uses Adams' divide-and-conquer algorithm with parallel
execution for large sets. In the current benchmark suite, roughly 4-20x
faster than clojure.set/union on the tested workloads.

Complexity: O(m log(n/m + 1)) where m <= n

Examples:
  (union (ordered-set [1 2]) (ordered-set [2 3]))  ; #{1 2 3}
  (union s1 s2 s3)                                  ; multiple sets
sourceraw docstring

update-fnclj

(update-fn coll k f)

Update the value at index k by applying f to the current value. O(log n).

Example: (def st (segment-tree + 0 {0 10, 1 20, 2 30})) (def st' (update-fn st 1 #(* % 2))) (query st' 0 2) ; => 80

Update the value at index k by applying f to the current value. O(log n).

Example:
  (def st (segment-tree + 0 {0 10, 1 20, 2 30}))
  (def st' (update-fn st 1 #(* % 2)))
  (query st' 0 2)  ; => 80
sourceraw docstring

update-valclj

(update-val coll k v)

Update the value at index k. O(log n).

Example: (def st (segment-tree + 0 {0 10, 1 20, 2 30})) (def st' (update-val st 1 100)) (query st' 0 2) ; => 140

Update the value at index k. O(log n).

Example:
  (def st (segment-tree + 0 {0 10, 1 20, 2 30}))
  (def st' (update-val st 1 100))
  (query st' 0 2)  ; => 140
sourceraw docstring

cljdoc builds & hosts documentation for Clojure/Script libraries

Keyboard shortcuts
Ctrl+kJump to recent docs
Move to previous article
Move to next article
Ctrl+/Jump to the search field
× close