Skip to content

Commit d10658a

Browse files
committed
Update docs
1 parent cf1a819 commit d10658a

1 file changed

Lines changed: 16 additions & 0 deletions

File tree

lib/circular_buffer.ex

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,16 @@ defmodule CircularBuffer do
99
```
1010
"""
1111

12+
@doc """
13+
Creates a new circular buffer with a given size.
14+
"""
1215
def new(size) when is_integer(size) and size > 0 do
1316
%{q: :queue.new(), max_size: size, count: 0}
1417
end
1518

19+
@doc """
20+
Inserts a new item into the next location of the circular buffer
21+
"""
1622
def insert(cb, item) do
1723
if cb.count < cb.max_size do
1824
%{cb | q: :queue.cons(item, cb.q), count: cb.count + 1}
@@ -26,19 +32,29 @@ defmodule CircularBuffer do
2632
end
2733
end
2834

35+
@doc """
36+
Converts a circular buffer to a list. The list is ordered from oldest to newest
37+
elements based on their insertion order.
38+
"""
2939
def to_list(cb) do
3040
cb.q
3141
|> :queue.reverse
3242
|> :queue.to_list
3343
end
3444

45+
@doc """
46+
Returns the newest element in the buffer
47+
"""
3548
def newest(cb) do
3649
case :queue.peek(cb.q) do
3750
{_, val} -> val
3851
:empty -> nil
3952
end
4053
end
4154

55+
@doc """
56+
Returns the oldest element in the buffer
57+
"""
4258
def oldest(cb) do
4359
case :queue.peek_r(cb.q) do
4460
{_, val} -> val

0 commit comments

Comments
 (0)