1 2 Introducton to Programmng Bertrand Meyer Lecture 13: Contaner data structures Last revsed 1 December 2003 Topcs for ths lecture 3 Contaner data structures 4 Contaners and genercty Contan other objects ( s ) Statc typng Example: a metro lne s among other thngs a contaner of stops Assessng algorthm performance: Bg-Oh notaton Lnked lsts Arrays Possble operatons on a contaner: Insert an Fnd out f an element s present Remove an element Traverse the structure to apply an operaton to every Many knds: lsts (ncl. lnked lst, ubly-lnked lsts ), crcular lsts, arrays, stacks, queues, prorty queues, hash tables A basc ssue wth contaners 5 Remnder: lst conventons 6 How we handle varants of a contaner class dstngushed only by the type of ther s? before after Metro lne: lst of stops Route: lst of segments Phone lst: lst of drectory entres Aga: lst of appontments 1 start back ndex forth (The Cursor) 1
Wthout genercty 7 Wthout genercty 8 class METRO_LINE feature : METRO_STOP s put_rght (x: METRO_STOP) s ext (x: METRO_STOP) s class ROUTE feature : SEGMENT s put_rght (x: SEGMENT) s ext (x: SEGMENT) s Wthout genercty 9 A non-generc lst class 10 class AGENDA feature : APPOINTMENT s put_rght (x: APPOINTMENT) s ext (x: APPOINTMENT) s class LIST1 feature : ANY s put_rght (x: ANY) s ext (x: ANY) s Usng the general lst 11 Solutons 12 my_route: LIST1 my_aga: LIST1 my_route.ext (seg) my_aga.ext (app) seg := my_route. app := my_aga. seg: SEGMENT app: APPOINTMENT Repeat code (not really acceptable) Allow conversons, or casts Unchecked: C, C++ Checked: Java, C# app?= my_aga. f app /= Vod then app := my_route. --????????? Make type parameterzaton explct (Effel): genercty 2
Soluton: genercty 13 Usng the general lst 14 class LINKED_LIST [G] feature Formal generc parameter : G s put_rght (x: G) s ext (x: G) s my_route: LIST1 my_aga: LIST1 my_route.ext (seg) my_aga.ext (app) seg := my_route. app := my_aga. app := my_route. --????????? seg: SEGMENT app: APPOINTMENT Usng the generc lst class 15 Statc typng 16 my_route: LIST [SEGMENT] my_aga: LIST [APPOINTMENT] my_route.ext (seg) my_aga.ext (app) seg := my_route. app := my_aga. seg: SEGMENT app: APPOINTMENT Actual generc parameter Every entty of the program s declared wth a type Every assgnment and feature call must satsfy type compatblty rules Goal: never apply to an object a feature that s not defned for that object app := my_route. -- Type-wrong, rejected The most mportant plattude n software engneerng! 17 A generc class: LINKED_LIST 18 It s better to catch an error early than late Demo (see EffelStudo) Better n analyss than n desgn Better n desgn than mplementaton Better n complaton than testng Better n testng than actual operaton 3
Computng the maxmum, verson 1 19 The routne body, verson 1 20 hghest_name (lne: METRO_LINE): STRING s -- Alphabetcally greatest staton name of lne lne_exsts: lne /= Vod fancy_lne.start ; Result := " nvarant varant fancy_lne.after Result := greater (Result, lne..name) fancy_lne.forth ensure Result /= Vod and then not Result.empty forth fancy_lne.start ; Result := "" fancy_lne.after Result := greater (Result, lne..name) fancy_lne.forth The routne body, verson 2 21 The routne body, verson 3 22 := 0 ; Result := "" > n := + 1 Result := greater (Result, lne..name) := + 1 ; Result := "" = 0 := 1 Result := greater (Result, lne..name) How fast s the algorthm? 23 Estmatng essental effcency 24 Deps on the hardware, operatng system, load on the machne But most fundamentally deps on the algorthm! How es the algorthm s executon tme (and memory occupaton) behave as a functon of the sze of the data, when that sze becomes large? Verson 1: tme roughly proportonal to. Verson 2 and 3: could be proportonal to, or to 2! 1 + 2 + + = * ( + 1) / 2 4
Bg Oh notaton 25 Formally 26 O (f (n)), where n represents the sze of the nput, means on the order of f (n). f s O (g (n) means there exsts a constant K such that for all n: Tme for verson 1 s O () f (n) / g (n) <= K Tme for versons 2 and 3 may be O () or O ( 2 ). O (1) thus means constant-tme, or tme bounded by a constant. Also used: f (n) = 3 n 2 + O (g (n)) Examples 27 Wth a 1000 tmes faster machne 28 n 2 = 3 n 2 = 3 n 2 + 2 n + 1 = O (n 2 ) O (n 2 ) O (n 2 ) Four algorthms: O (log (n)) Prevous maxmum sze: N New maxmum: N 1000 3 n 2 + 2 n + 1 = O (2 n 2 ) O (n) 1000 N O (n 2 ) 32 N 3 n 2 + 2 n + 1 = 3 n 2 + O (n) O (2 n ) N + 10 Lnked lsts 29 hghest_name, verson 2 30 Haldenegg rght 3 actve Central (The Cursor) frst_element rght Hauptbahnhof rght := 0 ; Result := "" > n := + 1 Result := greater (Result, lne..name) 5
hghest_name, verson 3 31 Performance 32 := + 1 ; Result := "" = 0 := 1 Result := greater (Result, lne..name) _th s O () (n verson 3) As a result, hghest_name, and any other such traversal, s O ( 2 )! 1 + 2 + + = * ( + 1) / 2 = O ( 2 ) Arrays 33 Array class nterface 34 class ARRAY [G] feature Indexed a lower bound to an upper bound Accessng or modfyng any element s O (1) (constant tme) () lower, upper: INTEGER make (l, h: INTEGER) s -- Allocate wth bound l and h. hgh >= low hgher >= low lower upper In memory: stored n contguous locatons (): G s -- Entry of ndex >= lower <= upper put (x: G; ) s -- Replace by x the value of the entry of ndex >= lower <= upper Usng arrays 35 A varant: hash tables 36 your_array: ARRAY [REAL] drectory: HASH_TABLE [METRO_STATION, STRING] create your_array.make (1, 100) your_array.put (35.6, 7) your_array.put ( 45.0, 8) create drectory.make (100) drectory.put (Staton_balard, "BALARD") drectory.put (Staton_montrouge, "MONTROUGE") prnt (your_array. (8)) prnt (drectory. ("MONTROUGE")) 6
37 End of lecture 13 7