From b010ad084290d4b66f6a3fff7a10a772801422f7 Mon Sep 17 00:00:00 2001 From: Rohan Sircar Date: Wed, 23 Dec 2020 13:52:28 +0530 Subject: [PATCH] Added mutable history container TODO make an imutable observable based history container based on this one --- .../scala/nova/monadic_sfx/util/History.scala | 31 +++++ src/test/scala/MutHistoryTest.scala | 116 ++++++++++++++++++ 2 files changed, 147 insertions(+) create mode 100644 src/main/scala/nova/monadic_sfx/util/History.scala create mode 100644 src/test/scala/MutHistoryTest.scala diff --git a/src/main/scala/nova/monadic_sfx/util/History.scala b/src/main/scala/nova/monadic_sfx/util/History.scala new file mode 100644 index 0000000..9f78b92 --- /dev/null +++ b/src/main/scala/nova/monadic_sfx/util/History.scala @@ -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 + } + } +} diff --git a/src/test/scala/MutHistoryTest.scala b/src/test/scala/MutHistoryTest.scala new file mode 100644 index 0000000..e60b437 --- /dev/null +++ b/src/test/scala/MutHistoryTest.scala @@ -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) +// } +//