G Programming Languages Spring 2010 Lecture 11. Robert Soulé, New York University

Similar documents
G Programming Languages - Fall 2012

use static size for this buffer

COEN244: Class & function templates

How the Adapters and Binders Work

STL components. STL: C++ Standard Library Standard Template Library (STL) Main Ideas. Components. Encapsulates complex data structures and algorithms

G Programming Languages Spring 2010 Lecture 8. Robert Grimm, New York University

CS197c: Programming in C++

G Programming Languages - Fall 2012

Today. andyoucanalsoconsultchapters6amd7inthetextbook. cis15-fall2007-parsons-lectvii.1 2

Homework 5. Yuji Shimojo CMSC 330. Instructor: Prof. Reginald Y. Haseltine

G Programming Languages Spring 2010 Lecture 6. Robert Grimm, New York University

General Advise: Don t reinvent the wheel

STL: C++ Standard Library

Type Aliases. Examples: using newtype = existingtype; // C++11 typedef existingtype newtype; // equivalent, still works

Templates and Vectors

List, Stack, and Queues

CPSC 427a: Object-Oriented Programming

STL Quick Reference for CS 241

Wentworth Institute of Technology COMP201 Computer Science II Spring 2015 Derbinsky. C++ Kitchen Sink. Lecture 14.

CS11 Advanced C++ Fall Lecture 1

Topics. bool and string types input/output library functions comments memory allocation templates classes

G52CPP C++ Programming Lecture 18

An introduction to C++ template programming

Template based set of collection classes STL collection types (container types)

Chapter 5. The Standard Template Library.

Module 9. Templates & STL

G52CPP C++ Programming Lecture 18. Dr Jason Atkin

CSCI-1200 Data Structures Fall 2017 Lecture 9 Iterators & STL Lists

The Ada Standard Generic Library (SGL)

Agenda. The main body and cout. Fundamental data types. Declarations and definitions. Control structures

SSE2034: System Software Experiment 3

CSCI-1200 Data Structures Fall 2014 Lecture 8 Iterators

Intermediate Programming, Spring 2017*

Lecture 2 Polymorphism, Traits, Policies, and Inheritance

Semantics of C++ Hauptseminar im Wintersemester 2009/10 Templates

September 10,

Short Notes of CS201

Outline. User-dened types Categories. Constructors. Constructors. 4. Classes. Concrete classes. Default constructor. Default constructor

Fast Introduction to Object Oriented Programming and C++

COP4530 Data Structures, Algorithms and Generic Programming Recitation 4 Date: September 14/18-, 2008

CS11 Advanced C++ Spring 2018 Lecture 2

CS201 - Introduction to Programming Glossary By

CSI33 Data Structures

Standard Template Library

CE221 Programming in C++ Part 1 Introduction

Outline. 1 About the course

CSCI-1200 Data Structures Fall 2010 Lecture 8 Iterators

CSCI-GA Scripting Languages

Container Notes. Di erent Kinds of Containers. Types Defined by Containers. C++11 Container Notes C++11

W3101: Programming Languages C++ Ramana Isukapalli

COSC 320 Exam 2 Key Spring Part 1: Hash Functions

Lecture 8. Xiaoguang Wang. February 13th, 2014 STAT 598W. (STAT 598W) Lecture 8 1 / 47

CSCI 104 Templates. Mark Redekopp David Kempe

CS 376b Computer Vision

Purpose of Review. Review some basic C++ Familiarize us with Weiss s style Introduce specific constructs useful for implementing data structures

Generic programming in OO Languages

THE STANDARD TEMPLATE LIBRARY (STL) Week 6 BITE 1513 Computer Game Programming

Polymorphism. Programming in C++ A problem of reuse. Swapping arguments. Session 4 - Genericity, Containers. Code that works for many types.

Introduction. Programming in C++ Pointers and arrays. Pointers in C and C++ Session 5 - Pointers and Arrays Iterators

Outline. Function calls and results Returning objects by value. return value optimization (RVO) Call by reference or by value?

Containers in C++ and Java

Chapter 15 - C++ As A "Better C"

When we program, we have to deal with errors. Our most basic aim is correctness, but we must

Where do we go from here?

C++ Modern and Lucid C++ for Professional Programmers

Outline. Variables Automatic type inference. Generic programming. Generic programming. Templates Template compilation

CS 247: Software Engineering Principles. C++ Templates. Reading: Eckel, Vol. 2 Ch. 5 Templates in Depth. U Waterloo CS247 (Spring 2017) p.

G Programming Languages Spring 2010 Lecture 4. Robert Grimm, New York University

CS93SI Handout 04 Spring 2006 Apr Review Answers

Where do we go from here?

Data Structures Lecture 3 Order Notation and Recursion

CSCI-1200 Data Structures Spring 2016 Lecture 7 Iterators, STL Lists & Order Notation

Dynamic Data Structures

Computing with functions

Due Date: See Blackboard

7 TEMPLATES AND STL. 7.1 Function Templates

A brief introduction to C++

Unit 1: Preliminaries Part 4: Introduction to the Standard Template Library

CSCI-1200 Computer Science II Fall 2008 Lecture 15 Associative Containers (Maps), Part 2

Software Development with C++ Templates

pointers & references

CS11 Advanced C++ Fall Lecture 7

Paytm Programming Sample paper: 1) A copy constructor is called. a. when an object is returned by value

1/29/2011 AUTO POINTER (AUTO_PTR) INTERMEDIATE SOFTWARE DESIGN SPRING delete ptr might not happen memory leak!

Evolution of Programming Languages

COMP6771 Advanced C++ Programming

Computer Science II Lecture 2 Strings, Vectors and Recursion

Advanced Systems Programming

Modern C++ for Computer Vision and Image Processing. Igor Bogoslavskyi

Introduction to C++ Systems Programming

CS 103 Unit 12 Slides

EE 355 Unit 10. C++ STL - Vectors and Deques. Mark Redekopp

Some Advanced ML Features

COMP 181. Agenda. Midterm topics. Today: type checking. Purpose of types. Type errors. Type checking

3.Constructors and Destructors. Develop cpp program to implement constructor and destructor.

CS 251 INTERMEDIATE SOFTWARE DESIGN SPRING C ++ Basics Review part 2 Auto pointer, templates, STL algorithms

COMP322 - Introduction to C++ Lecture 02 - Basics of C++

Lectures 11,12. Online documentation & links

Preface... (vii) CHAPTER 1 INTRODUCTION TO COMPUTERS

COMP322 - Introduction to C++

Transcription:

G22.2110-001 Programming Languages Spring 2010 Lecture 11 Robert Soulé, New York University 1

Review Last week Constructors, Destructors, and Assignment Operators Classes and Functions: Design and Declaration Classes and Functions: Implementation Inheritance and Object-Oriented Design 2

Outline Generic Programming Sources for today s lecture: PLP, 8.4 Osinski. Lecture notes, Summer 2008. Hickey. Introduction to Objective Caml. Chapter 13 3

Generic programming Subroutines provide a way to abstract over values. Generic programming lets us abstract over types. Examples: A sorting algorithm has the same structure, regardless of the types being sorted Stack primitives have the same semantics, regardless of the objects stored on the stack. One common use: algorithms on containers: updating, iteration, search Language models: C: macros (textual substitution) or unsafe casts ADA: generic units and instantiations C++, JAVA, C#: templates ML: parametric polymorphism, functors 4

Parameterizing software components Construct array subprogram ADA generic package ADA generic subprogram C++ class template C++ function template JAVA generic ML function ML type constructor ML functor Parameter(s): bounds, element type values (arguments) values, types, packages values, types values, types values, types classes values (including other functions) types values, types, structures 5

Templates in C++ template <typename T> class Array { public: explicit Array (size_t); // constructor T& operator[] (size_t); // subscript operator... // other operations private:... // a size and a pointer to an array ; Array<int> V1(100); Array<int> V2; // instantiation // use default constructor typedef Array<employee> Dept; // named instance 6

Type and value parameters template <typename T, unsigned int i> class Buffer { T v[i]; // storage for buffer unsigned int sz; // total capacity unsigned int count; // current contents public: Buffer () : sz(i), count(0) { T read (); void write (const T& elem); ; Buffer<Shape *, 100> picture; 7

Template Does not Guarantee Success template <typename T> class List { struct Link { // for a list node Link *pre, *succ; // doubly linked T val; Link (Link *p, Link *s, const T& v) : pre(p), succ(s), val(v) { ; Link *head; public: void print (std::ostream& os) { for (Link *p = head; p; p = p->succ) // will fail if operator<< does // not exist for T os << p->val << "\n"; ; 8

Function templates Instantiated implicitly at point of call: template <typename T> void sort (vector<t>&) {... void testit (vector<int>& vi) { sort(vi); // implicit instantiation // can also write sort<int>(vi); 9

Functions and function templates Templates and regular functions overload each other: template <typename T> class Complex {...; template <typename T> T sqrt (T); // template template <typename T> Complex<T> sqrt (Complex<T>); // different algorithm double sqrt (double); // regular function void testit (Complex<double> cd) { sqrt(2); // sqrt<int> sqrt(2.0); // sqrt (double): regular function sqrt(cd); // sqrt<complex<double> > 10

Iterators and containers Containers are data structures to manage collections of items Typical operations: insert, delete, search, count Typical algorithms over collections use: imperative languages: iterators functional languages: map, fold interface Iterator<E> { boolean hasnext (); // returns true if there are // more elements E next (); // returns the next element void remove (); // removes the current element // from the collection ; 11

The Standard Template Library The Standard Template Library (STL) is a set of useful data structures and algorithms in C++, mostly to handle collections. Sequential containers: list, vector, deque Associative containers: set, map We can iterate over these using (what else?) iterators. Iterators provided (for vector<t>): vector<t>::iterator vector<t>::const_iterator vector<t>::reverse_iterator vector<t>::const_reverse_iterator Note: Almost no inheritance used in STL. 12

Iterators in C++ For standard collection classes, we have member functions begin and end that return iterators. We can do the following with an iterator p (subject to restrictions): *p Dereference it to get the element it points to ++p, p++ Advance it to point to the next element --p, p-- p+i p-i Retreat it to point to the previous element Advance it i times Retreat it i times A sequence is defined by a pair of iterators: the first points to the first element in the sequence the second points to one past the last element in the sequence There are a variety of operations that work on sequences. 13

Iterator example 1 #include <vector> #include <iostream> using namespace std; int main() { vector<int> v; for (int i = 0; i < 10; ++i) v.push_back(i); // Print list vector<int>::iterator it; for (it = v.begin(); it!= v.end(); ++it) { cout << *it << " "; cout << endl << endl; // Use reverse iterator to print in reverse order vector<int>::reverse_iterator rit; for (rit = v.rbegin(); rit!= v.rend(); ++rit) { cout << *rit << " "; cout << endl; 14

Iterator example 2 #include <vector> #include <string> #include <iostream> using namespace std; int main () { vector<string> ss(20); // initialize to 20 empty strings for (int i = 0; i < 20; i++) ss[i] = string(1, a +i); // assign "a", "b", etc. vector<string>::iterator loc = find(ss.begin(), ss.end(), "d"); // find first "d" cout << "found: " << *loc << " at position " << loc - ss.begin() << endl; 15

STL algorithms, part 1 STL provides a wide variety of standard algorithms on sequences. Example: finding an element that matches a given condition // Find first 7 in the sequence list<int>::iterator p = find(c.begin(), c.end(), 7); #include <algorithm> // Find first number less than 7 in the sequence bool less_than_7 (int v) { return v < 7; list<int>::iterator p = find_if(c.begin(), c.end(), less_than_7); 16

STL algorithms, part 2 Example: doing something for each element of a sequence It is often useful to pass a function or something that acts like a function: #include <iostream> #include <algorithm> template <typename T> class Sum { T res; public: Sum (T i = 0) : res(i) { void operator() (T x) { res += x; T result () const { return res; ; // initialize // accumulate // return sum void f (list<double>& ds) { Sum<double> sum; sum = for_each(ds.begin(), ds.end(), sum); cout << "the sum is " << sum.result() << "\n"; 17

Function objects template <typename Arg, typename Res> struct unary_function { typedef Arg argument_type; typedef Res result_type; ; struct R { string name;... ; class R_name_eq : public unary_function<r, bool> { string s; public: explicit R_name_eq (const string& ss) : s(ss) { bool operator() (const R& r) const { return r.name == s; ; void f (list<r>& lr) { list<r>::iterator p = find_if(lr.begin(), lr.end(), R_name_eq("Joe"));... 18

Binary function objects template <typename Arg, typename Arg2, typename Res> struct binary_function { typedef Arg first_argument_type; typedef Arg2 second_argument_type; typedef Res result_type; ; template <typename T> struct less : public binary_function<t,t,bool> { bool operator() (const T& x, const T& y) const { return x < y; ; 19

Currying with function objects template <typename BinOp> class binder2nd : public unary_function<typename BinOp::first_argument_type, typename BinOp::result_type> { protected: BinOp op; typename BinOp::second_argument_type arg2; public: binder2nd (const BinOp& x, const typename BinOp::second_argument_type& v) : op(x), arg2(v) { return_type operator() (const argument_type& x) const { return op(x, arg2); ; template <typename BinOp, typename T> binder2nd<binop> bind2nd (const BinOp& op, const T& v) { return binder2nd<binop> (op, v); 20

Partial application with function objects void f (const list<int>& xs, int limit) { list<int>::const_iterator it = find_if(xs.begin(), xs.end(), bind2nd(less<int>(), limit)); int num = *it;... Is this readable?... The notation is logical, but it takes some getting used to. Stroustrup, p. 520 Equivalent to the following in ML: fun f xs limit = let val optnum = List.find (fn x => x < limit) xs in... end 21

C++ templates are Turing complete Templates in C++ allow for arbitrary computation to be done at compile time! template <int N> struct Factorial { enum { V = N * Factorial<N-1>::V ; ; template <> struct Factorial<1> { enum { V = 1 ; ; void f () { const int fact12 = Factorial<12>::V; cout << fact12 << endl; // 479001600 22

Generics in JAVA Only class parameters Implementation by type erasure: all instances share the same code interface Collection <E> { public void add (E x); public Iterator<E> iterator (); Collection <Thing> is a parametrized type Collection (by itself) is a raw type! 23

Generic methods in JAVA class Collection <A extends Comparable<A>> { public A max () { Iterator<A> xi = this.iterator(); A biggest = xi.next(); while (xi.hasnext()) { A x = xi.next(); if (biggest.compareto(x) < 0) biggest = x; return biggest;... 24

Functors in ML Why functors, when we have parametric polymorphic functions and type constructors (e.g. containers)? Functors can take structures as arguments. This is not possible with functions or type constructors. Sometimes a type needs to be parameterized on a value. This is not possible with type constructors. 25

Example functor: the signature and functor module type EqualSig = sig type t val equal : t -> t -> bool endl;; module MakeSet (Equal : EqualSig) = struct open Equal type elt = Equal.t type t = elt list let empty = [] let mem x s = List.exists (equal x) s let add = (::) let find x s = List.find (equal x) s end;; 26

Example functor: the implementation and instantiation module StringCaseEqual = struct type t = string let equal s1 s2 = String.lowercase s1 = String.lowercase s2 end;; module SSet = MakeSet (StringEqualCase);; 27

Example functor: use let s = SSet.add "Foo" SSet.empty;; SSet.mem "Foo" s;; 28