From 564d3c9f4d8aed1443e1c35d42ad96da461a5398 Mon Sep 17 00:00:00 2001 From: Kade Gibson Date: Fri, 18 Aug 2017 12:27:27 -0500 Subject: [PATCH 1/5] Add empty!() for CircularBuffer. Add circ_buffer.rst to docs. Add circ_buffer.rst and missing priority-queue.rst to index.rst --- README.rst | 2 ++ doc/source/circ_buffer.rst | 24 ++++++++++++++++++++++++ doc/source/index.rst | 5 ++++- src/circular_buffer.jl | 7 ++++++- 4 files changed, 36 insertions(+), 2 deletions(-) create mode 100644 doc/source/circ_buffer.rst diff --git a/README.rst b/README.rst index b654dd7c7..dd461ffcd 100644 --- a/README.rst +++ b/README.rst @@ -28,6 +28,8 @@ DataStructures.jl This package implements a variety of data structures, including * Deque (based on block-list) +* CircularBuffer +* CircularDeque * Stack * Queue * Accumulators and Counters diff --git a/doc/source/circ_buffer.rst b/doc/source/circ_buffer.rst new file mode 100644 index 000000000..10f1313fc --- /dev/null +++ b/doc/source/circ_buffer.rst @@ -0,0 +1,24 @@ +.. _ref-circular_buffer: + +-------------- +CircularBuffer +-------------- + +The ``CircularBuffer`` type implements a circular buffer of fixed ``capacity`` where new items are pushed to the back of the list, overwriting values in a circular fashion. + +Usage:: + + a = CircularBuffer{Int}(n) # allocate an Int buffer with maximum capacity n + isfull(a) # test whether the buffer is full + isempty(a) # test whether the buffer is empty + empty!(a) # reset the buffer + capacity(a) # return capacity + length(a) # get the number of elements currently in the buffer + size(a) # same as length(a) + push!(a, 10) # add an element to the back and overwrite front if full + unshift!(a, 10) # add an element to the front and overwrite back if full + append!(a, [1, 2, 3, 4]) # push at most last `capacity` items + convert(Vector{Float64}, a) # convert items to type Float64 + eltype(a) # return type of items + a[1] # get the element at the front + a[end] # get the element at the back diff --git a/doc/source/index.rst b/doc/source/index.rst index ce293d873..508ddf77c 100644 --- a/doc/source/index.rst +++ b/doc/source/index.rst @@ -4,6 +4,7 @@ DataStructures.jl This package implements a variety of data structures, including * Deque (based on block-list) +* CircularBuffer * CircularDeque (based on a circular buffer) * Stack * Queue @@ -17,6 +18,7 @@ This package implements a variety of data structures, including * Linked List * Sorted Dict, Sorted Multi-Dict and Sorted Set * DataStructures.IntSet +* Priority Queue Contents: @@ -24,8 +26,10 @@ Contents: :maxdepth: 2 deque.rst + circ_buffer.rst circ_deque.rst stack_and_queue.rst + priority-queue.rst accumulators.rst disjoint_sets.rst heaps.rst @@ -43,4 +47,3 @@ Indices and tables * :ref:`genindex` * :ref:`modindex` * :ref:`search` - diff --git a/src/circular_buffer.jl b/src/circular_buffer.jl index 011aaed53..321d88ddc 100644 --- a/src/circular_buffer.jl +++ b/src/circular_buffer.jl @@ -9,6 +9,11 @@ type CircularBuffer{T} <: AbstractVector{T} (::Type{CircularBuffer{T}}){T}(capacity::Int) = new{T}(capacity, 1, T[]) end +function Base.empty!(cb::CircularBuffer) + cb.buffer = typeof(cb.buffer)[] + cb +end + function _buffer_index(cb::CircularBuffer, i::Int) n = length(cb) if i < 1 || i > n @@ -54,7 +59,7 @@ function Base.unshift!(cb::CircularBuffer, data) end function Base.append!(cb::CircularBuffer, datavec::AbstractVector) - # push at most `capacity` items + # push at most last `capacity` items n = length(datavec) for i in max(1, n-capacity(cb)+1):n push!(cb, datavec[i]) From 5460ab996ef10adb21563d0c9fd24e169d12d8d7 Mon Sep 17 00:00:00 2001 From: Kade Gibson Date: Fri, 18 Aug 2017 12:28:22 -0500 Subject: [PATCH 2/5] Add missing CircularDeque functions to docs. --- doc/source/circ_deque.rst | 3 +++ 1 file changed, 3 insertions(+) diff --git a/doc/source/circ_deque.rst b/doc/source/circ_deque.rst index 0ab31d2b0..d94f63115 100644 --- a/doc/source/circ_deque.rst +++ b/doc/source/circ_deque.rst @@ -10,6 +10,8 @@ Usage:: a = CircularDeque{Int}(n) # allocate a deque with maximum capacity n isempty(a) # test whether the deque is empty + empty!(a) # reset the deque + capacity(a) # return capacity length(a) # get the number of elements currently in the deque push!(a, 10) # add an element to the back pop!(a) # remove an element from the back @@ -17,6 +19,7 @@ Usage:: shift!(a) # remove an element from the front front(a) # get the element at the front back(a) # get the element at the back + eltype(a) # return type of items *Note:* Julia's ``Vector`` type also provides this interface, and thus can be used as a deque. However, the ``CircularDeque`` type in this package is implemented as a circular buffer, and thus avoids copying elements when modifications are made to the front of the vector. From 142ecdc15fcb7c225703d0a1b749e66656efffd8 Mon Sep 17 00:00:00 2001 From: Kade Gibson Date: Fri, 18 Aug 2017 12:32:23 -0500 Subject: [PATCH 3/5] Move * Priority Queue to under * Queue --- doc/source/index.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/source/index.rst b/doc/source/index.rst index 508ddf77c..c4f0db3b3 100644 --- a/doc/source/index.rst +++ b/doc/source/index.rst @@ -8,6 +8,7 @@ This package implements a variety of data structures, including * CircularDeque (based on a circular buffer) * Stack * Queue +* Priority Queue * Accumulators and Counters * Disjoint Sets * Binary Heap @@ -18,7 +19,6 @@ This package implements a variety of data structures, including * Linked List * Sorted Dict, Sorted Multi-Dict and Sorted Set * DataStructures.IntSet -* Priority Queue Contents: From ffbbb801101181a1771593b596906cacb4e8f07b Mon Sep 17 00:00:00 2001 From: KadeG Date: Sun, 27 Aug 2017 17:47:58 -0500 Subject: [PATCH 4/5] empty!(cb) = cb.first = 1; empty!(cb.buffer) --- src/circular_buffer.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/circular_buffer.jl b/src/circular_buffer.jl index 321d88ddc..c8c3e56cf 100644 --- a/src/circular_buffer.jl +++ b/src/circular_buffer.jl @@ -10,8 +10,8 @@ type CircularBuffer{T} <: AbstractVector{T} end function Base.empty!(cb::CircularBuffer) - cb.buffer = typeof(cb.buffer)[] - cb + cb.first = 1 + empty!(cb.buffer) end function _buffer_index(cb::CircularBuffer, i::Int) From 625bcae0ff54a0af08b57991197ca996bbb5065e Mon Sep 17 00:00:00 2001 From: KadeG Date: Sun, 27 Aug 2017 17:56:09 -0500 Subject: [PATCH 5/5] Add empty!(cb) test --- test/test_circular_buffer.jl | 3 +++ 1 file changed, 3 insertions(+) diff --git a/test/test_circular_buffer.jl b/test/test_circular_buffer.jl index 97f031edb..94a9d36c3 100644 --- a/test/test_circular_buffer.jl +++ b/test/test_circular_buffer.jl @@ -43,4 +43,7 @@ using Base.Test for (idx, n) in enumerate(5:1) @test arr[idx] == n end + + # test empty!(cb) + @test length(empty!(cb)) == 0 end