From 8df221cd1bfb51292066740698a00a7b782ac32d Mon Sep 17 00:00:00 2001 From: norangebit Date: Mon, 11 Mar 2019 22:47:26 +0100 Subject: [PATCH] add peek to queue interface - update travis file - fix typo --- .travis.yml | 10 ++++++++++ .../algorithms/datastructures/queue/Queue.kt | 1 + .../datastructures/queue/ResizingArrayQueue.kt | 16 ++++++++++++---- .../queue/ResizingArrayQueueTest.kt | 11 +++++++++++ .../stack/ResizingArrayStackTest.kt | 4 ++-- 5 files changed, 36 insertions(+), 6 deletions(-) diff --git a/.travis.yml b/.travis.yml index 1b48652..92b9cbe 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,8 +1,18 @@ language: java + jdk: - oraclejdk8 + - oraclejdk9 + - openjdk10 + +cache: + directories: + - ~/.gradle/caches/ + - ~/.gradle/wrapper/ + before_install: - chmod +x gradlew - chmod +x gradle/wrapper/gradle-wrapper.jar + script: - ./gradlew test build diff --git a/src/main/kotlin/it/norangeb/algorithms/datastructures/queue/Queue.kt b/src/main/kotlin/it/norangeb/algorithms/datastructures/queue/Queue.kt index 095ca0c..1c20de0 100644 --- a/src/main/kotlin/it/norangeb/algorithms/datastructures/queue/Queue.kt +++ b/src/main/kotlin/it/norangeb/algorithms/datastructures/queue/Queue.kt @@ -30,6 +30,7 @@ import arrow.core.Option interface Queue { fun enqueue(elem: T) fun dequeue(): Option + fun peek(): Option fun isEmpty(): Boolean fun size(): Int fun clean() diff --git a/src/main/kotlin/it/norangeb/algorithms/datastructures/queue/ResizingArrayQueue.kt b/src/main/kotlin/it/norangeb/algorithms/datastructures/queue/ResizingArrayQueue.kt index 2cdc28a..6c24d29 100644 --- a/src/main/kotlin/it/norangeb/algorithms/datastructures/queue/ResizingArrayQueue.kt +++ b/src/main/kotlin/it/norangeb/algorithms/datastructures/queue/ResizingArrayQueue.kt @@ -51,10 +51,11 @@ class ResizingArrayQueue(capacity: Int = DEFAULT_CAPACITY) : Queue { } override fun dequeue(): Option { - if (isEmpty()) - return None + val elem = peek() + + if (elem is None) + return elem - val elem = queue[head] queue[head] = null head = (head + 1) % queue.size size-- @@ -62,7 +63,14 @@ class ResizingArrayQueue(capacity: Int = DEFAULT_CAPACITY) : Queue { if (isOneQuarterFull()) resizeArray(queue.size / RESIZE_FACTOR) - return elem.toOption() + return elem + } + + override fun peek(): Option { + if (isEmpty()) + return None + + return queue[head].toOption() } private fun resizeArray(capacity: Int) { diff --git a/src/test/kotlin/it/norangeb/algorithms/datastructures/queue/ResizingArrayQueueTest.kt b/src/test/kotlin/it/norangeb/algorithms/datastructures/queue/ResizingArrayQueueTest.kt index 1a2688a..be0bfdc 100644 --- a/src/test/kotlin/it/norangeb/algorithms/datastructures/queue/ResizingArrayQueueTest.kt +++ b/src/test/kotlin/it/norangeb/algorithms/datastructures/queue/ResizingArrayQueueTest.kt @@ -26,6 +26,7 @@ package it.norangeb.algorithms.datastructures.queue import arrow.core.None +import arrow.core.Some import arrow.core.getOrElse import org.amshove.kluent.`should be equal to` import org.junit.jupiter.api.Test @@ -62,6 +63,16 @@ class ResizingArrayQueueTest { queue.size() `should be equal to` 0 } + @Test + fun testPeek() { + val queue = ResizingArrayQueue() + + (queue.peek() is None) `should be equal to` true + queue.enqueue(1) + (queue.peek() is Some) `should be equal to` true + queue.size() `should be equal to` 1 + } + @Test fun testIncreaseSize() { val queue = ResizingArrayQueue() diff --git a/src/test/kotlin/it/norangeb/algorithms/datastructures/stack/ResizingArrayStackTest.kt b/src/test/kotlin/it/norangeb/algorithms/datastructures/stack/ResizingArrayStackTest.kt index b4142e1..9ea7669 100644 --- a/src/test/kotlin/it/norangeb/algorithms/datastructures/stack/ResizingArrayStackTest.kt +++ b/src/test/kotlin/it/norangeb/algorithms/datastructures/stack/ResizingArrayStackTest.kt @@ -71,7 +71,7 @@ class ResizingArrayStackTest { } @Test - fun testIncrease() { + fun testIncreaseSize() { val stack = ResizingArrayStack() stack.push(1) @@ -82,7 +82,7 @@ class ResizingArrayStackTest { } @Test - fun testDescrese() { + fun testDecreaseSize() { val stack = ResizingArrayStack() stack.push(1)