Using Associative Containers Lecture 18 Hartmut Kaiser

Slides:



Advertisements
Similar presentations
Why not just use Arrays? Java ArrayLists.
Advertisements

The Dictionary ADT Definition A dictionary is an ordered or unordered list of key-element pairs, where keys are used to locate elements in the list. Example:
C++ Templates. What is a template? Templates are type-generic versions of functions and/or classes Template functions and template classes can be used.
Quick Sort, Shell Sort, Counting Sort, Radix Sort AND Bucket Sort
C++ Sets and Multisets Set containers automatically sort their elements automatically. Multisets allow duplication of elements whereas sets do not. Usually,
1 Chapter 9 Maps and Dictionaries. 2 A basic problem We have to store some records and perform the following: add new record add new record delete record.
Sets and Maps Chapter 9. Chapter 9: Sets and Maps2 Chapter Objectives To understand the Java Map and Set interfaces and how to use them To learn about.
1 11/27/06CS150 Introduction to Computer Science 1 Searching Arrays.
CS 206 Introduction to Computer Science II 10 / 14 / 2009 Instructor: Michael Eckmann.
Programming Assignment 8 CS113 Spring Programming Assignment 8 Implement the sorting routine of your choice. Compare average performance of your.
Hash Tables1 Part E Hash Tables  
Hash Tables1 Part E Hash Tables  
Hash Tables1 Part E Hash Tables  
Hashing General idea: Get a large array
The Stack and Queue Types Lecture 10 Hartmut Kaiser
Standard Template Library C++ introduced both object-oriented ideas, as well as templates to C Templates are ways to write general code around objects.
Programming With Java ICS201 University Of Hail1 Chapter 12 UML and Patterns.
Week 4-5 Java Programming. Loops What is a loop? Loop is code that repeats itself a certain number of times There are two types of loops: For loop Used.
C++ How to Program, 8/e © by Pearson Education, Inc. All Rights Reserved. Note: C How to Program, Chapter 22 is a copy of C++ How to Program Chapter.
Data Structures Using C++ 2E
Lists in Python.
CS 202, Spring 2003 Fundamental Structures of Computer Science II Bilkent University1 Hashing CS 202 – Fundamental Structures of Computer Science II Bilkent.
CS212: DATA STRUCTURES Lecture 10:Hashing 1. Outline 2  Map Abstract Data type  Map Abstract Data type methods  What is hash  Hash tables  Bucket.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 3 Selections.
Using Associative Containers Lecture 18 Hartmut Kaiser
Chapter 13 Recursion. Learning Objectives Recursive void Functions – Tracing recursive calls – Infinite recursion, overflows Recursive Functions that.
CSE 143 Lecture 11 Maps Grammars slides created by Alyssa Harding
Arrays Module 6. Objectives Nature and purpose of an array Using arrays in Java programs Methods with array parameter Methods that return an array Array.
Lists. Container Classes Many applications in Computer Science require the storage of information for collections of entities e.g. a student registration.
Defining New Types Lecture 21 Hartmut Kaiser
Generic Programming Using the C++ Standard Template Library.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
Stacks. A stack is a data structure that holds a sequence of elements and stores and retrieves items in a last-in first- out manner (LIFO). This means.
STL multimap Container. STL multimaps multimaps are associative containers –Link a key to a value –AKA: Hashtables, Associative Arrays –A multimap allows.
Built-in Data Structures in Python An Introduction.
Working with Batches of Data Lecture 4 Hartmut Kaiser
Data TypestMyn1 Data Types The type of a variable is not set by the programmer; rather, it is decided at runtime by PHP depending on the context in which.
Can’t provide fast insertion/removal and fast lookup at the same time Vectors, Linked Lists, Stack, Queues, Deques 4 Data Structures - CSCI 102 Copyright.
Looping and Counting Lecture 3 Hartmut Kaiser
Taking Strings Apart (and putting them together) Lecture 11 Hartmut Kaiser
An Introduction to STL. The C++ Standard Template Libraries  In 1990, Alex Stepanov and Meng Lee of Hewlett Packard Laboratories extended C++ with a.
CSCI-383 Object-Oriented Programming & Design Lecture 18.
© 2004 Goodrich, Tamassia Hash Tables1  
Slides adapted from: Bjarne Stroustrup, Programming – Principles and Practice using C++ Chapter 6 Writing a Program Hartmut Kaiser
CHAPTER 8 SEARCHING CSEB324 DATA STRUCTURES & ALGORITHM.
Data Structures & Algorithms
Writing Generic Functions Lecture 20 Hartmut Kaiser
UNIT 5.  The related activities of sorting, searching and merging are central to many computer applications.  Sorting and merging provide us with a.
Standard C++ Library Part II. Last Time b String abstraction b Containers - vector.
CPSC 252 Hashing Page 1 Hashing We have already seen that we can search for a key item in an array using either linear or binary search. It would be better.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
Copyright © 2000, Department of Systems and Computer Engineering, Carleton University 1 Introduction An array is a collection of identical boxes.
Using Sequential Containers Lecture 8 Hartmut Kaiser
Slides prepared by Rose Williams, Binghamton University Chapter 16 Collections and Iterators.
Arrays Chapter 12. Overview Arrays and their properties Creating arrays Accessing array elements Modifying array elements Loops and arrays.
1 Data Structures CSCI 132, Spring 2014 Lecture 33 Hash Tables.
Chapter 8 Writing Generic Functions. Objectives Understand the use of generic functions. Learn about the use of templates, their advantages and pitfalls.
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.
CS Class 04 Topics  Selection statement – IF  Expressions  More practice writing simple C++ programs Announcements  Read pages for next.
CSCI  Sequence Containers – store sequences of values ◦ vector ◦ deque ◦ list  Associative Containers – use “keys” to access data rather than.
Chapter 7 Using Associative Containers. Objectives Introduce the concept of associative containers. Discuss the use of the map and pair structures. Introduce.
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.
CSE 332: C++ Exceptions Motivation for C++ Exceptions Void Number:: operator/= (const double denom) { if (denom == 0.0) { // what to do here? } m_value.
Sort & Search Algorithms
Using Associative Containers
CS210- Lecture 16 July 11, 2005 Agenda Maps and Dictionaries Map ADT
CS203 Lecture 15.
An Introduction to STL.
Arrays.
Presentation transcript:

Using Associative Containers Lecture 18 Hartmut Kaiser

Programming Principle of the Day Single Responsibility Principle - A component of code (e.g. class or function) should perform a single well defined task. Components should have exactly one reason to change. As an example, consider a module that compiles and prints a report.  Can be changed for two reasons:  The content of the report can change.  The format of the report can change.  These two things change for very different causes; one substantive, and one cosmetic. The single responsibility principle says that these two aspects of the problem are really two separate responsibilities, and should therefore be in separate components. It would be a bad design to couple two things that change for different reasons at different times. 03/24/2015 Lecture 17 CSC 1254, Spring 2015, Using Associative Containers 2

Abstract Associative containers arrange their elements in a certain sequence based on an ordering criteria for the elements themselves They employ ordering to quicker find elements Often they store key/value pairs, ordering the values based on the key We will investigate these containers and use maps to write compact and efficient look-up-intensive programs. 03/24/2015 Lecture 17 CSC 1254, Spring 2015, Using Associative Containers 3

Why Associative Containers? Finding an element in sequential containers entails sequential search  Potentially slow if container has many elements Alternative is to keep elements in sequential container in certain order  Devise a special search strategy, not easy  Potentially slow to insert as it might reorder elements Another alternative is to use associative containers 03/24/2015 Lecture 17 CSC 1254, Spring 2015, Using Associative Containers 4

Associative Containers Associative containers automatically arrange their elements into a sequence that depends on the values of the elements themselves, rather than the sequence in which they were inserted Allows to locate element with particular value quickly The part which is used to locate an element is the key, which sometimes has an associated value 03/24/2015 Lecture 17 CSC 1254, Spring 2015, Using Associative Containers 5

Counting Words Almost trivial with associative containers: int main() { string s; map counters; // store each word and an // associated counter // read the input, keeping track of each word and // how often we see it while (cin >> s) ++counters[s]; // write the words and associated counts for (map ::const_iterator it = counters.begin(); it != counters.end(); ++it) { cout first second << endl; } return 0; } 03/24/2015 Lecture 17 CSC 1254, Spring 2015, Using Associative Containers 6

Counting Words As std::map holds key/value pairs, we need to specify both: std::map  Holds values of type int (the word counters) with a key of type string (the counted words)  We call this a ‘map from string to int’ ‘Associative array’, we use a string as the index (the key )  Very much like vectors, except that index can be any type, not just integers 03/24/2015 Lecture 17 CSC 1254, Spring 2015, Using Associative Containers 7

Counting Words Necessary: #include while (cin >> s) ++counters[s]; Indexing operator[]: invoked with string ‘s’ Returns integer value associated with string ‘s’  We increment this integer: counting words  If no entry representing string ‘s’ exists, new entry is created and value initialized (integer is set to zero) cout first second << endl; Iterator ‘it’ refers to both, key and value  std::pair: pair of arbitrary types, stored in map  The parts are named: first, second 03/24/2015 Lecture 17 CSC 1254, Spring 2015, Using Associative Containers 8

Generating a Cross-Reference Table Write a program to generate a cross-reference table that indicates where each word occurs in the input  Read a line at a time, allowing to associate line numbers with words  Split line into words  Store more data in map: all lines a particular word occurred  map > 03/24/2015 Lecture 17 CSC 1254, Spring 2015, Using Associative Containers 9

Generating a Cross-Reference Table Find all the lines that refer to each word in the input: map > xref(istream& in, vector find_words(string const&) = split) { string line; // current line int line_number = 0; // current line number map > ret; // cross reference table // read the next line while (getline(in, line)) { // store current line number for each word //... } return ret; } 03/24/2015 Lecture 17 CSC 1254, Spring 2015, Using Associative Containers 10

Generating a Cross-Reference Table Store current line number for each word: while (getline(in, line)) { // adjust current line number ++line_number; // break the input line into words vector words = find_words(line); // remember that each word occurs on the current line for (auto const& s: words) { ret[s].push_back(line_number); } 03/24/2015 Lecture 17 CSC 1254, Spring 2015, Using Associative Containers 11

Generating a Cross-Reference Table Default argument specification: map > xref(istream& in, vector find_words(const string&) = split); Allows to leave out this argument at invocation: // uses split() to find words in the input stream... = xref(cin); // uses the function named find_urls to find words... = xref(cin, find_urls); 03/24/2015 Lecture 17 CSC 1254, Spring 2015, Using Associative Containers 12

Generating a Cross-Reference Table What is this doing here: ret[s].push_back(line_number);  s: the current word  ret[s]: returns the value associated with the key (s) yielding the vector of line numbers  If this is the first occurrence, an empty vector is put into the map  ret[s].push_back(): adds the current line number to the end of the vector 03/24/2015 Lecture 17 CSC 1254, Spring 2015, Using Associative Containers 13

Printing the Cross-Reference Print the generated map: int main() { // call xref using split by default map > xrefmap = xref(cin); // write the results for (auto it = xrefmap.begin(); it != xrefmap.end(); ++it) { // write the word followed by one or more line numbers cout first << " occurs on line(s): "; auto line_it = it->second.begin(); cout << *line_it++;// write the first line number // write the rest of the line numbers, if any for_each(line_it, it->second.end(), [](int line) { cout << ", " << line; }); // write a new line to separate each word from the next cout << endl; } return 0; } 03/24/2015 Lecture 17 CSC 1254, Spring 2015, Using Associative Containers 14

Generating Sentences CategoriesRules cat dog table large brown absurd jumps sits on the stairs under the sky wherever it wants the 03/24/2015 Lecture 17 CSC 1254, Spring 2015, Using Associative Containers 15 Example:the table jumps wherever it wants

Representing the Rules Categories, rules and ‘normal’ words  Categories: enclosed in angle brackets  Right hand side is a rule consisting out of a sequence of categories and words How to represent/store categories?  Let’s use a std::map to associate the categories with the corresponding rules  Several rules for same category using category = string; using rule = vector ; using rule_collection = vector ; using grammar = map ; 03/24/2015 Lecture 17 CSC 1254, Spring 2015, Using Associative Containers 16

Reading the Grammar // read a grammar from a given input stream grammar read_grammar(istream& in) { grammar ret; string line; // read the input while (getline(in, line)) { // split the input into words vector entry = split(line); if (!entry.empty()) { // use the category to store the associated rule ret[entry[0]].push_back( rule(entry.begin() + 1, entry.end())); } return ret; } 03/24/2015 Lecture 17 CSC 1254, Spring 2015, Using Associative Containers 17

Generating the random Sentence Start off with category ‚ ’  Assemble the output in pieces from various rules  Result is a vector holding the words of the generated sentence vector generate_sentence(grammar const& g) { vector ret; generate_aux(g, " ", ret); return ret; } 03/24/2015 Lecture 17 CSC 1254, Spring 2015, Using Associative Containers 18

Generating the random Sentence Our algorithm generate_aux() knows how to query the grammar and how to collect the words Needs to decide whether a string is a category: // return true if 's' represents a category bool bracketed(string const& s) { return s.size() > 1 && s[0] == ' '; } If it’s a category, look up rule and expand it If it’s not a category, copy word to output 03/24/2015 Lecture 17 CSC 1254, Spring 2015, Using Associative Containers 19

Generating the random Sentence void generate_aux(grammar const& g, string const& word, vector & ret) { if (!bracketed(word)) { ret.push_back(word); } else { // locate the rule that corresponds to word auto it = g.find(word); if (it == g.end()) throw logic_error("empty rule"); // fetch the set of possible rules rule_collection const& c = it->second; // from which we select one at random rule const& r = c[nrand(c.size())]; // recursively expand the selected rule for (auto i = r.begin(); i != r.end(); ++i) generate_aux(g, *i, ret); } 03/24/2015 Lecture 17 CSC 1254, Spring 2015, Using Associative Containers 20

Recursive Function Calls First looks like it can’t possibly work However, if the word is not a category (not bracketed), it obviously works Let’s assume the word is a category, but its rule has only non-bracketed words  Obviously works as this will return the words immediately Now, let’s assume the word is a category, it’s rule has bracketed words, but those refer to only non-bracketed right hand sides  Obviously works as well… 03/24/2015 Lecture 17 CSC 1254, Spring 2015, Using Associative Containers 21

Recursive Function Calls We do not know any sure way to explain recursion. Our experience is that people stare at recursive programs for a long time without understanding how they work. Then, one day, they suddenly get it—and they don't understand why they ever thought it was difficult. Evidently, the key to understanding recursion is to begin by understanding recursion. The rest is easy 03/24/2015 Lecture 17 CSC 1254, Spring 2015, Using Associative Containers 22

Pulling everything together int main() { // generate the sentence vector sentence = generate_sentence(read_grammar(cin)); // write the first word, if any auto it = sentence.begin(); if (!sentence.empty()) cout << *it++; // write the rest of the words, each preceded by a space for_each(it, sentence.end(), [](string s) { cout << " " << s; }); cout << endl; return 0; } 03/24/2015 Lecture 17 CSC 1254, Spring 2015, Using Associative Containers 23

Selecting a random Element Writing nrand(n) is tricky! Standard library has function rand() returning a (pseudo) random number in range [0, RAND_MAX) We need to map that range to new range [0, n) Simplest solution: rand() % n However, this is not good:  For small n, returned number tends to be not random  For large n, returned numbers are not randomly distributed anymore Solution: create n equally sized buckets and find what bucket a given random number falls into 03/24/2015 Lecture 17 CSC 1254, Spring 2015, Using Associative Containers 24

Selecting a Random Element // return a random integer in the range [0, n) int nrand(int n) { if (n RAND_MAX) throw domain_error("Argument to nrand is out of range"); int const bucket_size = RAND_MAX / n; int r; do { r = rand() / bucket_size; } while (r >= n); return r; } 03/24/2015 Lecture 17 CSC 1254, Spring 2015, Using Associative Containers 25

Selecting a Random Element Why does it print the same sentence whenever run?  Random number generators are not random  Generate a sequence of numbers with certain statistical properties  Each time they are used they generate the same sequence of numbers (by default) Use srand() to ‘seed’ the random number generator Use time(0) as a ‘random’ seed value 03/24/2015 Lecture 17 CSC 1254, Spring 2015, Using Associative Containers 26

Performance Considerations Unlike to associative containers in other languages, map<> is not implemented as a hash table  For each key type those need a hash function  Performance is exquisitely sensitive to the details of this hash function.  There is usually no easy way to retrieve the elements of a hash table in a useful order. C++ associative containers are hard to implement in terms of hash tables:  The key type needs only the < operator or equivalent comparison function  Associative-container elements are always kept sorted by key C++11 introduces unordered_map<>, a hash table 03/24/2015 Lecture 17 CSC 1254, Spring 2015, Using Associative Containers 27