@@ -15,9 +15,7 @@ import scala.math.Ordered.orderingToOrdered
15
15
* @see
16
16
* [[https://en.wikipedia.org/wiki/Merge_sort ]]
17
17
*/
18
- class MergeSort [A ] {
19
- import MergeSort ._
20
-
18
+ class MergeSort [A ](val smallChunkSize : Int , val smallChunkSort : FunctionalSort [A ]) {
21
19
// noinspection ScalaWeakerAccess
22
20
@ tailrec
23
21
final def merge (
@@ -38,11 +36,10 @@ class MergeSort[A] {
38
36
}
39
37
}
40
38
41
- def sortWithBuffers (
39
+ def sort (
42
40
input : Slice [A ],
43
41
primaryBuffer : WritableSlice [A ],
44
42
secondaryBuffer : WritableSlice [A ],
45
- smallChunkSort : SmallChunkSort [A ] = SmallChunkSort (chunkSize = 5 , sort = InsertionSort [A ]),
46
43
)(implicit ordering : Ordering [A ]): WritableSlice [A ] = {
47
44
require(
48
45
primaryBuffer.size == input.size,
@@ -95,46 +92,37 @@ class MergeSort[A] {
95
92
} else if (input.size == 1 ) {
96
93
primaryBuffer(0 ) = input(0 )
97
94
primaryBuffer
98
- } else if (smallChunkSort.chunkSize <= 1 ) {
95
+ } else if (smallChunkSize <= 1 ) {
99
96
sort(input, primaryBuffer, secondaryBuffer, sortedChunkSize = 1 )
100
97
} else {
101
98
val inChunks : Iterator [Slice [A ]] =
102
- input.grouped(size = smallChunkSort.chunkSize )
99
+ input.grouped(size = smallChunkSize )
103
100
val outChunks : Iterator [WritableSlice [A ]] =
104
- primaryBuffer.grouped(size = smallChunkSort.chunkSize )
101
+ primaryBuffer.grouped(size = smallChunkSize )
105
102
for ((in, out) <- inChunks zip outChunks) {
106
- smallChunkSort.sort. sortFunctionally(in, out)
103
+ smallChunkSort.sortFunctionally(in, out)
107
104
}
108
- if (smallChunkSort.chunkSize >= input.size) {
105
+ if (smallChunkSize >= input.size) {
109
106
primaryBuffer
110
107
} else {
111
108
sort(
112
109
input = primaryBuffer,
113
110
primaryBuffer = secondaryBuffer,
114
111
secondaryBuffer = primaryBuffer,
115
- sortedChunkSize = smallChunkSort.chunkSize ,
112
+ sortedChunkSize = smallChunkSize ,
116
113
)
117
114
}
118
115
}
119
116
}
120
117
121
- def sortWithBuffer (
118
+ def sort (
122
119
input : WritableSlice [A ],
123
120
buffer : WritableSlice [A ],
124
- smallChunkSort : SmallChunkSort [A ] =
125
- SmallChunkSort (chunkSize = 5 , sort = InsertionSort [A ]),
126
121
)(implicit ordering : Ordering [A ]): WritableSlice [A ] = {
127
- sortWithBuffers (
122
+ sort (
128
123
input = input,
129
124
primaryBuffer = buffer,
130
125
secondaryBuffer = input,
131
- smallChunkSort = smallChunkSort,
132
126
)
133
127
}
134
128
}
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
- }
0 commit comments