add peek to queue interface

- update travis file
- fix typo
This commit is contained in:
Raffaele Mignone 2019-03-11 22:47:26 +01:00
parent f9d26d9738
commit 8df221cd1b
Signed by: norangebit
GPG Key ID: F5255658CB220573
5 changed files with 36 additions and 6 deletions

View File

@ -1,8 +1,18 @@
language: java language: java
jdk: jdk:
- oraclejdk8 - oraclejdk8
- oraclejdk9
- openjdk10
cache:
directories:
- ~/.gradle/caches/
- ~/.gradle/wrapper/
before_install: before_install:
- chmod +x gradlew - chmod +x gradlew
- chmod +x gradle/wrapper/gradle-wrapper.jar - chmod +x gradle/wrapper/gradle-wrapper.jar
script: script:
- ./gradlew test build - ./gradlew test build

View File

@ -30,6 +30,7 @@ import arrow.core.Option
interface Queue<T> { interface Queue<T> {
fun enqueue(elem: T) fun enqueue(elem: T)
fun dequeue(): Option<T> fun dequeue(): Option<T>
fun peek(): Option<T>
fun isEmpty(): Boolean fun isEmpty(): Boolean
fun size(): Int fun size(): Int
fun clean() fun clean()

View File

@ -51,10 +51,11 @@ class ResizingArrayQueue<T>(capacity: Int = DEFAULT_CAPACITY) : Queue<T> {
} }
override fun dequeue(): Option<T> { override fun dequeue(): Option<T> {
if (isEmpty()) val elem = peek()
return None
if (elem is None)
return elem
val elem = queue[head]
queue[head] = null queue[head] = null
head = (head + 1) % queue.size head = (head + 1) % queue.size
size-- size--
@ -62,7 +63,14 @@ class ResizingArrayQueue<T>(capacity: Int = DEFAULT_CAPACITY) : Queue<T> {
if (isOneQuarterFull()) if (isOneQuarterFull())
resizeArray(queue.size / RESIZE_FACTOR) resizeArray(queue.size / RESIZE_FACTOR)
return elem.toOption() return elem
}
override fun peek(): Option<T> {
if (isEmpty())
return None
return queue[head].toOption()
} }
private fun resizeArray(capacity: Int) { private fun resizeArray(capacity: Int) {

View File

@ -26,6 +26,7 @@
package it.norangeb.algorithms.datastructures.queue package it.norangeb.algorithms.datastructures.queue
import arrow.core.None import arrow.core.None
import arrow.core.Some
import arrow.core.getOrElse import arrow.core.getOrElse
import org.amshove.kluent.`should be equal to` import org.amshove.kluent.`should be equal to`
import org.junit.jupiter.api.Test import org.junit.jupiter.api.Test
@ -62,6 +63,16 @@ class ResizingArrayQueueTest {
queue.size() `should be equal to` 0 queue.size() `should be equal to` 0
} }
@Test
fun testPeek() {
val queue = ResizingArrayQueue<Int>()
(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 @Test
fun testIncreaseSize() { fun testIncreaseSize() {
val queue = ResizingArrayQueue<Int>() val queue = ResizingArrayQueue<Int>()

View File

@ -71,7 +71,7 @@ class ResizingArrayStackTest {
} }
@Test @Test
fun testIncrease() { fun testIncreaseSize() {
val stack = ResizingArrayStack<Int>() val stack = ResizingArrayStack<Int>()
stack.push(1) stack.push(1)
@ -82,7 +82,7 @@ class ResizingArrayStackTest {
} }
@Test @Test
fun testDescrese() { fun testDecreaseSize() {
val stack = ResizingArrayStack<Int>() val stack = ResizingArrayStack<Int>()
stack.push(1) stack.push(1)