234122 תמ”מ Abstract Data Types
Transcript of 234122 תמ”מ Abstract Data Types
Copyright 1995 - 2013
C. Gotsman & Y.M. Kimchi
Computer Science Dept. Technion
מ”מת 234122 Abstract Data TypesAbstract Data Types
Page 1
bEgInSlIdE79
Copyright 1995 - 2013 C. Gotsman & Y.M. Kimchi, Computer Science Dept. Technion
Chapter 2
Abstract Data Types
bEgInSlIdE80
Copyright 1995 - 2013 C. Gotsman & Y.M. Kimchi, Computer Science Dept. Technion
Modularity
- functions.- function calls.
Software elements that are concerned with a single taskand are (usually) tightly coupled, should be organized
as modules (or other terms).
Their connection to the outside world should be minimal.
modules
modules
Copyright 1995 - 2013
C. Gotsman & Y.M. Kimchi
Computer Science Dept. Technion
מ”מת 234122 Abstract Data TypesAbstract Data Types
Page 2
bEgInSlIdE81
Copyright 1995 - 2013 C. Gotsman & Y.M. Kimchi, Computer Science Dept. Technion
Principles that justify Modularity
• Separation of concerns
• Encapsulation
• Minimal scope
• Narrow interface
• No Code Duplication
• One Definition
• Preserve Invariants
• Strong typing
• Localization
• Implementation independent (two ways)• Interface is independent of implementation
• Reusable
bEgInSlIdE82
Copyright 1995 - 2013 C. Gotsman & Y.M. Kimchi, Computer Science Dept. Technion
Modularity in C
swap.c
void swap(int *x, int *y)
{int t=*x; *x=*y; *y=t; }
main.c
int main(void)
{ int a=2,b=3;
swap(&a,&b);
}
gcc -c swap.c
gcc -c main.c
gcc main.o swap.o -o swap
• Code may be divided up between a number of physical source files.
• Each source file src.c may be compiled separately and independently using
gcc -c src.c
creating the object file src.o.
• Two (or more) object filessrc1.o, src2.o may be linked to an executable ex using
gcc src1.o src2.o -o ex
is achieved via Multi-Source Compilation
Copyright 1995 - 2013
C. Gotsman & Y.M. Kimchi
Computer Science Dept. Technion
מ”מת 234122 Abstract Data TypesAbstract Data Types
Page 3
bEgInSlIdE83
Copyright 1995 - 2013 C. Gotsman & Y.M. Kimchi, Computer Science Dept. Technion
Declarations and Definitions in Cswap.h
void swap(int*,int*);
swap.c
#include “swap.h”
void swap(int*x,int*y)
{int t=*x; *x=*y; *y=t;}
file1.c
#include “swap.h”
int a,b;
swap(&a,&b); /* OK */
file2.c
#include “swap.h”
int x=swap(3,4);/*Bang*/
• A function declaration informs the compiler of the types of the function arguments, and the type of the returned value (prototype). Declarations may appear many times (as long as they are consistent).
• A function definition contains the function body (implementation). This may appear only once. A definition serves as a declaration.
• All functions should be declared (or defined) before used. If a function is used in a file, but the definition is on a different file, the declarationshould be included into the file.
• In C, an undeclared function is assumed to return int, and its arguments are not checked.
Never use this “feature” !
bEgInSlIdE84
Copyright 1995 - 2013 C. Gotsman & Y.M. Kimchi, Computer Science Dept. Technion
From Source to Executable
Executable
linker
compiler
Processed
preprocessor
Source CodeUser Headerstd lib Header
C lib Object
*.h *.c*.h
*.o
lib*.a
a.out
C code C code
A translation unit
A preprocessing translation unit
Object fileObject file
Object file
Copyright 1995 - 2013
C. Gotsman & Y.M. Kimchi
Computer Science Dept. Technion
מ”מת 234122 Abstract Data TypesAbstract Data Types
Page 4
bEgInSlIdE85
Copyright 1995 - 2013 C. Gotsman & Y.M. Kimchi, Computer Science Dept. Technion
Static Functionsfile1.c
void do_it(double x, double y)
{ ... }
int a,b;
do_it(a,b); /* calls first do_it() */
file2.c
static void do_it(double x,double y)
{ ... }
double a,b;
do_it(a,b); /* calls second do_it()*/
file3.c
double i,j;
do_it(i,j); /* calls first do_it(),
no ambiguities */
• Regular functions are recognized (and may be used) by all C code linked together.
• Static functions are recognized only in the file in which they are defined.
• The linker will not handle two functions with the same name.
bEgInSlIdE86
Copyright 1995 - 2013 C. Gotsman & Y.M. Kimchi, Computer Science Dept. Technion
Abstract Data Type
Application
Interface
An ADT is a module performing well defined functions with a compact interface.
Example: Complex number.
ADT module
Interface should be minimal
and complete
Copyright 1995 - 2013
C. Gotsman & Y.M. Kimchi
Computer Science Dept. Technion
מ”מת 234122 Abstract Data TypesAbstract Data Types
Page 5
bEgInSlIdE87
Copyright 1995 - 2013 C. Gotsman & Y.M. Kimchi, Computer Science Dept. Technion
Example: Complex Number
Interface
• Create/Destroy complex number.
• Add/Subtract/Multiply/Divide/Conjugate complex numbers.
• Read/Print complex number.
Not always welcome
• Get real part.
• Get imaginary part.
• Get absolute value.
• Get arg (the direction).
Even worse
Set functions
bEgInSlIdE88
Copyright 1995 - 2013 C. Gotsman & Y.M. Kimchi, Computer Science Dept. Technion
Information Hiding (part of Encapsulation)
• User of ADT sees only the interface.
• Implementation details are hidden.
Advantages
• Implementation may be changed / improvedwithout user knowing, as long as interface is preserved.
• Provides user with abstract “black box”.
• No technical details to confuse user.
Copyright 1995 - 2013
C. Gotsman & Y.M. Kimchi
Computer Science Dept. Technion
מ”מת 234122 Abstract Data TypesAbstract Data Types
Page 6
bEgInSlIdE89
Copyright 1995 - 2013 C. Gotsman & Y.M. Kimchi, Computer Science Dept. Technion
Example: SET of Elements
Interface
• Create set of specific elements.
• Destroy set.
• Add element to set.
• Remove element from set.
• Is element in set ?
• Is set empty ?
• Enumerate set elements.
• Union of two sets.
• Intersection of two sets.
• Print set.
Copy set.Are two sets equal ?Is a set a subset of another ?Size of a set.
Why should (not) these functions be part of the interface ?
Other possible interface functions:
Interface should be minimal
and complete
bEgInSlIdE90
Copyright 1995 - 2013 C. Gotsman & Y.M. Kimchi, Computer Science Dept. Technion
Example:
ADT files: set.h set.cApplication file: set_app.c
ADT compilation:gcc -c set.c set.o
Application compilation:
gcc set.o set_app.c -o set_app
Implementing ADT’s in C
• Separate header (.h) and body (.c) files.– Interface declarations in header.
– Implementation in body.
• Separate Compilation– ADT body may be compiled to object (.o) file without application.
– Application may be linked with different implementations.
set_app
Copyright 1995 - 2013
C. Gotsman & Y.M. Kimchi
Computer Science Dept. Technion
מ”מת 234122 Abstract Data TypesAbstract Data Types
Page 7
bEgInSlIdE91
Copyright 1995 - 2013 C. Gotsman & Y.M. Kimchi, Computer Science Dept. Technion
• ff
typedef Element ...;
typedef Set ...;
Set SetCreate(...);
int SetAdd(...);
int SetRemove(...);
#include “set.h”
static int find(...)
{ . . . }
Set SetCreate(..)
{ . . . }
int SetAdd(...)
{ . . . }
int SetRemove(...)
{ . . . }
set.h
set.c
set.o
set_app.c
#include “set.h”
. . .
int main()
{
Set s = SetCreate();
. . .
}
!#?#
set_app
!!##$
cc set_app.c set.o -o set_app
gcc -c set.c
ADT Application
bEgInSlIdE92
Copyright 1995 - 2013 C. Gotsman & Y.M. Kimchi, Computer Science Dept. Technion
SET - Possible Implementations
• Array of elements
• Linked list
• Bit array
E1 E2 E3 En
E1 empty E2 En
1 0 0 1 1 . . 0 1
. . .
. .
Following are (only) a few of available container data-structures, that are capable of storing objects in such ways that allow the behaviour (and an interface) of a set. Even these examples are quite abstract, and each may be implemented in various ways.
Copyright 1995 - 2013
C. Gotsman & Y.M. Kimchi
Computer Science Dept. Technion
מ”מת 234122 Abstract Data TypesAbstract Data Types
Page 8
bEgInSlIdE93
Copyright 1995 - 2013 C. Gotsman & Y.M. Kimchi, Computer Science Dept. Technion
Array Implementation of SET• Finite length array of pointers to “elements”.
• Empty cells are marked by NULL.
• New element is placed in first empty cell.
• Element is removed by marking cell as empty.
• Element is searched for exhaustively.
--- --- ------ --- ---
3 --- ------ --- ---
3 5 ------ --- ---
--- 5 ------ --- ---
Create
Add 3
Add 5
Remove 3
Note: The diagram does not reflect the usage of pointers, as stated in the text
bEgInSlIdE94
Copyright 1995 - 2013 C. Gotsman & Y.M. Kimchi, Computer Science Dept. Technion
Other SET Implementations Using an Array
• Existing set elements are moved to fill in holes.
• Elements are sorted in some order (if possible).
• If the array is full, increase its size.
What are the pros and cons of these methods ?
Copyright 1995 - 2013
C. Gotsman & Y.M. Kimchi
Computer Science Dept. Technion
מ”מת 234122 Abstract Data TypesAbstract Data Types
Page 9
bEgInSlIdE95
Copyright 1995 - 2013 C. Gotsman & Y.M. Kimchi, Computer Science Dept. Technion
What are the pros and the consof keeping the list sorted ?
Linked-List Implementation
• Each record represents an element of the set.
• New element is placed at head of list.
• Element is removed by bypassing it.
• Element is searched for exhaustively.
Create
Add 3
Add 5
Remove 3
S
S
S
S
3
35
5 3
bEgInSlIdE96
Copyright 1995 - 2013 C. Gotsman & Y.M. Kimchi, Computer Science Dept. Technion
Bit Array Implementation
• Each possible element is represented by one bit (present / not present).
• Add / Remove by setting a single bit to 1 or 0.
• Search by checking a single bit.
• Intersection / Union by bitwise operators.
• Set domain must be fixed.
Create
Add 3
Add 5
Remove 3
0 0 0 0 0 0
0 0 1 0 0 0
0 0 1 0 1 0
0 0 0 0 1 0
Domain = {1,2,3,4,5,6}
Copyright 1995 - 2013
C. Gotsman & Y.M. Kimchi
Computer Science Dept. Technion
מ”מת 234122 Abstract Data TypesAbstract Data Types
Page 10
bEgInSlIdE97
Copyright 1995 - 2013 C. Gotsman & Y.M. Kimchi, Computer Science Dept. Technion
Array Linked List Bit Array
Set domain size
Maximal set size
Implement
Search complexity
unlimited unlimited limited
limited unlimited limited
easy difficult easy
O(n) O(n) O(1)
Implementation Pros and Cons
bEgInSlIdE98
Copyright 1995 - 2013 C. Gotsman & Y.M. Kimchi, Computer Science Dept. Technion
Q: What is an element ?
A: An element can be taken from a fixed domain.
Examples of domains:– Set of integers.
– Set of vectors.
– Set of structures.
– Set of sets !!
SET Contents
should
Copyright 1995 - 2013
C. Gotsman & Y.M. Kimchi
Computer Science Dept. Technion
מ”מת 234122 Abstract Data TypesAbstract Data Types
Page 11
bEgInSlIdE99
Copyright 1995 - 2013 C. Gotsman & Y.M. Kimchi, Computer Science Dept. Technion
SET Implementation in C
• Array of pointers to void.
• These pointers are castto the specific element type being used.
• User must supply four functions (the semantics) specific to the element domain on set creation.
– compare function.
– copy function.
– free function.
– label function (i.e, get a printable value).
• These four functions uniquely characterizethe set domain and its intended usage
– We may need more than four (or less).
bEgInSlIdE100
Copyright 1995 - 2013 C. Gotsman & Y.M. Kimchi, Computer Science Dept. Technion
Set Implementation - Coding Style
• In contrast to good practice, in many cases we omit braces ( {, } ) around a single statement at an if statement or at a loop statement, e.g,
if (..) instead of if (..) {
Do(); Do();
}
(think what happens if Do() is a statement MACRO and there is an else)
• This is done in order to save lines of code,
since space is limited on each slide.
• Note, however, that in the code, that is fully implemented in this chapter,
there is not a single function longer than 16 lines (a single slide only).
• Something to think about . . .
Copyright 1995 - 2013
C. Gotsman & Y.M. Kimchi
Computer Science Dept. Technion
מ”מת 234122 Abstract Data TypesAbstract Data Types
Page 12
bEgInSlIdE101
Copyright 1995 - 2013 C. Gotsman & Y.M. Kimchi, Computer Science Dept. Technion
set.h - type definitions
#ifndef SET_HDR_ /* ‘{‘ */
#define SET_HDR_
typedef enum {FALSE, TRUE} bool; /* C99 has a faked type bool */
/* The following is too primitive, but it’s just an example */
typedef enum {SUCCESS = 1, FAILURE = -1} Result;
typedef void* Element;
typedef struct set_rec* Set;
/* A helper type, for external communication. It should have been */
/* an ADT by itself – you will be able to fill in the details by yourself */
typedef char const **StrSeq; /* NULL terminated string-sequence */
StrSeq StrSeqPrint (StrSeq);
void StrSeqDestroy(StrSeq); /* Don’t assume knowing its structure */
Note that the contents of the structure set_rec is still unknown.
bEgInSlIdE102
Copyright 1995 - 2013 C. Gotsman & Y.M. Kimchi, Computer Science Dept. Technion
set.h - ADT Interface DeclarationsSet SetCreate (bool cmp(Element,Element),
/* These parameters */ Element cpy(Element),
/* are pointers */ char* lbl(Element),
/* to functions */ void fre(Element));
void SetDestroy (Set);
Result SetAdd (Set,Element);
Result SetRemove (Set,Element);
bool SetIsIn (Set,Element);
int SetSize (Set);
int SetMaxSize (Set); /* 0 (or -1) for unlimited */
Set SetIntersection (Set,Set);
Set SetUnion (Set,Set);
StrSeq Set2StrSeq (Set);
void SetPrint (Set);
#define SetIsEmpty(s) ((bool)(0 == SetSize(s)))
/* The most primitive iterator - nested loops are forbidden! */
Element SetNext (Set);
Element SetFirst (Set);
#define SET_FOREACH(e,s) \
for (e=SetFirst(s);e!=NULL;e=SetNext(s))
#endif /* SET_HDR_ ‘}’ */
Copyright 1995 - 2013
C. Gotsman & Y.M. Kimchi
Computer Science Dept. Technion
מ”מת 234122 Abstract Data TypesAbstract Data Types
Page 13
bEgInSlIdE103
Copyright 1995 - 2013 C. Gotsman & Y.M. Kimchi, Computer Science Dept. Technion
#include <stdio.h>
#include <stdlib.h> /* A simple application */
#include "set.h" /* using sets of integers. */
#define LABEL_SIZE 20
#define TO_INT(e) (*(int*)(e))
#define ALLOCATE(var,type) \
{ if((var =(type *) malloc(sizeof(type))) == NULL)\
{ fprintf(stderr,"Cannot allocate\n"); exit(1);}}
char* int_lbl(Element e)
{ char *s;
if ((s = malloc(LABEL_SIZE+1)) == NULL) return NULL;
sprintf(s,"%d",TO_INT(e));
return s;
}
set_app.c
bEgInSlIdE104
Copyright 1995 - 2013 C. Gotsman & Y.M. Kimchi, Computer Science Dept. Technion
set_app.c
bool int_cmp(Element e1,Element e2)
{ return (bool)(TO_INT(e1) == TO_INT(e2));
}
Element int_cpy(Element e)
{ int *j;
ALLOCATE(j,int);
*j = TO_INT(e);
return j;
}
Copyright 1995 - 2013
C. Gotsman & Y.M. Kimchi
Computer Science Dept. Technion
מ”מת 234122 Abstract Data TypesAbstract Data Types
Page 14
bEgInSlIdE105
Copyright 1995 - 2013 C. Gotsman & Y.M. Kimchi, Computer Science Dept. Technion
int main(void)
{ Set s1 = SetCreate(int_cmp,int_cpy,int_lbl,free),
s2 = SetCreate(int_cmp,int_cpy,int_lbl,free);
for (int j = 0; j < 20; j += 2) { // C99
SetAdd(s1,&j);
}
for (int j = 0; j < 12; j += 3) {
SetAdd(s2,&j);
}
Set s3 = SetUnion(s1,s2), /* C99 */
s4 = SetIntersection(s1,s2);
SetPrint(s1); SetPrint(s2);
SetPrint(s3); SetPrint(s4);
SetDestroy(s1); SetDestroy(s2);
SetDestroy(s3); SetDestroy(s4);
return 0;
}
set_app.c
bEgInSlIdE106
Copyright 1995 - 2013 C. Gotsman & Y.M. Kimchi, Computer Science Dept. Technion
Makefile
set.o: set.h set.c
gcc -c set.c
set_app: set_app.c set.h set.o
gcc set_app.c set.o -o set_app
ADT
Application
Client is not interested in(and does not have) set.c
output of set_app
0 2 4 6 8 10 12 14 16 18
0 3 6 9
0 2 4 6 8 10 12 14 16 18 3 9
0 6
Copyright 1995 - 2013
C. Gotsman & Y.M. Kimchi
Computer Science Dept. Technion
מ”מת 234122 Abstract Data TypesAbstract Data Types
Page 15
bEgInSlIdE107
Copyright 1995 - 2013 C. Gotsman & Y.M. Kimchi, Computer Science Dept. Technion
set.c - Implementation as array#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include “set.h”
#define MAX_SET_SIZE 1000
struct set_rec {
Element elem[MAX_SET_SIZE];
int n_elem; /* set size */
int last; /* index of last element */
int curr; /* index of iterator */
bool (*lm_cmp) (Element,Element);
Element (*lm_cpy) (Element);
char* (*lm_lbl) (Element);
void (*lm_fre) (Element);
};
#define CREATE_AS(s) \
SetCreate((s)->lm_cmp,(s)->lm_cpy,(s)->lm_lbl,(s)->lm_fre)
bEgInSlIdE108
Copyright 1995 - 2013 C. Gotsman & Y.M. Kimchi, Computer Science Dept. Technion
set.c - Internal functions
/* find element in set */
static int find(Set s, Element e)
{ for (int j = 0; j < s->last; j++) { // C99
if (s->elem[j]!=NULL && s->lm_cmp(s->elem[j],e))
return j;
}
return FAILURE;
}
/* find first empty position in array */
static int find_empty(Set s)
{ int j;
if (s->n_elem == MAX_SET_SIZE) return FAILURE;
for (j = 0; j < s->last && s->elem[j] != NULL; j++)
;
return j; // j cannot be local in for-loop
}
Copyright 1995 - 2013
C. Gotsman & Y.M. Kimchi
Computer Science Dept. Technion
מ”מת 234122 Abstract Data TypesAbstract Data Types
Page 16
bEgInSlIdE109
Copyright 1995 - 2013 C. Gotsman & Y.M. Kimchi, Computer Science Dept. Technion
Set SetCreate(bool lm_cmp(Element,Element),
Element lm_cpy(Element),
char* lm_lbl(Element),
void lm_fre(Element))
{ Set s = (Set) malloc(sizeof(struct set_rec));
if (s == NULL) return NULL;
s->lm_cmp = lm_cmp;
s->lm_cpy = lm_cpy;
s->lm_lbl = lm_lbl;
s->lm_fre = lm_fre;
s->n_elem = s->curr = s->last = 0;
return s; /* A pointer to a record on the heap */
}
void SetDestroy(Set s)
{ int i;
if (s == NULL) return;
for (i = 0; i < s->last; i++)
if (s->elem[i] != NULL) s->lm_fre(s->elem[i]);
free(s); return;
}
set.c - Interface Functions
bEgInSlIdE110
Copyright 1995 - 2013 C. Gotsman & Y.M. Kimchi, Computer Science Dept. Technion
Result SetAdd(Set s, Element e)
{ int empty;
if (find(s,e) != FAILURE) return SUCCESS;
if ((empty = find_empty(s)) == FAILURE) return FAILURE;
s->elem[empty] = s->lm_cpy(e);
s->n_elem++;
if (empty == s->last) s->last++;
return SUCCESS;
}
Result SetRemove(Set s, Element e)
{ int j = find(s,e);
if (j == FAILURE) return FAILURE;
s->lm_fre(s->elem[j]);
s->elem[j] = NULL;
s->n_elem--;
return SUCCESS;
}
set.c
A hidden bugin this function
Note the semantics
for SUCCESS
Copyright 1995 - 2013
C. Gotsman & Y.M. Kimchi
Computer Science Dept. Technion
מ”מת 234122 Abstract Data TypesAbstract Data Types
Page 17
bEgInSlIdE111
Copyright 1995 - 2013 C. Gotsman & Y.M. Kimchi, Computer Science Dept. Technion
bool SetIsIn(Set s, Element e)
{ return (bool)(find(s,e) != FAILURE);
}
int SetSize(Set s)
{ return s->n_elem;}
int SetMaxSize(Set s) /* returns 0 (or –1) if unlimited */
{ return MAX_SET_SIZE;} /* Cannot be a MACRO */
Element SetNext(Set s)
{ while (s->curr<s->last && s->elem[s->curr] == NULL) s->curr++;
if (s->curr == s->last) return NULL;
return s->elem[s->curr++];
}
Element SetFirst(Set s)
{ s->curr = 0;
return SetNext(s);
}
set.c
bEgInSlIdE112
Copyright 1995 - 2013 C. Gotsman & Y.M. Kimchi, Computer Science Dept. Technion
set.c
Set SetIntersection(Set s1, Set s2)
{ int i;
Set s3 = CREATE_AS(s1);
if (s3 == NULL) return NULL;
for (i = 0; i < s1->last; i++) {/* No SET_FOREACH */
Element e = s1->elem[i];
if (e == NULL) continue;
if (SetIsIn(s2,e)) {
if (SetAdd(s3,e) == FAILURE) { /* clean up */
SetDestroy(s3);
return NULL;
}
}
}
return s3;
}
These lines are going to be duplicated.
Look at set.c (addendum)for an improved implementation
Are s1 and s2
sets of the same kind ?
Copyright 1995 - 2013
C. Gotsman & Y.M. Kimchi
Computer Science Dept. Technion
מ”מת 234122 Abstract Data TypesAbstract Data Types
Page 18
bEgInSlIdE113
Copyright 1995 - 2013 C. Gotsman & Y.M. Kimchi, Computer Science Dept. Technion
Yes, they are duplicated.
Look at set.c (addendum)
set.c
Set SetUnion(Set s1, Set s2)
{ int i;
Set s3 = CREATE_AS(s1);
if (s3 == NULL) return NULL;
for (i = 0; i < s1->last; i++) {
Element e = s1->elem[i];
if (e == NULL) continue;
if (SetAdd(s3,e) == FAILURE) {
SetDestroy(s3);
return NULL;
} }
for (i = 0; i < s2->last; i++) {
Element e = s2->elem[i];
if ((e == NULL) continue;
if (SetAdd(s3,e) == FAILURE) {
SetDestroy(s3);
return NULL;
} }
return s3;
}
Yes, they are duplicated.
Look at set.c (addendum)
Are s1 and s2
sets of the same kind ?
bEgInSlIdE114
Copyright 1995 - 2013 C. Gotsman & Y.M. Kimchi, Computer Science Dept. Technion
void SetPrint(Set s)
{ Element e;
int i;
for (i = 0; i < s->last; i++) { /*DO NOT use SET_FOREACH */
char *label;
if ((e = s->elem[i]) == NULL) continue;
label = s->lm_lbl(e);
if (label==NULL) {
printf("ILLEGAL ");
}else {
printf("%s ",label);
free(label); /* A constraint on s->lm_lbl() */
}
}
printf("\n");
}
set.c
Look at set.c (addendum)for an improved version
Copyright 1995 - 2013
C. Gotsman & Y.M. Kimchi
Computer Science Dept. Technion
מ”מת 234122 Abstract Data TypesAbstract Data Types
Page 19
bEgInSlIdE115
Copyright 1995 - 2013 C. Gotsman & Y.M. Kimchi, Computer Science Dept. Technion
static Set addOrDestroy(Set s, Element e)
{ if (SetAdd(s,e) != FAILURE) return s; /* s is never NULL */
SetDestroy(s);
return NULL;
}
Set SetIntersection(Set s1, Set s2)
{ int i;
Set s3 = CREATE_AS(s1);
if (s3 == NULL) return NULL;
for (i = 0; i < s1->last; i++) { /* NO SET_FOREACH */
Element e = s1->elem[i];
if (e != NULL && SetIsIn(s2,e))
if (!addOrDestroy(s3,e)) return NULL;
}
return s3;
}
set.c (addendum)
Yes, the set s still exists
A helper function
Are s1 and s2
sets of the same kind ?
bEgInSlIdE116
Copyright 1995 - 2013 C. Gotsman & Y.M. Kimchi, Computer Science Dept. Technion
set.c (addendum)
Set SetUnion(Set s1, Set s2)
{ Set s3 = CREATE_AS(s1);
if (s3 != NULL)
if (!addToNew(s3,s1) || !addToNew(s3,s2)) return NULL;
return s3;
}
static Set addToNew(Set s0, Set s1)
{ /* C99 */
for (int i = 0; i < s1->last; i++) { /* NO SET_FOREACH */
Element e = s1->elem[i];
if (e != NULL && !addOrDestroy(s0,e)) return NULL;
}
return s0;
}
A helper function
Yes, the set s0 still exists
Are s1 and s2
sets of the same kind ?
Copyright 1995 - 2013
C. Gotsman & Y.M. Kimchi
Computer Science Dept. Technion
מ”מת 234122 Abstract Data TypesAbstract Data Types
Page 20
bEgInSlIdE117
Copyright 1995 - 2013 C. Gotsman & Y.M. Kimchi, Computer Science Dept. Technion
static char const * const illegal = “ILLEGAL”;
StrSeq Set2StrSeq(Set s) /* Do not use SET_FOREACH */{ StrSeq str_seq = malloc((s->n_elem+1) * sizeof(char*));
if (str_seq == NULL) return NULL;
str_seq[0] = NULL; /* Always keep last element NULL */
for (int i = 0, j = 0; i < s->last; i++) { // C99
Element e = s->elem[i];
if (e != NULL) {
str_seq[j] = s->lm_lbl(e); /*A constraint on s->lm_lbl() */
if (str_seq[j] == NULL) str_seq[j] = illegal;
str_seq[++j] = NULL; /* Always keep the invariant */
}
}
return str_seq;
}
set.c (addendum)
bEgInSlIdE118
Copyright 1995 - 2013 C. Gotsman & Y.M. Kimchi, Computer Science Dept. Technion
StrSeq StrSeqPrint(StrSeq seq)
{ if (!seq) return NULL;
int i = 0; // C99
while (seq[i] != NULL) { /*NULL terminated*/
printf("%s ", seq[i++]);
}
printf("\n");
return seq;
}
set.c (addendum)
Copyright 1995 - 2013
C. Gotsman & Y.M. Kimchi
Computer Science Dept. Technion
מ”מת 234122 Abstract Data TypesAbstract Data Types
Page 21
bEgInSlIdE119
Copyright 1995 - 2013 C. Gotsman & Y.M. Kimchi, Computer Science Dept. Technion
void StrSeqDestroy(StrSeq seq)
{ if (!seq) return;
int i = 0; // C99
while (seq[i] != NULL) { /*NULL terminated*/
if (seq[i] != illegal) free(seq[i]);
i++
}
free(seq);
return;
}
void SetPrint(Set s)
{ StrSeqDestroy(StrSeqPrint(Set2StrSeq(s)));
return;
}
set.c (addendum)
bEgInSlIdE120
Copyright 1995 - 2013 C. Gotsman & Y.M. Kimchi, Computer Science Dept. Technion
Q: What is an element ?
A: An element can be taken from a fixed domain.
But real problems are not that simple:
• A school of persons have students, lecturers, administrators, …
• A store has many kinds of items to be sold.
Can a Set contain elements of different types?
A Concluding Remark
should
Copyright 1995 - 2013
C. Gotsman & Y.M. Kimchi
Computer Science Dept. Technion
מ”מת 234122 Abstract Data TypesAbstract Data Types
Page 22
bEgInSlIdE121
Copyright 1995 - 2013 C. Gotsman & Y.M. Kimchi, Computer Science Dept. Technion
• Sticking to the Set example, each such object needs– compare, copy, free, label functions.
• And probably more … - all depend on the object’s type.
• Moreover, each such object (depending on its type) needs specific data members (a student is not admin).
A Concluding Remark (cont.)
We end up with a family of objects that look like this:
struct family {
void *data;
void *operations;
};
Oops…
Set contains only elements of a single type (family)
struct student {enum Family type; // STUDENT
char *name; ...
int *grades; ...
}; Each object has its own
struct student_ops {
student* (*copy)(void*); ...
double (*average)(student*);...
};
A unique object for each type
bEgInSlIdE122
Copyright 1995 - 2013 C. Gotsman & Y.M. Kimchi, Computer Science Dept. Technion