-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added Circular Buffers for Convenience (#26)
* circular buffers * fix * Update build.gradle
- Loading branch information
Showing
3 changed files
with
48 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
src/main/kotlin/org/team5499/monkeyLib/util/CircularBuffer.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
src/main/kotlin/org/team5499/monkeyLib/util/CircularDoubleBuffer.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} | ||
} |