TR1: C++ on the Move Pete Isensee Director XNA Developer Connection Microsoft.

Slides:



Advertisements
Similar presentations
SEG4110 – Advanced Software Design and Reengineering TOPIC J C++ Standard Template Library.
Advertisements

Object Oriented Programming Lect. Dr. Daniel POP Universitatea de Vest din Timişoara Facultatea de Matematică şi Informatică.
. STL: C++ Standard Library (continued). STL Iterators u Iterators are allow to traverse sequences u Methods  operator*  operator->  operator++, and.
C++ Templates. What is a template? Templates are type-generic versions of functions and/or classes Template functions and template classes can be used.
Lecture 11 oct 6 Goals: hashing hash functions chaining closed hashing application of hashing.
1 abstract containers hierarchical (1 to many) graph (many to many) first ith last sequence/linear (1 to 1) set.
Object Oriented Programming Elhanan Borenstein Lecture #12 copyrights © Elhanan Borenstein.
C++ Sets and Multisets Set containers automatically sort their elements automatically. Multisets allow duplication of elements whereas sets do not. Usually,
Rossella Lau Lecture 5, DCO20105, Semester A, DCO Data structures and algorithms  Lecture 5: Deque Comparison of sequence containers  Deque.
. The Standard C++ Library. 2 Main Ideas Purpose Flexibility Efficiency Simple & Uniform Interface.
Main Index Contents 11 Main Index Contents Container Types Container Types Sequence Containers Sequence Containers Associative Containers Associative Containers.
OOP Etgar 2008 – Recitation 101 Object Oriented Programming Etgar 2008 Recitation 10.
1 abstract containers hierarchical (1 to many) graph (many to many) first ith last sequence/linear (1 to 1) set.
Lecture 11 oct 7 Goals: hashing hash functions chaining closed hashing application of hashing.
Data Structures Using C++ 2E Chapter 9 Searching and Hashing Algorithms.
L. Grewe. Computing hash function for a string Horner’s rule: (( … (a 0 x + a 1 ) x + a 2 ) x + … + a n-2 )x + a n-1 ) int hash( const string & key )
Data Structures Using C++ 2E
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.
ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo,
Data Structures Using C++ 2E
Containers Overview and Class Vector
IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture8.
111 © 2002, Cisco Systems, Inc. All rights reserved.
Generic Programming Using the C++ Standard Template Library.
General Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University of Wisconsin.
Data structures Abstract data types Java classes for Data structures and ADTs.
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.
Chapter 9: Part 2: Vectors + Maps and STL Overview JPC and JWD © 2002 McGraw-Hill, Inc. Modified by S. Sudarshan.
Chapter 11 Hash Tables © John Urrutia 2014, All Rights Reserved1.
Standard Template Library The Standard Template Library was recently added to standard C++. –The STL contains generic template classes. –The STL permits.
Lists Chapter 8. 2 Linked Lists As an ADT, a list is –finite sequence (possibly empty) of elements Operations commonly include: ConstructionAllocate &
The ADT Table The ADT table, or dictionary Uses a search key to identify its items Its items are records that contain several pieces of data 2 Figure.
Collections Data structures in Java. OBJECTIVE “ WHEN TO USE WHICH DATA STRUCTURE ” D e b u g.
Lecture 7 : Intro. to STL (Standard Template Library)
CHAPTER 9 HASH TABLES, MAPS, AND SKIP LISTS ACKNOWLEDGEMENT: THESE SLIDES ARE ADAPTED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN C++,
STL – Standard Template Library L. Grewe. 2 Goals Lots of important algorithms, data structures in CS using Templates. is a software library partially.
1 Associative Containers Ordered Ordered Unordered UnorderedSets Maps as sets of pairs Set API Ex: Sieve of Eratosthenes Ex: Sieve of EratosthenesImplementation.
STL CSSE 250 Susan Reeder. What is the STL? Standard Template Library Standard C++ Library is an extensible framework which contains components for Language.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 240 Elementary Data Structures Array Lists Array Lists Dale.
Copyright © 2009 – Curt Hill Standard Template Library An Introduction.
CS6045: Advanced Algorithms Data Structures. Hashing Tables Motivation: symbol tables –A compiler uses a symbol table to relate symbols to associated.
C++ Review STL CONTAINERS.
Sets and Maps Chapter 9. Chapter Objectives  To understand the Java Map and Set interfaces and how to use them  To learn about hash coding and its use.
Chapter 17 – Templates. Function Templates u Express general form for a function u Example: template for adding two numbers Lesson 17.1 template Type.
STL Associative Containers navigating by key. Pair Class aggregates values of two, possibly different, types used in associative containers defined in.
CSC 143T 1 CSC 143 Highlights of Tables and Hashing [Chapter 11 p (Tables)] [Chapter 12 p (Hashing)]
1 Designing Hash Tables Sections 5.3, 5.4, 5.5, 5.6.
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.
Data Structures and Algorithm Analysis Dr. Ken Cosh Linked Lists.
LINKED LISTS.
Standard Template Library
C++ Standard Template Library
Regarding homework 9 Many low grades
Linked Lists Chapter 6 Section 6.4 – 6.6
Data Structure and Algorithms
Collections Intro What is the STL? Templates, collections, & iterators
Hash table another data structure for implementing a map or a set
Advanced Associative Structures
C++ STL Vector Container
CSE 303 Lecture 25 Loose Ends and Random Topics
The Standard Template Library
Objects Managing a Resource
Collections Intro What is the STL? Templates, collections, & iterators
Some Definitions vector, string, deque, and list are standard sequence containers. set, multiset, map, multimap, unordered_set, unordered_multiset, unordered_map.
Recitation Outline Hash tables in C++ STL Examples Recursive example
A dictionary lookup mechanism
Standard Template Library
Presentation transcript:

TR1: C++ on the Move Pete Isensee Director XNA Developer Connection Microsoft

Welcome to GDC  Do a little of everything  Expand your horizons  Talk with other developers  Enjoy the city

C++ TR Defined  Technical Report  An “informative document”  Not part of the C++ Standard  May become part of a future standard  Or not

TR1: A History  Formal work began in 2001  Most proposals came from Boost members  Approved in 2006 by ISO  In Spring 2006 all TR1 except math functions added to draft of next Standard  Next Standard (C++0x) is due before the decade is out

What’s in TR1  Fixed-sized arrays  Hash tables  Smart pointers  Function objects  Type traits  Random number generators  Tuples  Call wrappers  Regular expressions  Advanced math functions

Why You Should Care  Efficiency  TR1 is  Lean and mean: more on that in a bit  Standard: learn once, use everywhere  Proven: avoid the not-invented-here syndrome  Available: today, on platforms you care about

Where To Find TR1  Boost.org  Dinkumware.com  Gcc.gnu.org  Metrowerks

Getting Started // TR1 header #include // fully qualified namespace std::tr1::array a; // or more likely... using namespace std::tr1; array a;

Arrays: True Containers at Last  array  Size fixed at compile time; contiguous space  Same performance as C-style arrays  Member functions you’d expect  begin, end, size, op [], at, front, back  Works with all std algorithms  Can be initialized using standard array initializers std::tr1::array a = { 1,2,3,4 };

Arrays: Take Control  Advantages  Performance  Size is part of the type  Easy to convert array code to/from std::vector  More secure: must call data() to get ptr  Disadvantages  Not growable  No push_back, reserve, resize  Must call data() to get ptr: inconvenient  array.swap() is O(n)  vector is O(1)

Hash Tables  Considered for original standard  Basic concept  Super-fast search  You control the hash algorithm  Similar interface to other STL containers  Low overhead: on par with set/map

Hash Table Conceptual Diagram e e Hash(e) d d b b c c a a e e

Unordered Associative Containers  Unordered  A traversal is not ordered like set/map  Associative  Dictionary pairs (K,T)  Containers  Work with std iterators and algorithms  TR1 naming  std::tr1::unordered_ [multi] set  std::tr1::unordered_ [multi] map

Hash Table Code Example #include using namespace std::tr1; unordered_set ht; ht.insert( 41 ); // add elements ht.insert( 11 ); ht.insert( 18 ); unordered_set ::iterator it; it = ht.find( 41 ); // find element

Real-World Performance  Evaluated three types of objects  Light-weight: int  Hash = std::tr1::hash  Medium-weight: std::complex  Hash = (size_t)( real + imag )  Heavy-weight: bitmap  Allocates, copy ctor calls memcpy  Hash = (size_t)( sum of first few pixels )  50,000 items in hash table  Tested on Xbox 360  Few perturbations = consistent results

Element Insertion Performance  Sequence containers (vector, deque)  Complexity: O(1)  Associative containers (set, map)  Complexity: O(log n)  Unordered associative containers  Average time complexity: O(1)  Worst-cast time complexity: O(n)  What was real-world performance?

unordered_multiset vector ::iterator> buckets Implementation Example list elems

Search Performance  Sequence containers (vector, array)  Complexity: O(n), O(log n) if sorted  Associative containers (set, map)  Complexity: O(log n)  Unordered associative containers  Average time complexity: O(1)  Worst-cast time complexity: O(n)  What was real-world performance?

Hash Functions  Choosing the right hash function is critical!  Defaults provided for built-ins and std::string  For examples, see  Hash references  The Art of Computer Programming, Vol 3, Knuth  Algorithms in C++, Sedgewick

Custom Hash Functions typedef std::complex cpx; struct CpxHash { size_t operator()(const cpx& c) const { return (size_t)(c.real()+c.imag()); } }; // Specify hash fn as template param unordered_set ht;

Load Factors and Rehashing  Load factor = size() / bucket_count()  Smaller load factor = better performance  You control the maximum load factor  max_load_factor( float );  When maximum exceeded, auto rehashes  You can also rehash directly  rehash( size_type buckets );

Hash Tables: Take Control  Advantages  Search performance: O(1)  Tuning options (hash function, load factor)  Insertions/deletions amortized O(1)  Equivalent or smaller overhead/elem than set  Disadvantages  Not ordered  No set_union, set_intersection  No reverse traversal  Depends on great hash function  Requires key support op==()

Smart Pointers  C++ resource lifetimes  Global  Stack-based  Static  No direct support for resources that have an intermediate lifetime  Enter shared_ptr  Ensures resources are available as long as needed and disposed when no longer needed

High-Octane Power  Smart ptr copied: ref count incremented  Dtor decrements ref count  Ref count goes to zero: ptr delete’d  Initialize with raw ptr shared_ptr sp( new T(…) );  Masquerades as a pointer for common usage T t(*sp); // T& operator*() const sp->func(); // T* operator->() const  Direct access available, too p = sp.get(); // T* get() const

Extended Example, Part A typedef std::tr1::shared_ptr SPB; struct SPBHash { size_t operator()(const SPB& bp) const { // hash = raw ptr address return (size_t)( bp.get() ); } }; // Store smart bitmaps for fast lookup unordered_set ht;

Extended Example, Part B // Store bitmaps in hash table SPB spb( new Bitmap( … ) ); ht.insert( spb ); ht.insert( … ); // Insert some more // Fast lookup unordered_set ::iterator it; it = ht.find( spb ); int w = (*it)->GetWidth(); // Best part: no more code required // No more resource leaks

Smart Pointer Performance  Object management  Ctor: allocates block for ref count info  Copy: ref count update  Dtor: ref count update; deallocation calls  Mgmt costs are insignificant for most objects  Smart ptrs generally wrap much more costly objs  Smart ptr access is equivalent to raw ptrs  *sp produces same code as *p  sp-> produces same code as p->

Smart Ptrs: Take Control  Advantages  Automatic resource management  Avoid memory leaks  Can be stored in containers  Tested  Disadvantages  Ref count management overhead  Ref count memory overhead  Cycles require use of weak_ptr  May not be thread-safe; chk your implementation

TR1: Take it for a Spin!

Resources  Contact me   Homepage:  Blog: pkisensee.spaces.live.compkisensee.spaces.live.com  Useful websites    gcc.gnu.org gcc.gnu.org  Books and magazines  C++ Standard Library Extensions, Pete Becker  Dr. Dobbs Journal; search on “TR1”