-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
319 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
src/main/scala/com/github/skozlov/algorithms/sort/InPlaceSort.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
package com.github.skozlov.algorithms.sort | ||
|
||
import scala.collection.mutable | ||
import com.github.skozlov.commons.collection.WritableSlice | ||
|
||
trait InPlaceSort { | ||
def sortInPlace[A: Ordering](elements: mutable.IndexedSeq[A]): Unit | ||
def sortInPlace[A: Ordering](elements: WritableSlice[A]): Unit | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
106 changes: 106 additions & 0 deletions
106
src/main/scala/com/github/skozlov/commons/collection/Slice.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
package com.github.skozlov.commons.collection | ||
|
||
import com.github.skozlov.commons.collection.SliceOps.checkBounds | ||
|
||
import scala.collection.IndexedSeqView | ||
import scala.collection.IndexedSeqView.SomeIndexedSeqOps | ||
|
||
trait SliceOps[ | ||
A, | ||
Underlying <: SomeIndexedSeqOps[A], | ||
+SubSlice <: SliceOps[A, Underlying, SubSlice], | ||
] extends IndexedSeqView[A] { | ||
this: SubSlice => | ||
|
||
import SliceOps._ | ||
|
||
def underlying: Underlying | ||
def from: Int | ||
def until: Int | ||
|
||
override val length: Int = until - from | ||
|
||
// noinspection ScalaWeakerAccess | ||
@throws[IndexOutOfBoundsException] | ||
def indexToUnderlying(i: Int): Int = { | ||
if (i < 0 || i >= length) { | ||
throw IndexOutOfBoundsException( | ||
s"$i is out of bounds (min 0, max ${length - 1})" | ||
) | ||
} | ||
from + i | ||
} | ||
|
||
override def apply(i: Int): A = underlying(indexToUnderlying(i)) | ||
|
||
protected def newSubSlice( | ||
underlying: Underlying, | ||
from: Int, | ||
until: Int, | ||
): SubSlice | ||
|
||
override def slice(from: Int, until: Int): SubSlice = { | ||
if (from == 0 && until == this.length) { | ||
this | ||
} else { | ||
checkBounds(this, from, until) | ||
newSubSlice(underlying, this.from + from, this.from + until) | ||
} | ||
} | ||
|
||
override def take(n: Int): SubSlice = { | ||
slice(0, math.min(math.max(n, 0), length)) | ||
} | ||
|
||
override def drop(n: Int): SubSlice = { | ||
slice(math.min(math.max(n, 0), length), length) | ||
} | ||
|
||
override def takeRight(n: Int): SubSlice = drop(length - math.max(n, 0)) | ||
|
||
override def dropRight(n: Int): SubSlice = take(length - math.max(n, 0)) | ||
} | ||
|
||
object SliceOps { | ||
// noinspection ScalaWeakerAccess | ||
@throws[IllegalArgumentException] | ||
def checkBounds[A]( | ||
underlying: SomeIndexedSeqOps[A], | ||
from: Int, | ||
until: Int, | ||
): Unit = { | ||
require(from >= 0, s"Negative `from`: $from") | ||
require(from <= until, s"`from` ($from) is greater than `until` ($until)") | ||
require( | ||
until <= underlying.length, | ||
s"`until` ($until) is greater than underlying length (${underlying.length})", | ||
) | ||
} | ||
} | ||
|
||
type Slice[A] = SliceOps[A, _ <: SomeIndexedSeqOps[A], _ <: SliceOps[A, _, _]] | ||
|
||
object Slice { | ||
class Impl[A]( | ||
override val underlying: SomeIndexedSeqOps[A], | ||
override val from: Int, | ||
override val until: Int, | ||
) extends SliceOps[A, SomeIndexedSeqOps[A], Impl[A]] { | ||
|
||
checkBounds(underlying, from, until) | ||
|
||
override protected def newSubSlice( | ||
underlying: SomeIndexedSeqOps[A], | ||
from: Int, | ||
until: Int, | ||
): Impl[A] = { | ||
Impl(underlying, from, until) | ||
} | ||
} | ||
|
||
def apply[A]( | ||
underlying: SomeIndexedSeqOps[A] | ||
)(from: Int = 0, until: Int = underlying.length): Slice[A] = { | ||
Impl(underlying, from, until) | ||
} | ||
} |
49 changes: 49 additions & 0 deletions
49
src/main/scala/com/github/skozlov/commons/collection/WritableSlice.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package com.github.skozlov.commons.collection | ||
|
||
import com.github.skozlov.commons.collection.SliceOps.checkBounds | ||
|
||
import scala.collection.{View, mutable} | ||
|
||
trait WritableSliceOps[ | ||
A, | ||
Underlying <: SomeMutableIndexedSeqOps[A], | ||
+SubSlice <: WritableSliceOps[A, Underlying, SubSlice], | ||
] extends SliceOps[A, Underlying, SubSlice] | ||
with mutable.IndexedSeqOps[A, View, View[A]] { | ||
this: SubSlice => | ||
|
||
override def update(idx: Int, elem: A): Unit = { | ||
underlying(indexToUnderlying(idx)) = elem | ||
} | ||
} | ||
|
||
type WritableSlice[A] = WritableSliceOps[ | ||
A, | ||
_ <: SomeMutableIndexedSeqOps[A], | ||
_ <: WritableSliceOps[A, _, _], | ||
] | ||
|
||
object WritableSlice { | ||
class Impl[A]( | ||
override val underlying: SomeMutableIndexedSeqOps[A], | ||
override val from: Int, | ||
override val until: Int, | ||
) extends WritableSliceOps[A, SomeMutableIndexedSeqOps[A], Impl[A]] { | ||
|
||
checkBounds(underlying, from, until) | ||
|
||
override protected def newSubSlice( | ||
underlying: SomeMutableIndexedSeqOps[A], | ||
from: Int, | ||
until: Int, | ||
): Impl[A] = { | ||
Impl(underlying, from, until) | ||
} | ||
} | ||
|
||
def apply[A]( | ||
underlying: SomeMutableIndexedSeqOps[A] | ||
)(from: Int = 0, until: Int = underlying.length): WritableSlice[A] = { | ||
Impl(underlying, from, until) | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
src/main/scala/com/github/skozlov/commons/collection/package.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package com.github.skozlov.commons | ||
|
||
import scala.collection.mutable | ||
|
||
package object collection { | ||
type SomeMutableIndexedSeqOps[A] = mutable.IndexedSeqOps[A, AnyConstr, _] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package com.github.skozlov | ||
|
||
package object commons { | ||
type AnyConstr[X] = Any | ||
} |
2 changes: 1 addition & 1 deletion
2
.../com/github/skozlov/algorithms/Test.scala → src/test/scala/com/github/skozlov/Test.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.