Object-Oriented Programming (OOP) Lecture No. 40

Slides:



Advertisements
Similar presentations
DATA STRUCTURES AND ALGORITHMS Prepared by İnanç TAHRALI
Advertisements

Brown Bag #2 Advanced C++. Topics  Templates  Standard Template Library (STL)  Pointers and Smart Pointers  Exceptions  Lambda Expressions  Tips.
Lecture # 21 Chapter 6 Uptill 6.4. Type System A type system is a collection of rules for assigning type expressions to the various parts of the program.
Iterator COMP 401, Spring 2013 Lecture 07 1/31/2013.
CSE 332: C++ templates and generic programming I Motivation for Generic Programming in C++ We’ve looked at procedural programming –Reuse of code by packaging.
Object Oriented Data Structures
1 Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors Sections 3.1, 3.2, 3.3, 3.4 Abstract Data Types (ADT) Iterators Implementation of Vector.
1 Chapter 16-1 Linked Structures Dale/Weems. 2 Chapter 16 Topics l Meaning of a Linked List l Meaning of a Dynamic Linked List l Traversal, Insertion.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 4 Prepared by İnanç TAHRALI.
Programming Language C++ Xulong Peng CSC415 Programming Languages.
Binary Trees Chapter 10. Introduction Previous chapter considered linked lists –nodes connected by two or more links We seek to organize data in a linked.
Lists. Container Classes Many applications in Computer Science require the storage of information for collections of entities e.g. a student registration.
1 Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors Section 3.5, class notes Iterators Generic algorithms.
Recap Visual Perception and Data Visualization Types of Information Display Examples of Diagrams used for Data Display Planning Requirement for Data Visualization.
Object Oriented Programming (OOP) Lecture No. 10.
C arrays are limited: -they are represented by pointers (which may or may not be valid); -Indexes not checked (which means you can overrun your array);
CS212: Object Oriented Analysis and Design Lecture 24: Introduction to STL.
1 Chapter 13-1 Applied Arrays: Lists and Strings Dale/Weems.
Binary Trees Chapter 10. Introduction Previous chapter considered linked lists –nodes connected by two or more links We seek to organize data in a linked.
1 Chapter 3 Lists, Stacks, and Queues Reading: Sections 3.1, 3.2, 3.3, 3.4 Abstract Data Types (ADT) Iterators Implementation of Vector.
CSE 332: C++ STL iterators What is an Iterator? An iterator must be able to do 2 main things –Point to the start of a range of elements (in a container)
Recursion Powerful Tool
Motivation for Generic Programming in C++
CS212: Object Oriented Analysis and Design
Lecture 18 Control Flow.
Template Method Pattern Iterator Pattern
EECE 310: Software Engineering
Programming with ANSI C ++
Standard Template Library
How to be generic Lecture 10
Lecture 7-2 : STL Iterators
Reasoning with invariants
C++ Standard Library.
Generic Programming Techniques in C++
Map interface Empty() - return true if the map is empty; else return false Size() - return the number of elements in the map Find(key) - if there is an.
Prof. Michael Neary Lecture 7: The STL Prof. Michael Neary
Object-Oriented Programming (OOP) Lecture No. 32
Abstract Data Types Iterators Vector ADT Sections 3.1, 3.2, 3.3, 3.4
Lecture 7-2 : STL Iterators
5.1 The Stack Abstract Data Type
Object-Oriented Programming (OOP) Lecture No. 36
Building Java Programs
Object-Oriented Programming (OOP) Lecture No. 39
Building Java Programs
Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors
Lecture 2: Implementing ArrayIntList reading:
CMSC 341 Skip Lists.
Design Patterns Difficult to describe abstractly Elements:
Object-Oriented Programming (OOP) Lecture No. 37
Object Oriented Programming (OOP) Lecture No. 13
Lists - I The List ADT.
Lists - I The List ADT.
Object-Oriented Programming (OOP) Lecture No. 38
Queues Jyh-Shing Roger Jang (張智星)
Object-Oriented Programming (OOP) Lecture No. 33
Introduction to Data Structure
Lecture 8-2 : STL Iterators and Algorithms
COP 3330 Object-oriented Programming in C++
Data Structures and Algorithms Memory allocation and Dynamic Array
Object-Oriented Programming (OOP) Lecture No. 34
Lesson 25 Miscellaneous Topics
Software Design Lecture : 39.
Object-Oriented Programming (OOP) Lecture No. 23
Lecture 2: Bits, Bytes, Ints
Chapter 3 Lists, Stacks, and Queues
The List Container and Iterators
Object Oriented Programming (OOP) Lecture No. 10
Iterator Design Pattern Jim Fawcett CSE776 – Design Patterns Fall 2014
Data Structures & Programming
Presentation transcript:

Object-Oriented Programming (OOP) Lecture No. 40

Recap Generic algorithm requires three operations (++, *, !=) Implementation of these operations in Vector class Problems No support for multiple traversals Supports only a single traversal strategy Inconsistent behavior Operator !=

Cursors A better way is to use cursors A cursor is a pointer that is declared outside the container / aggregate object Aggregate object provides methods that help a cursor to traverse the elements T* first() T* beyond() T* next( T* )

Vector template< class T > class Vector { private: T* ptr; int size; public: Vector( int = 10 ); Vector( const Vector< T >& ); ~Vector(); int getSize() const;

…Vector const Vector< T >& operator =( const Vector< T >& ); T& operator []( int ); T* first(); T* beyond(); T* next( T* ); };

…Vector template< class T > T* Vector< T >::first() { return ptr; } T* Vector< T >::beyond() { return ( ptr + size );

…Vector template< class T > T* Vector< T >::next( T* current ) { if ( current < (ptr + size) ) return ( current + 1 ); // else return current; }

Example – Cursor int main() { Vector< int > iv( 3 ); iv[0] = 10; int* first = iv.first(); int* beyond = iv.beyond(); int* found = find(first,beyond,20); return 0; }

Generic Algorithm template< typename P, typename T > P find( P start, P beyond, const T& x ) { while ( start != beyond && *start != x ) ++start; return start; }

…Cursors This technique works fine for a contiguous sequence such as Vector However it does now work with containers that use complicated data structures There we have to rely on the container traversal operations

Example – Works Fine a b c d e f g … Cursor

Example – Problem a b c d … Cursor

Example – Problem int main() { Set< int > is( 3 ); is.add( 10 ); ET* first = iv.first(); ET* beyond = iv.beyond(); ET* found = find(first, beyond, 20); return 0; }

…Example – Problem template< typename P, typename T > P find( P start, P beyond, const T& x ) { while ( start != beyond && *start != x ) ++start; // Error return start; }

Works Fine template< typename CT, typename ET > P find( CT& cont, const ET& x ) { ET* start = cont.first(); ET* beyond = cont.beyond(); while ( start != beyond && *start != x ) start = cont.next( start ); return start; }

…Works Fine int main() { Set< int > is( 3 ); is.add( 10 ); int* found = find( is, 20 ); return 0; }

Cursors – Conclusion Now we can have more than one traversal pending on the aggregate object a b c d e f g … Cursor 1 Cursor 2

…Cursors – Conclusion However we are unable to use cursors in place of pointers for all containers

Iterators Iterator is an object that traverses a container without exposing its internal representation Iterators are for containers exactly like pointers are for ordinary data structures

Generic Iterators A generic iterator works with any kind of container To do so a generic iterator requires its container to provide three operations T* first() T* beyond() T* next( T* )

Example – Generic Iterator Container Iterator first() beyond() next() … operator * operator ++ …

Generic Iterator template< class CT, class ET > class Iterator { CT* container; ET* index; public: Iterator( CT* c, bool pointAtFirst = true ); Iterator( Iterator< CT, ET >& it ); Iterator& operator ++(); ET& operator *();

…Generic Iterator bool operator !=( Iterator< CT, ET >& it ); };

…Generic Iterator template< class CT, class ET > Iterator< CT, ET >::Iterator( CT* c, bool pointAtFirst ) { container = c; if ( pointAtFirst ) index = container->first(); else index = container->beyond(); }

…Generic Iterator template< class CT, class ET > Iterator< CT, ET >::Iterator( Iterator< CT, ET >& it ) { container = it.container; index = it.index; }

…Generic Iterator template< class CT, class ET > Iterator<CT,ET>& Iterator<CT,ET>:: operator ++() { index = container->next( index ); return *this; }

…Generic Iterator template< class CT, class ET > ET& Iterator< CT, ET >::operator *() { return *index; }

…Generic Iterator template< class CT, class ET > bool Iterator< CT, ET >::operator !=( Iterator< CT, ET >& it ) { if ( container != it.container || index != it.index ) return true; // else return false; }

…Generic Iterator int main() { Vector< int > iv( 2 ); Iterator< Vector<int>, int > it( &iv ), beyond( &iv, false ); iv[0] = 10; iv[1] = 20; Iterator< Vector<int>, int > found = find( it, beyond, 20 ); return 0; }

…Generic Iterator template< typename P, typename T > P find( P start, P beyond, const T& x ) { while ( start != beyond && *start != x ) ++start; return start; }

Iterators – Conclusion With iterators more than one traversal can be pending on a single container Iterators allow to change the traversal strategy without changing the aggregate object They contribute towards data abstraction by emulating pointers