Skip to content

Commit 469290c

Browse files
committed
chore(*): use kotlin 1.2.41 and style code according to kotlin guidelines
1 parent 19b288c commit 469290c

File tree

16 files changed

+53
-66
lines changed

16 files changed

+53
-66
lines changed

build.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ apply plugin: 'kotlin'
44
apply plugin: 'jacoco'
55

66
buildscript {
7-
ext.kotlin_version = '1.1.2-2'
7+
ext.kotlin_version = '1.2.41'
88

99
repositories {
1010
mavenCentral()

src/main/io/uuddlrlrba/ktalgs/datastructures/IndexedPriorityQueue.kt

+7-7
Original file line numberDiff line numberDiff line change
@@ -139,20 +139,20 @@ class IndexedPriorityQueue<T>(size: Int, val comparator: Comparator<T>? = null)
139139
}
140140

141141
private fun less(x: T, y: T): Boolean {
142-
if (comparator != null) {
143-
return comparator.compare(x, y) < 0
142+
return if (comparator != null) {
143+
comparator.compare(x, y) < 0
144144
} else {
145145
val left = x as Comparable<T>
146-
return left < y
146+
left < y
147147
}
148148
}
149149

150150
private fun greater(x: T, y: T): Boolean {
151-
if (comparator != null) {
152-
return comparator.compare(x, y) > 0
151+
return if (comparator != null) {
152+
comparator.compare(x, y) > 0
153153
} else {
154154
val left = x as Comparable<T>
155-
return left > y
155+
left > y
156156
}
157157
}
158158

@@ -172,7 +172,7 @@ class IndexedPriorityQueue<T>(size: Int, val comparator: Comparator<T>? = null)
172172
var k = n
173173
while (k > 1 && greater(k / 2, k)) {
174174
exch(k, k / 2)
175-
k = k / 2
175+
k /= 2
176176
}
177177
}
178178

src/main/io/uuddlrlrba/ktalgs/datastructures/tree/BinarySearchTree.kt

+21-27
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,10 @@ class BinarySearchTree<K: Comparable<K>, V>: Map<K, V> {
6262
override fun get(key: K): V? {
6363
var x = root
6464
while (x != null) {
65-
if (key < x.key) {
66-
x = x.left
67-
} else if (key > x.key) {
68-
x = x.right
69-
} else {
70-
return x.value
65+
x = when {
66+
key < x.key -> x.left
67+
key > x.key -> x.right
68+
else -> return x.value
7169
}
7270
}
7371
return null
@@ -87,12 +85,10 @@ class BinarySearchTree<K: Comparable<K>, V>: Map<K, V> {
8785

8886
private fun add(key: K, value: V, x: Node<K, V>?): Node<K, V> {
8987
if (x == null) return Node(key, value)
90-
if (key < x.key) {
91-
x.left = add(key, value, x.left)
92-
} else if (key > x.key) {
93-
x.right = add(key, value, x.right)
94-
} else {
95-
x.value = value
88+
when {
89+
key < x.key -> x.left = add(key, value, x.left)
90+
key > x.key -> x.right = add(key, value, x.right)
91+
else -> x.value = value
9692
}
9793
x.size = size(x.left) + size(x.right) + 1
9894
return x
@@ -104,25 +100,23 @@ class BinarySearchTree<K: Comparable<K>, V>: Map<K, V> {
104100

105101
private fun remove(key: K, root: Node<K, V>?): Node<K, V>? {
106102
var x: Node<K, V> = root ?: throw NoSuchElementException()
107-
if (key < x.key) {
108-
x.left = remove(key, x.left)
109-
} else if (key > x.key) {
110-
x.right = remove(key, x.right)
111-
} else {
112-
if (x.left == null) return x.right
113-
if (x.right == null) return x.left
114-
val tmp = x
115-
x = pollMin(tmp.right!!)!!
116-
x.right = min(tmp.right)
117-
x.left = tmp.left
103+
when {
104+
key < x.key -> x.left = remove(key, x.left)
105+
key > x.key -> x.right = remove(key, x.right)
106+
else -> {
107+
if (x.left == null) return x.right
108+
if (x.right == null) return x.left
109+
val tmp = x
110+
x = pollMin(tmp.right!!)!!
111+
x.right = min(tmp.right)
112+
x.left = tmp.left
113+
}
118114
}
119-
x.size = size(x.left) + size(x.right) + 1;
115+
x.size = size(x.left) + size(x.right) + 1
120116
return x
121117
}
122118

123-
private fun size(x: Node<K, V>?): Int {
124-
if (x == null) return 0 else return x.size
125-
}
119+
private fun size(x: Node<K, V>?): Int = x?.size ?: 0
126120

127121
fun height(): Int {
128122
return height(root)

src/main/io/uuddlrlrba/ktalgs/geometry/SierpinskiTriangle.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ class SierpinskiTriangle {
4343
}
4444
}
4545

46-
fun drawTriangles(arr: Array<BooleanArray>, top: Int, left: Int, bottom: Int, right: Int): Unit {
46+
fun drawTriangles(arr: Array<BooleanArray>, top: Int, left: Int, bottom: Int, right: Int) {
4747
val height = bottom - top
4848
val width = right - left
4949
for (i in 0..height) {

src/main/io/uuddlrlrba/ktalgs/graphs/BFS.kt

+4-4
Original file line numberDiff line numberDiff line change
@@ -26,21 +26,21 @@ import io.uuddlrlrba.ktalgs.datastructures.Queue
2626

2727
class BFS {
2828
companion object Implementations {
29-
fun iterative(graph: Graph, levelorder: ((Int) -> Unit)? = null) {
29+
fun iterative(graph: Graph, levelOrder: ((Int) -> Unit)? = null) {
3030
val visited = BooleanArray(graph.V)
3131
val queue = Queue<Int>()
32-
for (i in 0..graph.V - 1) {
32+
for (i in 0 until graph.V) {
3333
if (!visited[i]) {
3434
queue.add(i)
3535
visited[i] = true
36-
levelorder?.invoke(i)
36+
levelOrder?.invoke(i)
3737
while (!queue.isEmpty()) {
3838
val v = queue.poll()
3939
for (w in graph.adjacentVertices(v)) {
4040
if (!visited[w]) {
4141
queue.add(w)
4242
visited[w] = true
43-
levelorder?.invoke(i)
43+
levelOrder?.invoke(i)
4444
}
4545
}
4646
}

src/main/io/uuddlrlrba/ktalgs/graphs/DFS.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ class DFS {
3131
postorder: ((Int) -> Unit)? = null) {
3232
val visited = IntArray(graph.V)
3333
val queue = Stack<Int>()
34-
for (i in 0..graph.V - 1) {
34+
for (i in 0 until graph.V) {
3535
if (visited[i] == 0) {
3636
queue.push(i)
3737
visited[i] = 1

src/main/io/uuddlrlrba/ktalgs/graphs/Graph.kt

+2-4
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,5 @@ interface Graph {
2727
public var E: Int
2828
public fun adjacentVertices(from: Int): Collection<Int>
2929

30-
public fun vertices(): IntRange {
31-
return 0..V-1
32-
}
33-
}
30+
public fun vertices(): IntRange = 0 until V
31+
}

src/main/io/uuddlrlrba/ktalgs/graphs/directed/weighted/Dijkstra.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ class Dijkstra(graph: DWGraph, val from: Int) {
4343
private val pq: IndexedPriorityQueue<Double> = IndexedPriorityQueue(graph.V)
4444

4545
init {
46-
if (graph.edges().filter { it.weight < 0 }.isNotEmpty()) {
46+
if (graph.edges().any { it.weight < 0 }) {
4747
throw IllegalArgumentException("there is a negative weight edge")
4848
}
4949

src/main/io/uuddlrlrba/ktalgs/graphs/undirected/weighted/BoruvkaMST.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ class BoruvkaMST(G: UWGraph): MST {
5050
}
5151

5252
// add newly discovered edges to MST
53-
for (i in 0..G.V - 1) {
53+
for (i in 0 until G.V) {
5454
val e = closest[i]
5555
if (e != null) {
5656
val v = e.v

src/main/io/uuddlrlrba/ktalgs/search/BinarySearch.kt

+4-6
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,10 @@ class BinarySearch<T: Comparable<T>>: AbstractSearchStrategy<T>() {
2828
var hi = arr.size - 1
2929
while (lo <= hi) {
3030
val mid = (lo + hi) / 2
31-
if (element < arr[mid]) {
32-
hi = mid - 1
33-
} else if (element > arr[mid]) {
34-
lo = mid + 1
35-
} else {
36-
return mid
31+
when {
32+
element < arr[mid] -> hi = mid - 1
33+
element > arr[mid] -> lo = mid + 1
34+
else -> return mid
3735
}
3836
}
3937
return -1

src/main/io/uuddlrlrba/ktalgs/sorts/BubbleSort.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ class BubbleSort : AbstractSortStrategy() {
3737
var exchanged: Boolean
3838
do {
3939
exchanged = false
40-
for (i in 1..arr.size - 1) {
40+
for (i in 1 until arr.size) {
4141
if (arr[i] < arr[i - 1]) {
4242
arr.exch(i, i - 1)
4343
exchanged = true

src/main/io/uuddlrlrba/ktalgs/sorts/InsertionSort.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ package io.uuddlrlrba.ktalgs.sorts
3131
@StableSort
3232
class InsertionSort: AbstractSortStrategy() {
3333
override fun <T : Comparable<T>> perform(arr: Array<T>) {
34-
for (i in 1..arr.size - 1) {
34+
for (i in 1 until arr.size) {
3535
for (j in i downTo 1) {
3636
if (arr[j - 1] < arr[j]) break
3737
arr.exch(j, j - 1)

src/main/io/uuddlrlrba/ktalgs/sorts/MergeSort.kt

+5-8
Original file line numberDiff line numberDiff line change
@@ -50,14 +50,11 @@ class MergeSort: AbstractSortStrategy() {
5050
var i = lo
5151
var j = mid + 1
5252
for (k in lo..hi) {
53-
if (i > mid) {
54-
arr[k] = aux[j++]
55-
} else if (j > hi) {
56-
arr[k] = aux[i++]
57-
} else if (aux[j] < aux[i]) {
58-
arr[k] = aux[j++]
59-
} else {
60-
arr[k] = aux[i++]
53+
when {
54+
i > mid -> arr[k] = aux[j++]
55+
j > hi -> arr[k] = aux[i++]
56+
aux[j] < aux[i] -> arr[k] = aux[j++]
57+
else -> arr[k] = aux[i++]
6158
}
6259
}
6360
}

src/main/io/uuddlrlrba/ktalgs/sorts/SelectionSort.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ class SelectionSort : AbstractSortStrategy() {
3636
public override fun<T : Comparable<T>> perform(arr: Array<T>) {
3737
for (i in arr.indices) {
3838
var min = i
39-
for (j in i+1..arr.size-1) {
39+
for (j in i + 1 until arr.size) {
4040
if (arr[j] < arr[min]) {
4141
min = j
4242
}

src/main/io/uuddlrlrba/ktalgs/sorts/ShellSort.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ class ShellSort : AbstractSortStrategy() {
4040
}
4141

4242
while (h >= 1) {
43-
for (i in h..arr.size - 1) {
43+
for (i in h until arr.size) {
4444
for (j in i downTo h step h) {
4545
if (arr[j - h] < arr[j]) break
4646
arr.exch(j, j - h)

src/main/io/uuddlrlrba/ktalgs/substring/KMP.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class KMP(val pat: String) {
1212
var x = 0
1313
var j = 1
1414
while (j < m) {
15-
for (c in 0..R - 1) {
15+
for (c in 0 until R) {
1616
dfa[c][j] = dfa[c][x] // Copy mismatch cases.
1717
}
1818
dfa[pat[j].toInt()][j] = j + 1 // Set match case.

0 commit comments

Comments
 (0)