Skip to content

Commit f57330a

Browse files
authored
Merge pull request #770 from jonas-schulze/vector-trie-docs
Update docs
2 parents 62105c0 + 20d807a commit f57330a

File tree

9 files changed

+138
-72
lines changed

9 files changed

+138
-72
lines changed

docs/src/priority-queue.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,22 +33,22 @@ julia> # Julia code
3333
3434
julia> # Insert keys with associated priorities
3535
pq["a"] = 10; pq["b"] = 5; pq["c"] = 15; pq
36-
PriorityQueue{Any,Any,Base.Order.ForwardOrdering} with 3 entries:
36+
PriorityQueue{Any, Any, Base.Order.ForwardOrdering} with 3 entries:
3737
"b" => 5
3838
"a" => 10
3939
"c" => 15
4040
4141
julia> # Change the priority of an existing key
4242
pq["a"] = 0; pq
43-
PriorityQueue{Any,Any,Base.Order.ForwardOrdering} with 3 entries:
43+
PriorityQueue{Any, Any, Base.Order.ForwardOrdering} with 3 entries:
4444
"a" => 0
4545
"b" => 5
4646
"c" => 15
4747
```
4848
It is also possible to iterate over the priorities and elements of the queue in sorted order.
4949
```jldoctest
5050
julia> pq = PriorityQueue("a"=>2, "b"=>1, "c"=>3)
51-
PriorityQueue{String,Int64,Base.Order.ForwardOrdering} with 3 entries:
51+
PriorityQueue{String, Int64, Base.Order.ForwardOrdering} with 3 entries:
5252
"b" => 1
5353
"a" => 2
5454
"c" => 3

docs/src/robin_dict.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,14 @@ Examples:
1313

1414
```jldoctest
1515
julia> d = RobinDict{Int, Char}(1 => 'a', 2 => 'b')
16-
RobinDict{Int64,Char} with 2 entries:
16+
RobinDict{Int64, Char} with 2 entries:
1717
2 => 'b'
1818
1 => 'a'
1919
2020
julia> d[3] = 'c';
2121
2222
julia> collect(d)
23-
3-element Array{Pair{Int64,Char},1}:
23+
3-element Vector{Pair{Int64, Char}}:
2424
2 => 'b'
2525
3 => 'c'
2626
1 => 'a'
@@ -31,7 +31,7 @@ julia> d[1]
3131
'a': ASCII/Unicode U+0061 (category Ll: Letter, lowercase)
3232
3333
julia> d
34-
RobinDict{Int64,Char} with 2 entries:
34+
RobinDict{Int64, Char} with 2 entries:
3535
3 => 'c'
3636
1 => 'a'
3737

docs/src/swiss_dict.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,14 @@ Examples:
1212

1313
```jldoctest
1414
julia> d = SwissDict(1 => 'a', 2 => 'b')
15-
SwissDict{Int64,Char} with 2 entries:
15+
SwissDict{Int64, Char} with 2 entries:
1616
1 => 'a'
1717
2 => 'b'
1818
1919
julia> d[3] = 'c';
2020
2121
julia> collect(d)
22-
3-element Array{Pair{Int64,Char},1}:
22+
3-element Vector{Pair{Int64, Char}}:
2323
1 => 'a'
2424
2 => 'b'
2525
3 => 'c'
@@ -30,7 +30,7 @@ julia> d[1]
3030
'a': ASCII/Unicode U+0061 (category Ll: Letter, lowercase)
3131
3232
julia> d
33-
SwissDict{Int64,Char} with 2 entries:
33+
SwissDict{Int64, Char} with 2 entries:
3434
1 => 'a'
3535
3 => 'c'
3636

docs/src/trie.md

Lines changed: 62 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,85 @@
1+
```@meta
2+
DocTestSetup = :(using DataStructures)
3+
```
4+
15
# Trie
26

37
An implementation of the Trie data structure. This is an associative
4-
structure, with AbstractString keys:
8+
structure, with iterable keys:
59

6-
```julia
7-
t = Trie{Int}()
8-
t["Rob"] = 42
9-
t["Roger"] = 24
10-
haskey(t, "Rob") # true
11-
get(t, "Rob", nothing) # 42
12-
keys(t) # "Rob", "Roger"
13-
keys(subtrie(t, "Ro")) # "b", "ger"
10+
```jldoctest
11+
julia> t = Trie{Char,Int}();
12+
13+
14+
julia> t["Rob"] = 42;
15+
16+
17+
julia> t["Roger"] = 24;
18+
19+
20+
julia> haskey(t, "Rob")
21+
true
22+
23+
julia> get(t, "Rob", nothing)
24+
42
25+
26+
julia> keys(t)
27+
2-element Vector{String}:
28+
"Roger"
29+
"Rob"
30+
31+
julia> keys(subtrie(t, "Ro"))
32+
2-element Vector{String}:
33+
"ger"
34+
"b"
35+
```
36+
37+
Note that the keys don't need to be `String`s:
38+
39+
```jldoctest
40+
julia> t = Trie{Int,Char}();
41+
42+
julia> t[1:3] = 'a';
43+
44+
julia> t[[2,3,5]] = 'b';
45+
46+
julia> keys(t)
47+
2-element Vector{Vector{Int64}}:
48+
[2, 3, 5]
49+
[1, 2, 3]
1450
```
1551

1652
Constructors:
1753

1854
```julia
1955
Trie(keys, values) # construct a Trie with the given keys and values
20-
Trie(keys) # construct a Trie{Void} with the given keys and with values = nothing
56+
Trie(keys) # construct a Trie{K,Nothing} with the given keys and with values = nothing
2157
Trie(kvs::AbstractVector{(K, V)}) # construct a Trie from the given vector of (key, value) pairs
2258
Trie(kvs::AbstractDict{K, V}) # construct a Trie from the given associative structure
2359
```
2460

25-
This package also provides an iterator `partial_path(t::Trie, str)` for looping
26-
over all the nodes encountered in searching for the given string `str`.
61+
This package also provides an iterator `partial_path(t::Trie, prefix)` for looping
62+
over all the nodes encountered in searching for the given `prefix`.
2763
This obviates much of the boilerplate code needed in writing many trie
2864
algorithms. For example, to test whether a trie contains any prefix of a
29-
given string, use:
65+
given string `str`, use:
3066

3167
```julia
3268
seen_prefix(t::Trie, str) = any(v -> v.is_key, partial_path(t, str))
3369
```
3470

3571
`find_prefixes` can be used to find all keys which are prefixes of the given string.
3672

37-
```julia
38-
t = Trie(["A", "ABC", "ABCD", "BCE"])
39-
find_prefixes(t, "ABCDE") # "A", "ABC", "ABCD"
40-
```
73+
```jldoctest
74+
julia> t = Trie(["A", "ABC", "ABCD", "BCE"]);
75+
76+
julia> find_prefixes(t, "ABCDE")
77+
3-element Vector{String}:
78+
"A"
79+
"ABC"
80+
"ABCD"
81+
```
82+
83+
```@meta
84+
DocTestSetup = nothing
85+
```

src/heaps/arrays_as_heaps.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ Return `true` if an array is heap-ordered according to the given order.
122122
123123
```jldoctest
124124
julia> a = [1,2,3]
125-
3-element Array{Int64,1}:
125+
3-element Vector{Int64}:
126126
1
127127
2
128128
3

src/ordered_robin_dict.jl

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ are taken from 2-tuples `(key,value)` generated by the argument.
1212
# Examples
1313
```jldoctest
1414
julia> OrderedRobinDict([("A", 1), ("B", 2)])
15-
OrderedRobinDict{String,Int64} with 2 entries:
15+
OrderedRobinDict{String, Int64} with 2 entries:
1616
"A" => 1
1717
"B" => 2
1818
```
@@ -21,7 +21,7 @@ Alternatively, a sequence of pair arguments may be passed.
2121
2222
```jldoctest
2323
julia> OrderedRobinDict("A"=>1, "B"=>2)
24-
OrderedRobinDict{String,Int64} with 2 entries:
24+
OrderedRobinDict{String, Int64} with 2 entries:
2525
"A" => 1
2626
"B" => 2
2727
```
@@ -94,14 +94,15 @@ Remove all elements from a `collection`.
9494
# Examples
9595
```jldoctest
9696
julia> A = OrderedRobinDict("a" => 1, "b" => 2)
97-
OrderedRobinDict{String,Int64} with 2 entries:
97+
OrderedRobinDict{String, Int64} with 2 entries:
9898
"a" => 1
9999
"b" => 2
100100
101101
julia> empty!(A);
102102
103+
103104
julia> A
104-
OrderedRobinDict{String,Int64} with 0 entries
105+
OrderedRobinDict{String, Int64}()
105106
```
106107
"""
107108
function Base.empty!(h::OrderedRobinDict{K,V}) where {K, V}
@@ -191,14 +192,15 @@ Return the value stored for the given key, or if no mapping for the key is prese
191192
```jldoctest
192193
julia> d = OrderedRobinDict("a"=>1, "b"=>2, "c"=>3);
193194
195+
194196
julia> get!(d, "a", 5)
195197
1
196198
197199
julia> get!(d, "d", 4)
198200
4
199201
200202
julia> d
201-
OrderedRobinDict{String,Int64} with 4 entries:
203+
OrderedRobinDict{String, Int64} with 4 entries:
202204
"a" => 1
203205
"b" => 2
204206
"c" => 3
@@ -251,6 +253,7 @@ key is present.
251253
```jldoctest
252254
julia> d = OrderedRobinDict("a"=>1, "b"=>2);
253255
256+
254257
julia> get(d, "a", 3)
255258
1
256259
@@ -291,7 +294,7 @@ Determine whether a collection has a mapping for a given `key`.
291294
# Examples
292295
```jldoctest
293296
julia> D = OrderedRobinDict('a'=>2, 'b'=>3)
294-
OrderedRobinDict{Char,Int64} with 2 entries:
297+
OrderedRobinDict{Char, Int64} with 2 entries:
295298
'a' => 2
296299
'b' => 3
297300
@@ -313,7 +316,7 @@ Return the key matching argument `key` if one exists in `collection`, otherwise
313316
# Examples
314317
```jldoctest
315318
julia> D = OrderedRobinDict('a'=>2, 'b'=>3)
316-
OrderedRobinDict{Char,Int64} with 2 entries:
319+
OrderedRobinDict{Char, Int64} with 2 entries:
317320
'a' => 2
318321
'b' => 3
319322
@@ -364,13 +367,17 @@ Delete and return the mapping for `key` if it exists in `collection`, otherwise
364367
```jldoctest
365368
julia> d = OrderedRobinDict("a"=>1, "b"=>2, "c"=>3);
366369
370+
367371
julia> pop!(d, "a")
368372
1
369373
370374
julia> pop!(d, "d")
371375
ERROR: KeyError: key "d" not found
372376
Stacktrace:
373-
[...]
377+
[1] pop!(h::OrderedRobinDict{String, Int64}, key::String)
378+
@ DataStructures ~/.julia/dev/DataStructures/src/ordered_robin_dict.jl:357
379+
[2] top-level scope
380+
@ none:1
374381
375382
julia> pop!(d, "e", 4)
376383
4
@@ -389,12 +396,12 @@ Delete the mapping for the given key in a collection, and return the collection.
389396
# Examples
390397
```jldoctest
391398
julia> d = OrderedRobinDict("a"=>1, "b"=>2)
392-
OrderedRobinDict{String,Int64} with 2 entries:
399+
OrderedRobinDict{String, Int64} with 2 entries:
393400
"a" => 1
394401
"b" => 2
395402
396403
julia> delete!(d, "b")
397-
OrderedRobinDict{String,Int64} with 1 entry:
404+
OrderedRobinDict{String, Int64} with 1 entry:
398405
"a" => 1
399406
```
400407
"""

src/priorityqueue.jl

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ lowest priority element.
1717
1818
```jldoctest
1919
julia> PriorityQueue(Base.Order.Forward, "a" => 2, "b" => 3, "c" => 1)
20-
PriorityQueue{String,Int64,Base.Order.ForwardOrdering} with 3 entries:
20+
PriorityQueue{String, Int64, Base.Order.ForwardOrdering} with 3 entries:
2121
"c" => 1
2222
"a" => 2
2323
"b" => 3
@@ -214,13 +214,13 @@ Insert the a key `k` into a priority queue `pq` with priority `v`.
214214
215215
```jldoctest
216216
julia> a = PriorityQueue(PriorityQueue("a"=>1, "b"=>2, "c"=>3))
217-
PriorityQueue{String,Int64,Base.Order.ForwardOrdering} with 3 entries:
217+
PriorityQueue{String, Int64, Base.Order.ForwardOrdering} with 3 entries:
218218
"a" => 1
219219
"b" => 2
220220
"c" => 3
221221
222222
julia> enqueue!(a, "d"=>4)
223-
PriorityQueue{String,Int64,Base.Order.ForwardOrdering} with 4 entries:
223+
PriorityQueue{String, Int64, Base.Order.ForwardOrdering} with 4 entries:
224224
"a" => 1
225225
"b" => 2
226226
"c" => 3
@@ -255,7 +255,7 @@ Remove and return the lowest priority key from a priority queue.
255255
256256
```jldoctest
257257
julia> a = PriorityQueue(Base.Order.Forward, ["a" => 2, "b" => 3, "c" => 1])
258-
PriorityQueue{String,Int64,Base.Order.ForwardOrdering} with 3 entries:
258+
PriorityQueue{String, Int64, Base.Order.ForwardOrdering} with 3 entries:
259259
"c" => 1
260260
"a" => 2
261261
"b" => 3
@@ -264,7 +264,7 @@ julia> dequeue!(a)
264264
"c"
265265
266266
julia> a
267-
PriorityQueue{String,Int64,Base.Order.ForwardOrdering} with 2 entries:
267+
PriorityQueue{String, Int64, Base.Order.ForwardOrdering} with 2 entries:
268268
"a" => 2
269269
"b" => 3
270270
```
@@ -280,7 +280,7 @@ Remove and return a the lowest priority key and value from a priority queue as a
280280
281281
```jldoctest
282282
julia> a = PriorityQueue(Base.Order.Forward, "a" => 2, "b" => 3, "c" => 1)
283-
PriorityQueue{String,Int64,Base.Order.ForwardOrdering} with 3 entries:
283+
PriorityQueue{String, Int64, Base.Order.ForwardOrdering} with 3 entries:
284284
"c" => 1
285285
"a" => 2
286286
"b" => 3
@@ -289,7 +289,7 @@ julia> dequeue_pair!(a)
289289
"c" => 1
290290
291291
julia> a
292-
PriorityQueue{String,Int64,Base.Order.ForwardOrdering} with 2 entries:
292+
PriorityQueue{String, Int64, Base.Order.ForwardOrdering} with 2 entries:
293293
"a" => 2
294294
"b" => 3
295295
```
@@ -319,12 +319,12 @@ Delete the mapping for the given key in a priority queue, and return the priorit
319319
# Examples
320320
```jldoctest
321321
julia> q = PriorityQueue(Base.Order.Forward, "a"=>2, "b"=>3, "c"=>1)
322-
PriorityQueue{String,Int64,Base.Order.ForwardOrdering} with 3 entries:
322+
PriorityQueue{String, Int64, Base.Order.ForwardOrdering} with 3 entries:
323323
"c" => 1
324324
"a" => 2
325325
"b" => 3
326326
julia> delete!(q, "b")
327-
PriorityQueue{String,Int64,Base.Order.ForwardOrdering} with 2 entries:
327+
PriorityQueue{String, Int64, Base.Order.ForwardOrdering} with 2 entries:
328328
"c" => 1
329329
"a" => 2
330330
```

0 commit comments

Comments
 (0)