Skip to content

Commit

Permalink
Added Circular Buffers for Convenience (#26)
Browse files Browse the repository at this point in the history
* circular buffers

* fix

* Update build.gradle
  • Loading branch information
BBScholar authored Jan 31, 2019
1 parent 0219ea6 commit 5b4368b
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 1 deletion.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ dependencies {
}

group = 'org.team5499'
version = '2.2.1'
version = '2.3.0'

task sourcesJar(type: Jar) {
from sourceSets.main.allJava
Expand Down
29 changes: 29 additions & 0 deletions src/main/kotlin/org/team5499/monkeyLib/util/CircularBuffer.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package org.team5499.monkeyLib.util

public open class CircularBuffer<T>(maxSize: Int) {

private val mMaxSize: Int

public val elements: MutableList<T>

init {
mMaxSize = maxSize
if (mMaxSize <= 0) {
throw IllegalArgumentException("maxSize must be a positive integer.")
}
elements = mutableListOf()
}

public fun add(element: T): T? {
elements.add(element)
if (elements.size > mMaxSize) {
val poppedValue = elements.removeAt(0)
return poppedValue
}
return null
}

public fun clear() {
elements.clear()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package org.team5499.monkeyLib.util

public class CircularDoubleBuffer(maxSize: Int) : CircularBuffer<Double>(maxSize) {

public val sum: Double
get() {
var total = 0.0
for (num in super.elements) {
total += num
}
return total
}

public val average: Double
get() {
return sum / super.elements.size.toDouble()
}
}

0 comments on commit 5b4368b

Please sign in to comment.