cbuf v0.3.0 Cbuf
Link to this section Summary
Functions
Returns the count of the non-empty values in the buffer
Insert a value into a circular buffer. Values are inserted such that when the buffer is full, the oldest items are overwritten first
Queries buf
for the presence of val
Create a new circular buffer of a given size
See the oldest value in the buffer. Works in constant time
Calculate the allocated size for the buffer.
This is maximum addressable size of the buffer, not how many values it currently contains. For the number of values in the current buffer, see count/1
Convert a circular buffer to a list. The list is ordered by age, oldest to newest. This operation takes linear time
Link to this section Functions
Returns the count of the non-empty values in the buffer.
iex> Cbuf.new(5) |> Cbuf.insert("hi") |> Cbuf.count()
1
iex> Cbuf.new(5) |> Cbuf.count()
0
iex> Cbuf.new(5) |> Cbuf.insert(nil) |> Cbuf.count()
1
Insert a value into a circular buffer. Values are inserted such that when the buffer is full, the oldest items are overwritten first.
iex> buf = Cbuf.new(5)
iex> buf |> Cbuf.insert("a") |> Cbuf.insert("b")
#Cbuf<["a", "b"]>
iex> buf = Cbuf.new(3)
iex> Enum.reduce(1..20, buf, fn(val, acc) -> Cbuf.insert(acc, val) end)
#Cbuf<[18, 19, 20]>
iex> buf = Cbuf.new(1)
iex> Enum.reduce(1..20, buf, fn(val, acc) -> Cbuf.insert(acc, val) end)
#Cbuf<[20]>
Queries buf
for the presence of val
.
iex> Cbuf.new(5) |> Cbuf.insert("hello") |> Cbuf.member?("hello")
true
iex> Cbuf.new(5) |> Cbuf.insert("hello") |> Cbuf.member?("nope")
false
Create a new circular buffer of a given size.
iex> Cbuf.new(5)
#Cbuf<[]>
See the oldest value in the buffer. Works in constant time.
iex> buf = Enum.reduce(1..20, Cbuf.new(3), fn(val, acc) -> Cbuf.insert(acc, val) end)
iex> Cbuf.peek(buf)
18
iex> buf = Cbuf.new(20) |> Cbuf.insert("ok") |> Cbuf.insert("fine")
iex> Cbuf.peek(buf)
"ok"
iex> Cbuf.new(3) |> Cbuf.peek()
nil
Calculate the allocated size for the buffer.
This is maximum addressable size of the buffer, not how many values it currently contains. For the number of values in the current buffer, see count/1
iex> Cbuf.new(5) |> Cbuf.size()
5
Convert a circular buffer to a list. The list is ordered by age, oldest to newest. This operation takes linear time.
iex> buf = Cbuf.new(5)
iex> buf |> Cbuf.insert("a") |> Cbuf.insert("b") |> Cbuf.to_list()
["a", "b"]
iex> buf = Cbuf.new(3)
iex> Enum.reduce(1..20, buf, fn(val, acc) -> Cbuf.insert(acc, val) end) |> Cbuf.to_list()
[18, 19, 20]
iex> Cbuf.new(5) |> Cbuf.to_list()
[]