967
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
1 |
// Christian's Solution for CW 1
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
2 |
import scala.language.implicitConversions
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
3 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
4 |
// basic regular expressions
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
5 |
abstract class Rexp
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
6 |
case object ZERO extends Rexp
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
7 |
case object ONE extends Rexp
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
8 |
case class CHAR(c: Char) extends Rexp
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
9 |
case class ALT(r1: Rexp, r2: Rexp) extends Rexp
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
10 |
case class SEQ(r1: Rexp, r2: Rexp) extends Rexp
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
11 |
case class STAR(r: Rexp) extends Rexp
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
12 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
13 |
// extended regular expressions
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
14 |
case class RANGE(cs: Set[Char]) extends Rexp // set of characters
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
15 |
case class PLUS(r: Rexp) extends Rexp // plus
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
16 |
case class OPTIONAL(r: Rexp) extends Rexp // optional
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
17 |
case class INTER(r1: Rexp, r2: Rexp) extends Rexp // intersection
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
18 |
case class NTIMES(r: Rexp, n: Int) extends Rexp // n-times
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
19 |
case class UPTO(r: Rexp, n: Int) extends Rexp // up n-times
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
20 |
case class FROM(r: Rexp, n: Int) extends Rexp // from n-times
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
21 |
case class BETWEEN(r: Rexp, n: Int, m: Int) extends Rexp // between nm-times
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
22 |
case class NOT(r: Rexp) extends Rexp // not
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
23 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
24 |
// general version of CHAR, RANGE and ALL
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
25 |
case class CFUN(f: Char => Boolean) extends Rexp // subsuming CHAR and RANGE
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
26 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
27 |
def FCHAR(c: Char) = CFUN(c == _)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
28 |
val FALL = CFUN(_ => true)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
29 |
def FRANGE(cs: Set[Char]) = CFUN(cs.contains(_))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
30 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
31 |
// the nullable function: tests whether the regular
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
32 |
// expression can recognise the empty string
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
33 |
def nullable (r: Rexp) : Boolean = r match {
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
34 |
case ZERO => false
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
35 |
case ONE => true
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
36 |
case CHAR(_) => false
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
37 |
case ALT(r1, r2) => nullable(r1) || nullable(r2)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
38 |
case SEQ(r1, r2) => nullable(r1) && nullable(r2)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
39 |
case STAR(_) => true
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
40 |
case RANGE(_) => false
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
41 |
case PLUS(r) => nullable(r)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
42 |
case OPTIONAL(_) => true
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
43 |
case INTER(r1, r2) => nullable(r1) && nullable(r2)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
44 |
case NTIMES(r, n) => if (n == 0) true else nullable(r)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
45 |
case UPTO(_, _) => true
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
46 |
case FROM(r, n) => if (n == 0) true else nullable(r)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
47 |
case BETWEEN(r, n, m) => if (n == 0) true else nullable(r)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
48 |
case NOT(r) => !nullable(r)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
49 |
case CFUN(_) => false
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
50 |
}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
51 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
52 |
// the derivative of a regular expression w.r.t. a character
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
53 |
def der(c: Char, r: Rexp) : Rexp = r match {
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
54 |
case ZERO => ZERO
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
55 |
case ONE => ZERO
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
56 |
case CHAR(d) => if (c == d) ONE else ZERO
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
57 |
case ALT(r1, r2) => ALT(der(c, r1), der(c, r2))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
58 |
case SEQ(r1, r2) =>
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
59 |
if (nullable(r1)) ALT(SEQ(der(c, r1), r2), der(c, r2))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
60 |
else SEQ(der(c, r1), r2)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
61 |
case STAR(r1) => SEQ(der(c, r1), STAR(r1))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
62 |
case RANGE(cs) => if (cs contains c) ONE else ZERO
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
63 |
case PLUS(r) => SEQ(der(c, r), STAR(r))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
64 |
case OPTIONAL(r) => der(c, r)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
65 |
case INTER(r1, r2) => INTER(der(c, r1), der(c, r2))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
66 |
case NTIMES(r, n) =>
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
67 |
if (n == 0) ZERO else SEQ(der(c, r), NTIMES(r, n - 1))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
68 |
case UPTO(r, n) =>
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
69 |
if (n == 0) ZERO else SEQ(der(c, r), UPTO(r, n - 1))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
70 |
case FROM(r, n) =>
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
71 |
if (n == 0) SEQ(der(c, r), STAR(r)) else SEQ(der(c, r), FROM(r, n - 1))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
72 |
case BETWEEN(r, n, m) =>
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
73 |
if (m < n) ZERO else
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
74 |
if (n == 0 && m == 0) ZERO else
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
75 |
if (n == 0) SEQ(der(c, r), UPTO(r, m - 1))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
76 |
else SEQ(der(c, r), BETWEEN(r, n - 1, m - 1))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
77 |
case NOT(r) => NOT(der (c, r))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
78 |
case CFUN(f) => if (f(c)) ONE else ZERO
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
79 |
}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
80 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
81 |
// simplification
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
82 |
def simp(r: Rexp) : Rexp = r match {
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
83 |
case ALT(r1, r2) => (simp(r1), simp(r2)) match {
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
84 |
case (ZERO, r2s) => r2s
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
85 |
case (r1s, ZERO) => r1s
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
86 |
case (r1s, r2s) => if (r1s == r2s) r1s else ALT (r1s, r2s)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
87 |
}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
88 |
case SEQ(r1, r2) => (simp(r1), simp(r2)) match {
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
89 |
case (ZERO, _) => ZERO
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
90 |
case (_, ZERO) => ZERO
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
91 |
case (ONE, r2s) => r2s
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
92 |
case (r1s, ONE) => r1s
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
93 |
case (r1s, r2s) => SEQ(r1s, r2s)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
94 |
}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
95 |
case r => r
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
96 |
}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
97 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
98 |
// the derivative w.r.t. a string (iterates der)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
99 |
def ders(s: List[Char], r: Rexp) : Rexp = s match {
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
100 |
case Nil => r
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
101 |
case c::s => ders(s, simp(der(c, r)))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
102 |
}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
103 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
104 |
// the main matcher function
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
105 |
def matcher(r: Rexp, s: String) : Boolean =
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
106 |
nullable(ders(s.toList, r))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
107 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
108 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
109 |
// Test Cases
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
110 |
//============
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
111 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
112 |
// some syntactic convenience
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
113 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
114 |
def charlist2rexp(s: List[Char]) : Rexp = s match {
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
115 |
case Nil => ONE
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
116 |
case c::Nil => CHAR(c)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
117 |
case c::s => SEQ(CHAR(c), charlist2rexp(s))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
118 |
}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
119 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
120 |
given Conversion[String, Rexp] = (s => charlist2rexp(s.toList))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
121 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
122 |
extension (r: Rexp) {
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
123 |
def ~ (s: Rexp) = SEQ(r, s)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
124 |
def % = STAR(r)
|
968
|
125 |
def | (s: Rexp) = ALT(r, s)
|
967
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
126 |
}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
127 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
128 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
129 |
println("EMAIL:")
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
130 |
val LOWERCASE = ('a' to 'z').toSet
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
131 |
val DIGITS = ('0' to '9').toSet
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
132 |
val SYMBOLS1 = ("_.-").toSet
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
133 |
val SYMBOLS2 = (".-").toSet
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
134 |
val EMAIL = { PLUS(CFUN(LOWERCASE | DIGITS | SYMBOLS1)) ~ "@" ~
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
135 |
PLUS(CFUN(LOWERCASE | DIGITS | SYMBOLS2)) ~ "." ~
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
136 |
BETWEEN(CFUN(LOWERCASE | Set('.')), 2, 6) }
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
137 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
138 |
val my_email = "christian.urban@kcl.ac.uk"
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
139 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
140 |
println(EMAIL);
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
141 |
println(matcher(EMAIL, my_email))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
142 |
println(ders(my_email.toList,EMAIL))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
143 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
144 |
val ALL = CFUN((c:Char) => true)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
145 |
val COMMENT = "/*" ~ (NOT(ALL.% ~ "*/" ~ ALL.%)) ~ " * /"
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
146 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
147 |
println(matcher(COMMENT, "/**/"))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
148 |
println(matcher(COMMENT, "/*foobar*/"))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
149 |
println(matcher(COMMENT, "/*test*/test*/"))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
150 |
println(matcher(COMMENT, "/*test/*test*/"))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
151 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
152 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
153 |
println("\n\nTEST TEST\n")
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
154 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
155 |
val r1 = PLUS(PLUS(SEQ(CHAR('a'), SEQ(CHAR('a'), CHAR('a')))))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
156 |
val r2 = PLUS(PLUS(SEQ(BETWEEN(CHAR('a'), 19, 19), OPTIONAL(CHAR('a')))))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
157 |
val s1 = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
158 |
val s2 = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
159 |
val s3 = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
160 |
for (s <- List(s1,s2,s3)) {
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
161 |
println(matcher(r1, s))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
162 |
println(matcher(r2, s))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
163 |
}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
164 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
165 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
166 |
// for measuring time
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
167 |
def time_needed[T](i: Int, code: => T) = {
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
168 |
val start = System.nanoTime()
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
169 |
for (j <- 1 to i) code
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
170 |
val end = System.nanoTime()
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
171 |
(end - start) / (i * 1.0e9)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
172 |
}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
173 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
174 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
175 |
//@main
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
176 |
def test(file: String) = {
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
177 |
println("Test a{n}")
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
178 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
179 |
for (i <- 0 to 200000 by 5000) {
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
180 |
val re = NTIMES(SEQ(SEQ(CHAR('a'), CHAR('b')), CHAR('c')), i)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
181 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
182 |
print(f"$i: ${time_needed(2, matcher(re, "abc" * i))}%.5f")
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
183 |
println(s" ${matcher(re, "abcd" * i)}")
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
184 |
}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
185 |
}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
186 |
|