Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

empty!() for CircularDeque, add it to docs, add some other missing things to docs #317

Merged
merged 5 commits into from
Aug 28, 2017
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
24 changes: 24 additions & 0 deletions doc/source/circ_buffer.rst
Original file line number Diff line number Diff line change
@@ -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
3 changes: 3 additions & 0 deletions doc/source/circ_deque.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,16 @@ 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
unshift!(a, 20) # add an element to the front
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.

Expand Down
5 changes: 4 additions & 1 deletion doc/source/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ 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
* Priority Queue
* Accumulators and Counters
* Disjoint Sets
* Binary Heap
Expand All @@ -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
Expand All @@ -43,4 +47,3 @@ Indices and tables
* :ref:`genindex`
* :ref:`modindex`
* :ref:`search`

7 changes: 6 additions & 1 deletion src/circular_buffer.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you also need to reset cb.first = 1. Is there a reason you didn't just call empty!(cb.buffer)?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, could you add a quick test for this?

Copy link
Contributor Author

@KadeG KadeG Aug 27, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changes make. Test added. Your suggestion is much faster. Thanks for the help!


function _buffer_index(cb::CircularBuffer, i::Int)
n = length(cb)
if i < 1 || i > n
Expand Down Expand Up @@ -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])
Expand Down