updated
authorChristian Urban <christian.urban@kcl.ac.uk>
Tue, 29 Sep 2020 19:35:11 +0100
changeset 767 bdd12391d345
parent 766 e8402d8ec8e6
child 768 34f77b976b88
updated
data.sty
progs/catastrophic/catastrophic.swift
progs/matcher/re2.sc
slides/slides01.pdf
slides/slides01.tex
slides/slides02.pdf
slides/slides02.tex
--- a/data.sty	Tue Sep 29 12:52:07 2020 +0100
+++ b/data.sty	Tue Sep 29 19:35:11 2020 +0100
@@ -88,6 +88,19 @@
 32  32.190
 \end{filecontents}
 
+% Swift, example (a*)*b  
+\begin{filecontents}{re-swift.data}
+5   0.001
+10  0.001
+15  0.009
+20  0.178
+23  1.399
+24  2.893
+25  5.671
+26  11.357
+27  22.430
+\end{filecontents}
+
 % Java 8, example (a*)*b  
 \begin{filecontents}{re-java.data}
 5  0.00298
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/progs/catastrophic/catastrophic.swift	Tue Sep 29 19:35:11 2020 +0100
@@ -0,0 +1,43 @@
+// A case of catastrophic backtracking in Swift
+//---------------------------------------------
+// example provided by Martin Mikusovic
+//
+// regex: (a*)*b
+// strings: aa...a
+//
+// compile with
+//
+// swiftc catastrophic.swift
+//
+// run with for example
+//
+// time ./catastrophic 20
+
+import Foundation
+
+let args = CommandLine.arguments
+if args.count >= 2, let count = Int(args[1]) {
+    let s = String(repeating: "a", count: count)
+    calculateTime {
+        let _ = s ~= "(a*)*b"
+    }
+} else {
+    print("Please enter a valid number argument.")
+}
+
+func calculateTime(block: () -> ()) {
+    let start = DispatchTime.now()
+    block()
+    let end = DispatchTime.now()
+    let nanoTime = end.uptimeNanoseconds - start.uptimeNanoseconds
+    let timeInterval = Double(nanoTime) / 1_000_000_000
+    print("\(timeInterval) s")
+}
+
+extension String {
+    static func ~=(lhs: String, rhs: String) -> Bool {
+        guard let regex = try? NSRegularExpression(pattern: rhs) else { return false }
+        let range = NSRange(location: 0, length: lhs.utf16.count)
+        return regex.firstMatch(in: lhs, options: [], range: range) != nil
+    }
+}
--- a/progs/matcher/re2.sc	Tue Sep 29 12:52:07 2020 +0100
+++ b/progs/matcher/re2.sc	Tue Sep 29 19:35:11 2020 +0100
@@ -90,7 +90,7 @@
 def test2() = {
   println("Test (a*)* b")
 
-  for (i <- 0 to 20 by 2) {
+  for (i <- 0 to 30 by 2) {
     println(f"$i: ${time_needed(2, matcher(EVIL2, "a" * i))}%.5f")
   }
 }
Binary file slides/slides01.pdf has changed
--- a/slides/slides01.tex	Tue Sep 29 12:52:07 2020 +0100
+++ b/slides/slides01.tex	Tue Sep 29 19:35:11 2020 +0100
@@ -631,12 +631,13 @@
     axis lines=left,
     width=\textwidth,
     height=4cm, 
-    legend entries={Python, Java 8, JavaScript},  
+    legend entries={Python, Java 8, JavaScript, Swift},  
     legend pos=north west,
     legend cell align=left]
 \addplot[blue,mark=*, mark options={fill=white}] table {re-python2.data};   
 \addplot[cyan,mark=*, mark options={fill=white}] table {re-java.data};
 \addplot[red,mark=*, mark options={fill=white}] table {re-js.data};
+\addplot[magenta,mark=*, mark options={fill=white}] table {re-swift.data};
 \end{axis}
 \end{tikzpicture}
 %
Binary file slides/slides02.pdf has changed
--- a/slides/slides02.tex	Tue Sep 29 12:52:07 2020 +0100
+++ b/slides/slides02.tex	Tue Sep 29 19:35:11 2020 +0100
@@ -601,6 +601,7 @@
 
 \begin{center}
 \begin{tabular}{rl}
+0: & \bl{$\ONE$}\\    
 1: & \bl{$a$}\\
 2: & \bl{$a\cdot a$}\\
 3: & \bl{$a\cdot a\cdot a$}\\
@@ -773,12 +774,13 @@
     axis lines=left,
     width=9cm,
     height=5.5cm, 
-    legend entries={Java 8, Python, JavaScript},  
+    legend entries={Java 8, Python, JavaScript, Swift},  
     legend pos=north west,
     legend cell align=left]
 \addplot[blue,mark=*, mark options={fill=white}] table {re-python2.data};
 \addplot[cyan,mark=*, mark options={fill=white}] table {re-java.data};
 \addplot[red,mark=*, mark options={fill=white}] table {re-js.data};
+\addplot[magenta,mark=*, mark options={fill=white}] table {re-swift.data};
 \end{axis}
 \end{tikzpicture}
 \end{center}