Added mutable history container

TODO make an imutable observable based history container based on this
one
This commit is contained in:
Rohan Sircar 2020-12-23 13:52:28 +05:30
parent b0b914424b
commit b010ad0842
2 changed files with 147 additions and 0 deletions

View File

@ -0,0 +1,31 @@
package nova.monadic_sfx.util
class MutHistory[T](initValue: T) {
private var _values = Vector(initValue)
def values = _values
private var _sp = 0
def sp = _sp
// def current = if (ints.isEmpty) None else Some(ints(sp))
def current = _values(_sp)
def push(v: T) = {
if (_sp < _values.length - 1) {
_values = _values.splitAt(_sp)._1
_values = _values :+ v
} else {
_values = _values :+ v
_sp = _values.length - 1
}
}
def forward() = {
if (!_values.isEmpty && _sp < _values.length - 1) _sp += 1
}
def backward() = {
if (_sp > 0) {
_sp -= 1
}
}
}

View File

@ -0,0 +1,116 @@
import org.scalatest.funsuite.AnyFunSuite
import nova.monadic_sfx.util.MutHistory
import com.typesafe.scalalogging.LazyLogging
class MutHistoryTest extends AnyFunSuite with LazyLogging {
val h = new MutHistory(0)
test("init") {
assert(h.values == Vector(0))
assert(h.sp == 0)
assert(h.current == 0)
}
test("push 1") {
h.push(1)
// logger.debug(mutHistory.ints.toString)
assert(h.values == Vector(0, 1))
assert(h.values.length - 1 == h.sp)
assert(h.current == 1)
}
test("push 2") {
h.push(2)
// logger.debug(mutHistory.ints.toString)
assert(h.values == Vector(0, 1, 2))
assert(h.values.length - 1 == h.sp)
assert(h.current == 2)
}
test("first forward") {
// logger.debug(mutHistory.ints.toString)
// logger.debug(mutHistory.sp.toString)
h.forward()
assert(h.values == Vector(0, 1, 2))
assert(h.sp == 2)
assert(h.current == 2)
}
test("second forward") {
// logger.debug(mutHistory.ints.toString)
// logger.debug(mutHistory.sp.toString)
h.forward()
assert(h.values == Vector(0, 1, 2))
assert(h.sp == 2)
assert(h.current == 2)
}
test("first backward") {
h.backward()
assert(h.values == Vector(0, 1, 2))
assert(h.sp == 1)
assert(h.current == 1)
}
test("second backward") {
h.backward()
assert(h.values == Vector(0, 1, 2))
assert(h.sp == 0)
assert(h.current == 0)
}
test("third backward") {
h.backward()
assert(h.values == Vector(0, 1, 2))
assert(h.sp == 0)
assert(h.current == 0)
}
test("push 3") {
h.push(3)
assert(h.sp == 0)
assert(h.values == Vector(3))
assert(h.current == 3)
}
test("fourth backward") {
h.backward()
assert(h.values == Vector(3))
assert(h.sp == 0)
assert(h.current == 3)
}
test("lastly") {
h.push(4)
h.push(5)
h.push(6)
h.push(7)
h.push(8)
assert(h.values == Vector(3, 4, 5, 6, 7, 8))
assert(h.sp == 5)
assert(h.current == 8)
h.backward()
h.backward()
assert(h.current == 6)
assert(h.sp == 3)
h.push(9)
assert(h.values == Vector(3, 4, 5, 9))
assert(h.sp == 3)
assert(h.current == 9)
for (i <- 1 to 4) h.backward()
// assert(h.current == None)
// assert(h.ints == Vector.empty)
assert(h.sp == 0)
assert(h.current == 3)
h.push(1)
assert(h.current == 1)
assert(h.sp == 0)
}
}
// test("main") {
// assert(mutHistory.ints == Vector.empty)
// assert(mutHistory.sp == -1)
// mutHistory.push(1)
// assert(mutHistory.ints == Vector(1))
// assert(mutHistory.sp == 0)
// mutHistory.push(2)
// assert(mutHistory.ints == Vector(1, 2))
// assert(mutHistory.sp == 1)
// // mutHistory.forward()
// // assert(mutHistory.sp == 1)
// // mutHistory.forward()
// // assert(mutHistory.sp == 1)
// }
//