Skip to content

Commit fdbf887

Browse files
authored
Merge pull request #134 from jad-hamza/move-smartPos
Move smartPos function to use it in Stainless + Portfolio solver fix
2 parents 8709899 + f874de5 commit fdbf887

File tree

6 files changed

+55
-24
lines changed

6 files changed

+55
-24
lines changed

src/main/scala/inox/Reporter.scala

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
package inox
44

55
import utils._
6+
import Position.smartPos
67

78
abstract class DebugSection(val name: String)
89

@@ -166,20 +167,6 @@ class DefaultReporter(debugSections: Set[DebugSection]) extends Reporter(debugSe
166167
case DEBUG(_) => "["+Console.MAGENTA +" Debug "+Console.RESET+"]"
167168
}
168169

169-
def smartPos(p: Position): String = {
170-
if (p == NoPosition) {
171-
""
172-
} else {
173-
val target = p.file.getAbsolutePath()
174-
val here = new java.io.File(".").getAbsolutePath().stripSuffix(".")
175-
val diff = target.stripPrefix(here)
176-
177-
val filePos = diff+":"
178-
179-
filePos + p + ": "
180-
}
181-
}
182-
183170
def emit(msg: Message) = synchronized {
184171
println(reline(severityToPrefix(msg.severity), smartPos(msg.position) + msg.msg.toString))
185172
printLineContent(msg.position, false)

src/main/scala/inox/solvers/combinators/PortfolioSolver.scala

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,7 @@ trait PortfolioSolver extends Solver { self =>
5858
}
5959
}
6060

61-
tasks = Future.sequence(fs).map(_ => ())
62-
63-
val result = Future.find(fs.toList)(_._2 != Unknown)
64-
65-
val res = Await.result(result, Duration.Inf) match {
61+
inox.utils.FutureUtils.findFirst(fs)(_._2 != Unknown) match {
6662
case Some((s, r)) =>
6763
resultSolver = s.getResultSolver
6864
resultSolver.foreach { solv =>
@@ -72,14 +68,13 @@ trait PortfolioSolver extends Solver { self =>
7268
r
7369
case None =>
7470
reporter.debug("No solver succeeded")
75-
//fs.foreach(f => println(f.value))
7671
config.cast(Unknown)
7772
}
7873

7974
// TODO: Decide if we really want to wait for all the solvers.
8075
// I understand we interrupt them, but what if one gets stuck
8176
// fs foreach { Await.ready(_, Duration.Inf) }
82-
res
77+
// res
8378
}
8479

8580

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/* Copyright 2009-2021 EPFL, Lausanne */
2+
3+
package inox.utils
4+
5+
import scala.util._
6+
import scala.concurrent._
7+
import scala.concurrent.duration._
8+
import scala.concurrent.ExecutionContext.Implicits.global
9+
import java.util.concurrent.atomic.AtomicInteger
10+
11+
object FutureUtils {
12+
13+
def findFirst[T](futures: Seq[Future[T]])(cond: T => Boolean): Option[T] = {
14+
val p = Promise[Option[T]]()
15+
val n = futures.length
16+
val i = new AtomicInteger(0)
17+
18+
for (future <- futures) {
19+
future.onComplete((result: Try[T]) => result match {
20+
case Success(res) if cond(res) =>
21+
p.trySuccess(Some(res))
22+
case _ =>
23+
val finished = i.incrementAndGet()
24+
if (finished == n) p.trySuccess(None)
25+
})
26+
}
27+
Await.result(p.future, Duration.Inf)
28+
}
29+
30+
}

src/main/scala/inox/utils/Positions.scala

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,21 @@ abstract class Position extends Ordered[Position] {
3535
}
3636

3737
object Position {
38+
39+
def smartPos(p: Position): String = {
40+
if (p == NoPosition) {
41+
""
42+
} else {
43+
val target = p.file.getAbsolutePath()
44+
val here = new java.io.File(".").getAbsolutePath().stripSuffix(".")
45+
val diff = target.stripPrefix(here)
46+
47+
val filePos = diff+":"
48+
49+
filePos + p + ": "
50+
}
51+
}
52+
3853
def between(a: Position, b: Position): Position = {
3954
if (a.file == b.file) {
4055
if (a.line == b.line && a.col == b.col) {

src/main/scala/inox/utils/StringUtils.scala

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,17 @@ object StringUtils {
1717
}
1818

1919
def encodeByte(b: Byte): String = "\\x" + toHex((b >> 4) & 0xF) + toHex(b & 0xF)
20-
def decodeHex(s: String): Byte = ((fromHex(s.charAt(2)) << 4) + fromHex(s.charAt(3))).toByte
20+
def decodeHex(s: String): Byte = ((fromHex(s.charAt(0)) << 4) + fromHex(s.charAt(1))).toByte
2121

2222
private val hex = """^\\x[0-9A-Fa-f]{2}""".r
23+
private val uhex1 = """(?s)^\\u\{([0-9A-Fa-f])\}(.*)""".r
24+
private val uhex2 = """(?s)^\\u\{([0-9A-Fa-f]{2})\}(.*)""".r
2325

2426
object Hex {
2527
def unapply(s: String): Option[(Byte, String)] = {
2628
if (hex.findFirstIn(s).isDefined) {
2729
val (head, s2) = s.splitAt(4)
28-
Some((decodeHex(head), s2))
30+
Some((decodeHex(head.drop(2)), s2))
2931
} else {
3032
None
3133
}
@@ -44,6 +46,8 @@ object StringUtils {
4446
}
4547

4648
def decode(s: String): String = if (s.isEmpty) s else (s match {
49+
case uhex1(head, s2) => (fromHex(head.charAt(0)) & 0xF).toChar + decode(s2)
50+
case uhex2(head, s2) => (decodeHex(head) & 0xFF).toChar + decode(s2)
4751
case JavaEncoded(b, s2) => (b & 0xFF).toChar + decode(s2)
4852
case _ => s.head + decode(s.tail)
4953
})

src/main/scala/inox/utils/package.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* Copyright 2009-2018 EPFL, Lausanne */
1+
/* Copyright 2009-2021 EPFL, Lausanne */
22

33
package inox
44

0 commit comments

Comments
 (0)