• Stars
    star
    124
  • Rank 278,548 (Top 6 %)
  • Language
    Clojure
  • License
    Eclipse Public Li...
  • Created over 10 years ago
  • Updated 3 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Persistent sorted maps and sets with log-time rank queries

data.avl

Persistent sorted maps and sets with support for the full clojure.core sorted collections API (in particular clojure.core/(r)?(sub)?seq), transients and additional logarithmic time operations: rank queries (via clojure.core/nth and clojure.data.avl/rank-of), "nearest key" lookups, splits by index or key and subsets/submaps.

Persistent AVL trees are used as the underlying data structure.

Synopsis

data.avl supports both Clojure and ClojureScript. It exports a single namespace with nine public functions, four of which are constructor functions which can be used as drop-in replacements for clojure.core / cljs.core functions of the same names, while the remaining five expose data.avl-specific functionality:

(require '[clojure.data.avl :as avl])

;; drop-in replacements for clojure.core counterparts
(doc avl/sorted-map)
(doc avl/sorted-map-by)
(doc avl/sorted-set)
(doc avl/sorted-set-by)

;; find rank of element as primitive long, -1 if not found
(doc avl/rank-of)

;; find element closest to the given key and </<=/>=/> according
;; to coll's comparator
(doc avl/nearest)

;; split the given collection at the given key returning
;; [left entry? right]
(doc avl/split-key)

;; split the given collection at the given index; similar to
;; clojure.core/split-at, but operates on and returns data.avl
;; collections
(doc avl/split-at)

;; return subset/submap of the given collection; accepts arguments
;; reminiscent of clojure.core/{subseq,rsubseq}
(doc avl/subrange)

All data.avl collection-returning public functions return first-class collections (see below for a discussion).

Description

data.avl maps and sets behave like the core Clojure variants, with the following differences:

  1. They have transient counterparts:

     (persistent! (assoc! (transient (avl/sorted-map)) 0 0))
     ;= {0 0}
    

    and use transients during construction:

     (apply avl/sorted-map (interleave (range 32) (range 32)))
     ;; ^- uses transients
    
  2. They are typically noticeably faster during lookups and somewhat slower during non-transient "updates" (assoc, dissoc) than the built-in sorted collections. Note that batch "updates" using transients typically perform better than batch "updates" on the non-transient-enabled built-ins.

  3. They add some memory overhead -- a reference and two ints per key. The additional node fields are used to support transients (one reference field per key), rank queries (one int) and the rebalancing algorithm itself (the final int).

Additionally, data.avl collections support several features that the built-ins do not:

  1. Logarithmic time rank queries via clojure.core/nth and clojure.data.avl/rank-of:

     (nth (avl/sorted-map 0 0 1 1 2 2) 1)
     ;= [1 1]
     (nth (avl/sorted-set 0 1 2) 1)
     ;= 1
     
     (avl/rank-of (avl/sorted-map-by > 0 0 1 1 2 2) 0)
     2
     (avl/rank-of (avl/sorted-set-by > 0 1 2) 0)
     2
    
  2. Logarithmic time lookups of "nearest entries" via clojure.data.avl/nearest:

     (avl/nearest (avl/sorted-set 0 1 2) < 1)
     ;= 0
     (avl/nearest (avl/sorted-set 0 1 2) <= 1) ; or >=
     ;= 1
     (avl/nearest (avl/sorted-set 0 1 2) > 1)
     ;= 2
     (avl/nearest (avl/sorted-set 0 1 2) > 2)
     ;= nil
    
  3. Logarithmic time splitting by key:

     (avl/split-key 3 (avl/sorted-set 0 1 2 3 4 5))
     ;= [#{0 1 2} 3 #{4 5 6}]
     (avl/split-key 1 (avl/sorted-map 0 0 1 1 2 2))
     ;= [{0 0} [1 1] {2 2}]
     (avl/split-key 2 (avl/sorted-set 0 1 3 4))
     ;= [#{0 1} nil #{3 4}]
    

    The middle element of the returned vector is the entry at the given key for maps, stored copy of the key for sets and nil if the key is absent from the collection.

    The remaining two elements are the "left" and "right" subcollections of the original collection argument when split with the given key, comprising, respectively, the keys preceding and succeeding the given key in the order determined by the input collection's comparator.

  4. Logarithmic time splitting by index:

     (avl/split-at 2 (avl/sorted-set 0 1 2 3 4 5))
     ;= [#{0 1} #{2 3 4 5}]
    
  5. Logarithmic time slicing:

     (avl/subrange (avl/sorted-set 0 1 2 3 4 5) > 1)
     ;= #{2 3 4 5}
     (avl/subrange (avl/sorted-set 0 1 2 3 4 5) <= 4)
     ;= #{0 1 2 3 4}
     (avl/subrange (avl/sorted-set 0 1 2 3 4 5) >= 2 < 5)
     ;= #{2 3 4}
    
  6. clojure.data.avl/split-key, clojure.data.avl/split-at and clojure.data.avl/subrange all return first-class data.avl collections, completely independent of the originals. In particular, they do not prevent the originals from being garbage collected and they support insertion of arbitrary keys, including outside original subrange bounds.

Documentation

Releases and dependency information

data.avl requires Clojure >= 1.5.0. The ClojureScript version is regularly tested against the most recent ClojureScript release.

data.avl releases are available from Maven Central. Development snapshots are available from the Sonatype OSS repository.

Follow the first link above to discover the current release number.

CLI/deps.edn dependency information:

org.clojure/data.avl {:mvn/version "${version}"}

Leiningen dependency information:

[org.clojure/data.avl "${version}"]

Maven dependency information:

<dependency>
  <groupId>org.clojure</groupId>
  <artifactId>data.avl</artifactId>
  <version>${version}</version>
</dependency>

Gradle dependency information:

compile "org.clojure:data.avl:${version}"

Developer information

data.avl is being developed as a Clojure Contrib project, see the What is Clojure Contrib page for details. Patches will only be accepted from developers who have signed the Clojure Contributor Agreement.

Clojure(Script) code reuse

data.avl sorted maps and sets support the same basic functionality regular Clojure's sorted maps and sets do (with the additions listed above). Some of the code supporting various Clojure(Script) interfaces and protocols is adapted from the ClojureScript implementations of the red-black-tree-based sorted collections, which themselves are ports of Clojure's implementations written in Java. The Clojure(Script) source files containing the relevant code carry the following copyright notice:

Copyright (c) Rich Hickey. All rights reserved.
The use and distribution terms for this software are covered by the
Eclipse Public License 1.0 (https://opensource.org/licenses/eclipse-1.0.php)
which can be found in the file epl-v10.html at the root of this distribution.
By using this software in any fashion, you are agreeing to be bound by
  the terms of this license.
You must not remove this notice, or any other, from this software.

Licence

Copyright © 2013-2023 Michał Marczyk, Rich Hickey and contributors

Distributed under the Eclipse Public License, the same as Clojure.

More Repositories

1

clojure

The Clojure programming language
Java
10,259
star
2

clojurescript

Clojure to JS compiler
Clojure
9,164
star
3

core.async

Facilities for async programming and communication in Clojure
Clojure
1,933
star
4

clojure-clr

A port of Clojure to the CLR, part of the Clojure project
C#
1,531
star
5

core.logic

A logic programming library for Clojure & ClojureScript
Clojure
1,430
star
6

core.typed

An optional type system for Clojure
Clojure
1,280
star
7

core.match

An optimized pattern matching library for Clojure
Clojure
1,170
star
8

test.check

QuickCheck for Clojure
Clojure
1,109
star
9

java.jdbc

JDBC from Clojure (formerly clojure.contrib.sql)
Clojure
713
star
10

tools.cli

Command-line processing
Clojure
701
star
11

tools.nrepl

A Clojure network REPL that provides a server and client, along with some common APIs of use to IDEs and other tools that may need to evaluate Clojure code in remote environments.
Clojure
661
star
12

tools.namespace

Tools for managing namespaces in Clojure
Clojure
593
star
13

data.json

JSON in Clojure
Clojure
531
star
14

algo.monads

Macros for defining monads, and definition of the most common monads
Clojure
442
star
15

tools.deps.alpha

A functional API for transitive dependency graph expansion and the creation of classpaths
Clojure
435
star
16

core.cache

A caching library for Clojure implementing various cache strategies
Clojure
433
star
17

tools.logging

Clojure logging API
Clojure
380
star
18

tools.trace

1.3 update of clojure.contrib.trace
Clojure
355
star
19

math.combinatorics

Efficient, functional algorithms for generating lazy sequences for common combinatorial functions
Clojure
343
star
20

spec-alpha2

Clojure library to describe the structure of data and functions
Clojure
296
star
21

data.csv

CSV reader/writer to/from Clojure data structures
Clojure
266
star
22

core.memoize

A manipulable, pluggable, memoization framework for Clojure
Clojure
260
star
23

tools.analyzer

An analyzer for Clojure code, written in Clojure and producing AST in EDN
Clojure
255
star
24

clojure-site

clojure.org site
HTML
244
star
25

data.xml

Clojure
219
star
26

data.finger-tree

Finger Tree data structure
Clojure
213
star
27

spec.alpha

Clojure library to describe the structure of data and functions
Clojure
211
star
28

tools.reader

Clojure reader in Clojure
Clojure
203
star
29

tools.build

Clojure builds as Clojure programs
Clojure
193
star
30

core.rrb-vector

RRB-Trees in Clojure
Clojure
190
star
31

data.priority-map

Clojure priority map data structure
Clojure
186
star
32

math.numeric-tower

Math functions that deal intelligently with the various types in Clojure's numeric tower
Clojure
176
star
33

test.generative

Generative test runner
Clojure
161
star
34

core.unify

Unification library
Clojure
136
star
35

core.contracts

Contracts programming
Clojure
128
star
36

data.fressian

Read and write Fressian data from Clojure
Clojure
126
star
37

data.int-map

A map optimized for integer keys
Java
124
star
38

core.incubator

Proving ground for proposed new core fns
Clojure
116
star
39

tools.macro

Utilities for macro writers
Clojure
114
star
40

java.data

Functions for recursively converting Java beans to Clojure and vice versa
Clojure
113
star
41

tools.analyzer.jvm

Additional jvm-specific passes for tools.analyzer
Clojure
112
star
42

clojurescript-site

website for ClojureScript
Shell
106
star
43

tools.deps.graph

Dependency graphs for deps.edn projects
Clojure
102
star
44

java.jmx

Produce and consume JMX beans from Clojure
Clojure
93
star
45

algo.generic

Generic versions of commonly used functions, implemented as multimethods that can be implemented for any data type
Clojure
92
star
46

tools.emitter.jvm

A JVM bytecode generator for ASTs compatible with tools.analyzer(.jvm)
Clojure
87
star
47

data.generators

Random data generators
Clojure
85
star
48

data.zip

Utilities for clojure.zip
Clojure
82
star
49

brew-install

Clojure CLI installer
Shell
78
star
50

data.codec

Native codec implementations
Clojure
74
star
51

tools.gitlibs

API for retrieving, caching, and programatically accessing git libraries
Clojure
59
star
52

java.classpath

Examine the Java classpath from Clojure programs
Clojure
58
star
53

jvm.tools.analyzer

Clojure
53
star
54

core.specs.alpha

specs to describe Clojure core macros and functions
Clojure
46
star
55

tools.tools

Clojure CLI tool for managing Clojure CLI tools
Clojure
41
star
56

homebrew-tools

Clojure homebrew tap providing Clojure formulae
Ruby
40
star
57

test.benchmark

Benchmark and Regression Suite for Clojure
Roff
37
star
58

data.alpha.replicant-server

A Clojure library providing remote implementations of the Clojure data structures and a remote REPL server.
Clojure
35
star
59

clr.tools.nrepl

Clojure
26
star
60

build.ci

Support scripts for continuous integration
Clojure
23
star
61

tools.analyzer.js

Provides js-specific passes for tools.analyzer
Clojure
21
star
62

clojure-install

Java
16
star
63

algo.graph

Basic graph theory algorithms
Clojure
15
star
64

data.alpha.replicant-client

A Clojure library providing client-side implementations of Clojure datastructures served by replicant-server.
Clojure
11
star
65

clojure.github.com

Documentation repos
HTML
8
star
66

build.poms

Parent POMs
8
star
67

core.typed.analyzer.jvm

Clojure
7
star
68

clr.tools.namespace

Clojure
7
star
69

core.typed.runtime.jvm

Clojure
7
star
70

clr.data.json

JSON in Clojure on the CLR
Clojure
6
star
71

clr.tools.reader

Clojure
5
star
72

clr.test.generative

Clojure
5
star
73

clojure-api-doc

Clojure API doc build
Clojure
5
star
74

contrib-api-doc

Clojure contrib API doc build
Clojure
5
star
75

core.typed.annotator.jvm

Clojure
5
star
76

core.typed.checker.jvm

Clojure
4
star
77

core.typed.checker.js

Clojure
4
star
78

io.incubator

Proving ground for proposed new io fns
4
star
79

clr.data.generators

Random data generators for Clojure on the CLR
Clojure
4
star
80

clr.core.async

Port of Clojure core.async to the CLR
Clojure
3
star
81

clr.spec.alpha

spec on the CLR
Clojure
3
star
82

clr.tools.analyzer

Clojure
3
star
83

test.regression

Regression tests for Clojure
Clojure
3
star
84

tools.deps.cli

Deps functions
Clojure
2
star
85

clr.core.specs.alpha

core specs on CLR
HTML
2
star
86

clr.tools.gitlibs

An API for retrieving, caching, and programatically accessing git libraries
HTML
2
star
87

java.internal.invoke

2
star
88

clr.core.logic

Clojure
2
star
89

clr.tools.trace

1
star
90

clr.data.priority-map

ClojureCLR port of data.priority-map
Clojure
1
star
91

cljs.tools.closure

ClojureScript build of Google Closure
Shell
1
star
92

tools.analyzer.clr

additional clr-specific passes for tools.analyzer
Clojure
1
star
93

clr.test.check

Clojure
1
star
94

clr.core.cache

ClojureCLR port of core.cache
Clojure
1
star
95

clr.tools.logging

1
star
96

build.test

Dummy project for testing contrib build and deploy
Clojure
1
star
97

clr.core.memoize

ClojureCLR port of core.memoize
Clojure
1
star