Skip to content

Commit fc45368

Browse files
authored
Merge branch 'master' into avl_tree
2 parents 0dfe465 + 1195737 commit fc45368

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+1971
-750
lines changed

.travis.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ os:
44
- linux
55
- osx
66
julia:
7-
- 0.7
87
- 1.0
98
- nightly
109
notifications:

Project.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
name = "DataStructures"
22
uuid = "864edb3b-99cc-5e75-8d2d-829cb0a9cfe8"
3-
version = "0.17.17"
3+
version = "0.18.1"
44

55
[deps]
6+
Compat = "34da2185-b29b-5c13-b0c7-acf172513d20"
67
InteractiveUtils = "b77e0a4c-d291-57a0-90e8-8db25a27a240"
78
OrderedCollections = "bac558e1-5e72-5ebc-8fee-abe8a469f55d"
89

910
[compat]
11+
Compat = "3.0.0"
1012
OrderedCollections = "1.1.0"
1113
julia = "1"
1214

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
[![Test Coverage](https://coveralls.io/repos/github/JuliaCollections/DataStructures.jl/badge.svg?branch=master)](https://coveralls.io/github/JuliaCollections/DataStructures.jl?branch=master)
44
[![Test Coverage](https://codecov.io/github/JuliaCollections/DataStructures.jl/coverage.svg?branch=master)](https://codecov.io/github/JuliaCollections/DataStructures.jl?branch=master)
55
[![Documentation](https://img.shields.io/badge/docs-latest-blue.svg)](https://juliacollections.github.io/DataStructures.jl/latest)
6+
[![ColPrac: Contributor's Guide on Collaborative Practices for Community Packages](https://img.shields.io/badge/ColPrac-Contributor's%20Guide-blueviolet)](https://github.com/SciML/ColPrac)
67

78
DataStructures.jl
89
=================
@@ -29,6 +30,7 @@ This package implements a variety of data structures, including
2930
- DataStructures.IntSet
3031
- SparseIntSet
3132
- DiBitVector (in which each element can store two bits)
33+
- Red Black Tree
3234

3335
Resources
3436
---------

appveyor.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
environment:
22
matrix:
3-
- julia_version: 0.7
3+
- julia_version: 1.0
44
- julia_version: latest
55

66
platform:

benchmark/bench_heap.jl

Lines changed: 14 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,12 @@ heaptypes = [BinaryHeap, MutableBinaryHeap]
2626
aexps = [1,3]
2727
datatypes = [Int, Float64]
2828
baseorderings = Dict(
29-
"Min" => DataStructures.LessThan,
30-
#"Max" => DataStructures.GreaterThan,
29+
"Min" => Base.ForwardOrdering,
30+
#"Max" => Base.ReverseOrdering,
3131
)
3232
fastfloatorderings = Dict(
33-
# These will be enabled upon reordering change
34-
#"FastMin" => DataStructures.FasterForward(),
35-
#"FastMax" => DataStructures.FasterReverse(),
33+
"Min" => DataStructures.FasterForward,
34+
"Max" => DataStructures.FasterReverse,
3635
)
3736

3837
for heap in heaptypes
@@ -41,7 +40,8 @@ for heap in heaptypes
4140
Random.seed!(0)
4241
a = rand(dt, 10^aexp)
4342

44-
orderings = baseorderings
43+
# Dict types to force use of abstract type if containing single value
44+
orderings = Dict{String, DataType}(baseorderings)
4545
if dt == Float64
4646
# swap to faster ordering operation
4747
for (k,v) in orderings
@@ -66,38 +66,22 @@ for heap in heaptypes
6666
end
6767
end
6868

69-
# Quick check to ensure no Float regressions with Min/Max convenience functions
70-
# These don't fit in well with the above loop, since ordering is hardcoded.
71-
heapalias = Dict(
72-
"BinaryMinHeap" => BinaryMinHeap,
73-
"BinaryMaxHeap" => BinaryMaxHeap,
74-
"BinaryMinMaxHeap" => BinaryMinMaxHeap, # <- no alias issue
75-
)
76-
for (heapname, heap) in heapalias
77-
for aexp in aexps
78-
for dt in [Float64]
79-
Random.seed!(0)
80-
a = rand(dt, 10^aexp)
81-
prepath = [heapname]
82-
postpath = [string(dt), "10^"*string(aexp)]
83-
suite[vcat(prepath, ["make"], postpath)] =
84-
@benchmarkable $(heap)($a)
85-
suite[vcat(prepath, ["push"], postpath)] =
86-
@benchmarkable push_heap(h, $a) setup=(h=$(heap){$dt}())
87-
suite[vcat(prepath, ["pop"], postpath)] =
88-
@benchmarkable pop_heap(h) setup=(h=$(heap)($a))
89-
end
90-
end
91-
end
69+
fast_extreme_orderings = Dict(
70+
nsmallest => DataStructures.FasterForward(),
71+
nlargest => DataStructures.FasterReverse(),
72+
)
9273

9374
for func in [nlargest, nsmallest]
75+
fastord = fast_extreme_orderings[func]
9476
for aexp in [4]
9577
Random.seed!(0);
9678
a = rand(10^aexp);
9779
for nexp in [2]
9880
n = 10^nexp
99-
suite[[string(func), "a=rand(10^"*string(aexp)*")", "n=10^"*string(nexp)]] =
81+
suite[["Slow " * string(func), "a=rand(10^"*string(aexp)*")", "n=10^"*string(nexp)]] =
10082
@benchmarkable $(func)($n, $a)
83+
suite[[string(func), "a=rand(10^"*string(aexp)*")", "n=10^"*string(nexp)]] =
84+
@benchmarkable DataStructures.nextreme($fastord, $n, $a)
10185
end
10286
end
10387
end

docs/make.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ makedocs(
2525
"intset.md",
2626
"sorted_containers.md",
2727
"dibit_vector.md",
28+
"red_black_tree.md",
2829
],
2930
modules = [DataStructures],
3031
format = Documenter.HTML()

docs/src/heaps.md

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,25 +7,32 @@ All heaps in this package are derived from `AbstractHeap`, and provide
77
the following interface:
88

99
```julia
10-
# Let h be a heap, i be a handle, and v be a value.
10+
# Let `h` be a heap, `v` be a value, and `n` be an integer size
1111

12-
length(h) # returns the number of elements
12+
length(h) # returns the number of elements
1313

14-
isempty(h) # returns whether the heap is empty
14+
isempty(h) # returns whether the heap is empty
1515

16-
push!(h, v) # add a value to the heap
16+
push!(h, v) # add a value to the heap
1717

18-
top(h) # return the top value of a heap
18+
first(h) # return the first (top) value of a heap
1919

20-
pop!(h) # removes the top value, and returns it
20+
pop!(h) # removes the first (top) value, and returns it
2121

22+
extract_all!(h) # removes all elements and returns sorted array
23+
24+
extract_all_rev!(h) # removes all elements and returns reverse sorted array
25+
26+
sizehint!(h, n) # reserve capacity for at least `n` elements
2227
```
2328

2429
Mutable heaps (values can be changed after being pushed to a heap) are
2530
derived from `AbstractMutableHeap <: AbstractHeap`, and additionally
2631
provides the following interface:
2732

2833
```julia
34+
# Let `h` be a heap, `i` be a handle, and `v` be a value.
35+
2936
i = push!(h, v) # adds a value to the heap and and returns a handle to v
3037

3138
update!(h, i, v) # updates the value of an element (referred to by the handle i)
@@ -54,6 +61,21 @@ h = MutableBinaryMinHeap([1,4,3,2])
5461
h = MutableBinaryMaxHeap([1,4,3,2]) # create a mutable min/max heap from a vector
5562
```
5663

64+
Heaps may be constructed with a custom ordering. One use case for custom orderings
65+
is to achieve faster performance with `Float` elements with the risk of random ordering
66+
if any elements are `NaN`. The provided `DataStructures.FasterForward` and
67+
`DataStructures.FasterReverse` orderings are optimized for this purpose.
68+
Custom orderings may also be used for defining the order of structs as heap elements.
69+
```julia
70+
h = BinaryHeap{Float64, DataStructures.FasterForward}() # faster min heap
71+
h = BinaryHeap{Float64, DataStructures.FasterReverse}() # faster max heap
72+
73+
h = MutableBinaryHeap{Float64, DataStructures.FasterForward}() # faster mutable min heap
74+
h = MutableBinaryHeap{Float64, DataStructures.FasterReverse}() # faster mutable max heap
75+
76+
h = BinaryHeap{MyStruct, MyStructOrdering}() # heap containing custom struct
77+
```
78+
5779
## Min-max heaps
5880
Min-max heaps maintain the minimum _and_ the maximum of a set,
5981
allowing both to be retrieved in constant (`O(1)`) time.
@@ -73,7 +95,7 @@ popmax!(h, k) # remove and return the largest k elements
7395
popall!(h) # remove and return all the elements, sorted smallest to largest
7496
popall!(h, o) # remove and return all the elements according to ordering o
7597
```
76-
The usual `top(h)` and `pop!(h)` are defined to be `minimum(h)` and `popmin!(h)`,
98+
The usual `first(h)` and `pop!(h)` are defined to be `minimum(h)` and `popmin!(h)`,
7799
respectively.
78100

79101
This package includes an implementation of a binary min-max heap (`BinaryMinMaxHeap`).
@@ -97,5 +119,9 @@ nlargest(3, [0,21,-12,68,-25,14]) # => [68,21,14]
97119
nsmallest(3, [0,21,-12,68,-25,14]) # => [-25,-12,0]
98120
```
99121

100-
`nlargest(n, a)` is equivalent to `sort(a, lt = >)[1:min(n, end)]`, and
101-
`nsmallest(n, a)` is equivalent to `sort(a, lt = <)[1:min(n, end)]`.
122+
Note that if the array contains floats and is free of NaN values,
123+
then the following alternatives may be used to achieve a 2x performance boost.
124+
```
125+
DataStructures.nextreme(DataStructures.FasterReverse(), n, a) # faster nlargest(n, a)
126+
DataStructures.nextreme(DataStructures.FasterForward(), n, a) # faster nsmallest(n, a)
127+
```

docs/src/index.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,15 @@ This package implements a variety of data structures, including
1414
- Binary Heap
1515
- Mutable Binary Heap
1616
- Ordered Dicts and Sets
17-
- RobinDict (implemented with [Robin Hood Hashing](https://cs.uwaterloo.ca/research/tr/1986/CS-86-14.pdf))
17+
- RobinDict and OrderedRobinDict (implemented with [Robin Hood Hashing](https://cs.uwaterloo.ca/research/tr/1986/CS-86-14.pdf))
1818
- Dictionaries with Defaults
1919
- Trie
2020
- Linked List and Mutable Linked List
2121
- Sorted Dict, Sorted Multi-Dict and Sorted Set
2222
- DataStructures.IntSet
2323
- SparseIntSet
2424
- DiBitVector
25+
- Red Black Tree
2526

2627
## Contents
2728

@@ -46,6 +47,7 @@ Pages = [
4647
"intset.md",
4748
"sorted_containers.md",
4849
"sparse_int_set.md",
49-
"dibit_vector.md"
50+
"dibit_vector.md",
51+
"red_black_tree.md"
5052
]
5153
```

docs/src/red_black_tree.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
```@meta
2+
DocTestSetup = :(using DataStructures)
3+
```
4+
5+
# Red Black Tree
6+
7+
The `RBTree` type is an implementation of Red Black Tree in Julia. It is a self-balancing binary search tree with an extra bit of information, the color, in each of its node. Operations such as search, insert and delete can be done in `O(log n)` complexity, where `n` is the number of nodes in the `RBTree`.
8+
9+
Examples:
10+
11+
```jldoctest
12+
julia> tree = RBTree{Int}();
13+
14+
julia> for k in 1:2:20
15+
push!(tree, k)
16+
end
17+
18+
julia> haskey(tree, 3)
19+
true
20+
21+
julia> tree[4]
22+
7
23+
24+
julia> for k in 1:2:10
25+
delete!(tree, k)
26+
end
27+
28+
julia> haskey(tree, 5)
29+
false
30+
```
31+
32+
```@meta
33+
DocTestSetup = nothing
34+
```

docs/src/robin_dict.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ DocTestSetup = :(using DataStructures)
77
`RobinDict` provides a standard dictionary, conforming to the AbstractDict protocol, which uses the Robin Hood hashing algorithm with backward-shift deletion to provide improved average performance over Dict.
88

99
The interface of `RobinDict` replicates that of `Dict`.
10+
This has an ordered version called `OrderedRobinDict`, which replicates the interface of `OrderedDict`.
1011

1112
Examples:
1213

0 commit comments

Comments
 (0)