Skip to content

Commit 8554de9

Browse files
committed
Refactoring
1 parent faa1aa7 commit 8554de9

File tree

5 files changed

+32
-29
lines changed

5 files changed

+32
-29
lines changed

src/main/scala/com/github/skozlov/algorithms/sort/FunctionalSort.scala

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import scala.reflect.ClassTag
77
/** Sort which puts ordered elements into the output sequence and does not
88
* modify the input sequence.
99
*/
10-
trait FunctionalSort {
10+
trait FunctionalSort[A] {
1111

1212
/** Sorts the input sequence putting elements into the output sequence. Input
1313
* sequence will not be modified.
@@ -19,7 +19,7 @@ trait FunctionalSort {
1919
* @throws IllegalArgumentException
2020
* if the input and output sequences have different sizes
2121
*/
22-
def sortFunctionally[A: Ordering](in: Slice[A], out: WritableSlice[A]): Unit
22+
def sortFunctionally(in: Slice[A], out: WritableSlice[A])(implicit ordering: Ordering[A]): Unit
2323

2424
/** Sorts the input sequence putting elements into the new array. Input
2525
* sequence will not be modified.
@@ -28,7 +28,7 @@ trait FunctionalSort {
2828
* @return
2929
* array which contains sorted elements
3030
*/
31-
def sortFunctionally[A: Ordering: ClassTag](in: Slice[A]): Array[A] = {
31+
def sortFunctionally(in: Slice[A])(implicit ordering: Ordering[A], classTag: ClassTag[A]): Array[A] = {
3232
val out = Array.ofDim[A](in.size)
3333
sortFunctionally(in, WritableSlice(out)())
3434
out

src/main/scala/com/github/skozlov/algorithms/sort/InPlaceSort.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@ package com.github.skozlov.algorithms.sort
22

33
import com.github.skozlov.commons.collection.WritableSlice
44

5-
trait InPlaceSort {
6-
def sortInPlace[A: Ordering](elements: WritableSlice[A]): Unit
5+
trait InPlaceSort[A] {
6+
def sortInPlace(elements: WritableSlice[A])(implicit ordering: Ordering[A]): Unit
77
}

src/main/scala/com/github/skozlov/algorithms/sort/InsertionSort.scala

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ import scala.math.Ordered.orderingToOrdered
1414
* @see
1515
* [[https://en.wikipedia.org/wiki/Insertion_sort]]
1616
*/
17-
object InsertionSort extends InPlaceSort with FunctionalSort with StableSort {
18-
private def sort[A: Ordering](in: Slice[A], out: WritableSlice[A]): Unit = {
17+
class InsertionSort[A] extends InPlaceSort[A] with FunctionalSort[A] with StableSort {
18+
private def sort(in: Slice[A], out: WritableSlice[A])(implicit ordering: Ordering[A]): Unit = {
1919
require(
2020
in.size == out.size,
2121
s"in and out have different sizes: ${in.size} and ${out.size}",
@@ -61,14 +61,14 @@ object InsertionSort extends InPlaceSort with FunctionalSort with StableSort {
6161
}
6262
}
6363

64-
override def sortInPlace[A: Ordering](elements: WritableSlice[A]): Unit = {
64+
override def sortInPlace(elements: WritableSlice[A])(implicit ordering: Ordering[A]): Unit = {
6565
sort(in = elements, out = elements)
6666
}
6767

68-
override def sortFunctionally[A: Ordering](
68+
override def sortFunctionally(
6969
in: Slice[A],
7070
out: WritableSlice[A],
71-
): Unit = {
71+
)(implicit ordering: Ordering[A]): Unit = {
7272
sort(in, out)
7373
}
7474
}

src/main/scala/com/github/skozlov/algorithms/sort/MergeSort.scala

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,16 @@ import scala.math.Ordered.orderingToOrdered
1515
* @see
1616
* [[https://en.wikipedia.org/wiki/Merge_sort]]
1717
*/
18-
object MergeSort {
18+
class MergeSort[A] {
19+
import MergeSort._
20+
1921
// noinspection ScalaWeakerAccess
2022
@tailrec
21-
def merge[A: Ordering](
23+
final def merge(
2224
in1: Slice[A],
2325
in2: Slice[A],
2426
out: WritableSlice[A],
25-
): Unit = {
27+
)(implicit ordering: Ordering[A]): Unit = {
2628
require(
2729
out.size == in1.size + in2.size,
2830
s"out size = ${out.size} is not sum of in1 size = ${in1.size} and in2 size = ${in2.size}",
@@ -36,17 +38,12 @@ object MergeSort {
3638
}
3739
}
3840

39-
class SmallChunkSort(val chunkSize: Int, val sort: FunctionalSort) {
40-
require(chunkSize >= 0, "Negative chunk size: " + chunkSize)
41-
}
42-
43-
def sortWithBuffers[A: Ordering](
41+
def sortWithBuffers(
4442
input: Slice[A],
4543
primaryBuffer: WritableSlice[A],
4644
secondaryBuffer: WritableSlice[A],
47-
smallChunkSort: SmallChunkSort =
48-
SmallChunkSort(chunkSize = 5, sort = InsertionSort),
49-
): WritableSlice[A] = {
45+
smallChunkSort: SmallChunkSort[A] = SmallChunkSort(chunkSize = 5, sort = InsertionSort[A]),
46+
)(implicit ordering: Ordering[A]): WritableSlice[A] = {
5047
require(
5148
primaryBuffer.size == input.size,
5249
s"Size mismatch: input = ${input.size}, primary buffer = ${primaryBuffer.size}",
@@ -121,12 +118,12 @@ object MergeSort {
121118
}
122119
}
123120

124-
def sortWithBuffer[A: Ordering](
121+
def sortWithBuffer(
125122
input: WritableSlice[A],
126123
buffer: WritableSlice[A],
127-
smallChunkSort: SmallChunkSort =
128-
SmallChunkSort(chunkSize = 5, sort = InsertionSort),
129-
): WritableSlice[A] = {
124+
smallChunkSort: SmallChunkSort[A] =
125+
SmallChunkSort(chunkSize = 5, sort = InsertionSort[A]),
126+
)(implicit ordering: Ordering[A]): WritableSlice[A] = {
130127
sortWithBuffers(
131128
input = input,
132129
primaryBuffer = buffer,
@@ -135,3 +132,9 @@ object MergeSort {
135132
)
136133
}
137134
}
135+
136+
object MergeSort {
137+
class SmallChunkSort[A](val chunkSize: Int, val sort: FunctionalSort[A]) {
138+
require(chunkSize >= 0, "Negative chunk size: " + chunkSize)
139+
}
140+
}

src/test/scala/com/github/skozlov/algorithms/sort/SortTest.scala

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ class SortTest extends Test {
4040
}
4141
}
4242

43-
private def testInPlaceSort(sort: InPlaceSort): Unit = {
43+
private def testInPlaceSort(sort: InPlaceSort[(Int, Int)]): Unit = {
4444
val arrays: Seq[Array[(Int, Int)]] = cases map { _.toArray }
4545
val results: Seq[Seq[(Int, Int)]] = for (array <- arrays) yield {
4646
sort.sortInPlace(WritableSlice(array)())
@@ -49,7 +49,7 @@ class SortTest extends Test {
4949
checkResults(results, sort.isInstanceOf[StableSort])
5050
}
5151

52-
private def testFunctionalSort(sort: FunctionalSort): Unit = {
52+
private def testFunctionalSort(sort: FunctionalSort[(Int, Int)]): Unit = {
5353
val inputsAndOutputsAfterSort: Seq[(Array[(Int, Int)], Array[(Int, Int)])] =
5454
for {
5555
_case: Seq[(Int, Int)] <- cases
@@ -64,7 +64,7 @@ class SortTest extends Test {
6464
}
6565

6666
test("insertion sort") {
67-
testInPlaceSort(InsertionSort)
68-
testFunctionalSort(InsertionSort)
67+
testInPlaceSort(InsertionSort())
68+
testFunctionalSort(InsertionSort())
6969
}
7070
}

0 commit comments

Comments
 (0)