1 Ohne Genericity Ohne Genericity 7 8 class METRO_LINE feature - - PDF document

1
SMART_READER_LITE
LIVE PREVIEW

1 Ohne Genericity Ohne Genericity 7 8 class METRO_LINE feature - - PDF document

1 2 Einfhrung in die Programmierung Vorlesung 13: Container-Datenstrukturen Bertrand Meyer Letzte Bearbeitung 1. Dezember 2003 Chair of Software Engineering Introduction to Programming Lecture 13 Chair of Software Engineering


slide-1
SLIDE 1

1

Introduction to Programming – Lecture 13

1 Chair of Software Engineering

Einführung in die Programmierung

Bertrand Meyer

Letzte Bearbeitung 1. Dezember 2003

Introduction to Programming – Lecture 13

2 Chair of Software Engineering

Vorlesung 13: Container-Datenstrukturen

Introduction to Programming – Lecture 13

3 Chair of Software Engineering

Themen für diese Vorlesung

Container und Genericity Statische Typisierung Leistung von Algorithmen beurteilen: Big-Oh- Notation Verkettete Listen Arrays

Introduction to Programming – Lecture 13

4 Chair of Software Engineering

Container-Datenstrukturen

Enthalten andere Objekte (“items”) Beispiel: Eine Metroline ist — unter anderem — ein Container von Haltestellen Mögliche Operationen auf einem Container: Ein Item einfügen Herausfinden, ob ein Element enthalten ist Ein Element entfernen Die Struktur “traversieren” um eine Operation auf jedes Item anzuwenden Viele Arten: Listen (inkl. “linked list”, “doubly- linked lists”), zirkuläre Listen, Arrays, Stacks, Queues, Priority-Queues, Hashtabellen...

Introduction to Programming – Lecture 13

5 Chair of Software Engineering

Ein Grundproblem von Containern

Wie behandeln wir Varianten einer Container- Klasse, die sich nur durch den Typ ihrer Items unterscheiden? Metrolinie: Liste von Haltestellen Route: Liste von Segmenten Telefonliste: Liste von Verzeichniseinträgen Agenda: Liste von Verabredungen ...

Introduction to Programming – Lecture 13

6 Chair of Software Engineering

Erinnerung: Listen-Konventionen

item before after count forth back index start

(Der Cursor)

1

slide-2
SLIDE 2

2

Introduction to Programming – Lecture 13

7 Chair of Software Engineering

Ohne Genericity

class METRO_LINE feature start is do ... end forth is do ... end item: METRO_STOP is do ... end put_right (x: METRO_STOP) is

  • - Add x right of cursor.

do ... Something involving x ... end extend (x: METRO_STOP) is do ... end ... end

Introduction to Programming – Lecture 13

8 Chair of Software Engineering

Ohne Genericity

class ROUTE feature start is do ... end forth is do ... end item: SEGMENT is do ... end put_right (x: SEGMENT) is

  • - Add x right of cursor.

do ... Something involving x ... end extend (x: SEGMENT) is do ... end ... end

Introduction to Programming – Lecture 13

9 Chair of Software Engineering

Ohne Genericity

class AGENDA feature start is do ... end forth is do ... end item: APPOINTMENT is do ... end put_right (x: APPOINTMENT) is

  • - Add x right of cursor.

do ... Something involving x ... end extend (x: APPOINTMENT) is do ... end ... end

Introduction to Programming – Lecture 13

10 Chair of Software Engineering

Eine nicht-generische List-Klasse

class LIST1 feature start is do ... end forth is do ... end item: ANY is do ... end put_right (x: ANY) is

  • - Add x right of cursor.

do ... Something involving x ... end extend (x: ANY) is do ... end ... end

Introduction to Programming – Lecture 13

11 Chair of Software Engineering

Generalisierte Liste benutzen

my_route: LIST1

seg: SEGMENT

my_agenda: LIST1

app: APPOINTMENT

my_route.extend (seg) my_agenda.extend (app) seg := my_route.item app := my_agenda.item app := my_route.item

  • - ?????????

Introduction to Programming – Lecture 13

12 Chair of Software Engineering

Lösungen

Code wiederholen (nicht wirklich akzeptabel) Konversionen, oder “casts”, erlauben

Ungeprüft: C, C++ Geprüft: Java, C# app ?= my_agenda.item if app /= Void then ... end

Typen-Parametrisierung explizit machen (Eiffel): Genericity

slide-3
SLIDE 3

3

Introduction to Programming – Lecture 13

13 Chair of Software Engineering

Lösung: Genericity

class LINKED_LIST [G] feature start is do ... end forth is do ... end item: G is do ... end put_right (x: G) is

  • - Add x right of cursor.

do ... Something involving x ... end extend (x: G) is do ... end ... end

Formaler generischer Parameter

Introduction to Programming – Lecture 13

14 Chair of Software Engineering

Generalisierte Liste benutzen

my_route: LIST1

seg: SEGMENT

my_agenda: LIST1

app: APPOINTMENT

my_route.extend (seg) my_agenda.extend (app) seg := my_route.item app := my_agenda.item app := my_route.item

  • - ?????????

Introduction to Programming – Lecture 13

15 Chair of Software Engineering

Generische List-Klasse benutzen

my_route: LIST [SEGMENT]

seg: SEGMENT

my_agenda: LIST [APPOINTMENT]

app: APPOINTMENT

my_route.extend (seg) my_agenda.extend (app) seg := my_route.item app := my_agenda.item app := my_route.item

  • - Type-wrong, rejected

tatsächlicher generischer Parameter

Introduction to Programming – Lecture 13

16 Chair of Software Engineering

Statische Typisierung

Jede Entität des Programms ist mit einem Typen deklariert Jede Zuweisung und jeder Feature-Aufruf muss die Typkompatibilitäts-Regeln befolgen Ziel: Nie ein Feature auf ein Objekt anwenden, für das dieses Feature nicht definiert ist

Introduction to Programming – Lecture 13

17 Chair of Software Engineering

Wichtigste Platitüde in der Software-Entwicklung!

Besser einen Fehler früh als spät abfangen

Besser in der Analyse als im Design Besser im Design als in der Implementation Besser in der Kompilation als beim Testen Besser beim Testen als im Einsatz

Introduction to Programming – Lecture 13

18 Chair of Software Engineering

Eine generische Klasse: LINKED_LIST

Demo (siehe EiffelStudio)

slide-4
SLIDE 4

4

Introduction to Programming – Lecture 13

19 Chair of Software Engineering

Maximum berechnen, Version 1

highest_name (line: METRO_LINE): STRING is

  • - Alphabetisch grösster Stationsname der Linie

require line_exists: line /= Void do from fancy_line.start ; Result := "“ invariant ... variant ... until fancy_line.after loop Result := greater (Result, line.item.name) fancy_line.forth end ensure Result /= Void and then not Result.empty end

Introduction to Programming – Lecture 13

20 Chair of Software Engineering

Der Routine-Body, Version 1

do from fancy_line.start ; Result := "" until fancy_line.after loop Result := greater (Result, line.item.name) fancy_line.forth end end

item count forth

Introduction to Programming – Lecture 13

21 Chair of Software Engineering

Der Routine-Body, Version 2

local i: INTEGER do from i := 0 ; Result := "" until i > n loop i := i + 1 Result := greater (Result, line.i_th (i).name) end end

i_th (i) count i

Introduction to Programming – Lecture 13

22 Chair of Software Engineering

Der Routine-Body, Version 3

local i: INTEGER do from i := count + 1 ; Result := "" until i = 0 loop i := i − 1 Result := greater (Result, line.i_th (i).name) end end

i_th (i) count i

Introduction to Programming – Lecture 13

23 Chair of Software Engineering

Wie schnell ist der Algorithmus?

Abhängig von der Hardware, Betriebssystem, Belastung der Maschine... Aber am fundamentalsten abhängig vom Algorithmus!

Introduction to Programming – Lecture 13

24 Chair of Software Engineering

Wesentliche Effizienz abschätzen

Wie verhält sich die Laufzeit (und der Speicherverbrauch) des Algorithmus’ als Funktion der Grösse count der Daten, wenn diese Grösse riesig wird? Version 1: Zeit etwa proportional zu count. Versionen 2 und 3: könnte proportional zu count oder zu count2 sein!

1 + 2 + ... + count = count * (count + 1) / 2

slide-5
SLIDE 5

5

Introduction to Programming – Lecture 13

25 Chair of Software Engineering

“Big Oh” Notation

O (f (n)), wobei n die Grösse der Eingabe repräsentiert, bedeutet “in der Grössenordnung von f (n)”. Zeit für Version 1 ist O (count) Zeit für Versionen 2 und 3 kann O (count) oder O (count2) sein.

Introduction to Programming – Lecture 13

26 Chair of Software Engineering

Formell...

“f ist in O (g (n))” bedeutet, es gibt eine Konstante K, so dass für alle n gilt: f (n) / g (n) <= K O (1) bedeutet also in konstanter Zeit, oder durch eine Konstante beschränkte Zeit.

Auch verwendet: “f (n) = 3 ∗ n2 + O (g (n))”

Introduction to Programming – Lecture 13

27 Chair of Software Engineering

Beispiele

n2 = 3 ∗ n2 = 3 ∗ n2 + 2 ∗ n + 1 = 3 ∗ n2 + 2 ∗ n + 1 = 3 ∗ n2 + 2 ∗ n + 1 = O (n2) O (n2) O (n2) O (2 ∗ n2) 3 ∗ n2 + O (n)

Introduction to Programming – Lecture 13

28 Chair of Software Engineering

Mit einer 1000mal schnelleren Maschine...

Vier Algorithmen:

O (log (n)) O (n) O (n2) O (2n) N1000 1000 ∗ N 32 ∗ N ≈ N + 10

Vorherige Maxialgrösse: N Neues Maximum:

Introduction to Programming – Lecture 13

29 Chair of Software Engineering

Verkettete Listen

Haldenegg item right Central item right Haupt- bahnhof item right first_element active count 3

(Der Cursor)

Introduction to Programming – Lecture 13

30 Chair of Software Engineering

highest_name, Version 2

local i: INTEGER do from i := 0 ; Result := "" until i > n loop i := i + 1 Result := greater (Result, line.i_th (i).name) end end

i_th (i) count i

slide-6
SLIDE 6

6

Introduction to Programming – Lecture 13

31 Chair of Software Engineering

highest_name, Version 3

local i: INTEGER do from i := count + 1 ; Result := "" until i = 0 loop i := i − 1 Result := greater (Result, line.i_th (i).name) end end

i_th (i) count i

Introduction to Programming – Lecture 13

32 Chair of Software Engineering

Performance

  • i_th ist in O (count) (in Version 3)

Folglich ist highest_name, und jede andere solche Traversierung, in O (count2) !

1 + 2 + ... + count = count * (count + 1) / 2 = O (count2)

Introduction to Programming – Lecture 13

33 Chair of Software Engineering

Arrays

Indexiert von einer unteren bis zu einer oberen Schranke Zugriff auf oder Modifikation eines Elements ist in O (1) (konstante Zeit) Im Memory: in aufeinander folgenden Stellen gespeichert lower upper i item (i)

Introduction to Programming – Lecture 13

34 Chair of Software Engineering

Array Klassen-Interface

class ARRAY [G] feature lower, upper: INTEGER make (l, h: INTEGER) is

  • - Allocate with bound l and h.

require high >= low require higher >= low item (i: INTEGER): G is

  • - Entry of index i

require i >= lower i <= upper put (x: G; i: INTEGER) is

  • - Replace by x the value of the entry of index i

require i >= lower i <= upper end

Introduction to Programming – Lecture 13

35 Chair of Software Engineering

Arrays benutzen

your_array: ARRAY [REAL] ... create your_array.make (1, 100) ... your_array.put (35.6, 7) your_array.put (−45.0, 8) ... print (your_array.item (8))

Introduction to Programming – Lecture 13

36 Chair of Software Engineering

Variante: Hashtabellen

directory: HASH_TABLE [METRO_STATION, STRING] ... create directory.make (100) ... directory.put (Station_balard, "BALARD") directory.put (Station_montrouge, "MONTROUGE") ... print (directory.item ("MONTROUGE"))

slide-7
SLIDE 7

7

Introduction to Programming – Lecture 13

37 Chair of Software Engineering

Ende der Vorlesung 13