Skip to content

Commit

Permalink
Policy: add methods asAfter and asUntil
Browse files Browse the repository at this point in the history
They assume the policy is enclosed in a Delay or Expire and simplify
itself by removing redundant components.

This also potentially leads to checks such as `terminal` and `maxEndPos`
returning a different value and possibly leading to a different format.
  • Loading branch information
kitbellew committed Dec 21, 2024
1 parent 62fccd9 commit feba1ce
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ abstract class Policy {
def noDequeue: Boolean
def maxEndPos: End.WithPos

protected def asAfter(pos: End.WithPos): Policy
protected def asUntil(pos: End.WithPos): Policy

def filter(pred: Clause => Boolean): Policy
def exists(pred: Clause => Boolean): Boolean
def appliesUntil(nextft: FT)(pred: Clause => Boolean): Boolean
Expand All @@ -32,9 +35,8 @@ abstract class Policy {
if (other.isEmpty) this else new AndThen(this, other)
def |(other: Policy): Policy =
if (other.isEmpty) this else new OrElse(this, other)
def ==>(other: Policy): Policy =
if (other.isEmpty) this else new Relay(this, other)
def <==(pos: End.WithPos): Policy = new Expire(this, pos)
def ==>(other: Policy): Policy = Relay(this, other)
def <==(pos: End.WithPos): Policy = Expire(this, pos)
def ?(flag: Boolean): Policy = if (flag) this else NoPolicy

@inline
Expand Down Expand Up @@ -85,6 +87,9 @@ object Policy {
override def noDequeue: Boolean = false
override def maxEndPos: End.WithPos = End.Never

override protected def asAfter(pos: End.WithPos): Policy = this
override protected def asUntil(pos: End.WithPos): Policy = this

override def unexpired(split: Split, nextft: FT): Policy = this
override def appliesUntil(nextft: FT)(pred: Clause => Boolean): Boolean =
false
Expand Down Expand Up @@ -174,6 +179,9 @@ object Policy {
s"$prefixWithColon[$fl]${noDeqPrefix}d$suffixWithColon"
}

override protected def asAfter(pos: End.WithPos): Policy = this
override protected def asUntil(pos: End.WithPos): Policy = this

override def unexpired(split: Split, nextft: FT): Policy = this

override def filter(pred: Clause => Boolean): Policy =
Expand Down Expand Up @@ -202,6 +210,11 @@ object Policy {
extends Clause

abstract class WithConv extends Policy {
override protected def asAfter(pos: End.WithPos): Policy =
conv(_.asAfter(pos))
override protected def asUntil(pos: End.WithPos): Policy =
conv(_.asUntil(pos))

override def unexpired(split: Split, nextft: FT): Policy =
conv(_.unexpired(split, nextft))

Expand Down Expand Up @@ -288,7 +301,15 @@ object Policy {
p2.exists(pred)
}

private class Expire(policy: Policy, endPolicy: End.WithPos)
private object Expire {
def apply(policy: Policy, endPolicy: End.WithPos): Expire = policy
.asUntil(endPolicy) match {
case p: Expire => p
case p => new Expire(p, endPolicy)
}
}

private class Expire private (policy: Policy, endPolicy: End.WithPos)
extends WithConv {
override def toString: String = s"$policy <== $endPolicy"

Expand All @@ -298,13 +319,15 @@ object Policy {
override def noDequeue: Boolean = policy.noDequeue
override def maxEndPos: End.WithPos = endPolicy.min(policy.maxEndPos)

override def <==(pos: End.WithPos): Policy =
if (pos >= endPolicy) this else policy <== pos
override protected def asAfter(pos: End.WithPos): Policy =
if (pos >= endPolicy) NoPolicy
else {
val p = policy.asAfter(pos)
if (p eq policy) this else new Expire(p, endPolicy)
}
override protected def asUntil(pos: End.WithPos): Policy =
if (pos >= endPolicy) this else policy.asUntil(pos)

override def switch(trigger: T, on: Boolean): Policy = {
val res = policy.switch(trigger, on)
if (res eq policy) this else res <== endPolicy
}
override def unexpired(split: Split, nextft: FT): Policy =
if (!endPolicy.notExpiredBy(nextft)) NoPolicy
else super.unexpired(split, nextft)
Expand All @@ -325,7 +348,16 @@ object Policy {
override def exists(pred: Clause => Boolean): Boolean = policy.exists(pred)
}

private class Delay(policy: Policy, begPolicy: End.WithPos) extends Policy {
private object Delay {
def apply(policy: Policy, begPolicy: End.WithPos): Delay = policy
.asAfter(begPolicy) match {
case p: Delay => p
case p => new Delay(p, begPolicy)
}
}

private class Delay private (val policy: Policy, val begPolicy: End.WithPos)
extends Policy {
override def toString: String = s"$begPolicy ==> $policy"

override def f: Pf = PartialFunction.empty
Expand All @@ -335,7 +367,18 @@ object Policy {
override def maxEndPos: End.WithPos = begPolicy.max(policy.maxEndPos)

override def <==(pos: End.WithPos): Policy =
if (pos <= begPolicy) NoPolicy else new Expire(this, pos)
if (pos <= begPolicy) NoPolicy else Expire(this, pos)
override def ==>(other: Policy): Policy =
Relay(this, other.asAfter(begPolicy))

override protected def asAfter(pos: End.WithPos): Policy =
if (pos <= begPolicy) this else policy.asAfter(pos)
override protected def asUntil(pos: End.WithPos): Policy =
if (pos <= begPolicy) NoPolicy
else {
val p = policy.asUntil(pos)
if (p eq policy) this else new Delay(p, begPolicy)
}

override def filter(pred: Clause => Boolean): Policy = this
override def exists(pred: Clause => Boolean): Boolean = policy.exists(pred)
Expand Down Expand Up @@ -381,7 +424,12 @@ object Policy {
.exists(pred) || after.exists(pred)
}

private class Relay(val before: Policy, val after: Policy)
private object Relay {
def apply(before: Policy, after: Policy): Policy =
if (after.isEmpty) before else new Relay(before, after)
}

private class Relay private (val before: Policy, val after: Policy)
extends WithBeforeAndAfter {
override def toString: String = s"$before ==> $after"
override protected def withBefore(before: Policy)(
Expand Down Expand Up @@ -480,8 +528,7 @@ object Policy {
sealed trait WithPos extends Ordered[WithPos] {
val endIdx: Int
def notExpiredBy(ft: FT): Boolean = ft.idx <= endIdx
def ==>(policy: Policy): Policy =
if (policy.isEmpty) NoPolicy else new Delay(policy, this)
def ==>(policy: Policy): Policy = Delay(policy, this)
override final def compare(that: WithPos): Int = endIdx - that.endIdx
def max(that: WithPos): WithPos = if (this < that) that else this
def min(that: WithPos): WithPos = if (this > that) that else this
Expand Down
30 changes: 16 additions & 14 deletions scalafmt-tests/shared/src/test/resources/newlines/source_fold.stat
Original file line number Diff line number Diff line change
Expand Up @@ -1940,8 +1940,9 @@ val strings = Seq(
s" $bestEPStr",
"Metrics:",
s" $metricHeader: ${bestScore.score}"
) ++ otherMetricHeaders.zip(bestScore.otherScores)
.map { case (h, s) => s" $h: $s" } ++ outputPath.toSeq.map { p =>
) ++ otherMetricHeaders.zip(bestScore.otherScores).map { case (h, s) =>
s" $h: $s"
} ++ outputPath.toSeq.map { p =>
s"The best variant params can be found in $p"
}
<<< 8.6: assignment with short expression
Expand Down Expand Up @@ -6723,9 +6724,9 @@ if (
}
>>>
if (
settings.fuzzingMode &&
!system.settings.config
.hasPath("akka.stream.secret-test-fuzzing-warning-disable")
settings.fuzzingMode && !system.settings.config.hasPath(
"akka.stream.secret-test-fuzzing-warning-disable"
)
) {
//
}
Expand Down Expand Up @@ -6821,7 +6822,7 @@ object a {
}
))
}
>>> { stateVisits = 2039, stateVisits2 = 2039 }
>>> { stateVisits = 2070, stateVisits2 = 2070 }
object a {
buffer.append((
if (!fetchContent) {
Expand Down Expand Up @@ -9068,13 +9069,14 @@ val stats = Profiler.getStatistics().asScala.toSeq.map {
case (trace, count) => MethodCallTrace(trace.className, trace.methodName, trace.methodDescriptor) -> count.intValue
}
>>>
val stats = Profiler.getStatistics().asScala.toSeq.map {
case (trace, count) => MethodCallTrace(
val stats = Profiler.getStatistics().asScala.toSeq
.map { case (trace, count) =>
MethodCallTrace(
trace.className,
trace.methodName,
trace.methodDescriptor
) -> count.intValue
}
}
<<< #4133 partial function within apply, short
rdd.map(
{case (id, count) => (count, id)})
Expand Down Expand Up @@ -10061,7 +10063,7 @@ system.dynamicAccess.createInstanceFor[Serializer](fqn, Nil).recoverWith {
}
}
}
>>> { stateVisits = 6655, stateVisits2 = 6655 }
>>> { stateVisits = 3689, stateVisits2 = 3689 }
system.dynamicAccess.createInstanceFor[Serializer](fqn, Nil).recoverWith {
case _: NoSuchMethodException => system.dynamicAccess
.createInstanceFor[Serializer](
Expand Down Expand Up @@ -10568,10 +10570,10 @@ object a {
f) + ", " + pool(nameAndTypeRef)
}
}
>>> { stateVisits = 194, stateVisits2 = 194 }
>>> { stateVisits = 161, stateVisits2 = 161 }
object a {
def memberRef(description: String) = u2 ~ u2 ^^
add1 { case classRef ~ nameAndTypeRef =>
def memberRef(description: String) = u2 ~ u2 ^^ add1 {
case classRef ~ nameAndTypeRef =>
pool => description + ": " + pool(classRe f) + ", " + pool(nameAndTypeRef)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ class FormatTests extends FunSuite with CanRunTests with FormatAssertions {
val explored = Debug.explored.get()
logger.debug(s"Total explored: $explored")
if (!onlyUnit && !onlyManual)
assertEquals(explored, 1210389, "total explored")
assertEquals(explored, 1202529, "total explored")
val results = debugResults.result()
// TODO(olafur) don't block printing out test results.
// I don't want to deal with scalaz's Tasks :'(
Expand Down

0 comments on commit feba1ce

Please sign in to comment.