Queue & Stack

Add Queue and Stack, implemented with a resizing array
This commit is contained in:
Raffaele Mignone 2019-03-10 17:47:40 +01:00
parent 0106fd9866
commit f9d26d9738
Signed by: norangebit
GPG Key ID: F5255658CB220573
8 changed files with 757 additions and 0 deletions

View File

@ -0,0 +1,38 @@
/*
* MIT License
*
* Copyright (c) 2019 norangebit
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
*/
package it.norangeb.algorithms.datastructures.queue
import arrow.core.Option
interface Queue<T> {
fun enqueue(elem: T)
fun dequeue(): Option<T>
fun isEmpty(): Boolean
fun size(): Int
fun clean()
fun <A> map(transform: (T) -> A): Queue<A>
fun forEach(action: (T) -> Unit)
}

View File

@ -0,0 +1,133 @@
/*
* MIT License
*
* Copyright (c) 2019 norangebit
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
*/
package it.norangeb.algorithms.datastructures.queue
import arrow.core.None
import arrow.core.Option
import arrow.core.toOption
class ResizingArrayQueue<T>(capacity: Int = DEFAULT_CAPACITY) : Queue<T> {
private var queue: Array<T?> = arrayOfNulls<Any>(capacity) as Array<T?>
private var size = 0
private var tail = 0
private var head = 0
override fun isEmpty(): Boolean = size == 0
private fun isFull(): Boolean = queue.size == size
override fun size(): Int = size
override fun enqueue(elem: T) {
if (isFull())
resizeArray(size * RESIZE_FACTOR)
queue[tail] = elem
tail = (tail + 1) % queue.size
size++
}
override fun dequeue(): Option<T> {
if (isEmpty())
return None
val elem = queue[head]
queue[head] = null
head = (head + 1) % queue.size
size--
if (isOneQuarterFull())
resizeArray(queue.size / RESIZE_FACTOR)
return elem.toOption()
}
private fun resizeArray(capacity: Int) {
val copy: Array<T?> = arrayOfNulls<Any>(capacity) as Array<T?>
when {
head >= tail -> {
for (i in head until queue.size)
copy[i - head] = queue[i]
val alreadyInsert = queue.size - head
for (i in 0 until tail)
copy[i + alreadyInsert] = queue[i]
}
else ->
for (i in head until tail)
copy[i - head] = queue[i]
}
queue = copy
head = 0
tail = size
}
private fun isOneQuarterFull(): Boolean = size == queue.size / 4 &&
size > 0
override fun forEach(action: (T) -> Unit) {
for (elem in queue)
if (elem != null)
action(elem)
}
override fun <A> map(transform: (T) -> A): Queue<A> {
val transformedQueue: Array<A?> = arrayOfNulls<Any>(queue.size) as Array<A?>
for (i in 0 until queue.size) {
val elem = queue[i]
if (elem == null)
transformedQueue[i] = null
else
transformedQueue[i] = transform(elem)
}
return ResizingArrayQueue(transformedQueue, head, tail, size)
}
override fun clean() {
queue = arrayOfNulls<Any>(DEFAULT_CAPACITY) as Array<T?>
size = 0
tail = 0
head = 0
}
private constructor(queue: Array<T?>, head: Int, tail: Int, size: Int) : this() {
this.queue = queue
this.head = head
this.tail = tail
this.size = size
}
companion object {
const val RESIZE_FACTOR = 2
const val DEFAULT_CAPACITY = 2
}
}

View File

@ -0,0 +1,111 @@
/*
* MIT License
*
* Copyright (c) 2019 norangebit
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
*/
package it.norangeb.algorithms.datastructures.stack
import arrow.core.None
import arrow.core.Option
import arrow.core.toOption
class ResizingArrayStack<T>(capacity: Int = DEFAULT_CAPACITY) : Stack<T> {
private var stack: Array<T?> = arrayOfNulls<Any>(capacity) as Array<T?>
private var size = 0
override fun isEmpty(): Boolean = size == 0
private fun isFull(): Boolean = size == stack.size
override fun size(): Int = size
override fun peek(): Option<T> = if (size == 0)
None
else
stack[size - 1].toOption()
override fun pop(): Option<T> {
val elem = peek()
if (elem is None)
return elem
stack[--size] = null
if (isOneQuarterFull())
resizeArray(stack.size / RESIZE_FACTOR)
return elem
}
override fun push(elem: T) {
if (isFull())
resizeArray(size * RESIZE_FACTOR)
stack[size++] = elem
}
private fun resizeArray(capacity: Int) {
stack = stack.copyOf(capacity)
}
private fun isOneQuarterFull(): Boolean {
return size > 0 &&
size == stack.size / 4
}
override fun forEach(action: (T) -> Unit) {
for (elem in stack)
if (elem != null)
action(elem)
}
override fun <A> map(transform: (T) -> A): Stack<A> {
val transformedStack: Array<A?> = arrayOfNulls<Any>(stack.size) as Array<A?>
for (i in 0 until stack.size) {
val elem = stack[i]
if (elem == null)
transformedStack[i] = null
else
transformedStack[i] = transform(elem)
}
return ResizingArrayStack(transformedStack, size)
}
override fun clean() {
stack = arrayOfNulls<Any>(DEFAULT_CAPACITY) as Array<T?>
size = 0
}
private constructor(stack: Array<T?>, size: Int) : this() {
this.stack = stack
this.size = size
}
companion object {
const val RESIZE_FACTOR = 2
const val DEFAULT_CAPACITY = 2
}
}

View File

@ -0,0 +1,39 @@
/*
* MIT License
*
* Copyright (c) 2019 norangebit
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
*/
package it.norangeb.algorithms.datastructures.stack
import arrow.core.Option
interface Stack<T> {
fun push(elem: T)
fun pop(): Option<T>
fun peek(): Option<T>
fun size(): Int
fun isEmpty(): Boolean
fun clean()
fun <A> map(transform: (T) -> A): Stack<A>
fun forEach(action: (T) -> Unit)
}

View File

@ -0,0 +1,75 @@
/*
* MIT License
*
* Copyright (c) 2019 norangebit
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
*/
package it.norangeb.algorithms.datastructures.queue
import org.koin.dsl.module.module
import org.koin.standalone.KoinComponent
import org.koin.standalone.StandAloneContext.startKoin
import org.koin.standalone.inject
import java.io.PrintStream
import kotlin.random.Random
import kotlin.system.measureNanoTime
fun main(args: Array<String>) {
startKoin(listOf(queueModule))
val outEnqueueData = PrintStream(args[0])
val outEnDeQueueData = PrintStream(args[1])
TimeMeasure().run(outEnqueueData, outEnDeQueueData)
}
val queueModule = module {
single { ResizingArrayQueue<Int>() as Queue<Int> }
}
class TimeMeasure : KoinComponent {
private val queue: Queue<Int> by inject()
fun run(outEnqueueData: PrintStream, outEnDeQueueData: PrintStream) {
(0..100000).map {
measureNanoTime {
queue.enqueue(1)
}
}.forEach {
outEnqueueData.println(it)
}
val random = Random(System.currentTimeMillis())
queue.clean()
(0..100000).map {
measureNanoTime {
if (random.nextBoolean())
queue.enqueue(1)
else
queue.dequeue()
}
}.forEach {
outEnDeQueueData.println(it)
}
}
}

View File

@ -0,0 +1,139 @@
/*
* MIT License
*
* Copyright (c) 2019 norangebit
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
*/
package it.norangeb.algorithms.datastructures.queue
import arrow.core.None
import arrow.core.getOrElse
import org.amshove.kluent.`should be equal to`
import org.junit.jupiter.api.Test
class ResizingArrayQueueTest {
@Test
fun testEnqueue() {
val queue = ResizingArrayQueue<Int>()
queue.size() `should be equal to` 0
queue.enqueue(1)
queue.size() `should be equal to` 1
queue.enqueue(1)
queue.size() `should be equal to` 2
}
@Test
fun testIsEmpty() {
val queue = ResizingArrayQueue<Int>()
queue.isEmpty() `should be equal to` true
queue.enqueue(1)
queue.isEmpty() `should be equal to` false
}
@Test
fun testDequeue() {
val queue = ResizingArrayQueue<Int>()
queue.size() `should be equal to` 0
(queue.dequeue() is None) `should be equal to` true
queue.enqueue(1)
queue.dequeue().getOrElse { 0 } `should be equal to` 1
queue.size() `should be equal to` 0
}
@Test
fun testIncreaseSize() {
val queue = ResizingArrayQueue<Int>()
queue.enqueue(1)
queue.enqueue(2)
queue.enqueue(3)
queue.enqueue(4)
queue.enqueue(5)
}
@Test
fun testDecreaseSize() {
val queue = ResizingArrayQueue<Int>()
queue.enqueue(1)
queue.enqueue(2)
queue.enqueue(3)
queue.enqueue(4)
queue.enqueue(5)
queue.dequeue()
queue.dequeue()
queue.dequeue()
queue.dequeue()
queue.dequeue()
}
@Test
fun testMap() {
val queue = ResizingArrayQueue<Int>()
queue.enqueue(1)
queue.enqueue(2)
queue.enqueue(3)
queue.enqueue(4)
queue.enqueue(5)
val newQueue = queue.map { it * 2 } as ResizingArrayQueue
newQueue.size() `should be equal to` 5
newQueue.dequeue().getOrElse { 0 } `should be equal to` 2
newQueue.dequeue().getOrElse { 0 } `should be equal to` 4
newQueue.dequeue().getOrElse { 0 } `should be equal to` 6
newQueue.dequeue().getOrElse { 0 } `should be equal to` 8
newQueue.dequeue().getOrElse { 0 } `should be equal to` 10
}
@Test
fun testForEach() {
val queue = ResizingArrayQueue<Int>()
queue.enqueue(1)
queue.enqueue(2)
queue.enqueue(3)
queue.enqueue(4)
queue.enqueue(5)
var x = 0
queue.forEach { x++ }
x `should be equal to` 5
}
@Test
fun testClean() {
val queue = ResizingArrayQueue<Int>()
queue.enqueue(1)
queue.enqueue(2)
queue.clean()
queue.size() `should be equal to` 0
}
}

View File

@ -0,0 +1,75 @@
/*
* MIT License
*
* Copyright (c) 2019 norangebit
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
*/
package it.norangeb.algorithms.datastructures.stack
import org.koin.dsl.module.module
import org.koin.standalone.KoinComponent
import org.koin.standalone.inject
import org.koin.standalone.StandAloneContext.startKoin
import java.io.PrintStream
import kotlin.random.Random
import kotlin.system.measureNanoTime
fun main(args: Array<String>) {
startKoin(listOf(stackModule))
val outPushData = PrintStream(args[0])
val outPushPopData = PrintStream(args[1])
MeasureTime().run(outPushData, outPushPopData)
}
val stackModule = module {
single { ResizingArrayStack<Int>() as Stack<Int> }
}
class MeasureTime : KoinComponent {
private val stack: Stack<Int> by inject()
fun run(outPushData: PrintStream, outPushPopData: PrintStream) {
(0..100000).map {
measureNanoTime {
stack.push(1)
}
}.forEach {
outPushData.println(it)
}
val random = Random(System.currentTimeMillis())
stack.clean()
(0..100000).map {
measureNanoTime {
if (random.nextBoolean())
stack.push(1)
else
stack.pop()
}
}.forEach {
outPushPopData.println(it)
}
}
}

View File

@ -0,0 +1,147 @@
/*
* MIT License
*
* Copyright (c) 2019 norangebit
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
*/
package it.norangeb.algorithms.datastructures.stack
import arrow.core.None
import arrow.core.getOrElse
import org.amshove.kluent.`should be equal to`
import org.junit.jupiter.api.Test
class ResizingArrayStackTest {
@Test
fun testPush() {
val stack = ResizingArrayStack<Int>()
stack.size() `should be equal to` 0
stack.push(1)
stack.size() `should be equal to` 1
}
@Test
fun testIsEmpty() {
val stack = ResizingArrayStack<Int>()
stack.isEmpty() `should be equal to` true
stack.push(1)
stack.isEmpty() `should be equal to` false
}
@Test
fun testPeek() {
val stack = ResizingArrayStack<Int>()
(stack.peek() is None) `should be equal to` true
stack.push(1)
(stack.peek().getOrElse { 0 }) `should be equal to` 1
stack.size() `should be equal to` 1
}
@Test
fun testPop() {
val stack = ResizingArrayStack<Int>()
(stack.pop() is None) `should be equal to` true
stack.push(1)
(stack.pop().getOrElse { 0 }) `should be equal to` 1
stack.size() `should be equal to` 0
}
@Test
fun testIncrease() {
val stack = ResizingArrayStack<Int>()
stack.push(1)
stack.push(2)
stack.push(3)
stack.push(4)
stack.push(5)
}
@Test
fun testDescrese() {
val stack = ResizingArrayStack<Int>()
stack.push(1)
stack.push(2)
stack.push(3)
stack.push(4)
stack.push(5)
stack.pop()
stack.pop()
stack.pop()
stack.pop()
stack.pop()
}
@Test
fun testMap() {
val stack = ResizingArrayStack<Int>()
stack.push(1)
stack.push(2)
stack.push(3)
stack.push(4)
stack.push(5)
val newStack = stack.map { it * 2 } as ResizingArrayStack
newStack.size() `should be equal to` 5
newStack.pop().getOrElse { 0 } `should be equal to` 10
newStack.pop().getOrElse { 0 } `should be equal to` 8
newStack.pop().getOrElse { 0 } `should be equal to` 6
newStack.pop().getOrElse { 0 } `should be equal to` 4
newStack.pop().getOrElse { 0 } `should be equal to` 2
}
@Test
fun testForEach() {
val stack = ResizingArrayStack<Int>()
stack.push(1)
stack.push(2)
stack.push(3)
stack.push(4)
stack.push(5)
var x = 0
stack.forEach { x++ }
x `should be equal to` 5
}
@Test
fun testClean() {
val stack = ResizingArrayStack<Int>()
stack.push(1)
stack.push(2)
stack.clean()
stack.size() `should be equal to` 0
}
}