From ac729504bf53c231dc8b1cf4d96ea4a0e36a9ed5 Mon Sep 17 00:00:00 2001 From: Alex Tkachman Date: Wed, 1 Feb 2012 14:55:36 +0200 Subject: [PATCH] functional list, queues and package for concurrent --- stdlib/ktSrc/concurrent/FunctionalList.kt | 57 ++++++++++++++++++++++ stdlib/ktSrc/concurrent/FunctionalQueue.kt | 30 ++++++++++++ stdlib/ktSrc/{ => concurrent}/Locks.kt | 0 stdlib/ktSrc/{ => concurrent}/Thread.kt | 0 stdlib/ktSrc/{ => concurrent}/Timer.kt | 0 5 files changed, 87 insertions(+) create mode 100644 stdlib/ktSrc/concurrent/FunctionalList.kt create mode 100644 stdlib/ktSrc/concurrent/FunctionalQueue.kt rename stdlib/ktSrc/{ => concurrent}/Locks.kt (100%) rename stdlib/ktSrc/{ => concurrent}/Thread.kt (100%) rename stdlib/ktSrc/{ => concurrent}/Timer.kt (100%) diff --git a/stdlib/ktSrc/concurrent/FunctionalList.kt b/stdlib/ktSrc/concurrent/FunctionalList.kt new file mode 100644 index 00000000000..b285fedabc2 --- /dev/null +++ b/stdlib/ktSrc/concurrent/FunctionalList.kt @@ -0,0 +1,57 @@ +package std.concurrent + +abstract class FunctionalList(val size: Int) { + abstract val head: T + abstract val tail: FunctionalList + + val empty : Boolean + get() = size == 0 + + fun add(element: T) : FunctionalList = FunctionalList.Standard(element, this) + + fun reversed() : FunctionalList { + if(empty) + return this + + var cur = tail + var new = of(head) + + while(!cur.empty) { + new = new.add(cur.head) + cur = cur.tail + } + return new + } + + fun iterator() = object: Iterator { + var cur = this@FunctionalList + + override fun next(): T { + if(cur.empty) + throw java.util.NoSuchElementException() + + val head = cur.head + cur = cur.tail + return head + } + + override val hasNext: Boolean + get() = !cur.empty + } + + class object { + class Empty() : FunctionalList(0) { + override val head: T + get() = throw java.util.NoSuchElementException() + override val tail: FunctionalList + get() = throw java.util.NoSuchElementException() + } + + class Standard(override val head: T, override val tail: FunctionalList) : FunctionalList(tail.size+1) + + fun emptyList() = Empty() + + fun of(element: T) : FunctionalList = FunctionalList.Standard(element,emptyList()) + } +} + diff --git a/stdlib/ktSrc/concurrent/FunctionalQueue.kt b/stdlib/ktSrc/concurrent/FunctionalQueue.kt new file mode 100644 index 00000000000..36b5da65db0 --- /dev/null +++ b/stdlib/ktSrc/concurrent/FunctionalQueue.kt @@ -0,0 +1,30 @@ +package std.concurrent + +import java.util.concurrent.Executor +import jet.Iterator + +class FunctionalQueue( + val input: FunctionalList = FunctionalList.emptyList(), + val output: FunctionalList = FunctionalList.emptyList()) { + + val size : Int + get() = input.size + output.size + + val empty : Boolean + get() = size == 0 + + fun add(element: T) = FunctionalQueue(input add element, output) + + fun addFirst(element: T) = FunctionalQueue(input, output add element) + + fun removeFirst() : #(T,FunctionalQueue) = + if(output.empty) { + if(input.empty) + throw java.util.NoSuchElementException() + else + FunctionalQueue(FunctionalList.emptyList(), input.reversed()).removeFirst() + } + else { + #(output.head, FunctionalQueue(input, output.tail)) + } +} diff --git a/stdlib/ktSrc/Locks.kt b/stdlib/ktSrc/concurrent/Locks.kt similarity index 100% rename from stdlib/ktSrc/Locks.kt rename to stdlib/ktSrc/concurrent/Locks.kt diff --git a/stdlib/ktSrc/Thread.kt b/stdlib/ktSrc/concurrent/Thread.kt similarity index 100% rename from stdlib/ktSrc/Thread.kt rename to stdlib/ktSrc/concurrent/Thread.kt diff --git a/stdlib/ktSrc/Timer.kt b/stdlib/ktSrc/concurrent/Timer.kt similarity index 100% rename from stdlib/ktSrc/Timer.kt rename to stdlib/ktSrc/concurrent/Timer.kt