-
Notifications
You must be signed in to change notification settings - Fork 6
/
README
186 lines (121 loc) · 4.77 KB
/
README
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
SYCAMORE
========
A fast, purely functional data structure library in Common Lisp.
API Documentation: http://ndantam.github.io/sycamore
Features
========
* Fast, purely functional weight-balanced binary trees.
- http://en.wikipedia.org/wiki/Weight-balanced_tree
- Leaf nodes are simple-vectors, greatly reducing tree height.
* Interfaces for tree Sets and Maps (dictionaries).
* Ropes
- http://en.wikipedia.org/wiki/Rope_(data_structure)
* Purely functional pairing heaps
- http://en.wikipedia.org/wiki/Pairing_heap
* Purely functional amortized queue
Installation
============
* Sycamore uses ASDF (https://common-lisp.net/project/asdf/)
* See `INSTALL` file for details
Examples
========
See also `./example.lisp`
Sets
----
Define an ordering function:
CL-USER> (defun compare (a b)
(cond ((< a b) -1)
((> a b) 1)
(t 0)))
COMPARE
Create a set for integers:
CL-USER> (sycamore:tree-set #'compare 1 2 -10 40)
#<TREE-SET (-10 1 2 40)>
Insertion:
CL-USER> (sycamore:tree-set-insert (sycamore:tree-set #'compare 1 2)
0)
#<TREE-SET (0 1 2)>
Removal:
CL-USER> (sycamore:tree-set-remove (sycamore:tree-set #'compare 1 2 0)
0)
#<TREE-SET (1 2)>
Union operation:
CL-USER> (sycamore:tree-set-union (sycamore:tree-set #'compare 1 2)
(sycamore:tree-set #'compare 1 0 3))
#<TREE-SET (0 1 2 3)>
Intersection operation:
CL-USER> (sycamore:tree-set-intersection (sycamore:tree-set #'compare 1 2)
(sycamore:tree-set #'compare 1 0 3))
#<TREE-SET (1)>
Difference operation:
CL-USER> (sycamore:tree-set-difference (sycamore:tree-set #'compare 1 2)
(sycamore:tree-set #'compare 1 0 3))
#<TREE-SET (2)>
Map set:
CL-USER> (sycamore:map-tree-set 'list #'1+
(sycamore:tree-set #'compare 1 0 10 2))
(1 2 3 11)
Fold set:
CL-USER> (sycamore:fold-tree-set (lambda (list item) (cons item list))
nil
(sycamore:tree-set #'compare 1 0 10 2))
(10 2 1 0)
Ropes
-----
Create a Rope:
CL-USER> (sycamore:rope "Hello" #\Space 'World!)
#<ROPE "Hello WORLD!">
Also works on lists:
CL-USER> (sycamore:rope (list "Hello" #\Space 'World!))
#<ROPE "Hello WORLD!">
And arrays:
CL-USER> (sycamore:rope (vector "Hello" #\Space 'World!))
#<ROPE "Hello WORLD!">
Rope to string:
CL-USER> (sycamore:rope-string (sycamore:rope "Hello" #\Space 'World!))
"Hello WORLD!"
Print a rope:
CL-USER> (sycamore:rope-write (sycamore:rope "Hello" #\Space 'World!)
:escape nil :stream *standard-output*)
Hello WORLD!
Alternatives
============
There are many other Common Lisp data structure libraries. Here are a
few alternatives and their trade-offs relative to Sycamore.
FSet
----
https://common-lisp.net/project/fset/Site/FSet-CL.html
FSet implements finite sets with a CLOS-based set interface, while
Sycamore's finite sets take a parameter for a comparison function.
Both used weight-balanced trees with minor algorithmic differences.
Generic vs. explicit comparison functions is both an aesthetic and
performance issue. FSet's generic comparison functions do not need to
be passed explicitly, while Sycamore's explicit comparison function
parameter makes it easier to compare the same type differently if
needed, e.g., lexicographic vs. fast string comparison.
Included benchmarks show that Sycamore is 30-50% faster than FSet.
CL-Containers
-------------
https://github.com/gwkkwg/cl-containers
CL-Containers is stateful/mutable/imperative, while Sycamore is
purely-functional/persistent.
Lisp Interface Library (LIL)
----------------------------
https://github.com/fare/lisp-interface-library
Lisp Interface Library (LIL) provides abstracted data structures using
Interface Passing Style, while Sycamore provides a few concrete data
structures. LIL's Interface Passing Style presumably improves
flexibility at the cost of runtime overhead and API complexity.
Sycamore's explicit data structures have low-overhead, optimized
implementations and a simple, documented API.
References
==========
* Okasaki, Chris. "Purely Functional Data Structures." Cambridge
University Press. June 1999. ISBN: 978-0521663502.
* Boehm, H.J., Atkinson, R. and Plass, M., 1995. Ropes: an alternative
to strings. Software: Practice and Experience, 25(12), pp.1315-1330.
* Adams, Stephen., 1992. Implementing sets efficiently in a functional
language. University of Southampton. Tech Report CSTR 92-10.
Name
====
http://en.wikipedia.org/wiki/Platanus_occidentalis