Generic Programming David Rabinowitz.

Slides:



Advertisements
Similar presentations
Generics Programming in C# Generics CSE 494R (proposed course for 459 Programming in C#) Prof. Roger Crawfis.
Advertisements

The C ++ Language BY Shery khan. The C++ Language Bjarne Stroupstrup, the language’s creator C++ was designed to provide Simula’s facilities for program.
Java Review Interface, Casting, Generics, Iterator.
Collections Framework A very brief look at Java’s Collection Framework David Davenport May 2010.
Generic programming in Java
Java Generics.
Generic Programming David Rabinowitz. March 3rd, 2004 Object Oriented Design Course 2 The problem Assume we have a nice Stack implementation. Our stack.
Generic Programming Amit Shabtay. March 3rd, 2004 Object Oriented Design Course 2 The Problem Assume we have a nice Stack implementation. Our stack receives.
Generic Programming David Rabinowitz. June 14, 2006 Object Oriented Design Course 2 The problem Assume we have a nice Stack implementation. Our stack.
Templates. Example… A useful routine to have is void swap(int &a, int &b){ int tmp = a; a = b; b = tmp; }
Options for User Input Options for getting information from the user –Write event-driven code Con: requires a significant amount of new code to set-up.
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.
Intro to Generic Programming Templates and Vectors.
CPS 108 : Spring From C++ to Java l Java history: Oak, toaster-ovens, internet language, panacea  Not really a standard language like C++  Arguably.
C++ / G4MICE Course Session 3 Introduction to Classes Pointers and References Makefiles Standard Template Library.
STL Standard Template Library ● Good reference book: – The C++ Standard Library ● A Tutorial and Reference ● by Nicolai M. Josuttis ● 1999 – Addison Wesley.
CSE 332: C++ templates This Week C++ Templates –Another form of polymorphism (interface based) –Let you plug different types into reusable code Assigned.
Data Structures Using C++ 2E
Generics In Java 1.5 By Manjunath Beeraladinni. Generics ➲ New feature in JDK1.5. ➲ Generic allow to abstract over types. ➲ Generics make the code clearer.
Week 2 - Monday.  What did we talk about last time?  Exceptions  Threads  OOP  Interfaces.
Effective Java: Generics Last Updated: Spring 2009.
Generics and Collections. Introduction Generics New feature of J2SE 5.0 Provide compile-time type safety Catch invalid types at compile time Generic methods.
Chapter 18 Java Collections Framework
M180: Data Structures & Algorithms in Java Arrays in Java Arab Open University 1.
G ENERICS I N J AVA BY: Ankit Goyal Sankalp Singh.
Object Oriented Programming Elhanan Borenstein Lecture #10 copyrights © Elhanan Borenstein.
Standard Template Library The Standard Template Library was recently added to standard C++. –The STL contains generic template classes. –The STL permits.
Lecture 4 Generic programming Advanced Java Programming 1 dr hab. Szymon Grabowski dr inż. Wojciech Bieniecki
Java Coding OOP_3 David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. Some important Java interfaces +
CMSC 330: Organization of Programming Languages Java Generics.
©SoftMoore ConsultingSlide 1 Generics “Generics constitute the most significant change in the Java programming language since the 1.0 release.” – Cay Horstmann.
Copyright © 2009 – Curt Hill Standard Template Library An Introduction.
Generics. Writing typed checked generic code There are many instances where the type of the object is irrelevant : –The construction of data structures.
CPS What's in Compsci 100? l Understanding tradeoffs: reasoning, analyzing, describing…  Algorithms  Data Structures  Programming  Design l.
JAVA GENERICS Lecture 16 CS2110 – Spring 2016 Photo credit: Andrew Kennedy.
Motivation for Generic Programming in C++
Module 20/21/22: The Standard Template Library
Classes and Objects Introduced
Sixth Lecture ArrayList Abstract Class and Interface
Programming with ANSI C ++
Generic Programming David Rabinowitz.
How to be generic Lecture 10
C++ Templates.
Chapter 19 Java Data Structures
Exceptions, Templates, and the Standard Template Library (STL)
More on Java Generics Multiple Generic Types Bounded Generic Types
Java Generics Lecture 14 CS2110 – Fall 2016
Final review CSE331 Winter 2017.
Generics, Lambdas, Reflections
CS212: Object Oriented Analysis and Design
Starting Out with C++ Early Objects Eighth Edition
Java Generics.
Introduction to the Standard Template Library
עקרונות תכנות מונחה עצמים תרגול 21: Generics
Generics (Parametric Polymorphism)
CSE 143 Lecture 27: Advanced List Implementation
Lecture 26: Advanced List Implementation
Material pulled from last section and last year’s slides
Generic programming in Java
Material pulled from last section and last year’s slides
Exception Handling.
Type Safety, Generics, Lambdas, Class Object
Standard Version of Starting Out with C++, 4th Edition
Exceptions, Templates, and the Standard Template Library (STL)
Final review.
Generics, Lambdas, Reflections
Java Programming Language
Function Templates Class Templates
SPL – PS1 Introduction to C++.
Generics, Lambdas and Reflection
Presentation transcript:

Generic Programming David Rabinowitz

Object Oriented Design Course The problem Assume we have a nice Stack implementation. Our stack receives only Objects The problem: We need different stacks for int, String and Method. We don’t want the problems of: Stack.add(“clearly not a method”); … Method m = (Method)stack.pop(); June 14, 2006 Object Oriented Design Course

Object Oriented Design Course Solution (?) Let’s create IntStack , StringStack and MethodStack. All of them will rely on the original Stack as internal implementation. Are there any problems? A lot of code duplication It’s hard to add new types June 14, 2006 Object Oriented Design Course

Object Oriented Design Course David Talby June 14, 2006 Another problem We want to use a swap function between two ints. void swap(int& a, int&b) { int temp = a; a = b; b = temp; } What about swap(double&, double&) and swap(Method&, Method&) ? June 14, 2006 Object Oriented Design Course Object Oriented Design Course

Object Oriented Design Course The actual solution Generic programming. Write the code once, and worry about the type at compile time The code is suitable to all types Easy to add types later No code duplication Demands from types June 14, 2006 Object Oriented Design Course

Object Oriented Design Course So how do we do it? swap<Class T>(T& a, T& b) { T temp = a; a = b; b = temp; } Looks simple? June 14, 2006 Object Oriented Design Course

Object Oriented Design Course Uses Containers list set vector map Algorithms sort search copy June 14, 2006 Object Oriented Design Course

Object Oriented Design Course C++ Templates Most famous use of generic programming STL – Standard Template Library Containers vector, set, hash_map Algorithms for_each, swap, binary_search, min, max Very high performance library Compile time, Inlining, Specializations, … June 14, 2006 Object Oriented Design Course

Object Oriented Design Course What about Java? Until 1.5, Java had a large collection set Set, List, Map, Iterator, and more sort(), search(), fill(), copy(), max(), min() The collections were not type safe No problem to do: Map.put(“key”, “4”); Integer i = (Integer)map.get(“key” June 14, 2006 Object Oriented Design Course

Object Oriented Design Course Java Generics JSR 14, add in Java 1.5 (“Tiger”) Done in the compiler only Converts: String s = vector<String>.get(3) to: String s = (String)vector.get(3) “Forward Compatible” with earlier JDKs For objects only (not simple types) June 14, 2006 Object Oriented Design Course

Object Oriented Design Course Using Java Generics List<Integer> myIntList = new LinkedList<Integer>(); myIntList.add(new Integer(0)); Integer x = myIntList.iterator().next(); June 14, 2006 Object Oriented Design Course

Object Oriented Design Course Generic Collections public interface List<E> { void add(E x); Iterator<E> iterator(); } public interface Iterator<E> { E next(); boolean hasNext(); June 14, 2006 Object Oriented Design Course

Object Oriented Design Course Subtyping List<String> ls = new ArrayList<String>(); List<Object> lo = ls; lo.add(new Object()); // attempts to assign an Object // to a String! String s = ls.get(0); June 14, 2006 Object Oriented Design Course

Object Oriented Design Course Subtyping (cont.) Foo is subtype of Bar Foo extends Bar Foo implements Bar C is a generic container C<E> Results that C<Foo> is not subtype of C<Bar> June 14, 2006 Object Oriented Design Course

Object Oriented Design Course Generic Algorithms (1) How to print entire Collection? Do we have to use Collection<Object> ? Use wildcard void printCollection(Collection<?> c){ for (Object e : c) { System.out.println(e); } June 14, 2006 Object Oriented Design Course

Object Oriented Design Course Generic Algorithms (2) What happens when we want to use a specific method? public void drawAll(List<Shape> shapes) { for (Shape s: shapes) { s.draw(this); } What about subtyping? List<Circle> June 14, 2006 Object Oriented Design Course

Object Oriented Design Course Generic Algorithms (3) The solution public void drawAll(List<? extends Shape> shapes) {…} June 14, 2006 Object Oriented Design Course

Object Oriented Design Course C# Generics Were added to .NET 2.0 Generics designed in advance Visible to reflection, bytecode manipulation Very similar to Java: public struct Point<T> { public T X; public T Y; } Point<int> point; point.X = 1; point.Y = 2; More at: http://msdn.microsoft.com/ June 14, 2006 Object Oriented Design Course

C++ versus Java/C# Implementation The C++ Approach to wildcards Use any required method/operator The compiler verifies that all methods exist For example, STL assumes default constructor More flexible, yet undocumented Weird compiler errors Templates only compiled when instantiated June 14, 2006 Object Oriented Design Course

C++ Template Instantiation Multiple executable code segments may be compiled from one template Stack<int>, Stack<char>, Stack<Person*> This doesn’t happen in C#/Java Specialization may define optimizations template<class T> class Vector {…} template<> Vector<void*> { …} template<class T> class Vector<T*> : private Vector<void*> { …} June 14, 2006 Object Oriented Design Course

Object Oriented Design Course Summary Generics are a required feature Containers, Reusable algorithms Different Approaches C++ - Performance first, non-OO Java – Safety first, Forward Compatible C# - Safety first, integrate with OOP June 14, 2006 Object Oriented Design Course