Skip to content

Commit

Permalink
Added toit doc to the methodes
Browse files Browse the repository at this point in the history
  • Loading branch information
Informaticore committed Apr 5, 2024
1 parent 3b2ef66 commit 678df4a
Showing 1 changed file with 25 additions and 1 deletion.
26 changes: 25 additions & 1 deletion src/ringbuffer.toit
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,25 @@ class RingBuffer:
size_/int
buffer/List

/**
Constructs a new RingBuffer with a given size
*/
constructor size/int:
if size < 1: throw "RingBuffer size must be larger then 0"
size_ = size
buffer = List size 0.0


/**
Appends a given float value to the buffer
*/
append value/float:
buffer[head] = value
head = (head + 1) % size_
count = min (count + 1) size_

/**
Returns true if the buffer is empty, false otherwise
*/
is_empty -> bool:
return count == 0

Expand All @@ -29,16 +38,28 @@ class RingBuffer:
else: result = block.call result buffer[i]
return result

/**
Return the minimum value in the buffer
*/
minimum -> float:
return reduce : | a b | min a b

/**
Return the maximum value in the buffer
*/
maximum -> float:
return reduce : | a b | max a b

/**
Return the average value of all elements in the buffer
*/
average -> float:
sum := reduce : | a b | a + b
return sum / count

/**
Return the standard deviation of all elements in the buffer
*/
std_deviation -> float:
if is_empty: throw "Not enough elements"
if count < 2:
Expand All @@ -47,5 +68,8 @@ class RingBuffer:
variance := reduce : | a b | a += math.pow (b - avg) 2
return math.sqrt variance / size_

/**
Return the last value appended to the buffer
*/
get_last -> float:
return buffer[(head - 1 + size_) % size_]

0 comments on commit 678df4a

Please sign in to comment.