210 likes | 278 Views
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
E N D
Today’s Agenda Tuple Data Sundar B. BITS, Pilani.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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);
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.
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.
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.
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;
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.
Drama Club Revisited – Changes!? • Prof.-in-charge wants Dual-degree info. To be captured!? • Think! Sundar B. BITS, Pilani.