Generic programming starts with algorithms. Lift Minimal requirements: works with maximal family of types Concrete algorithm: requires specific data type.

Slides:



Advertisements
Similar presentations
M The University Of Michigan Andrew M. Morgan EECS Lecture 22 Savitch Ch. 16 Intro To Standard Template Library STL Container Classes STL Iterators.
Advertisements

Chapter 19 Standard Template Library. Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives Iterators Constant and mutable.
Lists and the Collection Interface Chapter 4. Chapter Objectives To become familiar with the List interface To understand how to write an array-based.
SEG4110 – Advanced Software Design and Reengineering TOPIC J C++ Standard Template Library.
. STL: C++ Standard Library (continued). STL Iterators u Iterators are allow to traverse sequences u Methods  operator*  operator->  operator++, and.
TEMPLATES Lecture Presented By SHERY KHAN Object Orienting Programming.
Multimaps. Resources -- web For each new class, browse its methods These sites are richly linked, and contain indexes, examples, documents and other resources.
Generic Programming and Formal Methods David R. Musser Rensselaer Polytechnic Institute.
C++ Sets and Multisets Set containers automatically sort their elements automatically. Multisets allow duplication of elements whereas sets do not. Usually,
Generic Programming in the STL and Beyond David R. Musser Rensselaer Polytechnic Institute.
. STL: C++ Standard Library. Main Ideas u General purpose: generic data structures & algorithms, templates u Flexibility: Allows for many combinations.
What is generic programming? The essence of the generic programming approach is concept development: systematic classification of computing components.
COMP 171 Data Structures and Algorithms Tutorial 1 Template and STL.
. The Standard C++ Library. 2 Main Ideas Purpose Flexibility Efficiency Simple & Uniform Interface.
Concept= a set of abstractions (e.g., types) (Generic Programming) Programming with Concepts defined by a set of requirements {vector, deque, list, set,
OOP Etgar 2008 – Recitation 101 Object Oriented Programming Etgar 2008 Recitation 10.
C++ Programming: Program Design Including Data Structures, Second Edition Chapter 22: Standard Template Library (STL)
Standard Template Library (STL) Overview – Part 1 Yngvi Bjornsson.
Data Structures Using C++1 Chapter 13 Standard Template Library (STL) II.
The Standard Template Library. Books on standard C++ library Nicolai M. Josuttis: C++ Standard Library: A tutorial and Reference, 1st, Pearson 1999, Nicolai.
. STL: C++ Standard Library. Main Ideas u General purpose: generic data structures & algorithms, templates u Flexibility: Allows for many combinations.
. STL: C++ Standard Library (continued). STL Iterators u Iterators are allow to traverse sequences u Methods  operator*  operator->  operator++, and.
Standard Template Library C++ introduced both object-oriented ideas, as well as templates to C Templates are ways to write general code around objects.
Writing Your Own STL Container Ray Lischner
Spring 2010 Advanced Programming Section 1-STL Computer Engineering Department Faculty of Engineering Cairo University Advanced Programming Spring 2010.
Data Structures Using C++ 2E
CNS  Sequences  vector,deque,list,(string),forward_list  Container Adapters  queue, stack, priority_queue  Associative Containers  set, unordered_set.
C++ How to Program, 8/e © by Pearson Education, Inc. All Rights Reserved.
111 © 2002, Cisco Systems, Inc. All rights reserved.
Generic Programming Using the C++ Standard Template Library.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Standard Template Library (STL)
Data Structures Using C++ 2E Chapter 13 Standard Template Library (STL) II.
Review for Final Andy Wang Data Structures, Algorithms, and Generic Programming.
C++ STL CSCI 3110.
Software Design 1.1 Tapestry classes -> STL l What’s the difference between tvector and vector  Safety and the kitchen sink What happens with t[21] on.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 22: Standard Template Library (STL)
CSE 332: C++ STL containers Review: C++ Standard Template Library (STL) The STL is a collection of related software elements –Containers Data structures:
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.
Standard Template Library The Standard Template Library was recently added to standard C++. –The STL contains generic template classes. –The STL permits.
1 Iterators Good reference site:
CS 403, Class 23Slide #1 CS Programming Languages Class 23 November 16, 2000.
Computer Science and Software Engineering University of Wisconsin - Platteville 11.Standard Template Library Yan Shi CS/SE 2630 Lecture Notes.
CS 403: Programming Languages Lecture 24 Fall 2003 Department of Computer Science University of Alabama Joel Jones.
Intro to the C++ STL Timmie Smith September 6, 2001.
STL – Standard Template Library L. Grewe. 2 Goals Lots of important algorithms, data structures in CS using Templates. is a software library partially.
Introduction The STL is a complex piece of software engineering that uses some of C++'s most sophisticated features STL provides an incredible amount.
C++ Review STL CONTAINERS.
CSCI 383 Object-Oriented Programming & Design Lecture 25 Martin van Bommel.
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)
Algorithms CNS 3370 Copyright 2003, Fresh Sources, Inc.
Chapter 17 – Templates. Function Templates u Express general form for a function u Example: template for adding two numbers Lesson 17.1 template Type.
Unit VI.  C++ templates are a powerful mechanism for code reuse, as they enable the programmer to write code (classes as well as functions) that behaves.
1 The Standard Template Library The STL is a collection of Container classes These are class templates for containers. A container is an object that stores.
Final Exam Review COP4530.
CS212: Object Oriented Analysis and Design
Programming with ANSI C ++
Exceptions, Templates, and the Standard Template Library (STL)
Standard Template Library (STL)
Starting Out with C++ Early Objects Eighth Edition
Prof. Michael Neary Lecture 7: The STL Prof. Michael Neary
Programming with Concepts
Chapter 22: Standard Template Library (STL)
CS212: Object Oriented Analysis and Design
Generic Programming Karl Lieberherr 12/1/2018 Generic Programming.
Containers, Iterators, Algorithms, Thrust
Standard Template Library
Standard Template Library
Some Definitions vector, string, deque, and list are standard sequence containers. set, multiset, map, multimap, unordered_set, unordered_multiset, unordered_map.
Standard Template Library
Presentation transcript:

Generic programming starts with algorithms

Lift Minimal requirements: works with maximal family of types Concrete algorithm: requires specific data type Less specialized: works with more than one type A 0 A 1 Lift A m Remove an unneeded requirement on the type Generic algorithm... Start here

Maximal with respect to what?

Lift Maintain usefulness of the algorithm – make efficiency part of the requirements Concrete algorithm A 0 A 1 Lift A m Generic algorithm...

float max_element(float a[], int N) { if (N == 0) throw MaxElementEmptyArrayError; int first = 0; float max = a[0]; while (++first < N) if (max < a[first]) max = a[first]; return max; } A Concrete Algorithm

int max_element(float a[], int first, int last) { if (first == last) return first; int result = first; while (++first != last) if (a[result] < a[first]) result = first; return result; } A More Useful Interface

float_list_node* max_element(float_list_node* first, float_list_node* last) { if (first == last) return first; float_list_node* result = first; while (first->next != last) if (result->data data) result = first; return result; } A Linked-List Counterpart

int max_element(float a[], int first, int last) { if (first == last) return first; int result = first; while (++first != last) if (a[result] < a[first]) result = first; return result; } Back to the Array Version

template int max_element(E a[], int first, int last) { if (first == last) return first; int result = first; while (++first != last) if (a[result] < a[first]) result = first; return result; } Generalize the Element Type

template T* max_element(T* first, T* last) { if (first == last) return first; T* result = first; while (++first != last) if (*result < *first) result = first; return result; } From Array Indexing to Pointers

template ForwardIterator max_element(ForwardIterator first, ForwardIterator last) { if (first == last) return first; ForwardIterator result = first; while (++first != last) if (*result < *first) result = first; return result; } Generalize from Pointers to Iterators

int a[] = {6, 3, 7, 5}; int* ai = max_element(a, a+4); vector v(a, a+4); vector ::iterator vi = max_element(v.begin(), v.end()); list x(a, a+4); list ::iterator li = max_element(x.begin(), x.end());... Use with any Container with appropriate iterators

Forward Container Concept max_element Generic algorithm on jk jk k = ++j *j = 3 *k = 7 ++i *i i==j

What is a concept?

Concept IntensionExtension Requirement 1 Requirement 2. Requirement N Abstraction 1 Abstraction 2. Abstraction K.

IntensionExtension Requirement 1 Requirement 2. Requirement N Abstraction 1 Abstraction 2. Abstraction K. An abstraction is in the Extension if and only if it satisfies all of the requirements in the Intension

Example: Polygon Concept IntensionExtension Closed plane figure N sides (for any N)...

Example: Container Concept IntensionExtension Must be a type whose objects can store other objects (elements) Must have an associated iterator type that can be used to traverse through the elements.... vector, for any T deque for any T list for any T slist for any T set for any T map for any T

A Algorithm A works with every type T in concept C Definition: an algorithm is generic if it works with every type in a concept “works”: is correct and efficient C T … …

Container Associative Container Sorted A. C. Unique A. C. Multiple A. C. Hashed A. C. Unique Sorted A. C. Multiple Sorted A. C. Unique Hashed A. C. Multiple Hashed A. C. Simple A. C. Pair A. C. SetMultisetH. Set H. Multiset H. Multi- map H. Map Multimap Front Insertion Sequence Back Insertion Sequence Random Access Container Forward Container SequenceReversible Container List Vector Deque Front & Back Insertion Sequence Slist STL Container Concepts  Back See also Click on any node in the above concept hierarchy to see the corresponding requirements as specified in the SGI STL Programmer’s Guide

Container Forward Container Sequence Front Insertion Sequence Back Insertion Sequence Reversible Container Random Access Container List Vector Deque Front & Back Insertion Sequence Slist STL Generic Algorithms on Forward Containers Requires input iterators Enables generic algorithms copy, for_each, equal, transform, … Requires forward iterators Enables find, merge, fill, replace, generate, remove, unique, rotate, … … Requires bidirectional iterators Enables reverse, partition, inplace_merge, … Requires random access iterators Enables sort, binary_search, random_shuffle, …  Back

Container Forward Container Associative Container STL Concepts Input Iterator Output Iterator Forward Iterator Bidirectional Iterator Random Access Iterator AlgorithmFunctorAdaptor Input Algorithm Output Algorithm Forward Algorithm Bidirectional Algorithm Random Access Algorithm Unary Functor Binary Functor Binary Predicate Order Relation Iterator Adaptor  Back

BGL Concepts STL Concepts ContainerIteratorAlgorithmFunctorAdaptor Graph Iterator Graph Incidence Graph Adj. Graph EdgeList Graph … New Concepts in the Boost Graph Library See also Graph Algorithms Visitor BFS Visitor DFS Visitor Uniform Cost Visitor …  Back

Standard Template Library (HP, RPI, SGI) Matrix Template Library (Indiana U.) Boost Graph Library (Indiana U.) Parallel Boost Graph Library (D. Gregor [RPI PhD], Indiana U.) Boost Array Library (N. Josuttis) Boost Multi-Array Library (Indiana U.) Boost Basic Linear Algebra Library (J. Walter, M. Koch) Boost Property Map Library (Indiana U.) Boost Random Number Generator Library (J. Mauer) Boost Threads Library (W. Kempf) Boost Concept Checking Library (Indiana U.) Computational Geometry Algorithms Library (H. Brőnnimann, S. Schirra, R. Veltkamp, …) Generic Libraries Designed and Developed Via the Concept-Based Approach