1
play

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


  1. 1 2 Einführung 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 Introduction to Programming – Lecture 13 Themen für diese Vorlesung Container-Datenstrukturen 3 4 � Container und Genericity � Enthalten andere Objekte (“ items ”) � Beispiel: Eine Metroline ist — unter anderem — ein � Statische Typisierung Container von Haltestellen � Mögliche Operationen auf einem Container: � Leistung von Algorithmen beurteilen: Big-Oh- � Ein Item einfügen Notation � Herausfinden, ob ein Element enthalten ist � Ein Element entfernen � Verkettete Listen � Die Struktur “ traversieren ” um eine Operation auf jedes Item anzuwenden � Arrays � Viele Arten: Listen (inkl. “linked list”, “doubly- linked lists”), zirkuläre Listen, Arrays, Stacks, Queues, Priority-Queues, Hashtabellen... Chair of Software Engineering Introduction to Programming – Lecture 13 Chair of Software Engineering Introduction to Programming – Lecture 13 Ein Grundproblem von Containern Erinnerung: Listen-Konventionen 5 6 Wie behandeln wir Varianten einer Container- Klasse, die sich nur durch den Typ ihrer Items after before unterscheiden? item � Metrolinie: Liste von Haltestellen index count 1 � Route: Liste von Segmenten forth start back � Telefonliste: Liste von Verzeichniseinträgen � Agenda: Liste von Verabredungen � ... (Der Cursor) Chair of Software Engineering Introduction to Programming – Lecture 13 Chair of Software Engineering Introduction to Programming – Lecture 13 1

  2. Ohne Genericity Ohne Genericity 7 8 class METRO_LINE feature class ROUTE feature start is do ... end start is do ... end forth is do ... end forth is do ... end item : METRO_STOP is do ... end item : SEGMENT is do ... end put_right ( x : METRO_STOP ) is put_right ( x : SEGMENT ) is -- Add x right of cursor. -- Add x right of cursor. do do ... Something involving x ... ... Something involving x ... end end extend ( x : METRO_STOP ) is do ... end extend ( x : SEGMENT ) is do ... end ... ... end end Chair of Software Engineering Introduction to Programming – Lecture 13 Chair of Software Engineering Introduction to Programming – Lecture 13 Ohne Genericity Eine nicht-generische List-Klasse 9 10 class AGENDA feature class LIST1 feature start is do ... end start is do ... end forth is do ... end forth is do ... end item : APPOINTMENT is do ... end item : ANY is do ... end put_right ( x : APPOINTMENT ) is put_right ( x : ANY ) is -- Add x right of cursor. -- Add x right of cursor. do do ... Something involving x ... ... Something involving x ... end end extend ( x : APPOINTMENT ) is do ... end extend ( x : ANY ) is do ... end ... ... end end Chair of Software Engineering Introduction to Programming – Lecture 13 Chair of Software Engineering Introduction to Programming – Lecture 13 Generalisierte Liste benutzen Lösungen 11 12 my_route : LIST1 seg : SEGMENT � Code wiederholen (nicht wirklich akzeptabel) my_agenda : LIST1 app : APPOINTMENT � Konversionen, oder “casts”, erlauben � Ungeprüft: C, C++ my_route.extend ( seg ) � Geprüft: Java, C# my_agenda.extend (app ) app ?= my_agenda.item if app /= Void then ... seg := my_route.item end app := my_agenda.item � Typen-Parametrisierung explizit machen (Eiffel): app := my_route.item -- ????????? Genericity Chair of Software Engineering Introduction to Programming – Lecture 13 Chair of Software Engineering Introduction to Programming – Lecture 13 2

  3. Lösung: Genericity Generalisierte Liste benutzen 13 14 my_route : LIST1 seg : SEGMENT class LINKED_LIST [ G ] feature my_agenda : LIST1 app : APPOINTMENT start is do ... end Formaler generischer forth is do ... end Parameter item : G is do ... end my_route.extend ( seg ) put_right ( x : G ) is -- Add x right of cursor. my_agenda.extend (app ) do ... Something involving x ... seg := my_route.item end app := my_agenda.item extend ( x : G ) is do ... end ... app := my_route.item -- ????????? end Chair of Software Engineering Introduction to Programming – Lecture 13 Chair of Software Engineering Introduction to Programming – Lecture 13 Generische List-Klasse benutzen Statische Typisierung 15 16 my_route : LIST [ SEGMENT ] seg : SEGMENT � Jede Entität des Programms ist mit einem Typen my_agenda : LIST [ APPOINTMENT ] app : APPOINTMENT deklariert � Jede Zuweisung und jeder Feature-Aufruf muss die Typkompatibilitäts-Regeln befolgen my_route.extend ( seg ) my_agenda.extend (app ) tatsächlicher � Ziel: Nie ein Feature auf ein Objekt anwenden, für generischer Parameter das dieses Feature nicht definiert ist seg := my_route.item app := my_agenda.item app := my_route.item -- Type-wrong, rejected Chair of Software Engineering Introduction to Programming – Lecture 13 Chair of Software Engineering Introduction to Programming – Lecture 13 Eine generische Klasse: LINKED_LIST Wichtigste Platitüde in der Software-Entwicklung! 17 18 Besser einen Fehler früh als spät abfangen � Demo (siehe EiffelStudio) 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 Chair of Software Engineering Introduction to Programming – Lecture 13 Chair of Software Engineering Introduction to Programming – Lecture 13 3

  4. Maximum berechnen, Version 1 Der Routine-Body, Version 1 19 20 highest_name ( line: METRO_LINE ) : STRING is -- Alphabetisch grösster Stationsname der Linie item require line_exists : line /= Void do count do forth from from fancy_line.start ; Result := "“ fancy_line.start ; Result := "" invariant ... variant ... until until fancy_line.after fancy_line.after loop loop Result := greater ( Result, line.item.name ) Result := greater ( Result, line.item.name ) fancy_line.forth end fancy_line.forth ensure end Result /= Void and then not Result.empty end end Chair of Software Engineering Introduction to Programming – Lecture 13 Chair of Software Engineering Introduction to Programming – Lecture 13 Der Routine-Body, Version 2 Der Routine-Body, Version 3 21 22 local local i : INTEGER i : INTEGER i_th ( i ) i_th ( i ) do do i count i count from from i := 0 ; Result := "" i := count + 1 ; Result := "" until until i > n i = 0 loop loop i := i + 1 i := i − 1 Result := greater ( Result, line.i_th ( i ) .name ) Result := greater ( Result, line.i_th ( i ) .name ) end end end end Chair of Software Engineering Introduction to Programming – Lecture 13 Chair of Software Engineering Introduction to Programming – Lecture 13 Wie schnell ist der Algorithmus? Wesentliche Effizienz abschätzen 23 24 � Abhängig von der Hardware, Betriebssystem, Wie verhält sich die Laufzeit (und der Belastung der Maschine... Speicherverbrauch) des Algorithmus’ als Funktion der Grösse count der Daten, wenn diese Grösse riesig wird? � Aber am fundamentalsten abhängig vom � Version 1: Zeit etwa proportional zu count . Algorithmus! � Versionen 2 und 3: könnte proportional zu count oder zu count 2 sein! 1 + 2 + ... + count = count * ( count + 1) / 2 Chair of Software Engineering Introduction to Programming – Lecture 13 Chair of Software Engineering Introduction to Programming – Lecture 13 4

  5. “Big Oh ” Notation Formell... 25 26 � O ( f ( n )), wobei n die Grösse der Eingabe “ f ist in O ( g ( n ))” bedeutet, es gibt eine Konstante repräsentiert, bedeutet “in der Grössenordnung K, so dass für alle n gilt : von f ( n )”. f ( n ) / g ( n ) <= K � Zeit für Version 1 ist O ( count ) � Zeit für Versionen 2 und 3 kann O ( count ) oder O (1) bedeutet also in konstanter Zeit, oder durch O ( count 2 ) sein. eine Konstante beschränkte Zeit. Auch verwendet: “ f ( n ) = 3 ∗ n 2 + O ( g ( n ))” Chair of Software Engineering Introduction to Programming – Lecture 13 Chair of Software Engineering Introduction to Programming – Lecture 13 Beispiele Mit einer 1000mal schnelleren Maschine... 27 28 Vorherige Maxialgrösse: N � n 2 = O ( n 2 ) Vier Algorithmen: Neues Maximum: � 3 ∗ n 2 = O ( n 2 ) � N 1000 � O ( log ( n )) � 3 ∗ n 2 + 2 ∗ n + 1 = O ( n 2 ) � 1000 ∗ N � O ( n ) � 3 ∗ n 2 + 2 ∗ n + 1 = O (2 ∗ n 2 ) � 32 ∗ N ≈ � O ( n 2 ) � 3 ∗ n 2 + 2 ∗ n + 1 3 ∗ n 2 + O ( n ) = � N + 10 � O (2 n ) Chair of Software Engineering Introduction to Programming – Lecture 13 Chair of Software Engineering Introduction to Programming – Lecture 13 Verkettete Listen highest_name , Version 2 29 30 local count 3 i : INTEGER i_th ( i ) first_element do i count from active i := 0 ; Result := "" until Haupt- i > n Haldenegg Central bahnhof loop item right item right item right i := i + 1 Result := greater ( Result, line.i_th ( i ) .name ) (Der Cursor) end end Chair of Software Engineering Introduction to Programming – Lecture 13 Chair of Software Engineering Introduction to Programming – Lecture 13 5

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend