Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 11.

Slides:



Advertisements
Similar presentations
Why not just use Arrays? Java ArrayLists.
Advertisements

Chair of Software Engineering Concurrent Object-Oriented Programming Prof. Dr. Bertrand Meyer Lecture 10: Advanced Object-Oriented Mechanisms (based on.
1 Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 13.
Chair of Software Engineering Concurrent Object-Oriented Programming Prof. Dr. Bertrand Meyer Exercise Session 1: Eiffel Introduction.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Lecture 7: References and Assignment.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Lecture 14: Container Data Structures.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 6.
Chair of Software Engineering OOSC - Summer Semester Object-Oriented Software Construction Bertrand Meyer.
Chair of Software Engineering ATOT - Lecture 24, 25 June Advanced Topics in Object Technology Bertrand Meyer.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Lecture 17: Topological Sort Algorithm.
Arrays Liang, Chpt 5. arrays Fintan Array of chars For example, a String variable contains an array of characters: An array is a data structure.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Lecture 14: Container Data Structures.
Introduction and a Review of Basic Concepts
Chair of Software Engineering OOSC - Summer Semester Object-Oriented Software Construction Bertrand Meyer.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 9.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Lecture 18: Undo/Redo.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Lecture 12: More on references and.
Chair of Software Engineering 1 Introduction to Programming Tuples and Agents Exercise Session 1 and 2 December 2008.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 10.
1 Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 13.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 4.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 10.
Chair of Software Engineering 1 Introduction to Programming Exercise Session Week 10 M. Piccioni 24/25 November 2008.
Chair of Software Engineering Object-Oriented Software Construction Bertrand Meyer Lecture 21: Agents and tuples.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 7.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 9.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Lecture 20: Multiple inheritance.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 10.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Lecture 9: Abstraction.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 3.
Array.
Hash Table March COP 3502, UCF.
Hashtables David Kauchak cs302 Spring Administrative Talk today at lunch Midterm must take it by Friday at 6pm No assignment over the break.
HASHING Section 12.7 (P ). HASHING - have already seen binary and linear search and discussed when they might be useful (based on complexity)
ITEC 2620A Introduction to Data Structures
CS 202, Spring 2003 Fundamental Structures of Computer Science II Bilkent University1 Hashing CS 202 – Fundamental Structures of Computer Science II Bilkent.
Chair of Software Engineering 1 Introduction to Programming Bertrand Meyer Exercise Session October 2008.
Chapter 12 Recursion, Complexity, and Searching and Sorting
5-Aug-2002cse Arrays © 2002 University of Washington1 Arrays CSE 142, Summer 2002 Computer Programming 1
ARRAYS 1 Week 2. Data Structures  Data structure  A particular way of storing and organising data in a computer so that it can be used efficiently 
Arrays and ArrayLists in Java L. Kedigh. Array Characteristics List of values. A list of values where every member is of the same type. Each member in.
Copyright © 2002, Systems and Computer Engineering, Carleton University Hashtable.ppt * Object-Oriented Software Development Unit 8.
Data structures Abstract data types Java classes for Data structures and ADTs.
CSE 501N Fall ‘09 11: Data Structures: Stacks, Queues, and Maps Nick Leidenfrost October 6, 2009.
Hashing Sections 10.2 – 10.3 CS 302 Dr. George Bebis.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 9.
CS201: Data Structures and Discrete Mathematics I Hash Table.
Chapter 10 Hashing. The search time of each algorithm depend on the number n of elements of the collection S of the data. A searching technique called.
Week # 2: Arrays.  Data structure  A particular way of storing and organising data in a computer so that it can be used efficiently  Types of data.
The List ADT A sequence of zero or more elements A 1, A 2, A 3, … A N-1 N: length of the list A 1 : first element A N-1 : last element A i : position i.
Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Chair of Software Engineering Complement to lecture 11 : Levenshtein.
Hash Table March COP 3502, UCF 1. Outline Hash Table: – Motivation – Direct Access Table – Hash Table Solutions for Collision Problem: – Open.
COSC 2007 Data Structures II Chapter 13 Advanced Implementation of Tables IV.
Hashing Fundamental Data Structures and Algorithms Margaret Reid-Miller 18 January 2005.
Chair of Software Engineering 1 Introduction to Programming Bertrand Meyer Exercise Session October 2008.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 3.
Georgia Institute of Technology Speed part 4 Barb Ericson Georgia Institute of Technology May 2006.
How to execute Program structure Variables name, keywords, binding, scope, lifetime Data types – type system – primitives, strings, arrays, hashes – pointers/references.
Hashtables David Kauchak cs302 Spring Administrative Midterm must take it by Friday at 6pm No assignment over the break.
1 Assertions. 2 A boolean expression or predicate that evaluates to true or false in every state In a program they express constraints on the state that.
Hashing O(1) data access (almost) -access, insertion, deletion, updating in constant time (on average) but at a price… references: Weiss, Goodrich & Tamassia,
Maps Nick Mouriski.
Chapter 8 Searching and Sorting © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
Data Structures Arrays and Lists Part 2 More List Operations.
Cse 373 April 24th – Hashing.
Einführung in die Programmierung Introduction to Programming Prof. Dr
Hashing Sections 10.2 – 10.3 Lecture 26 CS302 Data Structures
Einführung in die Programmierung Introduction to Programming Prof. Dr
Data Structures & Algorithms
Einführung in die Programmierung Introduction to Programming Prof. Dr
Presentation transcript:

Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 11

2 Today  Basic Data-structures  Arrays  Linked Lists  Hashtables  Tuples  Agents  Agents and Data-structures

3 Arrays An array is a very fundamental data-structure, which is very close to how your computer organizes its memory. An array is characterized by:  Constant time random reads  Constant time random writes  Costly to resize (including inserting elements in the middle of the array)  Must be indexed by an integer  Generally very space efficient In Eiffel the basic array class is generic, ARRAY [G].

4 Using Arrays Which of the following lines are valid? Which can fail, and why?  my_array : ARRAY [STRING]  my_array [“Fred”] := “Sam”  my_array [10] + “’s Hat”  my_array [5] := “Ed”  my_array.force (“Constantine”, 9) Which is not a constant-time array operation? Hands-On Valid, can’t fail Invalid Valid, can fail Valid, can’t fail

5 Linked Lists  Linked lists are one of the simplest data-structures  They consist of linkable cells class LINKABLE [G] create set_value feature set_value (v : G) do value := v end value : G set_next (n : LINKABLE[G]) do next := n end next : LINKABLE [G] end

6 Using Linked Lists Supposing you keep a reference to only the head of the linked list, what is the running time (using big O notation) to:  Insert at the beginning  Insert in the middle  Insert at the end  Find the length of the list What simple optimization could be made to make end- access faster? Hands-On O (1) O (n)

7 Hashtables Hashtables provide a way to use regular objects as keys (sort of like how we use INTEGER “keys” in arrays). This is essentially a trade-off:  we have to provide a hashing function   hashing function should be good (minimize collision)   our hashtable will always take up more space than it needs to 

8 Good points about Hashtables Hashtables aren’t all that bad though, they provide us with a great solution: they can store and retrieve objects quickly by key! This is a very common operation. For each, list what the key and values could be:  A telephone book  The index of a book  Google search Name  Telephone Number Concept  Page Search String  Websites Hands-On Would you use a hashtable or an array for storing the pages of a book?

9 Tuples  A tuple of type TUPLE [A, B, C] is a sequence of at least three values, first of type A, second of type B, third of type C.  In this case possible tuple values that conform are:  [a, b, c], [a, b, c, x],... where a is of type A, b of type B, c of type C and x of some type X  Tuple types (for any types A, B, C,... ): TUPLE TUPLE [A] TUPLE [A, B] TUPLE [A, B, C]...

10 Labeled Tuples  Tuples may be declared with labelled arguments: tuple: TUPLE [food: STRING; quantity: INTEGER]  Same as an unlabeled tuple: TUPLE [STRING, INTEGER] but provides easier (and safer!) access to its elements: May use io.print (tuple.food) ‏ instead of io.print (tuple.item(1)) ‏

11 Tuple Inheritance TUPLE [A,B] TUPLE TUPLE [A]...

12 Tuple conformance tuple_conformance local t0: TUPLE t2: TUPLE [INTEGER, INTEGER] do create t2 t2 := [10, 20] t0 := t2 print (t0.item (1).out + "%N") ‏ print (t0.item (3).out) ‏ end Not necessary in this case Runtime error, but will compile Implicit creation

13 What are agents in Eiffel?  Objects that represent operations  Can be seen as operation wrappers  Similar to  delegates in C#  anonymous inner classes in Java < 7  closures in Java 7  function pointers in C  functors in C++

14 Agent definition  Every agent has an associated routine, the one that the agent wraps and is able to invoke  To get an agent, use the agent keyword e.g. an_agent := agent my_routine  This is called agent definition  What’s the type of an_agent?

15 EiffelBase classes representing agents * ROUTINE + PROCEDURE + FUNCTION + PREDICATE call item

16 Agent Type Declarations p: PROCEDURE [ANY, TUPLE] Agent representing a procedure belonging to a class that conforms to ANY. At least 0 open arguments q: PROCEDURE [C, TUPLE [X, Y, Z]] Agent representing a procedure belonging to a class that conforms to C. At least 3 open arguments f: FUNCTION [ANY, TUPLE [X, Y], RES] Agent representing a function belonging to a class that conforms to ANY. At least 2 open arguments,result of type RES

17 Open and closed agent arguments  An agent can have both “closed” and “open” arguments:  closed arguments set at agent definition time  open arguments set at agent call time.  To keep an argument open, replace it by a question mark u := agent a0.f (a1, a2, a3) -- All closed w := agent a0.f (a1, a2, ?) ‏ x := agent a0.f (a1, ?, a3) y := agent a0.f (a1, ?, ?) z := agent a0.f (?, ?, ?) -- All open

18 Agent Calls An agent invokes its routine using feature “call” f (x1: T1; x2: T2; x3: T3) -- defined in class C with -- a0: C; a1: T1; a2: T2; a3: T3 u := agent a0.f (a1, a2, a3) ‏ v := agent a0.f (a1, a2, ?) w := agent a0.f (a1, ?, a3) x := agent a0.f (a1, ?, ?) y := agent a0.f (?, ?, ?) y.call ([a1, a2, a3]) x.call ([a2, a3]) w.call ([a2]) v.call ([a3]) u.call ([]) PROCEDURE [C, TUPLE] PROCEDURE [C, TUPLE [T3]] PROCEDURE [C, TUPLE [T2]] PROCEDURE [C, TUPLE [T2, T3]] PROCEDURE [C, TUPLE [T1,T2,T3] What are the types of the agents?

19 Doing something to a list do_all (do_this : PROCEDURE[ANY, TUPLE[G]]) local i : INTEGER do Hands-On from until loop end i := 1 i > count Given a simple ARRAY [G] class, with only the features `count’ and `at’, implement a feature which will take an agent and perform it on every element of the array. do_this.call ([at (i)]) i := i + 1

20 For-all quantifiers over lists for_all (pred : PREDICATE [ANY, TUPLE[G]]) local i : INTEGER do Hands-On from until loop end i := 1 i > count or not Result Result := True Result := pred.item ([at (i)]) i := i + 1

21 Using inline agents We can also define our agents as-we-go! Applying this to the previous `for_all’ function we made, we can do: for_all_ex (int_array : ARRAY [INTEGER]): BOOLEAN local greater_five : PREDICATE [ANY, TUPLE [INTEGER]] do greater_five := agent (i : INTEGER) : BOOLEAN do Result := i > 5 end Result := int_array.for_all (greater_five) end

22 Problems with Agents/Tuples We have already seen that TUPLE [A,B] conforms to TUPLE [A]. This raises a problem, consider the definition: f (proc : PROCEDURE [ANY, TUPLE[INTEGER]]). do proc.call ([5]) end Yes! Oh no… that procedure needs at least TWO arguments! Are we allowed to call this on something of type PROCEDURE [ANY, TUPLE[INTEGER,INTEGER]] ?