equal
deleted
inserted
replaced
1 |
1 // Version which attempts to move whole strings, not |
|
2 // just characters, under derivatives whenever possible |
|
3 |
2 abstract class Rexp |
4 abstract class Rexp |
3 case object ZERO extends Rexp |
5 case object ZERO extends Rexp |
4 case object ONE extends Rexp |
6 case object ONE extends Rexp |
5 case class CHAR(c: Char) extends Rexp |
7 case class CHAR(c: Char) extends Rexp |
6 case class ALT(r1: Rexp, r2: Rexp) extends Rexp |
8 case class ALT(r1: Rexp, r2: Rexp) extends Rexp |
48 case (r1s, r2s) => SEQ(r1s, r2s) |
50 case (r1s, r2s) => SEQ(r1s, r2s) |
49 } |
51 } |
50 case r => r |
52 case r => r |
51 } |
53 } |
52 |
54 |
|
55 // *new* |
53 // derivative w.r.t. a string (iterates der) |
56 // derivative w.r.t. a string (iterates der) |
54 def ders2(s: List[Char], r: Rexp) : Rexp = (s, r) match { |
57 def ders2(s: List[Char], r: Rexp) : Rexp = (s, r) match { |
55 case (Nil, r) => r |
58 case (Nil, r) => r |
56 case (s, ZERO) => ZERO |
59 case (s, ZERO) => ZERO |
57 case (s, ONE) => if (s == Nil) ONE else ZERO |
60 case (s, ONE) => if (s == Nil) ONE else ZERO |
66 |
69 |
67 |
70 |
68 //one or zero |
71 //one or zero |
69 def OPT(r: Rexp) = ALT(r, ONE) |
72 def OPT(r: Rexp) = ALT(r, ONE) |
70 |
73 |
|
74 |
|
75 // Test Cases |
|
76 |
71 def EVIL1(n: Int) = SEQ(NTIMES(OPT(CHAR('a')), n), NTIMES(CHAR('a'), n)) |
77 def EVIL1(n: Int) = SEQ(NTIMES(OPT(CHAR('a')), n), NTIMES(CHAR('a'), n)) |
72 val EVIL2 = SEQ(STAR(STAR(CHAR('a'))), CHAR('b')) |
78 val EVIL2 = SEQ(STAR(STAR(CHAR('a'))), CHAR('b')) |
73 |
79 |
74 |
80 |
75 def time_needed[T](i: Int, code: => T) = { |
81 def time_needed[T](i: Int, code: => T) = { |
77 for (j <- 1 to i) code |
83 for (j <- 1 to i) code |
78 val end = System.nanoTime() |
84 val end = System.nanoTime() |
79 (end - start)/(i * 1.0e9) |
85 (end - start)/(i * 1.0e9) |
80 } |
86 } |
81 |
87 |
82 // warmup |
|
83 //test: (a?{n}) (a{n}) |
88 //test: (a?{n}) (a{n}) |
84 for (i <- 1 to 7000001 by 500000) { |
89 for (i <- 1 to 7000001 by 500000) { |
85 println(i + " " + "%.5f".format(time_needed(2, matcher(EVIL1(i), "a" * i)))) |
90 println(i + " " + "%.5f".format(time_needed(2, matcher(EVIL1(i), "a" * i)))) |
86 } |
91 } |
87 |
92 |