Martin Thoma 11 years ago
parent
commit
860e3c066b

BIN
documents/Programmierparadigmen/Programmierparadigmen.pdf


+ 22 - 1
documents/Programmierparadigmen/X10.tex

@@ -26,8 +26,29 @@ Der Befehl \texttt{x10c HelloWorld.x10} erstellt eine ausführbare Datei namens
 \inputminted[numbersep=5pt, tabsize=4, frame=lines, label=HelloWorld.x10]{cpp}{scripts/x10/HelloWorld.x10}
 
 \section{Syntax}
+Genau wie Scala nutzt X10 \texttt{val}\xindex{val} und \texttt{var}\xindex{var}, wobei \texttt{val} für
+\enquote{value} steht und ein unveränderbarer Wert ist. \texttt{var} hingegen
+steht für \enquote{variable} und ist veränderbar.
+
+Eine Besonderheit sind sog. \textit{Constrianed types}\xindex{types!constrained}:
+
+\inputminted[numbersep=5pt, tabsize=4]{scala}{scripts/x10/constrained-type-example.x10}
+
 \section{Datentypen}
-Byte, UByte, Short, UShort, Char, Int, UInt, Long, ULong, Float, Double, Boolean, Complex, String, Point, Region, Dist, Array
+Byte, UByte, Short, UShort, Char, Int, UInt, Long, ULong, Float, Double, Boolean, 
+Complex, String, Point, Region, Dist, Array
+
+\subsection{struct}\xindex{struct}%
+In X10 gibt es, wie auch in C, den Typ \texttt{struct}. Dieser erlaubt im Gegensatz
+zu Objekten keine Vererbung, kann jedoch auch interfaces implementieren.
+
+Alle Felder eines X10-Structs sind \texttt{val}.
+
+Structs werden verwendet, da sie effizienter als Objekte sind.
+
+\begin{beispiel}[struct]
+    \inputminted[numbersep=5pt, tabsize=4]{scala}{scripts/x10/x10-struct-example.x10}
+\end{beispiel}
 
 \section{Beispiele}
 

+ 22 - 0
documents/Programmierparadigmen/scripts/x10/Fibonacci.x10

@@ -0,0 +1,22 @@
+// file Fibonacci.x10
+public class Fibonacci {
+    public static def fib(n:Int) {
+        if (n < 2) {
+            return n;
+        }
+
+        val f1:Int;
+        val f2:Int;
+        f1 = fib(n-1);
+        f2 = fib(n-2);
+        return f1 + f2;
+    }
+
+    public static def main(args:Rail[String]) {
+        x10.io.Console.OUT.println("This is fibonacci in X10.");
+        for (var i:Int=0; i < 10; ++i) {
+            x10.io.Console.OUT.println(i + ": " + fib(i));
+            fib(i);
+        }
+    }
+}

+ 0 - 85
documents/Programmierparadigmen/scripts/x10/Mergesort.x10

@@ -1,85 +0,0 @@
-import x10.io.Console;
-import x10.util.Random;
-
-public class Mergesort {
-  private static val rand = new Random();
-  private static val MIN_ELEMENTS_PARALLEL = 65536;
-
-  public static def sort(values : Array[Int](1)) : Array[Int](1) {
-    val numbers = new Array[Int](values.size);
-    val helper = new Array[Int](values.size);
-    Array.copy(values, numbers);
-
-    mergesort(0, values.size - 1, numbers, helper);
-
-    return numbers;
-  }
-
-  private static def mergesort(low : Int, high : Int, numbers : Array[Int](1), helper : Array[Int](1)) {
-    if (low < high) {
-      val middle = (low + high) / 2;
-      if (high - low >= MIN_ELEMENTS_PARALLEL) {
-        finish {
-          async mergesort(low, middle, numbers, helper);
-          async mergesort(middle + 1, high, numbers, helper);
-        }
-      } else {
-        mergesort(low, middle, numbers, helper);
-        mergesort(middle + 1, high, numbers, helper);
-      }
-      merge(low, middle, high, numbers, helper);
-    }
-  }
-
-  private static def merge(low : Int, middle : Int, high : Int, numbers : Array[Int](1), helper : Array[Int](1)) {
-    // Copy the part to be merged into the helper (from low to high)
-    Array.copy(numbers, low, helper, low, high - low + 1);
-
-    var left : Int = low;
-    var right : Int = middle + 1;
-    var position : Int = low;
-
-    while(left <= middle && right <= high) {
-      if (helper(left) <= helper(right)) {
-      numbers(position++) = helper(left++);
-      } else {
-      numbers(position++) = helper(right++);
-      }
-    }
-
-    while(left <= middle) {
-      numbers(position++) = helper(left++);
-    }
-    // Nothing needs to be done for the right half, because is still
-    // is where it was copied from, which happens to be the right
-    // location.
-  }
-
-  public static def main(args:Array[String](1)) {
-    if (args.size < 1) {
-      Console.OUT.println("Expect array length as argument");
-      return;
-    }
-
-    val sort_count = Int.parse(args(0));
-    val to_sort:Array[Int] = new Array[Int](sort_count, (_:Int) => { return rand.nextInt(); });
-
-    for (i in to_sort) {
-      Console.OUT.print(to_sort(i) + " ");
-    }
-    Console.OUT.println();
-
-    val start = System.nanoTime();
-    val sorted = sort(to_sort);
-    val duration = System.nanoTime() - start;
-
-    Console.OUT.println("Sorting took " + duration / 1000000.0 + "ms");
-
-    Console.OUT.println("Checking for sortedness...");
-
-    for (i in sorted) {
-       Console.OUT.print(sorted(i) + " ");
-    }
-    Console.OUT.println();
-  }
-}

+ 2 - 0
documents/Programmierparadigmen/scripts/x10/constrained-type-example.x10

@@ -0,0 +1,2 @@
+Int{self > 0}
+def dotProduct(x:Vec, y:Vec) {x.len == y.len}

+ 16 - 0
documents/Programmierparadigmen/scripts/x10/x10-struct-example.x10

@@ -0,0 +1,16 @@
+struct Complex {
+    val real:Double;
+    val img :Double;
+
+    def this(r:Double, i:Double) {
+        real = r; img = i;
+    }
+
+    def operator + (that:Complex) {
+        return
+            Complex(real + that.real,
+                    img + that.img);
+    }
+}
+
+val x = new Array[Complex](1..10);