1 Generic Positional Containers and Double-Ended Queues.

Slides:



Advertisements
Similar presentations
1 Linked lists Sections 3.2, 3.3, 3.5 Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors.
Advertisements

Lists: An internal look
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 18 Stacks.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 18: Stacks And Queues.
The Standard Template Library – part 2. auto_ptr Regular pointers may cause memory leaks Regular pointers may cause memory leaks void f() { SomeClass.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
Class template Describing a generic class Instantiating classes that are type-specific version of this generic class Also are called parameterized types.
1 Queues CPS212 Gordon College. 2 Introduction to Queues A queue is a waiting line – seen in daily life –Real world examples – toll booths, bank, food.
Main Index Contents 11 Main Index Contents Container Types Container Types Sequence Containers Sequence Containers Associative Containers Associative Containers.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 18: Stacks and.
Lecture 11 Standard Template Library Stacks, Queue, and Deque Lists Iterators Sets Maps.
CSIS 123A Lecture 12 Templates. Introduction  C++ templates  Allow very ‘general’ definitions for functions and classes  Type names are ‘parameters’
Data Structures Using C++ 2E
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 CSC 222: Computer Programming II Spring 2004 Pointers and linked lists  human chain analogy  linked lists: adding/deleting/traversing nodes  Node.
CNS  Sequences  vector,deque,list,(string),forward_list  Container Adapters  queue, stack, priority_queue  Associative Containers  set, unordered_set.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 12 Prepared by İnanç TAHRALI.
Adapted from instructor resources Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights.
Generic Positional Containers and Double-Ended Queues.
Copyright © 2012 Pearson Education, Inc. Chapter 18: Stacks And Queues.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 26: Exam 2 Preview.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 18 Stacks and Queues.
STL multimap Container. STL multimaps multimaps are associative containers –Link a key to a value –AKA: Hashtables, Associative Arrays –A multimap allows.
1. The term STL stands for ? a) Simple Template Library b) Static Template Library c) Single Type Based Library d) Standard Template Library Answer : d.
Friends & Standard Template Library CSCI3110 Advanced Data Structures Lecturer: Dr. Carroll and Nan Chen.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Stacks.
Review of Stacks and Queues Dr. Yingwu Zhu. How does a Stack Work? Last-in-First-out (LIFO) data structure Adding an item Push operation Removing an item.
A Generic List Class and Linked Lists Andy Wang Data Structures, Algorithms, and Generic Programming.
Standard Template Library The Standard Template Library was recently added to standard C++. –The STL contains generic template classes. –The STL permits.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
Copyright © 2012 Pearson Education, Inc. Chapter 17: Linked Lists.
Queue Queue – First In / First Out (FIFO) data structure that supports two operations: push for adding new element at the end of the queue pop for removing.
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Chapter 18: Stacks and Queues.
Lecture 7 : Intro. to STL (Standard Template Library)
ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo,
A gentle introduction to the standard template library (STL) Some references:
Data Structures Using C++1 Chapter 3 Pointers and Array-Based Lists.
1 STL Containers Copyright Kip Irvine, All rights reserved. Only students enrolled in a class at Florida International University may copy or print.
Linked lists. Data structures to store a collection of items Data structures to store a collection of items are commonly used Typical operations on such.
The List ADT Reading: Sections 3.2, 3.3, 3.5.
Exam Review 2 Chapter 5 – 9 CSC212 FG CS Dept, CCNY.
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.
1 Queues Chapter 4. 2 Objectives You will be able to Describe a queue as an ADT. Build a dynamic array based implementation of a queue ADT.
Lecture 7.  There are 2 types of libraries used by standard C++ The C standard library (math.h) and C++ The C++ standard template library  Allows us.
“The desire for safety stands against every great and noble enterprise.” – Tacitus Thought for the Day.
Chapter 17 – Templates. Function Templates u Express general form for a function u Example: template for adding two numbers Lesson 17.1 template Type.
CSCI  Sequence Containers – store sequences of values ◦ vector ◦ deque ◦ list  Associative Containers – use “keys” to access data rather than.
Standard Template Library a collection of useful tools.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 18: Stacks and Queues.
18 Chapter Stacks and Queues
CS505 Data Structures and Algorithms
Chapter 18: Stacks and Queues.
The List ADT Sections 3.2, 3.3, 3.5 Introduction
Vectors Holds a set of elements, like an array
Prof. Michael Neary Lecture 7: The STL Prof. Michael Neary
CMSC 341 Lecture 5 Stacks, Queues
Abstract Data Types Iterators Vector ADT Sections 3.1, 3.2, 3.3, 3.4
Chapter 19: Stacks and Queues.
Object Oriented Programming COP3330 / CGS5409
Generic Positional Containers and Double-Ended Queues
Chapter 18: Linked Lists.
Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors
Abstraction: Generic Programming, pt. 2
Doubly Linked List Implementation
Lists - I The List ADT.
Lists - I The List ADT.
Lab4 problems More about templates Some STL
Doubly Linked List Implementation
Chapter 3 Lists, Stacks, and Queues
Presentation transcript:

1 Generic Positional Containers and Double-Ended Queues

2 Generic Positional Container A generic container C that is –Organized and accessed by position The order of elements in container is determined by the order in which they are inserted into container –Sufficient to support either: push_front(), pop_front(), front() or push_back(), pop_back(), back() –Supporting associated iterators C ::Iterator is a “pContainer”, for short –Also known as “sequence containers” Examples: Vector, List, Stack, Queue, and Deque

3 Double-Ended Queue Deque (pronounced ‘Deck’) Deque operations –Push/Pop at either end –Retrieve data from either end –Data of proper type Assumptions on element type T (proper type) –Constructor T() and destructor ~T() –Copy constructor –Assignment operator=

4 Specifying Deque Requirements for Deque –O(1) average runtime, push_front(t), pop_front(), front() push_back(t), pop_back(), back() –O(size()) space –O(1) time and space for iterator operations –Random access iterator (for typical array-based implementation) Bracket operator ([ ]), also known as Pointer arithmetic

5 Deque Implementation Plan Circular array –Protected array content of size content_size –Content wraps around the end of the array to the beginning. –Illusion: content[content_size] == content[0] Relative Indexing –Protected integers begin, end –Bracket [] Operator Similar to Vector Element position relative to begin Front element is content[begin] Back element is content[end – 1] –Size is (end – begin + content_size) % content_size

6 Deque ::iterator Implementation Plan Public interface –Start with the public interface like that of List ::iterator i.e. like the iterator for vector or linked list –Add bracket operator

7 Deque D Illustrated content_size = 8 D.empty() == true beginend content

8 Deque D Illustrated (2) content_size = 8 D.push_back(‘M’) M beginend content

9 Deque D Illustrated (3) content_size = 8 D.push_back(‘e’) Me beginend content

10 Deque D Illustrated (4) content_size = 8 D.push_back(‘r’) Mer beginend content

11 Deque D Illustrated (5) content_size = 8 D.push_back(‘r’) Merr beginend content

12 Deque D Illustrated (6) content_size = 8 D.push_back(‘y’) Merry beginend content

13 Deque D Illustrated (7) content_size = 8 D.pop_front() O(1) erry beginend content

14 Deque D Illustrated (8) content_size = 8 D.pop_front() rry beginend content

15 Deque D Illustrated (9) content_size = 8 D.push_back(‘G’) rryG beginend content

16 Deque D Illustrated (10) content_size = 8 D.push_back(‘o’) D.size() == (7 – 2 + 8) % rryGo beginend content

17 Deque D Illustrated (11) content_size = 8 D.push_back(‘A’) D.size() = (0 – 2 + 8) % rryGoA beginend content

18 Deque D Illustrated (12) content_size = 8 D.push_back(‘r’) D.size() = (1 – 2 + 8) % rrryGoA beginend content

19 Deque D Illustrated (13) D.size() == content_size – 1 Now what? –Return full or –Double the capacity (as with Vector) rrryGoA beginend content

20 Defining Deque template class Deque { public: // type definitions typedef T value_type; typedef DequeIterator iterator; // constructors, destructor Deque(); Deque(size_t, const T&); Deque(const Deque &);// copy Deque(Deque &&);// move ~Deque(); // member operators Deque & operator = (const Deque &); // copy assignment Deque & operator=(Deque &&);// move assignment T& operator [] (size_t) const; // generic display methods void Display (ostream& os, char ofc = ‘ ') const; void Dump (ostream& os) const;

21 Defining Deque (2) // Container class protocol int Empty () const; size_t Size () const; int push_front (const T&); int pop_front (); int push_back (const T&); int pop_back (); void Clear (); T& Front () const; T& Back () const; // and move version of push_front and push_back // iterator support friend class DequeIterator ; iterator begin() const; iterator end() const; protected: // classic circular array implementation T* content; size_t content_size, begin, end; };

22 Defining Deque (3) // operator overloads (friend status not required) template ostream& operator & a); template int operator==(const Deque &, const Deque &); template int operator!=(const Deque &, const Deque &);

23 Defining DequeIterator template class DequeIterator { friend class Deque ; public: // terminology support typedef T value_type; // constructors DequeIterator(); DequeIterator(const Deque & Q); DequeIterator(const DequeIterator & I); // information/access T& retrieve() const; // return ptr to current Tval int valid() const; // cursor is valid element

24 Defining DequeIterator (2) // various operators int operator==(const DequeIterator & I2) const; int operator!=(const DequeIterator & I2) const; T& operator*() const; // return reference to current Tval T& operator[] (size_t i) const; //return reference to Tval at index i DequeIterator & operator=(const DequeIterator & I); DequeIterator & operator++(); // prefix DequeIterator operator++(int); // postfix DequeIterator & operator--(); // prefix DequeIterator operator--(int); // postfix

25 Defining DequeIterator (3) // pointer arithmetic long operator-(const DequeIterator & I2) const; DequeIterator & operator+=(long n); DequeIterator & operator-=(long n); DequeIterator operator+(long n) const; DequeIterator & operator+=(int n); DequeIterator & operator-=(int n); DequeIterator operator+(int n) const; DequeIterator & operator+=(unsigned long n); DequeIterator & operator-=(unsigned long n); DequeIterator operator+(unsigned long n) const; DequeIterator & operator+=(unsigned int n); DequeIterator & operator-=(unsigned int n); DequeIterator operator+(unsigned int n) const;

26 Defining DequeIterator (3) // Initializers void Initialize (const Deque & Q); void rInitialize (const Deque & Q); protected: const Deque * Qptr; size_t index; };

27 Implementing Deque Default constructor template Deque ::Deque() : content{nullptr}, begin{0}, end{0}, content_size{0} { content = new (nothrow) T[default_content_size]; if (content == nullptr) { // error } content_size = default_content_size; } static const size_t default_content_size = 10; // another way, using exception handling // try { // content = new T[default_content_size]; // } // catch (bad_alloc&) { // report error // }

28 Implementing Deque (2) Copy and move constructors template Deque ::Deque(const Deque & Q) : content_size(Q.content_size), begin(Q.begin), end(Q.end) { content = new T[content_size]; // error handling if memory is not properly allocated. for (size_t j = 0; j < content_size; j++) { content[j] = Q.content[j]; } Template Deque ::Deque(Deque &&Q) : content_size(Q.content_size), begin(Q.begin), end(Q.end), content(Q.content) { Q.content = nullptr; Q.content_size = 0; Q.begin = Q.end = 0; }

29 Implementing Deque (3) Copy and move assignment opeators template Deque & Deque ::operator=(const Deque & Q) { if (this != &Q) { T* newcontent = new T[Q.content_size]; // check for allocation delete[] content; content = newcontent; content_size = Q.content_size; begin = Q.begin; end = Q.end; // copy queue elements } return *this; } template Deque & Deque ::operator=(Deque && Q) { std::swap(content, Q.content); std::swap(content_size, Q.content_size); std::swap(begin, Q.begin); std::swap(end, Q.end); return *this; }

30 Implementing Deque (4) Index operator template T& Deque ::operator[] (size_t i) const { if (size() <= i) { // error } return content[(i + begin) % content_size]; } Display functions template void Deque ::Display(ostream& os, char ofc) const { for (size_t j = 0; j < size(); ++j) { os << operator[](j); os << ofc; } template void Deque ::Dump(ostream& os) const { for (size_t j = 0; j < content_size; ++j) { // print } }

31 Implementing Deque (5) operator overloads template ostream operator & Q) { Q.Display(os); return(os); } template int operator==(const Deque & Q1, const Deque & Q2) { if (Q1.size() != Q2.size()) { return 0; } for (size_t j = 0; j < Q1.size(); ++j) { if (Q1[j] != Q2[j]) { return 0; } } return 1; } template int operator!=(const Deque & Q1, const Deque & Q2) { return !(Q1 == Q2); }

32 Implementing Deque (6) Container class protocol template size_t Deque ::Size() const { return (end – begin + content_size) % content_size; } template int Deque ::empty() const { return begin == end; } template void Deque ::Clear() { begin = end = 0; }

33 Implementing Deque (7) template T& Deque ::front() const { // check for empty deque… return content[begin]; } template T& Deque ::back() const { // check for empty deque… if (end == 0) return content[content_size - 1]; return content[end - 1]; // or return content[(end – 1 + content_size) % content_size]; }

34 Implementing Deque (8) template int Deque ::push_back(const T& Tval) { if (size() + 1 >= content_size) { // deque is full unsigned j, k; size_t newcontent_size = 2 * content_size; if (content_size == 0) newcontent_size = 2; T* newcontent = new T[newcontent_size]; // check for allocation error for (j = k = begin; j != end; j = (j + 1) % content_size, ++k) { newcontent[k] = content[j]; } if (end < begin) {end += content_size; } delete[] content; content = newcontent; content_size = newcontent_size; } content[end] = Tval; end = (end + 1) % content_size; return 1; } How to implement the move version of the push_back function?

35 Implementing Deque (9) template int Deque ::push_front(const T& Tval) { if (size() + 1 >= content_size) { // deque is full unsigned j, k; size_t newcontent_size = 2 * content_size; if (content_size == 0) newcontent_size = 2; T* newcontent = new T[newcontent_size]; // check for allocation error for (j = k = begin; j != end; j = (j + 1) % content_size, ++k) { newcontent[k] = content[j]; } if (end < begin) { end += content_size; } delete[] content; content = newcontent; content_size = newcontent_size; } begin = (begin – 1 + content_size) % content_size; content[begin] = Tval; return 1; } How to implement the move version of push_front()?

36 Implementing Deque (10) template int Deque ::pop_front() { if (begin == end) return 0; begin = (begin + 1) % content_size; return 1; } template int Deque ::pop_back() { if (begin == end) return 0; end = (end – 1 + content_size) % content_size; return 1; }

37 Implementing Deque (11) Iterator support template DequeIterator Deque ::begin() const { Deque ::iterator I; I.Qptr = this; I.index = 0; return I; } template DequeIterator Deque ::end() const { Deque ::iterator I; I.Qptr = this; I.index = size(); return I; }

38 Implementing DequeIterator Constructors template DequeIterator ::DequeIterator() : Qptr{nullptr}, index{0} { } template DequeIterator ::DequeIterator(const Deque & Q) : Qptr{&Q}, index{0} { } template DequeIterator ::DequeIterator(const DequeIterator & I) : Qptr{I.Qptr}, index{I.index} { }

39 Implementing DequeIterator (2) Initialization routines template void DequeIterator ::Initialize(const Deque & Q) { Qptr = &Q; index = 0; } template void DequeIterator ::rInitialize(const Deque & Q) { Qptr = &Q; index = Q.size() – 1; }

40 Implementing DequeIterator (3) Helper functions template int DequeIterator ::valid() const { if (Qptr == nullptr) return 0; if (index >= Qptr->size()) return 0; return 1; } template T& DequeIterator ::operator[] (size_t i) const { if (!Qptr) { // error } return Qptr->operator[](index + i); }

41 Implementing DequeIterator (4) Helper functions template T& DequeIterator ::retrieve() const { if (Qptr == nullptr) { // error } if (Qptr->size() == 0) { // error } return Qptr->operator[](index); } template T& DequeIterator ::operator* () const { // check for validity return Qptr->operator[](index); }

42 Implementing DequeIterator (5) Comparators template int DequeIterator ::operator==(const DequeIterator & I2) const { if (Qptr != I2.Qptr) return 0; if (index != I2.index) return 0; return 1; } template int DequeIterator ::operator!=(const DequeIterator & I2) const { return !(*this == I2); }

43 Implementing DequeIterator (6) Assignment template DequeIterator & DequeIterator ::operator=(const DequeIterator & I) { if (this != &I) { Qptr = I.Qptr; index = I.index; } return *this; }

44 Implementing DequeIterator (7) Various operators template DequeIterator & DequeIterator ::operator++() { // prefix ++index; return *this; } template DequeIterator DequeIterator ::operator++(int) { // postfix DequeIterator I(*this); operator ++(); return I; }

45 Implementing DequeIterator (8) Various operators template DequeIterator & DequeIterator ::operator--() { // prefix --index; return *this; } template DequeIterator DequeIterator ::operator--(int) { // postfix DequeIterator I(*this); operator --(); return I; }

46 Implementing DequeIterator (9) Various operators template long DequeIterator ::operator-(const DequeIterator & I2) const { // return the distance between the two iterators return index – I2.index; } template DequeIterator DequeIterator ::operator+(long n) const { // advance the iterator by n DequeIterator I(*this); return I += n; }

47 Implementing DequeIterator (10) Various operators template DequeIterator & DequeIterator ::operator+=(long n) { index += n; return *this; } template DequeIterator & DequeIterator ::operator-=(long n) { index -= n; return *this; }

48 Reading assignment Chapter 4 Trees