Initial
This commit is contained in:
Generated
+7
@@ -0,0 +1,7 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="AntConfiguration">
|
||||
<defaultAnt bundledAnt="true" />
|
||||
</component>
|
||||
</project>
|
||||
|
||||
Generated
+24
@@ -0,0 +1,24 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="CompilerConfiguration">
|
||||
<option name="DEFAULT_COMPILER" value="Javac" />
|
||||
<resourceExtensions>
|
||||
<entry name=".+\.(properties|xml|html|dtd|tld)" />
|
||||
<entry name=".+\.(gif|png|jpeg|jpg)" />
|
||||
</resourceExtensions>
|
||||
<wildcardResourcePatterns>
|
||||
<entry name="?*.properties" />
|
||||
<entry name="?*.xml" />
|
||||
<entry name="?*.gif" />
|
||||
<entry name="?*.png" />
|
||||
<entry name="?*.jpeg" />
|
||||
<entry name="?*.jpg" />
|
||||
<entry name="?*.html" />
|
||||
<entry name="?*.dtd" />
|
||||
<entry name="?*.tld" />
|
||||
<entry name="?*.ftl" />
|
||||
</wildcardResourcePatterns>
|
||||
<annotationProcessing enabled="false" useClasspath="true" />
|
||||
</component>
|
||||
</project>
|
||||
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
<component name="CopyrightManager">
|
||||
<settings default="">
|
||||
<module2copyright />
|
||||
</settings>
|
||||
</component>
|
||||
Generated
+5
@@ -0,0 +1,5 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="Encoding" useUTFGuessing="true" native2AsciiForPropertiesFiles="false" />
|
||||
</project>
|
||||
|
||||
Generated
+30
@@ -0,0 +1,30 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="DependencyValidationManager">
|
||||
<option name="SKIP_IMPORT_STATEMENTS" value="false" />
|
||||
</component>
|
||||
<component name="JavadocGenerationManager">
|
||||
<option name="OUTPUT_DIRECTORY" />
|
||||
<option name="OPTION_SCOPE" value="protected" />
|
||||
<option name="OPTION_HIERARCHY" value="true" />
|
||||
<option name="OPTION_NAVIGATOR" value="true" />
|
||||
<option name="OPTION_INDEX" value="true" />
|
||||
<option name="OPTION_SEPARATE_INDEX" value="true" />
|
||||
<option name="OPTION_DOCUMENT_TAG_USE" value="false" />
|
||||
<option name="OPTION_DOCUMENT_TAG_AUTHOR" value="false" />
|
||||
<option name="OPTION_DOCUMENT_TAG_VERSION" value="false" />
|
||||
<option name="OPTION_DOCUMENT_TAG_DEPRECATED" value="true" />
|
||||
<option name="OPTION_DEPRECATED_LIST" value="true" />
|
||||
<option name="OTHER_OPTIONS" value="" />
|
||||
<option name="HEAP_SIZE" />
|
||||
<option name="LOCALE" />
|
||||
<option name="OPEN_IN_BROWSER" value="true" />
|
||||
</component>
|
||||
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_5" assert-keyword="true" jdk-15="true" project-jdk-name="1.6" project-jdk-type="JavaSDK">
|
||||
<output url="file://$PROJECT_DIR$/out" />
|
||||
</component>
|
||||
<component name="SvnBranchConfigurationManager">
|
||||
<option name="mySupportsUserInfoFilter" value="true" />
|
||||
</component>
|
||||
</project>
|
||||
|
||||
Generated
+9
@@ -0,0 +1,9 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ProjectModuleManager">
|
||||
<modules>
|
||||
<module fileurl="file://$PROJECT_DIR$/examples.iml" filepath="$PROJECT_DIR$/examples.iml" />
|
||||
</modules>
|
||||
</component>
|
||||
</project>
|
||||
|
||||
Generated
+37
@@ -0,0 +1,37 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="CodeStyleSettingsManager">
|
||||
<option name="PER_PROJECT_SETTINGS">
|
||||
<value>
|
||||
<ADDITIONAL_INDENT_OPTIONS fileType="groovy">
|
||||
<option name="INDENT_SIZE" value="2" />
|
||||
<option name="CONTINUATION_INDENT_SIZE" value="8" />
|
||||
<option name="TAB_SIZE" value="4" />
|
||||
<option name="USE_TAB_CHARACTER" value="false" />
|
||||
<option name="SMART_TABS" value="false" />
|
||||
<option name="LABEL_INDENT_SIZE" value="0" />
|
||||
<option name="LABEL_INDENT_ABSOLUTE" value="false" />
|
||||
</ADDITIONAL_INDENT_OPTIONS>
|
||||
<ADDITIONAL_INDENT_OPTIONS fileType="java">
|
||||
<option name="INDENT_SIZE" value="4" />
|
||||
<option name="CONTINUATION_INDENT_SIZE" value="8" />
|
||||
<option name="TAB_SIZE" value="4" />
|
||||
<option name="USE_TAB_CHARACTER" value="false" />
|
||||
<option name="SMART_TABS" value="false" />
|
||||
<option name="LABEL_INDENT_SIZE" value="0" />
|
||||
<option name="LABEL_INDENT_ABSOLUTE" value="false" />
|
||||
</ADDITIONAL_INDENT_OPTIONS>
|
||||
<ADDITIONAL_INDENT_OPTIONS fileType="xml">
|
||||
<option name="INDENT_SIZE" value="4" />
|
||||
<option name="CONTINUATION_INDENT_SIZE" value="8" />
|
||||
<option name="TAB_SIZE" value="4" />
|
||||
<option name="USE_TAB_CHARACTER" value="false" />
|
||||
<option name="SMART_TABS" value="false" />
|
||||
<option name="LABEL_INDENT_SIZE" value="0" />
|
||||
<option name="LABEL_INDENT_ABSOLUTE" value="false" />
|
||||
</ADDITIONAL_INDENT_OPTIONS>
|
||||
</value>
|
||||
</option>
|
||||
</component>
|
||||
</project>
|
||||
|
||||
Generated
+125
@@ -0,0 +1,125 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="Palette2">
|
||||
<group name="Swing">
|
||||
<item class="com.intellij.uiDesigner.HSpacer" tooltip-text="Horizontal Spacer" icon="/com/intellij/uiDesigner/icons/hspacer.png" removable="false" auto-create-binding="false" can-attach-label="false">
|
||||
<default-constraints vsize-policy="1" hsize-policy="6" anchor="0" fill="1" />
|
||||
</item>
|
||||
<item class="com.intellij.uiDesigner.VSpacer" tooltip-text="Vertical Spacer" icon="/com/intellij/uiDesigner/icons/vspacer.png" removable="false" auto-create-binding="false" can-attach-label="false">
|
||||
<default-constraints vsize-policy="6" hsize-policy="1" anchor="0" fill="2" />
|
||||
</item>
|
||||
<item class="javax.swing.JPanel" icon="/com/intellij/uiDesigner/icons/panel.png" removable="false" auto-create-binding="false" can-attach-label="false">
|
||||
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3" />
|
||||
</item>
|
||||
<item class="javax.swing.JScrollPane" icon="/com/intellij/uiDesigner/icons/scrollPane.png" removable="false" auto-create-binding="false" can-attach-label="true">
|
||||
<default-constraints vsize-policy="7" hsize-policy="7" anchor="0" fill="3" />
|
||||
</item>
|
||||
<item class="javax.swing.JButton" icon="/com/intellij/uiDesigner/icons/button.png" removable="false" auto-create-binding="true" can-attach-label="false">
|
||||
<default-constraints vsize-policy="0" hsize-policy="3" anchor="0" fill="1" />
|
||||
<initial-values>
|
||||
<property name="text" value="Button" />
|
||||
</initial-values>
|
||||
</item>
|
||||
<item class="javax.swing.JRadioButton" icon="/com/intellij/uiDesigner/icons/radioButton.png" removable="false" auto-create-binding="true" can-attach-label="false">
|
||||
<default-constraints vsize-policy="0" hsize-policy="3" anchor="8" fill="0" />
|
||||
<initial-values>
|
||||
<property name="text" value="RadioButton" />
|
||||
</initial-values>
|
||||
</item>
|
||||
<item class="javax.swing.JCheckBox" icon="/com/intellij/uiDesigner/icons/checkBox.png" removable="false" auto-create-binding="true" can-attach-label="false">
|
||||
<default-constraints vsize-policy="0" hsize-policy="3" anchor="8" fill="0" />
|
||||
<initial-values>
|
||||
<property name="text" value="CheckBox" />
|
||||
</initial-values>
|
||||
</item>
|
||||
<item class="javax.swing.JLabel" icon="/com/intellij/uiDesigner/icons/label.png" removable="false" auto-create-binding="false" can-attach-label="false">
|
||||
<default-constraints vsize-policy="0" hsize-policy="0" anchor="8" fill="0" />
|
||||
<initial-values>
|
||||
<property name="text" value="Label" />
|
||||
</initial-values>
|
||||
</item>
|
||||
<item class="javax.swing.JTextField" icon="/com/intellij/uiDesigner/icons/textField.png" removable="false" auto-create-binding="true" can-attach-label="true">
|
||||
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
|
||||
<preferred-size width="150" height="-1" />
|
||||
</default-constraints>
|
||||
</item>
|
||||
<item class="javax.swing.JPasswordField" icon="/com/intellij/uiDesigner/icons/passwordField.png" removable="false" auto-create-binding="true" can-attach-label="true">
|
||||
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
|
||||
<preferred-size width="150" height="-1" />
|
||||
</default-constraints>
|
||||
</item>
|
||||
<item class="javax.swing.JFormattedTextField" icon="/com/intellij/uiDesigner/icons/formattedTextField.png" removable="false" auto-create-binding="true" can-attach-label="true">
|
||||
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
|
||||
<preferred-size width="150" height="-1" />
|
||||
</default-constraints>
|
||||
</item>
|
||||
<item class="javax.swing.JTextArea" icon="/com/intellij/uiDesigner/icons/textArea.png" removable="false" auto-create-binding="true" can-attach-label="true">
|
||||
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
|
||||
<preferred-size width="150" height="50" />
|
||||
</default-constraints>
|
||||
</item>
|
||||
<item class="javax.swing.JTextPane" icon="/com/intellij/uiDesigner/icons/textPane.png" removable="false" auto-create-binding="true" can-attach-label="true">
|
||||
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
|
||||
<preferred-size width="150" height="50" />
|
||||
</default-constraints>
|
||||
</item>
|
||||
<item class="javax.swing.JEditorPane" icon="/com/intellij/uiDesigner/icons/editorPane.png" removable="false" auto-create-binding="true" can-attach-label="true">
|
||||
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
|
||||
<preferred-size width="150" height="50" />
|
||||
</default-constraints>
|
||||
</item>
|
||||
<item class="javax.swing.JComboBox" icon="/com/intellij/uiDesigner/icons/comboBox.png" removable="false" auto-create-binding="true" can-attach-label="true">
|
||||
<default-constraints vsize-policy="0" hsize-policy="2" anchor="8" fill="1" />
|
||||
</item>
|
||||
<item class="javax.swing.JTable" icon="/com/intellij/uiDesigner/icons/table.png" removable="false" auto-create-binding="true" can-attach-label="false">
|
||||
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
|
||||
<preferred-size width="150" height="50" />
|
||||
</default-constraints>
|
||||
</item>
|
||||
<item class="javax.swing.JList" icon="/com/intellij/uiDesigner/icons/list.png" removable="false" auto-create-binding="true" can-attach-label="false">
|
||||
<default-constraints vsize-policy="6" hsize-policy="2" anchor="0" fill="3">
|
||||
<preferred-size width="150" height="50" />
|
||||
</default-constraints>
|
||||
</item>
|
||||
<item class="javax.swing.JTree" icon="/com/intellij/uiDesigner/icons/tree.png" removable="false" auto-create-binding="true" can-attach-label="false">
|
||||
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
|
||||
<preferred-size width="150" height="50" />
|
||||
</default-constraints>
|
||||
</item>
|
||||
<item class="javax.swing.JTabbedPane" icon="/com/intellij/uiDesigner/icons/tabbedPane.png" removable="false" auto-create-binding="true" can-attach-label="false">
|
||||
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3">
|
||||
<preferred-size width="200" height="200" />
|
||||
</default-constraints>
|
||||
</item>
|
||||
<item class="javax.swing.JSplitPane" icon="/com/intellij/uiDesigner/icons/splitPane.png" removable="false" auto-create-binding="false" can-attach-label="false">
|
||||
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3">
|
||||
<preferred-size width="200" height="200" />
|
||||
</default-constraints>
|
||||
</item>
|
||||
<item class="javax.swing.JSpinner" icon="/com/intellij/uiDesigner/icons/spinner.png" removable="false" auto-create-binding="true" can-attach-label="true">
|
||||
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1" />
|
||||
</item>
|
||||
<item class="javax.swing.JSlider" icon="/com/intellij/uiDesigner/icons/slider.png" removable="false" auto-create-binding="true" can-attach-label="false">
|
||||
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1" />
|
||||
</item>
|
||||
<item class="javax.swing.JSeparator" icon="/com/intellij/uiDesigner/icons/separator.png" removable="false" auto-create-binding="false" can-attach-label="false">
|
||||
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3" />
|
||||
</item>
|
||||
<item class="javax.swing.JProgressBar" icon="/com/intellij/uiDesigner/icons/progressbar.png" removable="false" auto-create-binding="true" can-attach-label="false">
|
||||
<default-constraints vsize-policy="0" hsize-policy="6" anchor="0" fill="1" />
|
||||
</item>
|
||||
<item class="javax.swing.JToolBar" icon="/com/intellij/uiDesigner/icons/toolbar.png" removable="false" auto-create-binding="false" can-attach-label="false">
|
||||
<default-constraints vsize-policy="0" hsize-policy="6" anchor="0" fill="1">
|
||||
<preferred-size width="-1" height="20" />
|
||||
</default-constraints>
|
||||
</item>
|
||||
<item class="javax.swing.JToolBar$Separator" icon="/com/intellij/uiDesigner/icons/toolbarSeparator.png" removable="false" auto-create-binding="false" can-attach-label="false">
|
||||
<default-constraints vsize-policy="0" hsize-policy="0" anchor="0" fill="1" />
|
||||
</item>
|
||||
<item class="javax.swing.JScrollBar" icon="/com/intellij/uiDesigner/icons/scrollbar.png" removable="false" auto-create-binding="true" can-attach-label="false">
|
||||
<default-constraints vsize-policy="6" hsize-policy="0" anchor="0" fill="2" />
|
||||
</item>
|
||||
</group>
|
||||
</component>
|
||||
</project>
|
||||
|
||||
Generated
+7
@@ -0,0 +1,7 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="VcsDirectoryMappings">
|
||||
<mapping directory="" vcs="Git" />
|
||||
</component>
|
||||
</project>
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<module type="JAVA_MODULE" version="4">
|
||||
<component name="NewModuleRootManager" inherit-compiler-output="true">
|
||||
<exclude-output />
|
||||
<content url="file://$MODULE_DIR$">
|
||||
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
|
||||
</content>
|
||||
<orderEntry type="jdk" jdkName="1.6" jdkType="JavaSDK" />
|
||||
<orderEntry type="sourceFolder" forTests="false" />
|
||||
</component>
|
||||
</module>
|
||||
|
||||
@@ -0,0 +1,195 @@
|
||||
class BinaryTree<T> : IMutableSet<T> {
|
||||
private class TreeNode(
|
||||
var value : T, var parent : TreeNode
|
||||
) {
|
||||
var left : TreeNode
|
||||
var right : TreeNode
|
||||
|
||||
}
|
||||
|
||||
private val compare : MatchableComparison<T>
|
||||
private var root : TreeNode
|
||||
private var version = 0
|
||||
|
||||
override var size : Int { get; private set; }
|
||||
|
||||
this(compare : Comparison<T>) {
|
||||
this.compare = asMatchableComparison(comparison)
|
||||
}
|
||||
|
||||
this() : this(naturalOrder<T>) {
|
||||
}
|
||||
|
||||
private extension [operator] fun T.compareTo(other : T) : Int = compare(this, other)
|
||||
|
||||
override fun contains(item : T) : Boolean {
|
||||
return contains(root, item)
|
||||
|
||||
fun contains(node : TreeNode, item : T) : Boolean {
|
||||
if (node == null) return false
|
||||
compare(item, node.value) match {
|
||||
case EQ => true
|
||||
case LS => contains(node.left, item)
|
||||
case GT => contains(node.right, item)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override fun add(item : T) : Boolean {
|
||||
if (add(ref root, null)) {
|
||||
size++
|
||||
version++
|
||||
return true
|
||||
}
|
||||
return false
|
||||
|
||||
// In principle, this has access to item anyway, but then it's unreachable code
|
||||
// BAD: the naive implementation of ref will create H(T) ref objects, but can be optimized to create only one
|
||||
fun add(ref node : TreeNode, parent : TreeNode) : Boolean {
|
||||
if (node == null) {
|
||||
node = TreeNode(item, parent)
|
||||
return true
|
||||
}
|
||||
compare(item, node.value) match {
|
||||
case EQ => false
|
||||
case LS => add(ref node.left, node)
|
||||
case GT => add(ref node.right, node)
|
||||
}
|
||||
}
|
||||
|
||||
// In principle, this has access to item anyway
|
||||
fun addNoRef(node : TreeNode) : Boolean {
|
||||
if (node == null) {
|
||||
root = TreeNode(item, null)
|
||||
return true
|
||||
}
|
||||
compare(item, node.value) match {
|
||||
case EQ => return false
|
||||
case LS =>
|
||||
if (node.left == null) {
|
||||
node.left = TreeNode(item, node)
|
||||
return true
|
||||
} else return add(node.left)
|
||||
case GT =>
|
||||
if (node.right == null) {
|
||||
node.right = TreeNode(item, node)
|
||||
return true
|
||||
} else return add(node.right)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override fun remove(item : T) : Boolean {
|
||||
val toRemove = find(root, item)
|
||||
if (toRemove == null) return false
|
||||
remove(toRemove)
|
||||
size--
|
||||
version++
|
||||
return true
|
||||
|
||||
fun find(node : TreeNode) : TreeNode {
|
||||
if (node == null) return null
|
||||
compare(item, node.value) match {
|
||||
case EQ => node
|
||||
case LS => find(node.left)
|
||||
case GT => find(node.right)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun remove(node : TreeNode) {
|
||||
node match {
|
||||
case TreeNode(null, null) => replace(node, null)
|
||||
case TreeNode(null, right) => replace(node, right)
|
||||
case TreeNode(left, null) => replace(node, left)
|
||||
case TreeNode(left, right) => {
|
||||
val min = min(node.right)
|
||||
node.value = min.value
|
||||
remove(min)
|
||||
}
|
||||
}
|
||||
|
||||
fun replace(node : TreeNode, replace : TreeNode) {
|
||||
if (node == root) {
|
||||
root = replace
|
||||
return
|
||||
}
|
||||
if (node.parent.left == node) {
|
||||
node.parent.left = replace
|
||||
} else if (node.parent.right == node) {
|
||||
node.parent.right = replace
|
||||
} else Assert(false)
|
||||
}
|
||||
}
|
||||
|
||||
// Relies on tail-recursion optimization
|
||||
private fun min(node : TreeNode) {
|
||||
if (node.left == null) node else min(node.left)
|
||||
}
|
||||
|
||||
override fun iterator() : IIterator<T> = mutableIterator()
|
||||
|
||||
override fun mutableIterator() : IMutableIterator<T> = new IMutableIterator() {
|
||||
val version = BinaryTree.this.version
|
||||
val down = Stack<TreeNode>()
|
||||
val up = Stack<TreeNode>()
|
||||
var lastNode : TreeNode
|
||||
|
||||
this() {
|
||||
if (root != null)
|
||||
down.push(root)
|
||||
}
|
||||
|
||||
override fun next() : T {
|
||||
if (!hasNext)
|
||||
throw NoMoreElementsException()
|
||||
checkVersion()
|
||||
|
||||
lastNode = nextNode()
|
||||
return lastNode.value
|
||||
}
|
||||
|
||||
private fun nextNode() : TreeNode {
|
||||
while (true) {
|
||||
if (!down.isEmpty) {
|
||||
val curNode = down.pop()
|
||||
if (curNode.left != null) {
|
||||
up.push(curNode)
|
||||
down.push(curNode.left)
|
||||
} else {
|
||||
if (curNode.right != null) {
|
||||
down.push(curNode.right)
|
||||
}
|
||||
return curNode;
|
||||
}
|
||||
} else {
|
||||
val curNode = up.pop()
|
||||
if (curNode.right != null) {
|
||||
down.push(curNode.right)
|
||||
}
|
||||
return curNode
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override val hasNext : Boolean {
|
||||
get() = !down.isEmpty || !up.isEmpty
|
||||
}
|
||||
|
||||
override fun remove() {
|
||||
checkVersion()
|
||||
if (lastNode == null)
|
||||
throw IterationException("Nothing to remove")
|
||||
remove(lastNode)
|
||||
version++
|
||||
BinaryTree.this.version = version
|
||||
}
|
||||
|
||||
private fun checkVersion() {
|
||||
if (version != BinaryTree.this.version) {
|
||||
throw ConcurrentModificationException()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
enum class Color(val r, g, b : Int) {
|
||||
RED : Color(255, 0, 0)
|
||||
GREEN : Color(0, 255, 0)
|
||||
BLUE : Color(0, 0, 255)
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
sealed class Vertex<V>(val data : V)
|
||||
|
||||
sealed class Edge<V, E>(val from : V, val data : E, val to : V)
|
||||
|
||||
class Graph<V, E> {
|
||||
|
||||
private val mutableEdges = new ArrayList<Edge<V, E>>() // type is ArrayList, but I want IMutableList
|
||||
/* options:
|
||||
private val edges : IMutableList<Edge<V, E>> = new ArrayList<Edge<V, E>>()
|
||||
private val edges : IMutableList<Edge<V, E>> = new ArrayList() // not an erasure, but a request to infer parameters
|
||||
*/
|
||||
|
||||
private val mutableVertices = new HashSet<Vertex<V>>()
|
||||
|
||||
val edges : IList<Edge<V, E>> = mutableEdges;
|
||||
val vertices : ISet<Edge<V, E>> = mutableVertices;
|
||||
|
||||
fun addEdge(from : V, data : E, to : V) {
|
||||
mutableEdges.add(new Edge(from, data, to)) // constructor parameters are inferred
|
||||
}
|
||||
fun addVertex(v : V) {
|
||||
mutableEdges.add(new Edge(from, data, to)) // constructor parameters are inferred
|
||||
}
|
||||
|
||||
fun neighbours(v : Vertex<V>) = edges.filter{it.from == v}.map{it.to} // type is IIterable<Vertex<V>>
|
||||
|
||||
fun dfs(handler : {V => ()}) {
|
||||
val visited = new HashSet<Vertex<V>>()
|
||||
vertices.foreach{dfs(it, visited, handler)}
|
||||
|
||||
fun dfs(current : Vertex<V>, visited : ISet<Vertex<V>>, handler : {V => ()}) {
|
||||
if (!visited.add(current))
|
||||
return
|
||||
handler(current)
|
||||
neighbours(current).foreach{dfs(it, visited, handler)}
|
||||
}
|
||||
}
|
||||
|
||||
public fun traverse(pending : IPushPop<Vertex<V>>, visited : ISet<Vertex<V>>, handler : {V => ()}) {
|
||||
vertices.foreach {
|
||||
if (!visited.add(it))
|
||||
continue
|
||||
pending.push(it)
|
||||
while (!pending.isEmpty) {
|
||||
val current = pending.pop()
|
||||
handler(current);
|
||||
neighbours(current).foreach { n =>
|
||||
if (visited.add(n)) {
|
||||
pending.push(n)
|
||||
}
|
||||
}
|
||||
/* alternative
|
||||
pending->push(neighbours(current).filter{n => !visited[n])})
|
||||
// -> means that if push(x : T) and actual parameter y is IIterable<T>, this compiles into
|
||||
y.foreach{ n => push(n) }
|
||||
*/
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
interface class IPushPop<T> {
|
||||
val isEmpty : Boolean
|
||||
fun push(item : T)
|
||||
fun pop() : T
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
sealed class Queue<T> : IPushPop<T> {
|
||||
private class Item<T>(val data : T, var next : Item<T>)
|
||||
|
||||
private var head : Item<T> = null
|
||||
private var tail : Item<T> = null
|
||||
|
||||
override fun push(item : T) {
|
||||
val i = new Item(item)
|
||||
if (tail == null) {
|
||||
head = i
|
||||
tail = head
|
||||
} else {
|
||||
tail.next = i
|
||||
tail = i
|
||||
}
|
||||
}
|
||||
|
||||
override fun pop() =
|
||||
if (head == null)
|
||||
throw new UnderflowException()
|
||||
else {
|
||||
val result = head.data
|
||||
head = head.next
|
||||
if (head == null)
|
||||
tail = null
|
||||
result
|
||||
}
|
||||
|
||||
override val isEmpty {
|
||||
get() = head == null
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
sealed class Stack<T> : IPushPop<T> {
|
||||
private val data = new ArrayList<T>();
|
||||
|
||||
override fun push(item : T) {
|
||||
data.add(item) // Problem: I would like to write push(...) = data.add(...), but the types do not match
|
||||
}
|
||||
|
||||
override fun pop() = data.removeLast()
|
||||
|
||||
override val isEmpty { // TODO: This is UGLY :(
|
||||
get() = data.isEmpty
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
class UnionFind {
|
||||
private val data = IMutableList<Int>()
|
||||
|
||||
fun add() : Int {
|
||||
val size = data.size
|
||||
data.add(size)
|
||||
size
|
||||
}
|
||||
|
||||
private fun parent(x : Int) : Int {
|
||||
val p = data[x];
|
||||
if (p == x) {
|
||||
return x;
|
||||
}
|
||||
val result = parent(p);
|
||||
data[x] = result;
|
||||
}
|
||||
|
||||
fun union(a : Int, b : Int) {
|
||||
val pa = parent(a)
|
||||
val pb = parent(b)
|
||||
if (pa != pb) {
|
||||
if (Random.nextInt().isOdd) {
|
||||
data[pb] = pa
|
||||
} else {
|
||||
data[pa] = pb
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
extension val Int.isOdd : Boolean {
|
||||
get() = this % 2 != 0
|
||||
}
|
||||
@@ -0,0 +1,77 @@
|
||||
class ArrayList<T> : IMutableList<T> {
|
||||
private var data = MutableArray(10)
|
||||
private var used = 0
|
||||
private var version = 0
|
||||
|
||||
override fun iterator() : IIterator<T> = mutableIterator()
|
||||
|
||||
override fun mutableIterator() : IMutableIterator<T> = new IMutableIterator() { // T is inferred
|
||||
private val index = 0
|
||||
private var myVersion = version
|
||||
|
||||
private fun checkVersion() {
|
||||
if (version != myVersion)
|
||||
throw new ConcurrentModificationException()
|
||||
}
|
||||
|
||||
override fun next() {
|
||||
checkVersion()
|
||||
if (hasNext)
|
||||
throw new NoMoreElementsException()
|
||||
data[index++]
|
||||
}
|
||||
|
||||
override val hasNext {
|
||||
get() = index < used
|
||||
}
|
||||
|
||||
override fun remove() {
|
||||
checkVersion()
|
||||
val result = ArrayList.this.remove(index - 1)
|
||||
myVersion = version
|
||||
result
|
||||
}
|
||||
}
|
||||
|
||||
override fun get(index : Int) {
|
||||
checkIndex(index)
|
||||
data[index]
|
||||
}
|
||||
|
||||
private fun checkIndex(index : Int) {
|
||||
if (index > used)
|
||||
throw new IndexOutOfBoundsException(index)
|
||||
}
|
||||
|
||||
override val isEmpty {
|
||||
get() = used == 0
|
||||
}
|
||||
|
||||
override val size {
|
||||
get() = used
|
||||
}
|
||||
|
||||
override fun set(index : Int, value : T) {
|
||||
checkIndex(index)
|
||||
var result = data[index]
|
||||
data[index] = value
|
||||
result
|
||||
}
|
||||
|
||||
override fun add(index : Int, value : T) {
|
||||
ensureSize(used + 1)
|
||||
if (index == used) {
|
||||
data[used++] = value
|
||||
} else if (index < used) {
|
||||
for (i in used-1..index) // backwards, special operator... Need to optimize this to be a real indexed loop
|
||||
data[i + 1] = data[i]
|
||||
data[index] = value
|
||||
used++
|
||||
} else throw IndexOutOfBoundsException(index)
|
||||
}
|
||||
|
||||
override fun remove(index : Int) {
|
||||
for (i in index..used-1)
|
||||
data[i] = data[i + 1]
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
interface class IIterable<out T> {
|
||||
fun iterator() : IIterator<T>
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
interface class IIterator<out T> {
|
||||
fun next() : T
|
||||
val hasNext : Boolean
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
interface class IList<out T> : IIterable<T>, ISizable {
|
||||
[operator] fun get(index : Int) : T
|
||||
val isEmpty : Boolean
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
interface class IMutableIterable<out T> : IIterable<T> {
|
||||
fun mutableIterator() : IMutableIterator<T>
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
interface class IMutableIterator<out T> : IIterator<T> {
|
||||
fun remove() : T
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
interface class IMutableList<T> : IList<T>, IMutableIterable<T> {
|
||||
fun set(index : Int, value : T) : T
|
||||
fun add(index : Int, value : T)
|
||||
fun remove(index : Int) : T
|
||||
fun mutableIterator() : IMutableIterator<T>
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
interface class IMutableSet<T> : ISet<T>, IMutableIterable<T> {
|
||||
fun add(item : T) : Boolean
|
||||
fun remove(item : T) : Boolean
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
interface class ISet<T> : IIterable<T>, ISized {
|
||||
fun contains(item : T) : Boolean
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
interface class ISized {
|
||||
val size : Int
|
||||
}
|
||||
@@ -0,0 +1,73 @@
|
||||
class LinkedList<T> : IMutableList<T> {
|
||||
private class Item(var value : Item) {
|
||||
var next : Item
|
||||
var previous : Item
|
||||
}
|
||||
|
||||
private var head : Item = null
|
||||
private var tail : Item = null
|
||||
override var size { get; private set; }
|
||||
|
||||
override fun add(index : Int, value : T) {
|
||||
size++
|
||||
checkIndex(index)
|
||||
val newItem = Item(value)
|
||||
if (index == 0) {
|
||||
newItem.next = head
|
||||
head = newItem
|
||||
if (tail == null) {
|
||||
tail = head
|
||||
}
|
||||
} else {
|
||||
var insertAfter = itemAt(index)
|
||||
newItem.next = insertAfter.next
|
||||
insertAfter.next = newItem
|
||||
if (tail == insertAfter) {
|
||||
tail = newItem
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun checkIndex(index : Int) {
|
||||
if (!index in [0..size-1]) {
|
||||
throw IndexOutOfBoundsException(index)
|
||||
}
|
||||
}
|
||||
|
||||
override fun remove(index : Int) : T {
|
||||
checkIndex(index)
|
||||
val item = itemAt(index)
|
||||
if (item == head) {
|
||||
head = item.next
|
||||
if (head == null)
|
||||
tail= null
|
||||
} else {
|
||||
item.previous.next = item.next
|
||||
if (item.next == null) {
|
||||
item.next.previous = item.previous
|
||||
} else {
|
||||
tail = tail.previous
|
||||
}
|
||||
}
|
||||
size--
|
||||
return item.value
|
||||
}
|
||||
|
||||
override fun set(index : Int, value : T) : T {
|
||||
checkIndex(index)
|
||||
val item = itemAt(index)
|
||||
val result = item.value
|
||||
item.value = value
|
||||
return result
|
||||
}
|
||||
|
||||
private fun itemAt(index : Int) {
|
||||
var result = head
|
||||
for (i in [1..index]) {
|
||||
result = result.next
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
override fun mutableIterator() : IMutableIterator<T>
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
enum class List<out T>(theSize : Int) : IList<T> {
|
||||
Nil : List<Nothing>(0)
|
||||
|
||||
Cons<T>(val value : T, val tail : List<T>) : List<T>(1 + tail.size)
|
||||
|
||||
override val size : Int {
|
||||
get() = theSize
|
||||
}
|
||||
|
||||
override val isEmpty : Boolean {
|
||||
get() = this == Nil
|
||||
}
|
||||
|
||||
override fun iterator() = new IIterator() {
|
||||
private var current = List.this
|
||||
|
||||
override val hasNext {
|
||||
get() = current == Nil
|
||||
}
|
||||
|
||||
override fun next() {
|
||||
val result = current.value
|
||||
current = current.tail
|
||||
return result
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
interface class IMap<in K, out V> {
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,104 @@
|
||||
class BinaryHeap<T> : IPriorityQueue<T> {
|
||||
private val data : IMutableList<T>
|
||||
private val compare : Comparison<T>
|
||||
|
||||
fun this(data : IIterable<T>, compare : Comparison<T> = naturalOrder<T>) {
|
||||
this.compare = compare
|
||||
this.data = ArrayList(data)
|
||||
siftDown(* this.data.size / 2 .. 0)
|
||||
/*
|
||||
for (val i in data.size / 2 .. 0) {
|
||||
siftDown(i)
|
||||
}
|
||||
*/
|
||||
}
|
||||
|
||||
fun this(compare : Comparison<T>) {
|
||||
this.compare = compare
|
||||
this.data = ArrayList()
|
||||
}
|
||||
|
||||
fun this() {
|
||||
this.data = ArrayList()
|
||||
Assert(T is IComparable<T>)
|
||||
this.comparator = naturalOrder<T>
|
||||
}
|
||||
|
||||
override fun extract() : T {
|
||||
if (this.isEmpty)
|
||||
throw UnderflowException()
|
||||
data.swap(0, data.lastIndex)
|
||||
data.remove(data.lastIndex)
|
||||
siftDown(0)
|
||||
}
|
||||
|
||||
override fun add(item : T) {
|
||||
data.add(item)
|
||||
siftUp(data.lastItem)
|
||||
}
|
||||
|
||||
private fun siftDown(index : Int) {
|
||||
var current = index
|
||||
while (current.left.exists) {
|
||||
var min = current
|
||||
if (current.left.value < min.value) {
|
||||
min = current.left
|
||||
}
|
||||
if (current.right.exists && current.right.value < min.value) {
|
||||
min = current.right
|
||||
}
|
||||
if (min == current) break
|
||||
data.swap(min, current)
|
||||
current = min
|
||||
}
|
||||
}
|
||||
|
||||
private fun siftUp(index : Int) {
|
||||
if (!current.exists) return
|
||||
var current = index
|
||||
while (current.parent.exists) {
|
||||
if (current.value < current.parent.value) {
|
||||
data.swap(current, current.parent)
|
||||
current = current.parent
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private extension HeapIndex for Int {
|
||||
val parent : Int {
|
||||
get() = (this - 1) / 2
|
||||
}
|
||||
|
||||
val left : Int {
|
||||
get() = this * 2 + 1
|
||||
}
|
||||
|
||||
val right : Int {
|
||||
get() = this * 2 + 2
|
||||
}
|
||||
|
||||
val value : T {
|
||||
get() = data[this]
|
||||
}
|
||||
|
||||
val exists : Boolean {
|
||||
get() = (this < data.size) && (this >= 0)
|
||||
}
|
||||
}
|
||||
|
||||
private extension for T {
|
||||
[operator] fun compareTo(other : T) : Int = compare(this, other)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
extension fun IMutableList<T>.swap(a : Int, b : Int) {
|
||||
val t = this[a]
|
||||
this[a] = this[b]
|
||||
this[b] = t
|
||||
}
|
||||
|
||||
extension val IList<T>.lastIndex : Int {
|
||||
get() = this.size - 1
|
||||
}
|
||||
|
||||
@@ -0,0 +1,5 @@
|
||||
interface class IPriorityQueue<T> {
|
||||
fun extract() : T
|
||||
fun add(item : T)
|
||||
val isEmpty : Boolean
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
class PriorityQueueAsPushPop<T> wraps(wrapped : IPriorityQueue<T>) : IPushPop<T> {
|
||||
override fun pop() = wrapped.extract()
|
||||
override fun push(item : T) = wrapped.add(item)
|
||||
override val isEmpty {
|
||||
get() = wrapped.isEmpty
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
type Comparison<in T> = {T, T => Int}
|
||||
|
||||
fun naturalOrder<in T : IComparable<T>>(a : T, b : T) : Int = a.compareTo(b)
|
||||
|
||||
fun castingNaturalOrder(a : Object, b : Object) : Int = a.as<Comparable<Object>>.compareTo(b.as<Comparable<Object>>)
|
||||
|
||||
enum class ComparisonResult {
|
||||
LS,EQ, GR
|
||||
}
|
||||
|
||||
type MatchableComparison<in T> = {T, T, => ComparisonResult}
|
||||
|
||||
fun asMatchableComparison<T>(cmp : Comparison<T>) : MatchableComparison<T> = {a, b =>
|
||||
val res = cmp(a, b)
|
||||
if (res == 0) return ComparisonResult.EQ
|
||||
if (res < 0) return ComparisonResult.LS
|
||||
return ComparisonResult.GR
|
||||
}
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
interface class IComparable<in T> {
|
||||
fun compareTo(other : T) : Int
|
||||
}
|
||||
Reference in New Issue
Block a user