CS112_41_slides-integer sets

CS112_41_slides-integer sets - Integer Sets Yinglin WANG CS...

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 Integer Sets Yinglin WANG CS Dept., SJTU CS112 Fundamentals of Programming Abstractions About the final grade It will be computed as follows: Programs 40% The first 2 Written home work 8% Your attendance and the test 2% Finals 50%
Background image of page 1

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

View Full DocumentRight Arrow Icon
Programming Abstractions • Here is the actual code. It is another use of iterators with sets. Implementing a setADT setADT Union(setADT s1, setADT s2) { iteratorADT iterator; setElementT element; setADT set; if (s1–>class != s2–>class || s1–>cmpFn != s2–>cmpFn) { Error("Union: Set types do not match"); } set = NewSet(s1–>class, s1–>cmpFn); iterator = NewIterator(s1); while (StepIterator(iterator, &element)) { AddERef(set, &element); } FreeIterator(iterator); iterator = NewIterator(s2); while (StepIterator(iterator, &element)) { AddERef(set, &element); } FreeIterator(iterator); return (set); } CS112 Fundamentals of Programming Abstractions Implementing a polymorphic iterator A mechanism for any collection type to export an iterator. The problem: the items in different collection types can vary in size and structure. • We are going to give you an advance view of the next topic you will read about-- building a polymorphic iterator. • What we mean by that is building an iteratorthat will work for any collection type--sets, symbol tables , etc.
Background image of page 2
CS112 Fundamentals of Programming Abstractions Implementing a polymorphic iterator NewIterator builds a list of the items in the collection Recall what an iterator does when it is created. It builds a linked list of the items in the collection. CS112 Fundamentals of Programming Abstractions Implementing a polymorphic iterator NewIterator builds a list of the items in the collection Now it has to work no matter what the size of the elements in collection type. How can we make this work for different collection types?
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 Implementing a polymorphic iterator Solution: Each collection type will store a pointer to a function that knows how to build an appropriate iterator. setADT set; iteratorADT it; ... it = NewIterator(set); • The way we can make this work is to have each collection type have a function that can be used to build an iterator. But that function is going to have to be called from inside iterator.c. Next question: how can code in iterator.c know how to find the iterator building function in a variety of ADTs, each with a different CDT whose structure is hidden?
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 / 19

CS112_41_slides-integer sets - Integer Sets Yinglin WANG CS...

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