using value notion instead of key for the elements in the circular cache

This commit is contained in:
Adam Brown 2022-09-16 16:33:17 +01:00
parent 4a0cda3268
commit 0385f387d9
3 changed files with 7 additions and 7 deletions

View File

@ -259,8 +259,8 @@ private fun createIntCache(cacheSize: Int): Pair<CircularCache<Int>, Array<Int?>
return CircularCache(cacheSize, factory) to internalData!! return CircularCache(cacheSize, factory) to internalData!!
} }
private fun CircularCache<Int>.putInOrder(vararg keys: Int) { private fun CircularCache<Int>.putInOrder(vararg values: Int) {
keys.forEach { put(it) } values.forEach { put(it) }
} }
``` ```

View File

@ -29,13 +29,13 @@ class CircularCache<T : Any>(cacheSize: Int, factory: (Int) -> Array<T?>) {
private val cache = factory(cacheSize) private val cache = factory(cacheSize)
private var writeIndex = 0 private var writeIndex = 0
fun contains(key: T): Boolean = cache.contains(key) fun contains(value: T): Boolean = cache.contains(value)
fun put(key: T) { fun put(value: T) {
if (writeIndex == cache.size) { if (writeIndex == cache.size) {
writeIndex = 0 writeIndex = 0
} }
cache[writeIndex] = key cache[writeIndex] = value
writeIndex++ writeIndex++
} }
} }

View File

@ -66,7 +66,7 @@ class CircularCacheTest {
return CircularCache(cacheSize, factory) to internalData!! return CircularCache(cacheSize, factory) to internalData!!
} }
private fun CircularCache<Int>.putInOrder(vararg keys: Int) { private fun CircularCache<Int>.putInOrder(vararg values: Int) {
keys.forEach { put(it) } values.forEach { put(it) }
} }
} }