1 / 21

Today’s Agenda

Today’s Agenda. Tuple Data. Course Agenda. Module 1 – Personal Software Process  Module 2 – Data Driven Programming Data Abstraction  Linear Collections – Lists and Sets  Tuple Data Searching and Sorting. Tuple Types - Motivation. Recall Drama Club Example

asasia
Download Presentation

Today’s Agenda

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Today’s Agenda Tuple Data Sundar B. BITS, Pilani.

  2. Course Agenda • Module 1 – Personal Software Process  • Module 2 – Data Driven Programming • Data Abstraction  • Linear Collections – Lists and Sets  • Tuple Data • Searching and Sorting Sundar B. BITS, Pilani.

  3. Tuple Types - Motivation • Recall Drama Club Example • List of Member elements • Member element represented as “unsigned int” • Professor-in-charge wants a change: • Each Member element will contain an ID, a Name, and a Group. • What should be the new representation? Sundar B. BITS, Pilani.

  4. Tuple Types - Motivation • First attempt: 3 lists (i.e. arrays) • ID array, Name array, and Group array. • Problems: Consider add / delete operations. • Shifting to be done in 3 arrays separately. • 3 arrays and the 3 element values passed as parameters. • Data representation “does not say” the 3 things are related. • Better Solution: 1 list of triples • Each triple is of the form (ID, Name, Group) • add / delete operation: Shifting for one array only. Sundar B. BITS, Pilani.

  5. Tuple Types – Definition • Recall that “a type is a set of values” • So we define a new type • Member = ID x Name x Group assuming types ID, Name and Group are available. • Thus each element of type Member will be a triple (i,n,g) such that • i is in ID, • n is in Name, and • g is in Group. Sundar B. BITS, Pilani.

  6. Tuple Types – Definition • Suppose we further define • Name = String i.e. array of characters and • Group = { A1, A2, B1, B2, C1, C2, D1, D2 } • then • (5, “Micky”, A1) and (7, “Minnie”, C1) will be elements of type Member. • We can generalize to “n-tuples”! T = A1 x A2 x … An n > 0 Sundar B. BITS, Pilani.

  7. Tuple Types – Definition • Often we are interested in “ordered tuples” (5, “Micky”, A1) is not the same as (“Micky”, 5, A1) • Thus if m = (5, “Micky”, A1) then we can refer to m.1 which is 5, m.2 which is “Micky” and m.3 which is A1. Sundar B. BITS, Pilani.

  8. Tuple Types – Definition • But it is more “readable” to select using “labels” than using “numbers”: • m1.i which is 5, • m1.n which is “Micky” • m1.g which is A1 • Thus we have “labeled” tuples. • Also known as records or structures. • Labels also referred to as (field) tags. Sundar B. BITS, Pilani.

  9. Tuple Types – Representation in C /* file: dramaClubMember.h */ typedef unsigned int ID; typedef enum { A1=1, A2, B1, B2, C1, C2, D1, D2 } Group; #define NAME_LEN 50 typedef struct { ID i; char n[NAME_LEN]; Group g; } Member; Sundar B. BITS, Pilani.

  10. Drama Club Revisited • Interfaces for the Drama Club operations depend on Member • Types for the Drama Club list depend on Member Sundar B. BITS, Pilani.

  11. Drama Club Revisited – Interfaces /* file: dramaClubOps.h */ #include “dramaClubList.h” extern ListSize add(Member m, List ms, ListSize n); extern ListSize delete(Member m, List ms, ListSize n); extern ListSize isMember(Member m, List ms, ListSize n); Sundar B. BITS, Pilani.

  12. Drama Club Revisited – Types /* file: dramaClubList.h */ #include “dramaClubMember.h” typedef unsigned int ListSize; #define MAX 1000 // List is an array of at most MAX elements of type Member typedef Member List[MAX]; Sundar B. BITS, Pilani.

  13. Drama Club Revisited - Implementation /* file: dramaClubOps.c */ #include “dramaClubOps.h” ListSize add(Member m, List ms, ListSize n) { } … for (pos=0; pos < n; pos++) { if (compare(ms[pos], m) == GREATER) break; if (compare(ms[pos], m) == EQUAL) return; } … Sundar B. BITS, Pilani.

  14. Drama Club Revisited - Implementation • Function “compare” has been abstracted! • Need to define “compare” so that each member is uniquely determined: • Notion of a key or index needed. • ID is the key in our example – it uniquely determines a Member element in the list. Sundar B. BITS, Pilani.

  15. Drama Club Revisited - Implementation /* file: compare.h */ #include “dramaClubMember.h” typedef enum { LESS = -1, EQUAL = 0, GREATER = 1 } ORDER; /* Pre-condition: m1 and m2 can be ordered by a key field. Post-condition: return EQUAL if keys are equal, return LESS if keys are in order, return GREATER if keys are out of order. */ extern ORDER compare(Member m1, Member m2);

  16. Drama Club Revisited - Implementation /* file: compare.c */ #include “dramaClubMember.h” ORDER compare(Member m1, Member m2) { if (m1.i == m2.i) return EQUAL; else if (m1.i < m2.i) return LESS; else return GREATER; } Sundar B. BITS, Pilani.

  17. Drama Club Revisited – Changes!? • Prof.-in-charge wants following info. added: • First degree or Higher Degree • PS or TS • No problem! • Redefine Member Member = ID x Name x Group x Degree x PSTS where Degree = { FIRST, HIGH } PSTS = { PS, TS } • No other changes needed! Sundar B. BITS, Pilani.

  18. Drama Club Revisited – Changes!? • Prof.-in-charge wants ID to be year-wise: • Year and Year-wise ID • i.e. ID is unique given a Year value • To determine a Member element uniquely you need Year and ID values. • No problem! • Redefine Member • Redefine compare Sundar B. BITS, Pilani.

  19. Drama Club Revisited – Changes!? /* file dramaClubMember.h * typedef unsigned int ID; typedef unsigned int Year; typedef enum { A1=1, A2, B1, B2, C1, C2, D1, D2 } Group; typedef enum { FIRST=1, HIGH=2 } Degree; typedef enum { PS=1, TS=2 } PSTS; #define NAME_LEN 50 typedef struct { Year y; ID i; char n[NAME_LEN]; Degree d; Group g; PSTS p } Member;

  20. Drama Club Revisited – Changes!? /* file: compare.c */ #include “dramaClubMember.h” ORDER compare(Member m1, Member m2) { if (m1.y == m2.y) { if (m1.i == m2.i) return EQUAL; else if (m1.i < m2.i) return LESS; else return GREATER; } else if (m1.y < m2.y) return LESS; else return GREATER; } Sundar B. BITS, Pilani.

  21. Drama Club Revisited – Changes!? • Prof.-in-charge wants Dual-degree info. To be captured!? • Think! Sundar B. BITS, Pilani.

More Related