Skip to content

Commit b45e931

Browse files
committed
improved build structure
1 parent e8a1e0d commit b45e931

25 files changed

+244
-60
lines changed

src/kotlin/BUILD

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ define_kt_toolchain(
1414
kt_jvm_library(
1515
name = "utils",
1616
srcs = [
17-
"_util/Utils.kt",
17+
"_util/IntArrayUtils.kt",
18+
"_util/CollectionUtils.kt",
1819
"//sequential/shuffling/fisheryates:FisherYatesShuffle.kt",
19-
2020
],
2121
visibility = ["//:__subpackages__"]
2222
)

src/kotlin/_util/CollectionUtils.kt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package _util
2+
3+
import sequential.shuffling.fisheryates.fisherYatesShuffle
4+
import kotlin.random.Random
5+
6+
7+
private const val MAX_LIST_VALUE = 999
8+
private const val LIST_SIZE = 50
9+
10+
fun randomList(size: Int = LIST_SIZE, range: IntRange = 0..MAX_LIST_VALUE) =
11+
List(size) {
12+
Random.nextInt(range.first, range.last + 1)
13+
}
14+
15+
fun <T: Comparable<T>> Collection<T>.isSorted(): Boolean =
16+
asSequence().zipWithNext { a, b -> a <= b }.all { it }

src/kotlin/_util/IntArrayUtils.kt

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package _util
2+
3+
import sequential.shuffling.fisheryates.fisherYatesShuffle
4+
import kotlin.random.Random
5+
6+
7+
private const val MAX_ARRAY_VALUE = 999
8+
private const val ARRAY_SIZE = 50
9+
10+
fun randomIntArray(size: Int = ARRAY_SIZE, range: IntRange = 0..MAX_ARRAY_VALUE) =
11+
IntArray(size) {
12+
Random.nextInt(range.first, range.last + 1)
13+
}
14+
15+
fun IntArray.shuffle() {
16+
fisherYatesShuffle()
17+
}
18+
19+
fun IntArray.exch(i: Int, j: Int) {
20+
this[i] = this[j].also { this[j] = this[i] }
21+
}
22+
23+
fun IntArray.isSorted(): Boolean {
24+
for (i in 1..lastIndex)
25+
if (this[i] < this[i - 1]) return false
26+
return true
27+
}
28+
29+
fun IntArray.print() {
30+
println(contentToString())
31+
}

src/kotlin/_util/Utils.kt

Lines changed: 0 additions & 12 deletions
This file was deleted.

src/kotlin/sequential/graph/Graph.kt

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package sequential.graph
2+
3+
4+
/**
5+
* Graph representation using hash table.
6+
* Properties: unweighted, directed.
7+
*/
8+
private typealias GraphMap<T> = Map<T, List<T>>
9+
10+
val socialNetwork: GraphMap<String> = mapOf(
11+
"you" to listOf("tony", "steve", "nick"),
12+
"tony" to listOf("clint"),
13+
"steve" to listOf("phil", "clint"),
14+
"nick" to listOf("thor", "natasha")
15+
)
16+
17+
18+
/**
19+
* Graph representation using hash table.
20+
* Properties: weighted, directed.
21+
*/
22+
private typealias WeightedGraphMap<T> = Map<T, Map<T, Int>>
23+
24+
val waypoints: WeightedGraphMap<String> = mapOf(
25+
"Start" to mapOf("A" to 5),
26+
"A" to mapOf("B" to 4, "C" to 7),
27+
"B" to mapOf("Finish" to 4),
28+
"C" to mapOf("Finish" to 3)
29+
)
30+
31+
32+
/**
33+
* Graph representation using boolean adjacency matrix.
34+
* Properties: unweighted, directed/undirected. (If matrix is symmetric - undirected, otherwise - directed).
35+
*/
36+
private typealias GraphAdjMatrix = Array<BooleanArray>
37+
38+
val graphAdjMatrix: GraphAdjMatrix = arrayOf(
39+
booleanArrayOf(true, false, false, false),
40+
booleanArrayOf(false, true, false, false),
41+
booleanArrayOf(false, true, true, false),
42+
booleanArrayOf(false, true, false, true)
43+
)
44+
45+
46+
/**
47+
* Graph representation using int adjacency matrix.
48+
* Properties: weighted, directed/undirected. (If matrix is symmetric - undirected, otherwise - directed).
49+
*/
50+
private typealias WeightedGraphAdjMatrix = Array<IntArray>
51+
52+
val weightedGraphAdjMatrix: WeightedGraphAdjMatrix = arrayOf(
53+
intArrayOf(0, 0, 1, 5),
54+
intArrayOf(0, 1, 0, 0),
55+
intArrayOf(0, 0, 1, 5),
56+
intArrayOf(0, 0, 1, 5)
57+
)
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package sequential.graph.bfs
2+
3+
import kotlin.collections.ArrayDeque
4+
5+
6+
private typealias Graph<T> = Map<T, List<T>>
7+
8+
private typealias Queue<T> = ArrayDeque<T>
9+
10+
11+
fun <T> Graph<T>.bfs(root: T): Collection<T> {
12+
val graph = this
13+
14+
val explored = mutableSetOf<T>()
15+
val searchQueue = Queue<T>(listOf(root))
16+
17+
while (searchQueue.isNotEmpty()) {
18+
val node = searchQueue.removeFirst()
19+
explored.add(node)
20+
21+
val successors = graph[node].orEmpty()
22+
for (succ in successors) {
23+
if (succ !in explored) searchQueue.addLast(succ)
24+
}
25+
}
26+
27+
return explored
28+
}
29+
30+
31+
fun main() {
32+
// TODO
33+
}
Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,24 @@
11
package sequential.shuffling.fisheryates
22

3-
import _util.swap
3+
import _util.*
44
import kotlin.random.Random
55

66

77
fun IntArray.fisherYatesShuffle() {
88
for (i in lastIndex downTo 1) {
99
val j = Random.nextInt(i + 1)
10-
swap(i, j)
10+
exch(i, j)
1111
}
1212
}
1313

1414

1515
fun main() {
16-
println("FisherYatesShuffle")
16+
val array = randomIntArray(20)
17+
array.sort()
18+
array.print()
19+
20+
array.fisherYatesShuffle()
21+
22+
println("Shuffling is successful: ${!array.isSorted()}")
23+
array.print()
1724
}

src/kotlin/sequential/shuffling/sattolo/BUILD

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@ kt_jvm_binary(
44
name = "sattolo_shuffle",
55
srcs = ["SattoloShuffle.kt"],
66
main_class = "sequential.shuffling.sattolo.SattoloShuffleKt",
7-
deps = ["//_util:utils"],
7+
deps = ["//:utils"],
88
)
Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,24 @@
11
package sequential.shuffling.sattolo
22

3-
import sequential.sorting._util.swap
3+
import _util.*
44
import kotlin.random.Random
55

66

77
fun IntArray.sattoloShuffle() {
88
for (i in lastIndex downTo 1) {
99
val j = Random.nextInt(i)
10-
swap(i, j)
10+
exch(i, j)
1111
}
1212
}
1313

1414

1515
fun main() {
16-
println("SattoloShuffle")
16+
val array = randomIntArray(20)
17+
array.sort()
18+
array.print()
19+
20+
array.sattoloShuffle()
21+
22+
println("Shuffling is successful: ${!array.isSorted()}")
23+
array.print()
1724
}

src/kotlin/sequential/sorting/_analysis/PartitioningFunction.kt

Lines changed: 0 additions & 3 deletions
This file was deleted.

src/kotlin/sequential/sorting/_analysis/SortAnalysis.kt

Lines changed: 0 additions & 6 deletions
This file was deleted.

src/kotlin/sequential/sorting/_analysis/BUILD renamed to src/kotlin/sequential/sorting/_measurement/BUILD

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@ load("@io_bazel_rules_kotlin//kotlin:kotlin.bzl", "kt_jvm_library")
33
package(default_visibility = ["//sequential/sorting:__subpackages__"])
44

55
kt_jvm_library(
6-
name = "sort_analysis",
6+
name = "_measurement",
77
srcs = glob(["*.kt"]),
88
)
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package sequential.sorting._measurement
2+
3+
4+
interface MeasuredElements<T> {
5+
6+
}
7+
8+

src/kotlin/sequential/sorting/insertionsort/BUILD

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,12 @@ kt_jvm_library(
44
name = "insertion_sort_lib",
55
srcs = [
66
"InsertionSort.kt",
7-
"ranged/InsertionSort.kt",
7+
"RangedInsertionSort.kt",
8+
],
9+
deps = [
10+
"//sequential/sorting/_measurement",
11+
"//:utils",
812
],
9-
deps = ["//sequential/sorting/_analysis:sort_analysis"],
1013
visibility = ["//visibility:public"],
1114
)
1215

@@ -18,6 +21,6 @@ java_binary(
1821

1922
java_binary(
2023
name = "insertion_sort_ranged",
21-
main_class = "sequential.sorting.insertionsort.ranged.InsertionSortKt",
24+
main_class = "sequential.sorting.insertionsort.RangedInsertionSortKt",
2225
runtime_deps = [":insertion_sort_lib"],
2326
)

src/kotlin/sequential/sorting/insertionsort/InsertionSort.kt

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package sequential.sorting.insertionsort
22

3-
import sequential.sorting._utils.exch
3+
import _util.*
44

55

66
fun IntArray.insertionSort() {
@@ -29,5 +29,11 @@ fun IntArray.insertionSort() {
2929

3030

3131
fun main() {
32-
println("InsertionSort")
32+
val array = randomIntArray(size = 20)
33+
array.print()
34+
35+
array.insertionSort()
36+
37+
println("Sorting is successful: ${array.isSorted()}")
38+
array.print()
3339
}

src/kotlin/sequential/sorting/insertionsort/ranged/InsertionSort.kt renamed to src/kotlin/sequential/sorting/insertionsort/RangedInsertionSort.kt

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
package sequential.sorting.insertionsort.ranged
1+
package sequential.sorting.insertionsort
22

3-
import sequential.sorting._utils.exch
3+
import _util.*
44

55

66
fun IntArray.insertionSort(start: Int, end: Int) {
@@ -30,5 +30,11 @@ fun IntArray.insertionSort(start: Int, end: Int) {
3030

3131

3232
fun main() {
33-
33+
val array = randomIntArray(size = 20)
34+
array.print()
35+
36+
array.insertionSort(0, array.size)
37+
38+
println("Sorting is successful: ${array.isSorted()}")
39+
array.print()
3440
}

src/kotlin/sequential/sorting/insertionsort/_measurement/MeasuredInsertionSort.kt

Whitespace-only changes.

src/kotlin/sequential/sorting/insertionsort/_test/InsertionSortTest.kt

Whitespace-only changes.

src/kotlin/sequential/sorting/quicksort/BUILD

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,13 @@ load("@io_bazel_rules_kotlin//kotlin:kotlin.bzl", "kt_jvm_library")
22

33
kt_jvm_library(
44
name = "quicksort_lib",
5-
srcs = ["QuickSort.kt"] + glob(["partition/*.kt"]),
5+
srcs = [
6+
"QuickSort.kt",
7+
"FunctionalQuickSort.kt",
8+
] + glob(["partition/*.kt"]),
69
deps = [
710
"//sequential/sorting/insertionsort:insertion_sort_lib",
8-
"//sequential/sorting/_analysis:sort_analysis",
11+
"//sequential/sorting/_measurement",
912
"//:utils"
1013
]
1114
)
@@ -15,3 +18,9 @@ java_binary(
1518
main_class = "sequential.sorting.quicksort.QuickSortKt",
1619
runtime_deps = [":quicksort_lib"],
1720
)
21+
22+
java_binary(
23+
name = "quicksort_functional",
24+
main_class = "sequential.sorting.quicksort.FunctionalQuickSortKt",
25+
runtime_deps = [":quicksort_lib"],
26+
)
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package sequential.sorting.quicksort
2+
3+
import _util.*
4+
5+
6+
fun <T : Comparable<T>> List<T>.quickSort(): List<T> = when {
7+
size < 2 -> toList()
8+
else -> {
9+
val pivot = first()
10+
val (smaller, greater) = drop(1).partition { it <= pivot }
11+
smaller.quickSort() + pivot + greater.quickSort()
12+
}
13+
}
14+
15+
16+
fun main() {
17+
randomList(size = 20)
18+
.also(::println)
19+
.run { quickSort() }
20+
.also { println("Sorting is successful: ${it.isSorted()}") }
21+
.also(::println)
22+
}

0 commit comments

Comments
 (0)