Predictive Engineering and Computational Sciences. Data Structures and Generic Programming. Roy H. Stogner. The University of Texas at Austin

Size: px
Start display at page:

Download "Predictive Engineering and Computational Sciences. Data Structures and Generic Programming. Roy H. Stogner. The University of Texas at Austin"

Transcription

1 PECOS Predictive Engineering and Computational Sciences Data Structures and Generic Programming Roy H. Stogner The University of Texas at Austin November 16, 2012 Stogner Data Structures November 16, / 36

2 Outline 1 Computational Complexity 2 Data Structures 3 Template Programming 4 Standard Template Library 5 Generic Algorithms 6 Choosing Data Structures Stogner Data Structures November 16, / 36

3 Computational Complexity 1 Computational Complexity 2 Data Structures 3 Template Programming 4 Standard Template Library 5 Generic Algorithms 6 Choosing Data Structures Stogner Data Structures November 16, / 36

4 Computational Complexity Algorithm Scalability Algorithms Input data Series of constant-expense instructions Floating-point operations outweigh integer logic Cache misses outweigh floating-point operations Parallel communication outweighs cache misses... Output data Examples Flag the 10% of N e finite elements with greatest estimated error Do an in-place LU decomposition of a N N dense matrix Stogner Data Structures November 16, / 36

5 Computational Complexity Algorithm Scalability Asymptotic Algorithm Cost Quantify input data size Quantify processor count for parallel algorithms Quantify input norms for iterative numerics Algorithm operation counts are a function of input data sizes Examples N M matrix multiply with M-vector bi A ij x i NM multiplies, (N 1)M adds Bubble sort list of N values Iterate through list Swap any unsorted adjacent pairs Quit when no unsorted pairs found Up to N(N 1) iterations, swaps Stogner Data Structures November 16, / 36

6 Computational Complexity Best-, Average-, Worst-case; Amortized Examples Bubble sort List reverse-sorted: N(N 1) iterations, N(N 1) swaps List already sorted: N 1 iterations, 0 swaps List sorted except first entry: 2(N 1) iterations, < N swaps Appending to a memory-contiguous array Into pre-reserved memory: 1 memory set Requiring new memory: 1 block allocation (of size N + 1), N copies How might we build an array one element at a time with fewer than N(N 1) copies? Stogner Data Structures November 16, / 36

7 Computational Complexity Best-, Average-, Worst-case; Amortized Examples Bubble sort List reverse-sorted: N(N 1) iterations, N(N 1) swaps List already sorted: N 1 iterations, 0 swaps List sorted except first entry: 2(N 1) iterations, < N swaps Appending to a memory-contiguous array Into pre-reserved memory: 1 memory set Requiring new memory: 1 block allocation (of size N + 1), N copies How might we build an array one element at a time with fewer than N(N 1) copies? Block-Allocating Append If pre-reserved memory remains, use it. Otherwise, block allocate 2N memory: for N copies, 1 new element, N 1 pre-reserved slots Stogner Data Structures November 16, / 36

8 Computational Complexity Best-case, Worst-case, Amortized Amortized Block-Allocating Append If pre-reserved memory remains, use it. Otherwise, block allocate 2N memory: for N copies, 1 new element, N 1 reserved slots When the array is of length N with 2 i < N 2 i+1 : N 2 i elements have never been copied 2 i 1 elements have been copied once 2 i 2 elements have been copied twice...etcetera... For fewer than N total copies. Worst-case append cost: N copies, 1 set Amortized append cost: 1 copy, 1 set Stogner Data Structures November 16, / 36

9 Computational Complexity Big-O Notation Cost is some function f(n) of data size. As N goes to infinity, only the highest order components of f matter. Many factors (CPU, language, compiler) multiply performance by a constant, so we ignore constants too. If there exists some constant M and size N 0 such that f(n) M g(n) N > N 0 Then we say f is O (g), of the order of g. Examples Dense LU decomposition is O ( N 3) Balanced octree search is O (log N) General sorting is O (N log N) Stogner Data Structures November 16, / 36

10 Data Structures 1 Computational Complexity 2 Data Structures 3 Template Programming 4 Standard Template Library 5 Generic Algorithms 6 Choosing Data Structures Stogner Data Structures November 16, / 36

11 Data Structures Containers Arrays are fast at some things: O (1) lookup by index O (1) amortized append And slow at others: O (N) insert O (N) search by value And incapable of others: lookup by key Stogner Data Structures November 16, / 36

12 Data Structures Container Operations Find the cardinality of the container Iterate from one entry to the next Iterate from one entry to the previous Erase an entry or entries Swap two containers Sequences Push or pop an entry at the front or back Insert an entry at a specific point Special Containers Map from indices to values Map from keys to values Find/count value(s) by index, key, value bound Stogner Data Structures November 16, / 36

13 Data Structures Arrays Contiguous memory, iterate by incrementing pointers, Possible Implementation vectorfloat { float *vectorstart; float *vectorend; int sizereserved; Start pointers point to the front element, or to NULL End pointers point one past the back element Increasing size may require reallocation, invalidate iterators Adding memory before the start would create a deque Stogner Data Structures November 16, / 36

14 Data Structures Lists What if we don t need random-access, but we do need fast inserts? Use scattered memory, iterate by following pointers Possible Implementation listfloatentry { float data; listfloatentry* next; Inserting requires redirecting two next pointers Insertion doesn t invalidate iterators Adding a previous pointer would create a doubly-linked list Deleting requires redirecting the next entry s previous pointer and the previous entry s next pointer Deletion only invalidates iterators pointing to the deleted entry Stogner Data Structures November 16, / 36

15 Data Structures Binary Trees Instead of connecting entries in a single chain, connect them in a sorted tree NIL NIL NIL NIL NIL NIL NIL NIL NIL NIL NIL Iteration ascends/descends tree Sequence can be automatically sorted! If tree is automatically balanced: Insertion, deletion, search are O (log N) Stogner Data Structures November 16, / 36

16 Data Structures Sets, Multisets Binary trees (among other options) make it easy to implement an ordered set (with unique elements) or multiset (with potentially multiple copies of elements): Items are sorted upon inserting Duplicates can be cheaply omitted or retained upon inserting Unions, intersections, set differences are cheaper after sorting Examples Collect set of all Nodes connected to local Elements Take union of sets of boundary ids located on multiple processors Stogner Data Structures November 16, / 36

17 Data Structures Maps, Multimaps Definition Take a set of pairs of key, value data. Define sorting operation to be a comparison of keys Duplicate keys can be cheaply omitted or retained upon insertion With C++ operator overloading this gives us a structure that looks like an array, but isn t limited to integer keys or dense storage phone_number["john Smith"] = " "; Examples Multimap of all boundary ids associated with each Finite Element side Map from degree of freedom indices to hanging node constraint equations Stogner Data Structures November 16, / 36

18 Data Structures Hash Tables Instead of storing a pointer, calculate an index from a hash of some key data. Store a pointer to a list (usually empty) of keys at each index value. keys John Smith Lisa Smith Sandra Dee hash function buckets : : Insertion, Deletion, Search average O (1) cost Only unordered iteration is possible Storing key/value pairs in a hash table, with only keys considered by the hash function, gives us unordered maps, multimaps. Stogner Data Structures November 16, / 36

19 Template Programming 1 Computational Complexity 2 Data Structures 3 Template Programming 4 Standard Template Library 5 Generic Algorithms 6 Choosing Data Structures Stogner Data Structures November 16, / 36

20 Template Programming Cut and Paste Code Example VectorFloat { float dot(const VectorFloat& v) const { float r = 0; for(int i = 0; i!= size; ++i) r += data[i] * v[i]; return r; float* data; Stogner Data Structures November 16, / 36

21 Template Programming Cut and Paste Code Example VectorFloat { float dot(const VectorFloat& v) const { float r = 0; for(int i = 0; i!= size; ++i) r += data[i] * v[i]; return r; float* data; VectorDouble { Stogner Data Structures November 16, / 36

22 Template Programming Cut and Paste Code Example VectorFloat { float dot(const VectorFloat& v) const { float r = 0; for(int i = 0; i!= size; ++i) r += data[i] * v[i]; return r; float* data; VectorDouble { VectorComplexDouble { Stogner Data Structures November 16, / 36

23 Template Programming Cut and Paste Code Example VectorFloat { float dot(const VectorFloat& v) const { float r = 0; for(int i = 0; i!= size; ++i) r += data[i] * v[i]; return r; float* data; VectorDouble { VectorComplexDouble { VectorTendonitis { Stogner Data Structures November 16, / 36

24 Template Programming Cut and Paste Code Example VectorFloat { float dot(const VectorFloat& v) const { float r = 0; for(int i = 0; i!= size; ++i) r += data[i] * v[i]; return r; float* data; VectorDouble { VectorComplexDouble { VectorTendonitis { Never ever do this. Stogner Data Structures November 16, / 36

25 Template Programming Data Type Independence: Object Oriented Programming Example Create an abstract interface Number { virtual Number& operator+=(const Number& n) = 0; // Pure virtual Stogner Data Structures November 16, / 36

26 Template Programming Data Type Independence: Object Oriented Programming Example Number { virtual Number& operator+=(const Number& n) = 0; // Pure virtual Create an abstract interface Derive concrete subes from it FloatNumber : public Number { DoubleNumber : public Number { Stogner Data Structures November 16, / 36

27 Template Programming Data Type Independence: Object Oriented Programming Example Number { virtual Number& operator+=(const Number& n) = 0; // Pure virtual FloatNumber : public Number { virtual Number& operator+=(const Number& n) { datum += n.data(); return *this; } float datum; Create an abstract interface Derive concrete subes from it Implement the interface s functions in each sub Stogner Data Structures November 16, / 36

28 Template Programming Data Type Independence: Object Oriented Programming Example Number { FloatNumber : public Number { virtual Number& operator+=(const Number& n) { datum += n.data(); return *this; } float datum; Create an abstract interface Derive concrete subes from it Implement the interface s functions in each sub Hold pointers or references to the base VectorNumber { Number** data; Stogner Data Structures November 16, / 36

29 Template Programming Data Type Independence: Object Oriented Programming Example Number { FloatNumber : public Number { VectorNumber { Number dot(const VectorNumber& v) const { Number r = 0; for(int i = 0; i!= size; ++i) r += (*data)[i] * v[i]; return r; } Number** data; Create an abstract interface Derive concrete subes from it Implement the interface s functions in each sub Hold pointers or references to the base Write code to use the interface Stogner Data Structures November 16, / 36

30 Template Programming Data Type Independence: Object Oriented Programming Example Number { FloatNumber : public Number { VectorNumber { Number dot(const VectorNumber& v) const { Number r = 0; for(int i = 0; i!= size; ++i) r += (*data)[i] * v[i]; return r; } Number** data; Stogner Data Structures November 16, / 36

31 Template Programming Data Type Independence: Object Oriented Programming Example Number { FloatNumber : public Number { VectorNumber { Number dot(const VectorNumber& v) const { Number r = 0; for(int i = 0; i!= size; ++i) r += (*data)[i] * v[i]; return r; } Number** data; Advantages: Stogner Data Structures November 16, / 36

32 Template Programming Data Type Independence: Object Oriented Programming Example Number { FloatNumber : public Number { VectorNumber { Number dot(const VectorNumber& v) const { Number r = 0; for(int i = 0; i!= size; ++i) r += (*data)[i] * v[i]; return r; } Number** data; Advantages: Works correctly Stogner Data Structures November 16, / 36

33 Template Programming Data Type Independence: Object Oriented Programming Example Number { FloatNumber : public Number { VectorNumber { Number dot(const VectorNumber& v) const { Number r = 0; for(int i = 0; i!= size; ++i) r += (*data)[i] * v[i]; return r; } Number** data; Advantages: Works correctly Defines concept relations Number is an Abstract Base Class FloatNumber is-a Number Stogner Data Structures November 16, / 36

34 Template Programming Data Type Independence: Object Oriented Programming Example Number { FloatNumber : public Number { VectorNumber { Number dot(const VectorNumber& v) const { Number r = 0; for(int i = 0; i!= size; ++i) r += (*data)[i] * v[i]; return r; } Number** data; Advantages: Works correctly Defines concept relations Number is an Abstract Base Class FloatNumber is-a Number Heterogenous containers! data[0] can be double, data[1] can be complex... Stogner Data Structures November 16, / 36

35 Template Programming Data Type Independence: Object Oriented Programming Example Number { FloatNumber : public Number { VectorNumber { Number dot(const VectorNumber& v) const { Number r = 0; for(int i = 0; i!= size; ++i) r += (*data)[i] * v[i]; return r; } Number** data; Advantages: Works correctly Defines concept relations Number is an Abstract Base Class FloatNumber is-a Number Heterogenous containers! data[0] can be double, data[1] can be complex... Disadvantages: Stogner Data Structures November 16, / 36

36 Template Programming Data Type Independence: Object Oriented Programming Example Number { FloatNumber : public Number { VectorNumber { Number dot(const VectorNumber& v) const { Number r = 0; for(int i = 0; i!= size; ++i) r += (*data)[i] * v[i]; return r; } Number** data; Advantages: Works correctly Defines concept relations Number is an Abstract Base Class FloatNumber is-a Number Heterogenous containers! data[0] can be double, data[1] can be complex... Disadvantages: Can require shim es A float isn t a FloatNumber! Stogner Data Structures November 16, / 36

37 Template Programming Data Type Independence: Object Oriented Programming Example Number { FloatNumber : public Number { VectorNumber { Number dot(const VectorNumber& v) const { Number r = 0; for(int i = 0; i!= size; ++i) r += (*data)[i] * v[i]; return r; } Number** data; Advantages: Works correctly Defines concept relations Number is an Abstract Base Class FloatNumber is-a Number Heterogenous containers! data[0] can be double, data[1] can be complex... Disadvantages: Can require shim es A float isn t a FloatNumber! Slow as hell Fragmented dynamic memory Virtual function calls for every FLOP! Stogner Data Structures November 16, / 36

38 Template Programming Data Type Independence: Generic Programming Template arguments can be data types Example template <typename T> Stogner Data Structures November 16, / 36

39 Template Programming Data Type Independence: Generic Programming Example Template arguments can be data types Class data members can depend on template argument template <typename T> NumericVector { T* data; Stogner Data Structures November 16, / 36

40 Template Programming Data Type Independence: Generic Programming Example template <typename T> NumericVector { T dot(const NumericVector<T>& v) const { T r = 0; for(int i = 0; i!= size; ++i) r += data[i] * v[i]; return r; T* data; Template arguments can be data types Class data members can depend on template argument Class methods can depend on template argument Stogner Data Structures November 16, / 36

41 Template Programming Data Type Independence: Generic Programming Example template <typename T> NumericVector { T dot(const NumericVector<T>& v) const { T r = 0; for(int i = 0; i!= size; ++i) r += data[i] * v[i]; return r; T* data; Template arguments can be data types Class data members can depend on template argument Class methods can depend on template argument Templated es can be instantiated with plain data types NumericVector<float> floatvec; Stogner Data Structures November 16, / 36

42 Template Programming Data Type Independence: Generic Programming Example template <typename T> NumericVector { T dot(const NumericVector<T>& v) const { T r = 0; for(int i = 0; i!= size; ++i) r += data[i] * v[i]; return r; T* data; NumericVector<float> floatvec; NumericVector<complex<double> > complexdoublevec; Template arguments can be data types Class data members can depend on template argument Class methods can depend on template argument Templated es can be instantiated with plain data types... or with other instantiated templated types Stogner Data Structures November 16, / 36

43 Template Programming Data Type Independence: Generic Programming Example template <typename T> NumericVector { T dot(const NumericVector<T>& v) const { T r = 0; for(int i = 0; i!= size; ++i) r += data[i] * v[i]; return r; T* data; NumericVector<float> floatvec; NumericVector<complex<double> > complexdoublevec; NumericVector<NumericVector<float> > floatmatrix; Template arguments can be data types Class data members can depend on template argument Class methods can depend on template argument Templated es can be instantiated with plain data types... or with other instantiated templated types... or with other instantiations of themselves! Stogner Data Structures November 16, / 36

44 Template Programming Data Type Independence: Generic Programming Example Advantages template <typename T> NumericVector { T dot(const NumericVector<T>& v) const { T r = 0; for(int i = 0; i!= size; ++i) r += data[i] * v[i]; return r; T* data; NumericVector<float> floatvec; NumericVector<complex<double> > complexdoublevec; NumericVector<NumericVector<float> > floatmatrix; Stogner Data Structures November 16, / 36

45 Template Programming Data Type Independence: Generic Programming Example template <typename T> NumericVector { T dot(const NumericVector<T>& v) const { T r = 0; for(int i = 0; i!= size; ++i) r += data[i] * v[i]; return r; T* data; Advantages Works with existing types NumericVector<float> floatvec; NumericVector<complex<double> > complexdoublevec; NumericVector<NumericVector<float> > floatmatrix; Stogner Data Structures November 16, / 36

46 Template Programming Data Type Independence: Generic Programming Example template <typename T> NumericVector { T dot(const NumericVector<T>& v) const { T r = 0; for(int i = 0; i!= size; ++i) r += data[i] * v[i]; return r; T* data; Advantages Works with existing types Functions can be inlined, not just virtual NumericVector<float> floatvec; NumericVector<complex<double> > complexdoublevec; NumericVector<NumericVector<float> > floatmatrix; Stogner Data Structures November 16, / 36

47 Template Programming Data Type Independence: Generic Programming Example template <typename T> NumericVector { T dot(const NumericVector<T>& v) const { T r = 0; for(int i = 0; i!= size; ++i) r += data[i] * v[i]; return r; T* data; Advantages Works with existing types Functions can be inlined, not just virtual Intermediate calculation types can be implicit NumericVector<float> floatvec; NumericVector<complex<double> > complexdoublevec; NumericVector<NumericVector<float> > floatmatrix; Stogner Data Structures November 16, / 36

48 Template Programming Data Type Independence: Generic Programming Example template <typename T> NumericVector { T dot(const NumericVector<T>& v) const { T r = 0; for(int i = 0; i!= size; ++i) r += data[i] * v[i]; return r; T* data; NumericVector<float> floatvec; NumericVector<complex<double> > complexdoublevec; NumericVector<NumericVector<float> > floatmatrix; Advantages Works with existing types Functions can be inlined, not just virtual Intermediate calculation types can be implicit Disadvantages Makes assumptions about input types Is that NumericVector operator* an element by element multiplication or a dot product? Stogner Data Structures November 16, / 36

49 Template Programming Data Type Independence: Generic Programming Example template <typename T> NumericVector { T dot(const NumericVector<T>& v) const { T r = 0; for(int i = 0; i!= size; ++i) r += data[i] * v[i]; return r; T* data; NumericVector<float> floatvec; NumericVector<complex<double> > complexdoublevec; NumericVector<NumericVector<float> > floatmatrix; Advantages Works with existing types Functions can be inlined, not just virtual Intermediate calculation types can be implicit Disadvantages Makes assumptions about input types Is that NumericVector operator* an element by element multiplication or a dot product? Functions are undefined until instantiation Compile-time bugs go undetected until triggered Stogner Data Structures November 16, / 36

50 Standard Template Library 1 Computational Complexity 2 Data Structures 3 Template Programming 4 Standard Template Library 5 Generic Algorithms 6 Choosing Data Structures Stogner Data Structures November 16, / 36

51 Standard Template Library STL Containers What s better than reusing your own code instead of rewriting it? Stogner Data Structures November 16, / 36

52 Standard Template Library STL Containers What s better than reusing your own code instead of rewriting it? Using someone else s code instead of rewriting it. #include <list> #include <set> #include <vector> #include <unordered_map> // C++11 using namespace std; typedef list< pair< double, map< string, NumericVector<Number> >>> solution_history_type; Stogner Data Structures November 16, / 36

53 Standard Template Library STL Container Operations Operator overloading makes vectors, maps look like arrays. Class methods give access to more advanced operations. vector<float> weights(12, 1.0); // Creates a dozen 1.0 values weights[5] *= 2; // Doubles the sixth value weights.resize(24, 0.0); // Let s make that two dozen, // with new entries zero map<elem *, double> err; // Any data can be a key err[my_element] = 1e-6; for (auto i = err.begin(); i!= err.end; ++i) *i = 1.0; // Not a simple pointer dereference if (err.find(other_element) == err.end()) I_found_it = false; // Search without inserting Stogner Data Structures November 16, / 36

54 Generic Algorithms 1 Computational Complexity 2 Data Structures 3 Template Programming 4 Standard Template Library 5 Generic Algorithms 6 Choosing Data Structures Stogner Data Structures November 16, / 36

55 Generic Algorithms Duck Typing So we can duct tape our templated type to any type T that behaves like we expect math to behave? Is that safe? Stogner Data Structures November 16, / 36

56 Generic Algorithms Duck Typing So we can duct tape our templated type to any type T that behaves like we expect math to behave? Is that safe? If it walks like a duck, quacks like a duck, looks like a duck, it must be a duck. Stogner Data Structures November 16, / 36

57 Generic Algorithms Concepts Container types may be very different Each exposes homologous methods under the same names begin(), end() of containers ++,, ==, etc. operators on iterators Containers may support different Concepts Is it a sequence? Does it associate keys and values? Iterators may support different Concepts Can you increment it? Can you also decrement it? Can you read from it? Can you write to it? Algorithms support any containers and iterators that implement the required concepts. User-defined data structures work as soon as they support the right semantics Stogner Data Structures November 16, / 36

58 Generic Algorithms Iterators Iterator types already discussed: vector::iterator increments a pointer map::iterator walks a tree More complicated iterator types: istream::iterator reads from terminal, file, network, etc. ostream::iterator writes to any of the above reverse iterator goes backwards instead of forwards mesh inserter iterator adds Node, Elem, or Point objects to a Mesh (not in the STL, but works with it as an Output Iterator) Stogner Data Structures November 16, / 36

59 Generic Algorithms Ranges Containers and subsets of containers can be specified as ranges, pairs of iterators. First iterator identifies first item in range Second iterator identifies one past last item in range. First==Last for empty ranges. Simple examples my_set.insert(my_list.begin(), my_list.end()); iter = find(my_vec.begin(), my_vec.end(), element); Stogner Data Structures November 16, / 36

60 Generic Algorithms Algorithms find was our first generic algorithm: linear search through any range (and so through any container). template <typename Iter, typename Value> Iter find (Iter first, Iter last, Value v) { while (first!= last && *first!= v) first++; return first; } STL includes this and others: sort(vec.begin(), vec.end()); copy (vec.begin(), vec.end(), ostream_iterator<int>(cout, "\n" cout << accumulate(values.begin(), values.end(), 0.0); vec.erase(remove(vec.begin(), vec.end(), 1), myvec.end()); Stogner Data Structures November 16, / 36

61 Generic Algorithms Functors Defining functions and/or objects which behave like functions can add meat to the standard algorithms. Predicates define whether or not a value meets some user-specified condition: bool is_positive (int val) { return (v > 0); } int number_of_positives = count_if(vec.begin(), vec.end(), is_ Examples Predicate iterators: libmesh elements begin() becomes active elements begin() or local elements begin() or active local elements begin() after adding tests for finite element refinement and partitioning Selective operations: remove if() strips elements from a range based on a predicate test Stogner Data Structures November 16, / 36

62 Generic Algorithms Functors Functors can modify data as well, transforming input ranges to output ranges (in place if requested): transform(text.begin(), text.end(), text.begin(), tolower); Replacing iteration and loop bodies with algorithms and functors adds more flexibility: Singlethreaded code can be compiler-optimized Template specialization / function overloading Multithreaded code can take functors and ranges Intel Threaded Building Blocks Stogner Data Structures November 16, / 36

63 Choosing Data Structures 1 Computational Complexity 2 Data Structures 3 Template Programming 4 Standard Template Library 5 Generic Algorithms 6 Choosing Data Structures Stogner Data Structures November 16, / 36

64 Choosing Data Structures What the STL Won t Tell You Complexity Constants // N set insertions, each O(log(N_unique)) or better set sorted_set(vec.begin(), vec.end()); // O(N log(n)) sorting algorithm sort(vec.begin(), vec.end()); // O(N) renumbering and erasure vec.erase(unique(vec.begin(), vec.end() Which is faster? Are further sort/insert operations needed? Search operations? Is the ratio N unique /N bounded below? Will a partial sort do? Stogner Data Structures November 16, / 36

65 Choosing Data Structures What the STL Won t Tell You Complexity Constants What compiler are you using? Optimization level? Debugging level? C++11 move constructors, unordered set? STL can be optimized.. What processor are you using? How important is vectorization? How bad are cache misses? What data are you using? O (f(n)) is less critical for small N Worst-case hash table, unbalanced tree performance is atrocious In hot spots, use typedef & template specializations to enable easy switching between containers; benchmark each! Stogner Data Structures November 16, / 36

66 Choosing Data Structures What the STL Won t Do For You Multidimensional Data Structures Multi-dimensional arrays vector<vector<t> > fakes a[i][j] Poorer cache locality? Individually resizeable rows?? Eigen, Boost, Blitz++, etc. third-party options Graphs Boost, GTL, VTGL, CGTL? General Trees B-tree, other logical structures Quadtree, Octree, etc. spatial structures Non-general Container Methods vector isn t a numeric data type valarray numeric containers aren t STL-conforming Sub container types? STL containers have non-virtual destructors Stogner Data Structures November 16, / 36

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

When we program, we have to deal with errors. Our most basic aim is correctness, but we must Chapter 5 Errors When we program, we have to deal with errors. Our most basic aim is correctness, but we must deal with incomplete problem specifications, incomplete programs, and our own errors. When

More information

COEN244: Class & function templates

COEN244: Class & function templates COEN244: Class & function templates Aishy Amer Electrical & Computer Engineering Templates Function Templates Class Templates Outline Templates and inheritance Introduction to C++ Standard Template Library

More information

STL: C++ Standard Library

STL: C++ Standard Library STL: C++ Standard Library Encapsulates complex data structures and algorithms CSC 330 OO Software Design 1 We ve emphasized the importance of software reuse. Recognizing that many data structures and algorithms

More information

use static size for this buffer

use static size for this buffer Software Design (C++) 4. Templates and standard library (STL) Juha Vihavainen University of Helsinki Overview Introduction to templates (generics) std::vector again templates: specialization by code generation

More information

Predictive Engineering and Computational Sciences. Data Structures and Methods for Unstructured Distributed Meshes. Roy H. Stogner

Predictive Engineering and Computational Sciences. Data Structures and Methods for Unstructured Distributed Meshes. Roy H. Stogner PECOS Predictive Engineering and Computational Sciences Data Structures and Methods for Unstructured Distributed Meshes Roy H. Stogner The University of Texas at Austin May 23, 2012 Roy H. Stogner Distributed

More information

CS11 Advanced C++ Fall Lecture 1

CS11 Advanced C++ Fall Lecture 1 CS11 Advanced C++ Fall 2006-2007 Lecture 1 Welcome! ~8 lectures Slides are posted on CS11 website http://www.cs.caltech.edu/courses/cs11 ~6 lab assignments More involved labs 2-3 week project at end CS

More information

C++ 11 and the Standard Library: Containers, Iterators, Algorithms

C++ 11 and the Standard Library: Containers, Iterators, Algorithms and the Standard Library:,, Comp Sci 1575 Outline 1 2 3 4 Outline 1 2 3 4 #i n clude i n t main ( ) { i n t v a l u e 0 = 5 ; // C++ 98 i n t v a l u e 1 ( 5 ) ; // C++ 98 i n t v a

More information

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

Outline. Variables Automatic type inference. Generic programming. Generic programming. Templates Template compilation Outline EDAF30 Programming in C++ 4. The standard library. Algorithms and containers. Sven Gestegård Robertz Computer Science, LTH 2018 1 Type inference 2 3 The standard library Algorithms Containers Sequences

More information

Advanced C++ STL. Tony Wong

Advanced C++ STL. Tony Wong Tony Wong 2017-03-25 C++ Standard Template Library Algorithms Sorting Searching... Data structures Dynamically-sized array Queues... The implementation is abstracted away from the users The implementation

More information

CSE 100: C++ TEMPLATES AND ITERATORS

CSE 100: C++ TEMPLATES AND ITERATORS CSE 100: C++ TEMPLATES AND ITERATORS Announcements iclickers: Please register at ted.ucsd.edu. Start ASAP!! For PA1 (Due next week). 10/6 grading and 10/8 regrading How is Assignment 1 going? A. I haven

More information

Absolute C++ Walter Savitch

Absolute C++ Walter Savitch Absolute C++ sixth edition Walter Savitch Global edition This page intentionally left blank Absolute C++, Global Edition Cover Title Page Copyright Page Preface Acknowledgments Brief Contents Contents

More information

Section 05: Solutions

Section 05: Solutions Section 05: Solutions 1. Memory and B-Tree (a) Based on your understanding of how computers access and store memory, why might it be faster to access all the elements of an array-based queue than to access

More information

Section 05: Solutions

Section 05: Solutions Section 05: Solutions 1. Asymptotic Analysis (a) Applying definitions For each of the following, choose a c and n 0 which show f(n) O(g(n)). Explain why your values of c and n 0 work. (i) f(n) = 5000n

More information

SETS AND MAPS. Chapter 9

SETS AND MAPS. Chapter 9 SETS AND MAPS Chapter 9 The set Functions Required methods: testing set membership (find) testing for an empty set (empty) determining set size (size) creating an iterator over the set (begin, end) adding

More information

Function Templates. Consider the following function:

Function Templates. Consider the following function: Function Templates Consider the following function: void swap (int& a, int& b) { int tmp = a; a = b; b = tmp; Swapping integers. This function let's you swap the contents of two integer variables. But

More information

Advanced Functional Programming with C++ Templates

Advanced Functional Programming with C++ Templates Advanced Functional Programming with C++ Templates Roy H. Stogner The University of Texas at Austin Feb 29, 2012 Roy H. Stogner Generic Feb 29, 2012 1 / 56 Stuff We Covered Last Time Data Types Outline

More information

CSCI-1200 Data Structures Fall 2017 Lecture 10 Vector Iterators & Linked Lists

CSCI-1200 Data Structures Fall 2017 Lecture 10 Vector Iterators & Linked Lists CSCI-1200 Data Structures Fall 2017 Lecture 10 Vector Iterators & Linked Lists Review from Lecture 9 Explored a program to maintain a class enrollment list and an associated waiting list. Unfortunately,

More information

Chapter 5. The Standard Template Library.

Chapter 5. The Standard Template Library. Object-oriented programming B, Lecture 11e. 1 Chapter 5. The Standard Template Library. 5.1. Overview of main STL components. The Standard Template Library (STL) has been developed by Alexander Stepanov,

More information

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

CSCI-1200 Data Structures Fall 2017 Lecture 9 Iterators & STL Lists Review from Lecture 8 CSCI-1200 Data Structures Fall 2017 Lecture 9 Iterators & STL Lists Designing our own container classes Dynamically allocated memory in classes Copy constructors, assignment operators,

More information

CSCI-1200 Data Structures Spring 2018 Lecture 8 Templated Classes & Vector Implementation

CSCI-1200 Data Structures Spring 2018 Lecture 8 Templated Classes & Vector Implementation CSCI-1200 Data Structures Spring 2018 Lecture 8 Templated Classes & Vector Implementation Review from Lectures 7 Algorithm Analysis, Formal Definition of Order Notation Simple recursion, Visualization

More information

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

CSCI-1200 Data Structures Spring 2016 Lecture 7 Iterators, STL Lists & Order Notation Today CSCI-1200 Data Structures Spring 2016 Lecture 7 Iterators, STL Lists & Order Notation Another vector operation: pop back Erasing items from vectors is inefficient! Iterators and iterator operations

More information

CIS 190: C/C++ Programming. Lecture 12 Student Choice

CIS 190: C/C++ Programming. Lecture 12 Student Choice CIS 190: C/C++ Programming Lecture 12 Student Choice Outline Hash Maps Collisions Using Open Addressing Collisions Chaining Collisions In C++ C++ STL Containers C++ GUI Resources Hash Maps (AKA Hash Tables)

More information

CSCI-1200 Data Structures Fall 2010 Lecture 8 Iterators

CSCI-1200 Data Structures Fall 2010 Lecture 8 Iterators CSCI-1200 Data Structures Fall 2010 Lecture 8 Iterators Review from Lecture 7 Designing our own container classes Dynamically allocated memory in classes Copy constructors, assignment operators, and destructors

More information

Section 05: Midterm Review

Section 05: Midterm Review Section 05: Midterm Review 1. Asymptotic Analysis (a) Applying definitions For each of the following, choose a c and n 0 which show f(n) O(g(n)). Explain why your values of c and n 0 work. (i) f(n) = 5000n

More information

CSCI-1200 Data Structures Fall 2014 Lecture 8 Iterators

CSCI-1200 Data Structures Fall 2014 Lecture 8 Iterators Review from Lecture 7 CSCI-1200 Data Structures Fall 2014 Lecture 8 Iterators Designing our own container classes Dynamically allocated memory in classes Copy constructors, assignment operators, and destructors

More information

CSE030 Fall 2012 Final Exam Friday, December 14, PM

CSE030 Fall 2012 Final Exam Friday, December 14, PM CSE030 Fall 2012 Final Exam Friday, December 14, 2012 3-6PM Write your name here and at the top of each page! Name: Select your lab session: Tuesdays Thursdays Paper. If you have any questions or need

More information

Lecture-5. STL Containers & Iterators

Lecture-5. STL Containers & Iterators Lecture-5 STL Containers & Iterators Containers as a form of Aggregation Fixed aggregation An object is composed of a fixed set of component objects Variable aggregation An object is composed of a variable

More information

Templates and Vectors

Templates and Vectors Templates and Vectors 1 Generic Programming function templates class templates 2 the STL vector class a vector of strings enumerating elements with an iterator inserting and erasing 3 Writing our own vector

More information

The Cut and Thrust of CUDA

The Cut and Thrust of CUDA The Cut and Thrust of CUDA Luke Hodkinson Center for Astrophysics and Supercomputing Swinburne University of Technology Melbourne, Hawthorn 32000, Australia May 16, 2013 Luke Hodkinson The Cut and Thrust

More information

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

CS 251 INTERMEDIATE SOFTWARE DESIGN SPRING C ++ Basics Review part 2 Auto pointer, templates, STL algorithms CS 251 INTERMEDIATE SOFTWARE DESIGN SPRING 2011 C ++ Basics Review part 2 Auto pointer, templates, STL algorithms AUTO POINTER (AUTO_PTR) //Example showing a bad situation with naked pointers void MyFunction()

More information

Object-Oriented Programming for Scientific Computing

Object-Oriented Programming for Scientific Computing Object-Oriented Programming for Scientific Computing Dynamic Memory Management Ole Klein Interdisciplinary Center for Scientific Computing Heidelberg University ole.klein@iwr.uni-heidelberg.de 2. Mai 2017

More information

1. The term STL stands for?

1. The term STL stands for? 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 2. Which of the following statements regarding the

More information

Exception Namespaces C Interoperability Templates. More C++ David Chisnall. March 17, 2011

Exception Namespaces C Interoperability Templates. More C++ David Chisnall. March 17, 2011 More C++ David Chisnall March 17, 2011 Exceptions A more fashionable goto Provides a second way of sending an error condition up the stack until it can be handled Lets intervening stack frames ignore errors

More information

Predictive Engineering and Computational Sciences. C++14 Generic Programming as a Domain-Specific Language for PDEs. Roy H.

Predictive Engineering and Computational Sciences. C++14 Generic Programming as a Domain-Specific Language for PDEs. Roy H. PECOS Predictive Engineering and Computational Sciences C++14 Generic Programming as a Domain-Specific Language for PDEs Roy H. Stogner 1 1 Institute for Computational Engineering and Sciences The University

More information

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

1/29/2011 AUTO POINTER (AUTO_PTR) INTERMEDIATE SOFTWARE DESIGN SPRING delete ptr might not happen memory leak! //Example showing a bad situation with naked pointers CS 251 INTERMEDIATE SOFTWARE DESIGN SPRING 2011 C ++ Basics Review part 2 Auto pointer, templates, STL algorithms void MyFunction() MyClass* ptr( new

More information

MODULE 33 --THE STL-- ALGORITHM PART I

MODULE 33 --THE STL-- ALGORITHM PART I MODULE 33 --THE STL-- ALGORITHM PART I My Training Period: hours Note: Compiled using Microsoft Visual C++.Net, win32 empty console mode application. g++ compilation examples given at the end of this Module.

More information

7 TEMPLATES AND STL. 7.1 Function Templates

7 TEMPLATES AND STL. 7.1 Function Templates 7 templates and STL:: Function Templates 7 TEMPLATES AND STL 7.1 Function Templates Support generic programming functions have parameterized types (can have other parameters as well) functions are instantiated

More information

PHY4321 Summary Notes

PHY4321 Summary Notes PHY4321 Summary Notes The next few pages contain some helpful notes that summarize some of the more useful material from the lecture notes. Be aware, though, that this is not a complete set and doesn t

More information

CS11 Advanced C++ Spring 2018 Lecture 2

CS11 Advanced C++ Spring 2018 Lecture 2 CS11 Advanced C++ Spring 2018 Lecture 2 Lab 2: Completing the Vector Last week, got the basic functionality of our Vector template working It is still missing some critical functionality Iterators are

More information

DATA STRUCTURE : A MCQ QUESTION SET Code : RBMCQ0305

DATA STRUCTURE : A MCQ QUESTION SET Code : RBMCQ0305 Q.1 If h is any hashing function and is used to hash n keys in to a table of size m, where n

More information

CSCI-1200 Data Structures Spring 2018 Lecture 10 Vector Iterators & Linked Lists

CSCI-1200 Data Structures Spring 2018 Lecture 10 Vector Iterators & Linked Lists CSCI-1200 Data Structures Spring 2018 Lecture 10 Vector Iterators & Linked Lists Review from Lecture 9 Explored a program to maintain a class enrollment list and an associated waiting list. Unfortunately,

More information

Measuring algorithm efficiency

Measuring algorithm efficiency CMPT 225 Measuring algorithm efficiency Timing Counting Cost functions Cases Best case Average case Worst case Searching Sorting O Notation O notation's mathematical basis O notation classes and notations

More information

6. Pointers, Structs, and Arrays. 1. Juli 2011

6. Pointers, Structs, and Arrays. 1. Juli 2011 1. Juli 2011 Einführung in die Programmierung Introduction to C/C++, Tobias Weinzierl page 1 of 50 Outline Recapitulation Pointers Dynamic Memory Allocation Structs Arrays Bubble Sort Strings Einführung

More information

Dynamic Data Structures. John Ross Wallrabenstein

Dynamic Data Structures. John Ross Wallrabenstein Dynamic Data Structures John Ross Wallrabenstein Recursion Review In the previous lecture, I asked you to find a recursive definition for the following problem: Given: A base10 number X Assume X has N

More information

CMSC 341 Lecture 16/17 Hashing, Parts 1 & 2

CMSC 341 Lecture 16/17 Hashing, Parts 1 & 2 CMSC 341 Lecture 16/17 Hashing, Parts 1 & 2 Prof. John Park Based on slides from previous iterations of this course Today s Topics Overview Uses and motivations of hash tables Major concerns with hash

More information

Data Structures - CSCI 102. CS102 Hash Tables. Prof. Tejada. Copyright Sheila Tejada

Data Structures - CSCI 102. CS102 Hash Tables. Prof. Tejada. Copyright Sheila Tejada CS102 Hash Tables Prof. Tejada 1 Vectors, Linked Lists, Stack, Queues, Deques Can t provide fast insertion/removal and fast lookup at the same time The Limitations of Data Structure Binary Search Trees,

More information

Advanced Database Systems

Advanced Database Systems Lecture IV Query Processing Kyumars Sheykh Esmaili Basic Steps in Query Processing 2 Query Optimization Many equivalent execution plans Choosing the best one Based on Heuristics, Cost Will be discussed

More information

CMSC 341 Lecture 6 Templates, Stacks & Queues. Based on slides by Shawn Lupoli & Katherine Gibson at UMBC

CMSC 341 Lecture 6 Templates, Stacks & Queues. Based on slides by Shawn Lupoli & Katherine Gibson at UMBC CMSC 341 Lecture 6 Templates, Stacks & Queues Based on slides by Shawn Lupoli & Katherine Gibson at UMBC Today s Topics Data types in C++ Overloading functions Templates How to implement them Possible

More information

Lecture 16. Reading: Weiss Ch. 5 CSE 100, UCSD: LEC 16. Page 1 of 40

Lecture 16. Reading: Weiss Ch. 5 CSE 100, UCSD: LEC 16. Page 1 of 40 Lecture 16 Hashing Hash table and hash function design Hash functions for integers and strings Collision resolution strategies: linear probing, double hashing, random hashing, separate chaining Hash table

More information

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

STL components. STL: C++ Standard Library Standard Template Library (STL) Main Ideas. Components. Encapsulates complex data structures and algorithms STL: C++ Standard Library Standard Template Library (STL) Encapsulates complex data structures and algorithms is a library of generic container classes which are both efficient and functional C++ STL developed

More information

2. (a) Explain the concept of virtual functions in C++ with suitable examples. (b) Explain the concept of operator overloading in C++.

2. (a) Explain the concept of virtual functions in C++ with suitable examples. (b) Explain the concept of operator overloading in C++. Code No: R059211201 Set No. 1 1. (a) What is a friend function? Explain the advantages and disadvantages of it? (b) What is static member function? Explain it s limitations. [8+8] 2. (a) Explain the concept

More information

C The new standard

C The new standard C++11 - The new standard Lars Kühne Institut für Informatik Lehrstuhl für theoretische Informatik II Friedrich-Schiller-Universität Jena January 16, 2013 Overview A little bit of history: C++ was initially

More information

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

EE 355 Unit 10. C++ STL - Vectors and Deques. Mark Redekopp 1 EE 355 Unit 10 C++ STL - Vectors and Deques Mark Redekopp 2 Templates We ve built a list to store integers But what if we want a list of double s or char s or other objects We would have to define the

More information

(8 1) Container Classes & Class Templates D & D Chapter 18. Instructor - Andrew S. O Fallon CptS 122 (October 8, 2018) Washington State University

(8 1) Container Classes & Class Templates D & D Chapter 18. Instructor - Andrew S. O Fallon CptS 122 (October 8, 2018) Washington State University (8 1) Container Classes & Class Templates D & D Chapter 18 Instructor - Andrew S. O Fallon CptS 122 (October 8, 2018) Washington State University Key Concepts Class and block scope Access and utility functions

More information

CSCI-1200 Data Structures Fall 2018 Lecture 21 Hash Tables, part 1

CSCI-1200 Data Structures Fall 2018 Lecture 21 Hash Tables, part 1 Review from Lecture 20 CSCI-1200 Data Structures Fall 2018 Lecture 21 Hash Tables, part 1 Finishing binary search trees & the ds set class Operators as non-member functions, as member functions, and as

More information

pointers & references

pointers & references pointers & references 1-22-2013 Inline Functions References & Pointers Arrays & Vectors HW#1 posted due: today Quiz Thursday, 1/24 // point.h #ifndef POINT_H_ #define POINT_H_ #include using

More information

6. Pointers, Structs, and Arrays. March 14 & 15, 2011

6. Pointers, Structs, and Arrays. March 14 & 15, 2011 March 14 & 15, 2011 Einführung in die Programmierung Introduction to C/C++, Tobias Weinzierl page 1 of 47 Outline Recapitulation Pointers Dynamic Memory Allocation Structs Arrays Bubble Sort Strings Einführung

More information

The Standard Template Library. An introduction

The Standard Template Library. An introduction 1 The Standard Template Library An introduction 2 Standard Template Library (STL) Objective: Reuse common code Common constructs: Generic containers and algorithms STL Part of the C++ Standard Library

More information

CSE 100 Advanced Data Structures

CSE 100 Advanced Data Structures CSE 100 Advanced Data Structures Overview of course requirements Outline of CSE 100 topics Review of trees Helpful hints for team programming Information about computer accounts Page 1 of 25 CSE 100 web

More information

CSI33 Data Structures

CSI33 Data Structures Outline Department of Mathematics and Computer Science Bronx Community College November 22, 2017 Outline Outline 1 Chapter 12: C++ Templates Outline Chapter 12: C++ Templates 1 Chapter 12: C++ Templates

More information

CSCI-1200 Data Structures Fall 2009 Lecture 20 Hash Tables, Part II

CSCI-1200 Data Structures Fall 2009 Lecture 20 Hash Tables, Part II Review from Lecture 19 CSCI-1200 Data Structures Fall 2009 Lecture 20 Hash Tables, Part II Operators as non-member functions, as member functions, and as friend functions. A hash table is a table implementation

More information

CSCI-1200 Data Structures Fall 2018 Lecture 22 Hash Tables, part 2 & Priority Queues, part 1

CSCI-1200 Data Structures Fall 2018 Lecture 22 Hash Tables, part 2 & Priority Queues, part 1 Review from Lecture 21 CSCI-1200 Data Structures Fall 2018 Lecture 22 Hash Tables, part 2 & Priority Queues, part 1 the single most important data structure known to mankind Hash Tables, Hash Functions,

More information

The following is an excerpt from Scott Meyers new book, Effective C++, Third Edition: 55 Specific Ways to Improve Your Programs and Designs.

The following is an excerpt from Scott Meyers new book, Effective C++, Third Edition: 55 Specific Ways to Improve Your Programs and Designs. The following is an excerpt from Scott Meyers new book, Effective C++, Third Edition: 55 Specific Ways to Improve Your Programs and Designs. Item 47: Use traits classes for information about types. The

More information

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

Container Notes. Di erent Kinds of Containers. Types Defined by Containers. C++11 Container Notes C++11 Di erent Kinds of Containers Container Notes A container is an object that stores other objects and has methods for accessing the elements. There are two fundamentally di erent kinds of containers: Sequences

More information

Computer Science II Lecture 2 Strings, Vectors and Recursion

Computer Science II Lecture 2 Strings, Vectors and Recursion 1 Overview of Lecture 2 Computer Science II Lecture 2 Strings, Vectors and Recursion The following topics will be covered quickly strings vectors as smart arrays Basic recursion Mostly, these are assumed

More information

Linked Lists. Linked list: a collection of items (nodes) containing two components: Data Address (link) of the next node in the list

Linked Lists. Linked list: a collection of items (nodes) containing two components: Data Address (link) of the next node in the list Linked Lists Introduction : Data can be organized and processed sequentially using an array, called a sequential list Problems with an array Array size is fixed Unsorted array: searching for an item is

More information

Linked Lists. Linked list: a collection of items (nodes) containing two components: Data Address (link) of the next node in the list

Linked Lists. Linked list: a collection of items (nodes) containing two components: Data Address (link) of the next node in the list Linked Lists Introduction : Data can be organized and processed sequentially using an array, called a sequential list Problems with an array Array size is fixed Unsorted array: searching for an item is

More information

Lesson 13 - Vectors Dynamic Data Storage

Lesson 13 - Vectors Dynamic Data Storage Lesson 13 - Vectors Dynamic Data Storage Summary In this lesson we introduce the Standard Template Library by demonstrating the use of Vectors to provide dynamic storage of data elements. New Concepts

More information

Writing Generic Functions. Lecture 20 Hartmut Kaiser hkaiser/fall_2013/csc1254.html

Writing Generic Functions. Lecture 20 Hartmut Kaiser  hkaiser/fall_2013/csc1254.html Writing Generic Functions Lecture 20 Hartmut Kaiser hkaiser@cct.lsu.edu http://www.cct.lsu.edu/ hkaiser/fall_2013/csc1254.html Code Quality Programming Principle of the Day Minimize Coupling - Any section

More information

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

Polymorphism. Programming in C++ A problem of reuse. Swapping arguments. Session 4 - Genericity, Containers. Code that works for many types. Session 4 - Genericity, Containers Polymorphism Code that works for many types. Dr Christos Kloukinas City, UoL http://staff.city.ac.uk/c.kloukinas/cpp (slides originally produced by Dr Ross Paterson)

More information

Structuur van Computerprogramma s 2

Structuur van Computerprogramma s 2 Structuur van Computerprogramma s 2 dr. Dirk Deridder Dirk.Deridder@vub.ac.be http://soft.vub.ac.be/ Vrije Universiteit Brussel - Faculty of Science and Bio-Engineering Sciences - Computer Science Department

More information

CSE373 Fall 2013, Second Midterm Examination November 15, 2013

CSE373 Fall 2013, Second Midterm Examination November 15, 2013 CSE373 Fall 2013, Second Midterm Examination November 15, 2013 Please do not turn the page until the bell rings. Rules: The exam is closed-book, closed-note, closed calculator, closed electronics. Please

More information

Exercise 6.2 A generic container class

Exercise 6.2 A generic container class Exercise 6.2 A generic container class The goal of this exercise is to write a class Array that mimics the behavior of a C++ array, but provides more intelligent memory management a) Start with the input

More information

ESC101N: Fundamentals of Computing End-sem st semester

ESC101N: Fundamentals of Computing End-sem st semester ESC101N: Fundamentals of Computing End-sem 2010-11 1st semester Instructor: Arnab Bhattacharya 8:00-11:00am, 15th November, 2010 Instructions 1. Please write your name, roll number and section below. 2.

More information

Chapter 2: Complexity Analysis

Chapter 2: Complexity Analysis Chapter 2: Complexity Analysis Objectives Looking ahead in this chapter, we ll consider: Computational and Asymptotic Complexity Big-O Notation Properties of the Big-O Notation Ω and Θ Notations Possible

More information

STL Quick Reference for CS 241

STL Quick Reference for CS 241 STL Quick Reference for CS 241 Spring 2018 The purpose of this document is to provide basic information on those elements of the C++14 standard library we think are most likely to be needed in CS 241.

More information

4.1 COMPUTATIONAL THINKING AND PROBLEM-SOLVING

4.1 COMPUTATIONAL THINKING AND PROBLEM-SOLVING 4.1 COMPUTATIONAL THINKING AND PROBLEM-SOLVING 4.1.2 ALGORITHMS ALGORITHM An Algorithm is a procedure or formula for solving a problem. It is a step-by-step set of operations to be performed. It is almost

More information

CMSC 341 Hashing (Continued) Based on slides from previous iterations of this course

CMSC 341 Hashing (Continued) Based on slides from previous iterations of this course CMSC 341 Hashing (Continued) Based on slides from previous iterations of this course Today s Topics Review Uses and motivations of hash tables Major concerns with hash tables Properties Hash function Hash

More information

CPSC 427a: Object-Oriented Programming

CPSC 427a: Object-Oriented Programming CPSC 427a: Object-Oriented Programming Michael J. Fischer Lecture 17 November 1, 2011 CPSC 427a, Lecture 17 1/21 CPSC 427a, Lecture 17 2/21 CPSC 427a, Lecture 17 3/21 A bit of history C++ standardization.

More information

19.1 The Standard Template Library

19.1 The Standard Template Library Chapter 19: The Template Library From a review of Effective STL : 50 Specific Ways to Improve Your Use of the Standard Template Library by ScottMeyers: It s hard to overestimate the importance of the Standard

More information

CSCI-1200 Data Structures Fall 2013 Lecture 9 Iterators & Lists

CSCI-1200 Data Structures Fall 2013 Lecture 9 Iterators & Lists Review from Lecture 8 CSCI-1200 Data Structures Fall 2013 Lecture 9 Iterators & Lists Explored a program to maintain a class enrollment list and an associated waiting list. Unfortunately, erasing items

More information

Lecture 4a Using The Standard Template Library. Jack Applin, Guest Lecturer

Lecture 4a Using The Standard Template Library. Jack Applin, Guest Lecturer Lecture 4a Using The Standard Template Library Jack Applin, Guest Lecturer November 28 th, 2017 2017-12-04 CS253 Fall 2017 Bruce Draper 1 PA9 is online Announcements Efficiency assignment Same as PA8 But

More information

Data Structures and Algorithms

Data Structures and Algorithms Data Structures and Algorithms Alice E. Fischer Lecture 3 217 Alice E. Fischer Data Structures L3... 1/31 1 Arrays Growing Arrays vectors 2 3 Empty Lists Inserting Data - Unsorted Inserting Data - Sorted

More information

Fast Introduction to Object Oriented Programming and C++

Fast Introduction to Object Oriented Programming and C++ Fast Introduction to Object Oriented Programming and C++ Daniel G. Aliaga Note: a compilation of slides from Jacques de Wet, Ohio State University, Chad Willwerth, and Daniel Aliaga. Outline Programming

More information

CS93SI Handout 04 Spring 2006 Apr Review Answers

CS93SI Handout 04 Spring 2006 Apr Review Answers CS93SI Handout 04 Spring 2006 Apr 6 2006 Review Answers I promised answers to these questions, so here they are. I'll probably readdress the most important of these over and over again, so it's not terribly

More information

Data Structures Question Bank Multiple Choice

Data Structures Question Bank Multiple Choice Section 1. Fundamentals: Complexity, Algorthm Analysis 1. An algorithm solves A single problem or function Multiple problems or functions Has a single programming language implementation 2. A solution

More information

Summary. Design. Layout

Summary. Design. Layout Flat containers wording Document number: P0460R0 Date: 2016-10-15 Reply-to: Sean Middleditch sean@middleditch.us Project: ISO JTC1/SC22/WG21: Programming Language C++ Audience: Library Evolution Working

More information

Sets and MultiSets. Contents. Steven J. Zeil. July 19, Overview of Sets and Maps 4

Sets and MultiSets. Contents. Steven J. Zeil. July 19, Overview of Sets and Maps 4 Steven J. Zeil July 19, 2013 Contents 1 Overview of Sets and Maps 4 1 2 The Set ADT 6 2.1 The template header................................. 14 2.2 Internal type names.................................

More information

CPSC 427: Object-Oriented Programming

CPSC 427: Object-Oriented Programming CPSC 427: Object-Oriented Programming Michael J. Fischer Lecture 22 November 28, 2016 CPSC 427, Lecture 22 1/43 Exceptions (continued) Code Reuse Linear Containers Ordered Containers Multiple Inheritance

More information

To become familiar with array manipulation, searching, and sorting.

To become familiar with array manipulation, searching, and sorting. ELECTRICAL AND COMPUTER ENGINEERING 06-88-211: COMPUTER AIDED ANALYSIS LABORATORY EXPERIMENT #2: INTRODUCTION TO ARRAYS SID: OBJECTIVE: SECTIONS: Total Mark (out of 20): To become familiar with array manipulation,

More information

CSE 12 Week Eight, Lecture One

CSE 12 Week Eight, Lecture One CSE 12 Week Eight, Lecture One Heap: (The data structure, not the memory area) - A binary tree with properties: - 1. Parent s are greater than s. o the heap order - 2. Nodes are inserted so that tree is

More information

Intermediate Programming, Spring 2017*

Intermediate Programming, Spring 2017* 600.120 Intermediate Programming, Spring 2017* Misha Kazhdan *Much of the code in these examples is not commented because it would otherwise not fit on the slides. This is bad coding practice in general

More information

MID TERM MEGA FILE SOLVED BY VU HELPER Which one of the following statement is NOT correct.

MID TERM MEGA FILE SOLVED BY VU HELPER Which one of the following statement is NOT correct. MID TERM MEGA FILE SOLVED BY VU HELPER Which one of the following statement is NOT correct. In linked list the elements are necessarily to be contiguous In linked list the elements may locate at far positions

More information

Programming, numerics and optimization

Programming, numerics and optimization Programming, numerics and optimization Lecture A-4: Object-oriented programming Łukasz Jankowski ljank@ippt.pan.pl Institute of Fundamental Technological Research Room 4.32, Phone +22.8261281 ext. 428

More information

Dynamic Data Structures

Dynamic Data Structures Dynamic Data Structures We have seen that the STL containers vector, deque, list, set and map can grow and shrink dynamically. We now examine how some of these containers can be implemented in C++. To

More information

Splicing Maps and Sets (Revision 1)

Splicing Maps and Sets (Revision 1) Document number: N3645 Date: 2013-05-04 Project: Programming Language C++ Reference: N3485 Reply to: Alan Talbot cpp@alantalbot.com Howard Hinnant howard.hinnant@gmail.com James Dennett jdennett@google.com

More information

calling a function - function-name(argument list); y = square ( z ); include parentheses even if parameter list is empty!

calling a function - function-name(argument list); y = square ( z ); include parentheses even if parameter list is empty! Chapter 6 - Functions return type void or a valid data type ( int, double, char, etc) name parameter list void or a list of parameters separated by commas body return keyword required if function returns

More information

Course Review. Cpt S 223 Fall 2009

Course Review. Cpt S 223 Fall 2009 Course Review Cpt S 223 Fall 2009 1 Final Exam When: Tuesday (12/15) 8-10am Where: in class Closed book, closed notes Comprehensive Material for preparation: Lecture slides & class notes Homeworks & program

More information

Matrix Multiplication

Matrix Multiplication Matrix Multiplication CPS343 Parallel and High Performance Computing Spring 2013 CPS343 (Parallel and HPC) Matrix Multiplication Spring 2013 1 / 32 Outline 1 Matrix operations Importance Dense and sparse

More information

by Pearson Education, Inc. All Rights Reserved. 2

by Pearson Education, Inc. All Rights Reserved. 2 An important part of every container is the type of iterator it supports. This determines which algorithms can be applied to the container. A vector supports random-access iterators i.e., all iterator

More information