diff --git a/build.gradle b/build.gradle index 379ab27..02999ee 100644 --- a/build.gradle +++ b/build.gradle @@ -35,5 +35,6 @@ repositories { dependencies { testCompile 'junit:junit:4.12' + testCompile 'com.carrotsearch:junit-benchmarks:0.7.2' compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version" } diff --git a/src/main/io/uuddlrlrba/ktalgs/datastructures/LinkedList.kt b/src/main/io/uuddlrlrba/ktalgs/datastructures/LinkedList.kt new file mode 100644 index 0000000..f095028 --- /dev/null +++ b/src/main/io/uuddlrlrba/ktalgs/datastructures/LinkedList.kt @@ -0,0 +1,49 @@ +/* + * Copyright (c) 2017 Kotlin Algorithm Club + * + * 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 io.uuddlrlrba.ktalgs.datastructures + +enum class LinkedType { + END, NODE +} + +class LinkedListKotlin<T>: Cloneable{ + + private data class Node<T>(val item: T? = null, val tail: LinkedListKotlin<T>? = null) + + private var type: LinkedType = LinkedType.END + private var node: Node<T>? = null + + fun push(item: T) { + val tail = this.clone() as? LinkedListKotlin<T> + this.node = Node(item, tail) + type = LinkedType.NODE + } + + fun pop(): T? { + val returnItem = this.node?.copy()?.item + val nextNode = this.node?.tail?.node + this.node = Node(nextNode?.item, nextNode?.tail) + return returnItem + } +} + diff --git a/src/test/io/uuddlrlrba/ktalgs/datastructures/LinkedListTest.kt b/src/test/io/uuddlrlrba/ktalgs/datastructures/LinkedListTest.kt new file mode 100644 index 0000000..11bf6b8 --- /dev/null +++ b/src/test/io/uuddlrlrba/ktalgs/datastructures/LinkedListTest.kt @@ -0,0 +1,70 @@ +/* + * Copyright (c) 2017 Kotlin Algorithm Club + * + * 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 io.uuddlrlrba.ktalgs.datastructures + +import com.carrotsearch.junitbenchmarks.BenchmarkOptions +import com.carrotsearch.junitbenchmarks.BenchmarkRule +import org.junit.Before +import org.junit.Rule +import org.junit.Test + + +class LinkedListTest { + + private lateinit var linkedList: LinkedListKotlin<Int> + + @Before + fun init() { + linkedList = LinkedListKotlin() + } + + @get:Rule + public val benchmarkRule: BenchmarkRule = BenchmarkRule() + + @Test + fun emptyTest() { + val test2 = linkedList.pop() + } + + + @Test + fun pushAndPopTest() { + val linkedList: LinkedListKotlin<Int> = LinkedListKotlin() + linkedList.push(1) + linkedList.push(2) + linkedList.push(3) + val test1 = linkedList.pop() + val test2 = linkedList.pop() + } + + @Test + @BenchmarkOptions(concurrency = 1, warmupRounds = 0, benchmarkRounds = 1) + fun benchmarkSomeWork() { + for (i in 0..1000000) { + linkedList.push(i) + } + for (i in 0..1000000) { + linkedList.pop() + } + } +}