CS112_40_slides-sets - Sets Yinglin WANG CS Dept., SJTU...

Info iconThis preview shows pages 1–5. Sign up to view the full content.

View Full Document Right Arrow Icon
CS112 Fundamentals of Programming Abstractions Sets Yinglin WANG CS Dept., SJTU CS112 Fundamentals of Programming Abstractions Sets When we want to represent a collection of elements where the order is not important, the collection is what mathematicians would call a set. Saying that the order is not important means that we won’ t be asking questions like " what is the second element ?" Instead, we will just be asking whether a given value is a member of the set. We can specify a set by listing its members or by stating a rule that identifies the members. Small even numbers: {2, 4, 6, 8} Single digit primes: {2, 3, 5, 7} Strange numbers: {11, 17, 31, 53, 79} Primary colors: {red, green, blue} Small even numbers: {x | x is even and x < 10} A set is an unordered collection of distinct elements.
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
CS112 Fundamentals of Programming Abstractions Set operations There are some fundamental operations that let us combine sets to make new sets. S1: {2, 4, 6, 8} S2: {2, 3, 5, 7} Union of S1 and S2 S1 S2 {2, 3, 4, 5, 6, 7, 8} Intersection of S1 and S2 S1 S2 {2} Difference of S1 and S2 S1 S2 {4, 6, 8} {1, 3, 5} {2, 4, 6} = (the empty set) CS112 Fundamentals of Programming Abstractions Venn diagrams Venn diagrams are used in mathematics to illustrate set identities. A (B C) (A B) (A C) B C A (B C) A B A C
Background image of page 2
CS112 Fundamentals of Programming Abstractions setADT Our goal is to implement a setADT. These are the characteristics it must have. The set.h interface must support the following operations: Allocating and deallocating sets Adding and deleting elements from a set Testing whether a value is a member of a set High-level operations such as union, intersection,set difference, subset, and equality Some mechanism for iterating over the elements. CS112 Fundamentals of Programming Abstractions As discussed in the text, clients differ in what they would like the type of the elements in the set to be. Fundamental issues: • What is the base type? • How can sets be represented? Clients generally want either efficient sets of integers or general purpose sets of void *'s
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
CS112 Fundamentals of Programming Abstractions setADT One solution is to have two different types of sets. By exporting them from the same interface, we will find that many functions can be exported that will work for both types of sets. This simplifies things for the client. For example, we will only need one function NElements. Clients can use it on either type of set. Solution The setADT will export two classes of sets IntSet sets of integers PtrSet sets of void *'s CS112 Fundamentals of Programming Abstractions Set.h Here are some of the functions in set.h. In some cases we need separate functions for integer sets and pointer sets. The functions check to see that they have received the correct kind of set. For example, if you call AddIntElement and pass it a pointer set, the function will call Error.
Background image of page 4
Image of page 5
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 04/06/2010 for the course CS 112 taught by Professor Yinlinwang during the Spring '08 term at Shanghai Jiao Tong University.

Page1 / 16

CS112_40_slides-sets - Sets Yinglin WANG CS Dept., SJTU...

This preview shows document pages 1 - 5. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online