E81 CSE 532S: Advanced MultiParadigm Software Development - PowerPoint PPT Presentation

1 / 34
About This Presentation
Title:

E81 CSE 532S: Advanced MultiParadigm Software Development

Description:

multimap. Reversible Container. Additional Expressions Complexity ... multimap (Hashed Associative Container) Additional Expressions Complexity ... – PowerPoint PPT presentation

Number of Views:35
Avg rating:3.0/5.0
Slides: 35
Provided by: csWu4
Category:

less

Transcript and Presenter's Notes

Title: E81 CSE 532S: Advanced MultiParadigm Software Development


1
E81 CSE 532S Advanced Multi-Paradigm Software
Development
STL Containers
Christopher Gill, Haijun Qiu, Zhao
Xing Department of Computer Science and
Engineering Washington University, St.
Louis cdgill_at_cse.wustl.edu
2
Assigned Reading
  • STL Containers
  • Austern, Chapters 5 and 9
  • optional Austern, Chapter 16

3
Review C Standard Template Library
  • The STL is a collection of related software
    elements
  • Iterators (define ranges)
  • Variables used to give flexible access to the
    values in a container
  • Algorithms (operate over ranges)
  • Functions that use iterators to access values in
    containers
  • Perform computations that modify values, or
    creates new ones
  • Function objects (modify algorithms operations)
  • Encapsulate a function as an object
  • Containers (the subject of todays lecture)
  • Data structures store item ranges according to
    specific organization
  • STL implementation requires advanced C features
  • Function/class templates, typedefs, traits,
    associated types
  • An example of, and a framework for, generic
    programming

4
Intro to C STL Containers
  • Goals
  • See how STL containers are implemented
  • See how differences in implementation affect use
  • Well cover several kinds
  • Focus on template concepts
  • And how each containers concept relates to its
    implementation
  • Example to the left prints
  • v0 is 1
  • v1 is 2
  • v2 is 4
  • include ltiostreamgt
  • include ltvectorgt
  • using namespace std
  • int main (int, char )
  • vectorltintgt v
  • // This would be asking for trouble....
  • // v0 1 v1 2 v2 4
  • // ... but this works fine...
  • v.push_back (1)
  • v.push_back (2)
  • v.push_back (4)
  • // ... and now this is ok ...
  • for (size_t s 0 s lt v.size() s)
  • cout ltlt "v" ltlt s ltlt " is "
  • ltlt vs ltlt endl

5
Example STL Container from a C Array
template ltclass T, size_t Ngt struct block
typedef T value_type //
type traits typedef value_type pointer
typedef const value_type const_pointer
typedef value_type reference typedef
const value_type const_reference typedef
size_t size_type typedef pointer
iterator // iterator types
typedef const_pointer const_iterator
From Matthew H. Austern, Generic Programming and
the STL, pp. 61
6
Array-based STL Container, Continued
template ltclass T, size_t Ngt struct block
iterator begin() return data //
iterator accessors iterator end() return
data N const_iterator begin() const
return data const_iterator end() const
return data N reference operator
(size_type n) // container interface
return datan const_reference operator
(size_type n) const return datan
size_type size() const return N T data
N // contained
elements
7
Array-based STL Container, Continued
template ltclass T, size_t Ngt struct block ...
// reverse iterator accessors // ri(i)
(i-1) (ri.base()-1) typedef
reverse_iteratorltiteratorgt reverse_iterator
typedef reverse_iteratorltconst_iteratorgt
const_reverse_iterator iterator rbegin()
return reverse_iterator(end())
iterator rend() return
reverse_iterator(begin()) const_iterator
rbegin() const return const_reverse_iterat
or(end()) const_iterator rend() const
return const_reverse_iterator(begin()) ...

8
Requirements for all Containers
  • Contain instances of a parameterized type
  • Contained sets of instances cannot overlap
  • An instance cannot belong to more than one
    container
  • Creation or duplication of a range copies
    instances (by value)
  • A container owns its instances (nested lifetimes)
  • Containers are designed to work with iterators
  • Declare appropriate iterator types (traits)
  • Provide iterators through accessor/factory
    methods
  • Containers provide an interface to the instances
  • Functions to add/remove instances (e.g.,
    push_back())
  • Operators on the containers to allow access to
    instances
  • E.g., random access containers like vector, deque
    provide
  • Interfaces must obey the principle of least
    surprise
  • E.g., list and slist dont have since cant
    guarantee O(1)

9
Containers Must Also Work with Iterators
  • Declare appropriate iterator types (traits)
  • typedef pointer iterator
  • typedef const_pointer const_iterator
  • Provide iterator accessor/factory methods
  • iterator begin() return data
  • iterator end() return dataN
  • const_iterator begin() const return data
  • const_iterator end() const return dataN
  • iterator rbegin() return reverse_iterator(end())
  • iterator rend() return reverse_iterator(begin()
    )
  • const_iterator rbegin() const
  • return const_reverse_iterator(end())
  • const_iterator rend() const
  • return const_reverse_iterator(begin())

10
Hierarchy of STL Container Concepts
From Matthew H. Austern, Generic Programming
and the STL
Container
Forward Container
Associative
Container
Sequence
Reversible Container
Simple Associative Container
Pair Associative container
Front Insertion Sequence
Back Insertion Sequence
Random Access Container
Unique Associative Container
Multiple Associative Container
Sorted Associative Container
Hashed Associative Container
11
Containers Can Model Multiple Concepts
Container
  • Notice containers can have multiple
    classifications
  • Useful to look at differences between data
    structures!
  • Back vs. front insertion
  • Forward vs. reversible vs. random access
  • More general concepts higher in the hierarchy
  • More specific concepts appear farther down

Forward Container
slist
Sequence
list
Reversible Container
Front Insertion Sequence
Back Insertion Sequence
Random Access Container
deque
vector
refined by
models
12
Container Top of its Concept Hierarchy
Container
  • Valid Expressions Complexity
  • Copy constructor X(a) linear
  • Copy constructor X b(a) linear
  • Assignment operator b a linear
  • Destructor a.X() linear
  • Beginning of range a.begin() constant
  • End of range a.end() constant
  • Size a.size() container-dependent
  • Maximum size a.max_size() constant
  • Empty a.empty()
    constant
  • Swap a.swap(b)
    linear

13
Forward Container
Container
deque
vector
Forward Container
slist
list
map
set
multimap
multiset
  • Additional Expressions Complexity
  • Equality a b linear
  • Inequality a ! b
    linear
  • Less than a lt b
    linear
  • Greater than a gt b
    linear
  • Less than or equal a lt b
    linear
  • Greater than or equal a gt b
    linear

14
Reversible Container
Container
list
Forward Container
vector
Reversible Container
deque
  • Additional Expressions Complexity
  • Beginning of reverse range a.rbegin()
    constant
  • End of reverse range a.rend() constant

15
Random Access Container
Container
Forward Container
vector
Reversible Container
deque
Random Access Container
  • Additional Expressions Complexity
  • Element access an constant

16
Sequence Containers
A linear range of elements NO special rule on
ordering elements. Can add or delete an element
at any point in that range.
S.insert(p,x)
p iterator pointing to an element
S.erase(p)
x a new element
17
Sequence Containers
Container
vector
deque
Forward Container
list
slist
Sequence
  • Additional Expressions Complexity
  • Fill constructor X(n,t) linear
  • Fill constructor X(n) linear
  • Default constructor X()
    linear
  • Range constructor X(i,j) linear
  • Insert a.insert(p,t)
    sequence-dependent
  • Fill insert a.insert(p,n,t) linear
  • Range insert a.insert(p,i,j) linear
  • Erase a.erase(p)
    sequence-dependent
  • Range erase a.erase(p,q) linear
  • Front a.front()
    constant

18
Front Insertion Sequence Containers
Container
slist
Forward Container
list
Sequence
deque
Front Insertion Sequence
  • Additional Expressions Complexity
  • Push front a.push_front(t)
    constant
  • Pop front a.pop_front(t)
    constant

19
Back Insertion Sequence Containers
Container
list
Forward Container
vector
Sequence
deque
Back Insertion Sequence
  • Additional Expressions Complexity
  • Back a.back()
    constant
  • Push back a.push_back(t)
    constant
  • Pop back a.pop_back(t)
    constant

20
Two Questions to Ask About
Insertion and Deletion
What are the effects of insertion/deletion on a
sequences other elements and iterators?
What is the time complexity of insert and erase?
21
Effects of Insertion/Deletion on Lists
  • time complexity per node inserted /deleted is
    constant
  • existing nodes are not moved
  • iterators to existing nodes remain valid

22
Effects of Insertion/Deletion on Vectors
  • time complexity per node inserted/deleted is
    linear in size
  • existing nodes may be moved
  • iterators to existing nodes may become invalid

23
Other Forms of Insert and Erase
Insert or erase an entire range
Examples
V.insert(V.begin(),L.begin(),L.end())
V(L.begin(), L.end())
Insert or erase at the front or back
front(), push_front(), pop_front()
Examples
back(), push_back(), pop_back()
24
Insertion vs. Overwrite Semantics
25
Associative Containers
Elements are always ordered according to some
rule, usually to provide faster lookup
An element is associated with a key of a type
defined by the container.
a. Different insert usage than Sequence (need not
specify location)
C.insert(x) //single element insertion
C.insert(first, last) //insert range of elements
b. Other member functions
C.find(key)
C.count(key)
26
Refinements of Associative Container
Are the value_type and key the same?
Simple associative container yes, same instance
Pair associative container no, different
instances (and possibly types)
Can multiple elements have the same key?
Unique associative container no
Multiple associative container yes
How are elements organized by the container?
Sorted associative container
Hashed associative container (currently
non-standard)
27
Associative Container Concepts
Container
refined by
models
Forward Container
Associative
Container
multiset
Pair Associative container
Simple Associative Container
set
multimap
map
Multiple Associative Container
hash_multiset
Unique Associative Container
hash_set
hash_multimap
Hashed Associative Container
Sorted Associative Container
hash_map
28
Associative Container
hash_set
hash_multiset
multiset
multimap
set
map
hash_map
hash_multimap
  • Additional Expressions Complexity
  • Default constructor X () constant
  • Default constructor X a constant
  • Find a.find(k) logarithmic
  • Count a.count(k)
    O(log(size())count(k))
  • Equal range a.equal_range(k))
    logarithmic
  • Erase key a.erase(k)
    O(log(size())count(k))
  • Erase element a.erase(p) constant
  • Erase range a.erase(p,q)
    O(log(size())count(k))

29
Unique Associative Container
hash_set
set
map
hash_map
  • Additional Expressions Complexity
  • Range constructor X a(i,j) linear
  • Insert element a.insert(t)
    logarithmic
  • Insert range a.insert(i,j)
    O(Nlog(size()N))

30
Multiple Associative Container
hash_multiset
multiset
multimap
hash_multimap
  • Additional Expressions Complexity
  • Range constructor X a(i,j) linear
  • Insert element a.insert(t)
    logarithmic
  • Insert range a.insert(i,j)
    O(Nlog(size()N))

31
Sorted Associative Container
multimap
map
multiset
set
  • Additional Expressions Complexity
  • Default constructors X () X a constant
  • Default constructor with comparator X a(c)
    constant
  • Range constructor X(i,j)
    O(NlogN)
  • Range constructor w/ comparator X
    a(i,j,c) O(NlogN)
  • Key comparison a.key_comp() constant
  • Value comparison a.value_comp() constant
  • Lower bound a.lower_bound(k)
    logarithmic
  • Upper bound a.upper_bound(k)
    logarithmic
  • Equal range a.equal_range(k)
    logarithmic
  • Insert with hint a.insert(p,t)
    logarithmic

32
(Hashed Associative Container)
hash_set
hash_multiset
hash_map
hash_multimap
  • Additional Expressions Complexity
  • Default constructors X () X a
    constant
  • Default constructor with bucket count X a(n)
    constant
  • Default constructor with hash function X a(n,h)
    constant
  • Default constructor with key equality X a(n,h,k)
    constant
  • Range constructor X a(i,j) linear
  • Range constructor with bucket count X a(i,j,n)
    linear
  • Range constructor w/ hash fxn X a(i,j,n,h)
    linear
  • Range constructor w/ key eq X a(i,j,n,h,k)
    linear
  • Hash function a.hash_funct()
    constant
  • Key equality a.key_eq()
    constant
  • Bucket count a.bucket_count()
    constant
  • Resize a.resize()
    linear

33
An Associated Concept Allocator
  • Notation
  • T,U Any types
  • X Allocator whose
  • value type is T
  • Y Corresponding
  • Allocator whose
  • value type is U
  • a,b Objects of type X
  • Y Object of type Y
  • T Object of type T
  • p,q Objects of type
  • Xpointer
  • N Value of type
  • Xsize_type
  • Valid Expressions
  • Default constructor X a
  • Copy constructor X b(a)
  • Generalized copy constructor X a(y)
  • Comparison ab
  • Allocate a.allocate(n)
  • Allocate with hint a.allocate(n, q)
  • Deallocate a.deallocate(p, n)
  • Maximum size a.max_size()
  • Construct a.construct(p, t)
  • Destroy a.destroy(p)
  • Address a.address(r)

34
Questions for Discussion
  • STL algorithms operate on ranges of elements.
    Where are those elements stored?
  • What are some frequently used containers?
  • What concepts do they model?
  • What are the elements of a container concept?
  • What are the key differences between fixed sized
    containers and those with variable sizes?
Write a Comment
User Comments (0)
About PowerShow.com