Renamed tuples from other code.

This commit is contained in:
Evgeny Gerashchenko
2013-03-18 22:04:06 +04:00
parent c458ed36f6
commit 655f9e01a2
4 changed files with 26 additions and 26 deletions
+3 -3
View File
@@ -62,9 +62,9 @@ var JFrame.title : String
get() = getTitle()!! get() = getTitle()!!
set(t) {setTitle(t)} set(t) {setTitle(t)}
var JFrame.size : #(Int, Int) var JFrame.size : Pair<Int, Int>
get() = #(getSize()!!.getWidth().toInt(), getSize()!!.getHeight().toInt()) get() = Pair(getSize()!!.getWidth().toInt(), getSize()!!.getHeight().toInt())
set(dim) {setSize(Dimension(dim._1, dim._2))} set(dim) {setSize(Dimension(dim.first, dim.second))}
var JFrame.height : Int var JFrame.height : Int
get() = getSize()!!.getHeight().toInt() get() = getSize()!!.getHeight().toInt()
@@ -25,11 +25,11 @@ import java.util.*
* free space (a path does not go through walls). One can move only * free space (a path does not go through walls). One can move only
* straightly up, down, left or right, no diagonal moves allowed. * straightly up, down, left or right, no diagonal moves allowed.
*/ */
fun findPath(maze : Maze) : List<#(Int, Int)>? { fun findPath(maze : Maze) : List<Pair<Int, Int>>? {
val previous = HashMap<#(Int, Int), #(Int, Int)> val previous = HashMap<Pair<Int, Int>, Pair<Int, Int>>
val queue = LinkedList<#(Int, Int)> val queue = LinkedList<Pair<Int, Int>>
val visited = HashSet<#(Int, Int)> val visited = HashSet<Pair<Int, Int>>
queue.offer(maze.start) queue.offer(maze.start)
visited.add(maze.start) visited.add(maze.start)
@@ -47,7 +47,7 @@ fun findPath(maze : Maze) : List<#(Int, Int)>? {
if (previous[maze.end] == null) return null if (previous[maze.end] == null) return null
val path = ArrayList<#(Int, Int)>() val path = ArrayList<Pair<Int, Int>>()
var current = previous[maze.end] var current = previous[maze.end]
while (current != maze.start) { while (current != maze.start) {
path.add(0, current) path.add(0, current)
@@ -59,8 +59,8 @@ fun findPath(maze : Maze) : List<#(Int, Int)>? {
/** /**
* Find neighbors of the (i, j) cell that are not walls * Find neighbors of the (i, j) cell that are not walls
*/ */
fun Maze.neighbors(i : Int, j : Int) : List<#(Int, Int)> { fun Maze.neighbors(i : Int, j : Int) : List<Pair<Int, Int>> {
val result = ArrayList<#(Int, Int)> val result = ArrayList<Pair<Int, Int>>
addIfFree(i - 1, j, result) addIfFree(i - 1, j, result)
addIfFree(i, j - 1, result) addIfFree(i, j - 1, result)
addIfFree(i + 1, j, result) addIfFree(i + 1, j, result)
@@ -68,12 +68,12 @@ fun Maze.neighbors(i : Int, j : Int) : List<#(Int, Int)> {
return result return result
} }
fun Maze.addIfFree(i : Int, j : Int, result : List<#(Int, Int)>) { fun Maze.addIfFree(i : Int, j : Int, result : List<Pair<Int, Int>>) {
if (i !in 0..height-1) return if (i !in 0..height-1) return
if (j !in 0..width-1) return if (j !in 0..width-1) return
if (walls[i][j]) return if (walls[i][j]) return
result.add(#(i, j)) result.add(Pair(i, j))
} }
/** /**
@@ -87,9 +87,9 @@ class Maze(
// true for a wall, false for free space // true for a wall, false for free space
val walls : Array<out Array<out Boolean>>, val walls : Array<out Array<out Boolean>>,
// The starting point (must not be a wall) // The starting point (must not be a wall)
val start : #(Int, Int), val start : Pair<Int, Int>,
// The target point (must not be a wall) // The target point (must not be a wall)
val end : #(Int, Int) val end : Pair<Int, Int>
) { ) {
} }
@@ -137,7 +137,7 @@ fun printMaze(str : String) {
val path = findPath(maze) val path = findPath(maze)
for (i in 0..maze.height - 1) { for (i in 0..maze.height - 1) {
for (j in 0..maze.width - 1) { for (j in 0..maze.width - 1) {
val cell = #(i, j) val cell = Pair(i, j)
print( print(
if (maze.walls[i][j]) "O" if (maze.walls[i][j]) "O"
else if (cell == maze.start) "I" else if (cell == maze.start) "I"
@@ -179,16 +179,16 @@ fun makeMaze(s : String) : Maze {
})!! })!!
val data = Array<Array<Boolean>>(lines.size) {Array<Boolean>(w.size) {false}} val data = Array<Array<Boolean>>(lines.size) {Array<Boolean>(w.size) {false}}
var start : #(Int, Int)? = null var start : Pair<Int, Int>? = null
var end : #(Int, Int)? = null var end : Pair<Int, Int>? = null
for (line in lines.indices) { for (line in lines.indices) {
for (x in lines[line].indices) { for (x in lines[line].indices) {
val c = lines[line]!![x] val c = lines[line]!![x]
data[line][x] = c == 'O' data[line][x] = c == 'O'
when (c) { when (c) {
'I' -> start = #(line, x) 'I' -> start = Pair(line, x)
'$' -> end = #(line, x) '$' -> end = Pair(line, x)
else -> {} else -> {}
} }
} }
@@ -348,9 +348,9 @@ public trait Traversable<T>: Iterable<T> {
* @includeFunctionBody ../../test/ListTest.kt withIndices * @includeFunctionBody ../../test/ListTest.kt withIndices
*/ */
/* /*
public fun withIndices(): java.lang.Iterable<#(Int, T)> { public fun withIndices(): java.lang.Iterable<Pair(Int, T)> {
return object : java.lang.Iterable<#(Int, T)> { return object : java.lang.Iterable<Pair(Int, T)> {
public override fun iterator(): Iterator<#(Int, T)> { public override fun iterator(): Iterator<Pair(Int, T)> {
return NumberedIterator<T>(iterator()) return NumberedIterator<T>(iterator())
} }
} }
+4 -4
View File
@@ -119,7 +119,7 @@ class MapJsTest {
/* /*
test fun createLinkedMap() { test fun createLinkedMap() {
val map = linkedMap(#("c", 3), #("b", 2), #("a", 1)) val map = linkedMapOf("c" to 3, "b" to 2, "a" to 1)
assertEquals(1, map.get("a")) assertEquals(1, map.get("a"))
assertEquals(2, map.get("b")) assertEquals(2, map.get("b"))
assertEquals(3, map.get("c")) assertEquals(3, map.get("c"))
@@ -183,7 +183,7 @@ class MapJsTest {
} }
test fun createSortedMap() { test fun createSortedMap() {
val map = sortedMap(#("c", 3), #("b", 2), #("a", 1)) val map = sortedMapOf("c" to 3, "b" to 2, "a" to 1)
assertEquals(1, map.get("a")) assertEquals(1, map.get("a"))
assertEquals(2, map.get("b")) assertEquals(2, map.get("b"))
assertEquals(3, map.get("c")) assertEquals(3, map.get("c"))
@@ -191,7 +191,7 @@ class MapJsTest {
} }
test fun toSortedMap() { test fun toSortedMap() {
val map = hashMap<String,Int>(#("c", 3), #("b", 2), #("a", 1)) val map = hashMapOf<String,Int>("c" to 3, "b" to 2, "a" to 1)
val sorted = map.toSortedMap<String,Int>() val sorted = map.toSortedMap<String,Int>()
assertEquals(1, sorted.get("a")) assertEquals(1, sorted.get("a"))
assertEquals(2, sorted.get("b")) assertEquals(2, sorted.get("b"))
@@ -200,7 +200,7 @@ class MapJsTest {
} }
test fun toSortedMapWithComparator() { test fun toSortedMapWithComparator() {
val map = hashMap(#("c", 3), #("bc", 2), #("bd", 4), #("abc", 1)) val map = hashMapOf("c" to 3, "bc" to 2, "bd" to 4, "abc" to 1)
val c = comparator<String>{ a, b -> val c = comparator<String>{ a, b ->
val answer = a.length() - b.length() val answer = a.length() - b.length()
if (answer == 0) a.compareTo(b) else answer if (answer == 0) a.compareTo(b) else answer