-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #22 from j-mie6/refactor
Refactor
- Loading branch information
Showing
23 changed files
with
896 additions
and
967 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,7 +3,7 @@ on: [push, pull_request] | |
env: | ||
CI: true | ||
CI_SNAPSHOT_RELEASE: +publishSigned | ||
SCALA_VERSION: 2.12.12 | ||
SCALA_VERSION: 2.13.4 | ||
jobs: | ||
validate: | ||
name: Scala ${{ matrix.scala }}, Java ${{ matrix.java }} | ||
|
@@ -34,3 +34,27 @@ jobs: | |
run: sbt ++$SCALA_VERSION test | ||
- name: Scaladoc | ||
run: sbt ++$SCALA_VERSION doc | ||
coverage: | ||
needs: [validate] | ||
name: Test Coverage | ||
runs-on: ubuntu-20.04 | ||
steps: | ||
- uses: actions/[email protected] | ||
- uses: olafurpg/setup-scala@v10 | ||
with: | ||
java-version: [email protected] | ||
- uses: actions/cache@v1 | ||
with: | ||
path: ~/.cache/coursier | ||
key: sbt-coursier-cache | ||
- uses: actions/cache@v1 | ||
with: | ||
path: ~/.sbt | ||
key: sbt-${{ hashFiles('**/build.sbt') }} | ||
- run: sbt clean coverage test | ||
- uses: paambaati/[email protected] | ||
env: | ||
CC_TEST_REPORTER_ID: ${{ secrets.CC_TEST_REPORTER_ID }} | ||
with: | ||
coverageCommand: sbt coverageReport | ||
coverageLocations: ${{github.workspace}}/target/scala-2.13/coverage-report/cobertura.xml:cobertura |
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 |
---|---|---|
|
@@ -8,6 +8,6 @@ target/ | |
.cache-main | ||
.classpath | ||
.project | ||
.jvmopts | ||
*.class | ||
*.log | ||
parsley*.jar |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package parsley.internal | ||
|
||
import Radix.Entry | ||
import scala.collection.mutable | ||
import scala.language.implicitConversions | ||
|
||
class Radix[A] { | ||
private var x = Option.empty[A] | ||
private val m = mutable.Map.empty[Char, Entry[A]] | ||
|
||
def get(key: String): Option[A] = { | ||
if (key.isEmpty) x | ||
else for | ||
{ | ||
e <- m.get(key.head) | ||
if key.startsWith(e.prefix) | ||
v <- e.radix.get(key.drop(e.prefix.length)) | ||
} yield v | ||
} | ||
|
||
def isEmpty: Boolean = x.isEmpty && m.isEmpty | ||
def nonEmpty: Boolean = !isEmpty | ||
|
||
def suffixes(c: Char): Radix[A] = m.get(c) match { | ||
case Some(e) => | ||
// We have to form a new root | ||
if (e.prefix.length > 1) Radix(new Entry(e.prefix.tail, e.radix)) | ||
else e.radix | ||
case None => Radix.empty | ||
} | ||
|
||
def contains(key: String): Boolean = get(key).nonEmpty | ||
def apply(key: String): A = get(key).getOrElse(throw new NoSuchElementException(key)) | ||
|
||
def update(key: String, value: A): Unit = | ||
if (key.isEmpty) x = Some(value) | ||
else { | ||
val e = m.getOrElseUpdate(key.head, new Entry(key, Radix.empty[A])) | ||
if (key.startsWith(e.prefix)) e.radix(key.drop(e.prefix.length)) = value | ||
else { | ||
// Need to split the tree: find their common prefix first | ||
val common = key.view.zip(e.prefix).takeWhile(Function.tupled(_ == _)).map(_._1).mkString | ||
e.dropInPlace(common.length) | ||
val radix = Radix(e) | ||
// Continue inserting the key | ||
radix(key.drop(common.length)) = value | ||
// Insert our new entry | ||
m(common.head) = new Entry(common, radix) | ||
} | ||
} | ||
} | ||
|
||
object Radix { | ||
def empty[A]: Radix[A] = new Radix | ||
|
||
private def apply[A](e: Entry[A]): Radix[A] = { | ||
val radix = empty[A] | ||
radix.m(e.prefix.head) = e | ||
radix | ||
} | ||
|
||
def apply[A](xs: Iterable[String]): Radix[Unit] = { | ||
val r = Radix.empty[Unit] | ||
for (x <- xs) r(x) = () | ||
r | ||
} | ||
|
||
private class Entry[A](var prefix: String, val radix: Radix[A]) { | ||
def dropInPlace(n: Int): Unit = prefix = prefix.drop(n) | ||
} | ||
} |
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.