Sets and Maps - PowerPoint PPT Presentation

About This Presentation
Title:

Sets and Maps

Description:

Sets and Maps. Andy Wang. Data Structures, Algorithms, ... MultiMap. Stores (key, object) pairs. Multimodal: duplicate keys OK. Client Needs. Insert an object ... – PowerPoint PPT presentation

Number of Views:42
Avg rating:3.0/5.0
Slides: 46
Provided by: csF2
Learn more at: http://www.cs.fsu.edu
Category:
Tags: maps | multimap | sets

less

Transcript and Presenter's Notes

Title: Sets and Maps


1
Sets and Maps
  • Andy Wang
  • Data Structures, Algorithms, and Generic
    Programming

2
Sets
  • Abstract associative containers
  • Set
  • Stores objects
  • Unimodal duplicate objects not allows
  • MultiSet
  • Stores objects
  • Multimodal duplicate objects OK
  • Also known as bags

3
Maps
  • Abstract associative containers
  • Map
  • Stores (key, object) pairs
  • Unimodal duplicate keys not allows
  • AKA table, associative array
  • MultiMap
  • Stores (key, object) pairs
  • Multimodal duplicate keys OK

4
Client Needs
  • Insert an object
  • Remove a specified object
  • Remove all copies of an object
  • Inspect an object
  • Iterate through all objects
  • Optional
  • Performance constraints
  • Iteration order constraints

5
Client Does Not Need
  • Implementation details
  • Data structure used to implement the container

6
Example Set Clients
  • Inventory
  • struct StockItem
  • // barcode, name, amount
  • void print_inventory(stdostreamos, const
    setltStockItemgt inventory)
  • setltStockItemgtIterator
  • for (I inventory.Begin() I !
    inventory.End() I)
  • os ltlt I

7
Example Set Clients
  • Customer accounts
  • class Customer
  • // ssn, account_number, last_name, first_name
  • int main()
  • setltCustomergt customers

8
Example Set Clients
  • Exceptional instances
  • setltStringgt dictionary
  • setltStringgt wordset
  • setltStringgt unknown wordset dictionary
  • for (setltStringgtIterator I unknown.Begin() I
    ! unknown.End() I)
  • stdcout ltlt I ltlt endl // output possible
    mispelled words

9
Example Set Clients
  • Instances in common
  • setltwidgetgt S1, S2
  • setltwidgetgt S S1 S2
  • // S contains the widgets that are in both S1 and
    S2

10
Example Set Clients
  • Password server
  • struct User
  • String username
  • unsigned long signature
  • class PWServer
  • setltUsergt users

11
Example Set Clients
  • Password server
  • int PWServerCheckPW(const String uid, const
    String pw)
  • String uid_pw(uid pw)
  • unsigned long hash secure_hash_function(uid_pw)
  • User u(uid, hash)
  • setltUsergtIterator I users.Includes(u)
  • if ((I).username u.username)
    (I).signature user.signature))
  • return 1
  • return 0

12
Example Set Clients
  • Any map client
  • typedef setltpairltK, Vgt gt mapltK, Vgt

13
Example Map Clients
  • Password server
  • typedef String username
  • typedef unsigned long signature
  • class PWServer
  • map ltusername, signaturegt users
  • int PWServerCheckPW(const String uid, const
    String pw)
  • unsigned long hash secure_hash_function(uid
    pw)
  • mapltusername, signaturegt Iterator I
    users.Includes(uid)
  • if (I.Valid() (I).key uid (I).value
    signature)
  • return 1
  • return 0

14
Example Map Clients
  • Internet router
  • map ltDestIPNumber, NextHopIPNumbergt routemap
  • Dictionary
  • map ltWord, Definitiongt dictionary
  • Keyword index
  • map ltWord, Listltpage_numbersgt gt concordance

15
Set Tools (Sorted)
  • Sorted sets
  • CSetltT, Cgt adaptor
  • Classic choices for C sorted list, binary
    search tree, or red-black tree
  • Adaptation of sorted associative container
  • Search operations LowerBound(), UpperBound(),
    and Includes()
  • Sorted order traversal
  • Operators union, intersection, difference,
    subset
  • Modality determined by adaptee

16
Map Tools (Sorted)
  • Sorted maps
  • CMap and CMultiMap adaptors
  • Adaptation of sorted associative container
  • Search operations LowerBound(), UpperBound(),
    and Includes()
  • Sorted order traversal
  • Generic set algorithms apply
  • Modality determined by adaptor

17
Set and Map Tools (Sorted)
  • Client usage
  • CSetltchar, TUSListltchargt gt S1
  • Sorted list
  • Unimodal
  • CSetltchar, TMSListltchar, TGreaterThanltchargt gt gt
    S2
  • Sorted list in reverse order
  • Multimodal
  • CMapltString, int, TUSListltTAssociationltString,
    intgt gt gt S3
  • Sorted list
  • Unimodal

18
Set and Map Tools (Sorted)
  • typedef String key_type
  • typedef int data_type
  • typedef TAssociationltkey_type, data_typegt
    pair_type
  • typedef special_class predicate_type
  • typedef TMBSTltpair_type, predicate_typegt
    container_type
  • CMultiMapltkey_type, container_typegt M1
  • Binary search tree
  • Multimodal
  • Special predicate class

19
Set and Map Tools (Unsorted)
  • Unsorted sets and maps
  • CHashSet, CHashMultiSet adaptors
  • CHashMap, CHashMultiMap adaptors
  • Adaption as vector of containers
  • Search operation Includes()
  • Random order traversal
  • Modality determined by adaptor

20
The CSet Adaptor
  • template lttypename T, class Cgt
  • class CSet
  • friend class CSetIteratorltK, V, Cgt
  • public
  • typedef T value_type
  • typedef C container_type
  • typedef CSetIteratorltT, Cgt Iterator
  • // constructors
  • CSet() c()
  • CSet(const CSetltT, Cgt S) c(S.c)
  • CSet() Clear()
  • CSetltT, Cgtoperator(const CSetltT, Cgt S)
  • if (this ! S)
  • c S.c
  • return this

21
The CSet Adaptor
  • // element operations
  • Iterator Insert(const value_type t)
  • CSetltT, CgtIterator I
  • I.i c.Insert(t)
  • return I
  • int Insert(Iterator I, const value_type t)
  • return c.Insert(I.i, t)
  • size_t Remove(const value_type t) return
    c.Remove(t)
  • int Remove(Iterator I) return c.Remove(I.i)
  • void Clear() c.Clear()

22
The CSet Adaptor
  • // locator operations
  • Iterator LowerBound(const value_type t) const
  • CSetltT, CgtIterator I
  • I.i c.LowerBound(t)
  • return I
  • Iterator UpperBound(const value_type t) const
  • CSetltT, CgtIterator I
  • I.i c.UpperBound(t)
  • return I
  • Iterator Includes(const value_type t) const
  • CSetltT, CgtIterator I
  • I.i c.Includes(t)
  • return I

23
The CSet Adaptor
  • Iterator Begin() const
  • CSetltT, CgtIterator I
  • I.i c.Begin(t)
  • return I
  • Iterator End() const
  • CSetltT, CgtIterator I
  • I.i c.End(t)
  • return I
  • Iterator rBegin() const
  • CSetltT, CgtIterator I
  • I.i c.Begin(t)
  • return I
  • Iterator rEnd() const
  • CSetltT, CgtIterator I
  • I.i c.rEnd(t)
  • return I

24
The CSet Adaptor
  • // size operations
  • int Empty() const return c.Empty()
  • size_t Size() const return c.Size()
  • protected
  • C c

25
Global Operators on CSet
  • // containment operators
  • bool operatorlt(const CSetltT, Cgt S1, const
    CSetltT, Cgt S2)
  • return g_subset_of(S1.Begin(), S1.End(),
    S2.Begin(), S2.End()
  • bool operatorlt(const CSetltT, Cgt S1, const
    CSetltT, Cgt S2)
  • return (S1.Size() lt S1.Size() S1 lt S2)
  • bool operatorgt(const CSetltT, Cgt S1, const
    CSetltT, Cgt S2)
  • return S2 lt S1
  • bool operatorgt(const CSetltT, Cgt S1, const
    CSetltT, Cgt S2)
  • return S2 lt S1

26
Global Operators on CSet
  • // union
  • CSetltT, Cgt operator(const CSetltT, Cgt S1, const
    CSetltT, Cgt S2)
  • CSetltT, Cgt S
  • InsertIteratorltCSetltT, Cgt gt I(S)
  • g_set_union(S1.Begin(), S1.End(), S2.Begin(),
    S2.End(), I)
  • return S
  • // difference
  • CSetltT, Cgt operator-(const CSetltT, Cgt S1, const
    CSetltT, Cgt S2)
  • CSetltT, Cgt S
  • InsertIteratorltCSetltT, Cgt gt I(S)
  • g_set_difference(S1.Begin(), S1.End(),
    S2.Begin(), S2.End(), I)
  • return S

27
Global Operators on CSet
  • CSetltT, Cgt operator(const CSetltT, Cgt S1, const
    CSetltT, Cgt S2)
  • CSetltT, Cgt S
  • InsertIteratorltCSetltT, Cgt gt I(S)
  • g_set_intersection(S1.Begin(), S1.End(),
    S2.Begin(), S2.End(), I)
  • return S
  • bool operator(const CSetltT, Cgt S1, const
    CSetltT, Cgt S2)
  • if (S1.Size() ! S2.Size())
  • return 0
  • CSetltT, CgtIterator I1(S1), I2(S2)
  • while (I1.Valid()
  • if ((I1) ! (I2))
  • return 0
  • return 1

28
Global Operators on CSet
  • bool operator!(const CSetltT, Cgt S1, const
    CSetltT, Cgt S2)
  • return !(S1 S2)
  • templatelttypename T, class Cgt
  • stdostream operatorltlt(stdostream os, const
    CSetltT, Cgt S)
  • CSetltT, CgtIterator I
  • for (I S.Begin() I ! S.End() I)
  • os ltlt I
  • return os

29
The CSetIterator Adaptor
  • template lttypename T, class Cgt
  • class CSetIterator
  • friend class CSetltT, Cgt
  • public
  • // constructors
  • CSetIterator() i()
  • CSetIterator(const CSetltT, Cgt S) i() i
    S.c.Begin()
  • CSetIterator(const CSetIteratorltT, Cgt I)
    i(I.i)
  • // initializers
  • void Initialize(const CSetltT, Cgt S)
    i.Initialize(S.c)
  • void rInitialize(cocnst CSetltT, Cgt S)
    i.rInitialize(S.c)
  • // informationals
  • value_type Retrieve() const return
    i.Retrieve()
  • int Valid() const return i.Valid()

30
The CSetIterator Adaptor
  • // operators
  • int operator(const CSetIteratorltT, Cgt I2)
    const return i I2.i
  • int operator!(const CSetIteratorltT, Cgt I2)
    const return i ! I2.i
  • value_type operator() const return i
  • CSetIteratorltT, Cgt operator(const
    CSetIteratorltT, Cgt I)
  • i I.i
  • return this
  • CSetIteratorltT, Cgt operator()
  • i
  • return this
  • CSetIteratorltT, Cgt operator(int)
  • CSetIteratorltT, Cgt I this
  • CSetIteratorltT, Cgtoperator()
  • return I

31
The CSetIterator Adaptor
  • CSetIteratorltT, Cgt operator--()
  • --i
  • return this
  • CSetIteratorltT, Cgt operator--(int)
  • CSetIteratorltT, Cgt I this
  • CSetIteratorltT, Cgtoperator--()
  • return I
  • protected
  • typename CIterator i

32
The CMap Adaptor
  • template lttypename K, class V, class Cgt
  • class CMap
  • friend class CMapIteratorltK, V, Cgt
  • public
  • typedef K key_type
  • typedef C container_type
  • typedef typename Cvalue_type value_type
  • typedef CMapIteratorltK, V, Cgt Iterator
  • // proper type
  • CMap() c()
  • CMap(const CMapltK, V, Cgt M) c(M.c)
  • CMap() c.Clear()
  • CMap operator(const CMapltK, V, Cgt M)
  • if (this ! M)
  • c M.c
  • return this

33
The CMap Adaptor
  • // associative array operator unimodal
  • V operator(const K k)
  • TAssociationltK, Vgt p
  • p.key k
  • typename CIterator i c.LowerBound(p)
  • if (!i.Valid() p ! i) // if not in map
    or not identical key
  • c.Insert(i, p)
  • return (i).value

34
The CMap Adaptor
  • Iterator Insert(const K k, const V v)
  • TAssociationltK, Vgt p
  • Iterator I
  • I.i c.LowerBound(p)
  • if (!I.Valid() p ! I) // not found
  • if (c.Insert(I.i, p)
  • return I
  • else
  • return End()
  • (I).value v // overwrite if found
  • return I
  • int Insert(Iterator I, const K k, const V v)
  • TAssociationltK, Vgt p(k, v)
  • return c.Insert(I.i, p)

35
The CMap Adaptor
  • unsigned int Remove(const K k)
  • TAssociationltK, Vgt p
  • p.key k
  • return c.Remove(p)
  • int Remove(Iterator I) return c.Remove(I.i)
  • void Clear() return c.Clear()
  • // size operations
  • unsigned long Size() const return c.Size()
  • int Empty() const return c.Empty()

36
The CMap Adaptor
  • // locator operations
  • Iterator Includes(const K k) const
  • CMapIteratorltK, V, Cgt I
  • TAssociationltK, Vgt p
  • p.key k
  • I.i c.LowerBound(p)
  • if (I.Valid() (k (I).key)
  • return I
  • return End()
  • Iterator LowerBound(const K k) const
  • Iterator UpperBound(const K k) const

37
The CMap Adaptor
  • Iterator Begin() const
  • CMapIteratorltK, V, Cgt I
  • I.i c.Begin()
  • return I
  • Iterator End() const
  • Iterator rBegin() const
  • Iterator rEnd() const
  • protected
  • C c

38
The CMapIterator
  • templatelttypename K, class V, class Cgt
  • class CMapIterator
  • friend class CMapltK, V, Cgt
  • public
  • // bidirectional iterator interface
  • protected
  • typename CIterator i

39
The CMultiMap Adaptor
  • templatelttypename K, class V, class Cgt
  • class CMultiMap
  • friend class CMapIteratorltK, V, Cgt
  • public
  • typedef K key_type
  • typedef C container_type
  • typedef typename Cvalue_type value_type
  • typedef CMapIteratorltK, V, Cgt Iterator
  • // proper type
  • CMultiMap() c()
  • CMultiMap(const CMultiMapltK, V, Cgt MM)
    c(MM.c)
  • CMultiMap() c.Clear()
  • CMultiMap operator(const CMultiMap MM)

40
The CMultiMap Adaptor
  • // operators
  • Iterator Insert(const K k, const V v)
  • TassociationltK, Vgt p(k, v)
  • Iterator I
  • I.i c.Insert(p)
  • return I
  • int Insert(Iterator I, const K k, const V v)
  • TAssociationltK, Vgt p(k, v)
  • return c.Insert(I.i, p)
  • unsigned int Remove(const K k)
  • TAssociationltK, Vgt p
  • p.key k
  • return c.Remove(p)
  • int Remove(Iterator I) return c.Remove(I.i)
  • void Clear() return c.Clear()

41
The CMultiMap Adaptor
  • // size operations
  • unsigned long Size() const return c.Size()
  • int Empty() const return c.Empty()
  • // locator operations
  • Iterator Includes(const K k) const
  • Iterator I
  • TAssociationltK, Vgt p
  • p.key k
  • I.i c.Includes(p)
  • return I
  • Iterator LowerBound(const K k) const
  • Iterator UpperBound(const K k) const

42
The CMultiMap Adaptor
  • Iterator Begin() const
  • CMultiMapIteratorltK, V, Cgt I
  • I.i c.Begin()
  • return I
  • Iterator End() const
  • Iterator rBegin() const
  • Iterator rEnd()
  • protected
  • C c

43
The CHashSet Adaptor
  • template lttypename T, class H, class Cgt
  • class CHashSet
  • friend class CHashSetIteratorltT, H, Cgt
  • public
  • typedef T value_type
  • typedef H hash_type
  • typedef C bucket_type
  • typedef CHashSetIteratorltT, H, Cgt Iterator
  • // constructor
  • CHashSet(size_t number_of_buckets)
  • CHashSet()
  • // element operations
  • Iterator Insert(const value_type t)
  • int Insert(Iterator I, const value_type t)
  • size_t Remove(const value_type t)
  • int Remove(Iterator I)

44
The CHashSet Adaptor
  • // locator operations
  • Iterator Includes(const value_type t) const
  • Iterator Begin() const
  • Iterator End() const
  • Iterator rBegin() const
  • Iterator rEnd() const
  • // size operations
  • int Empty() const
  • size_t Size() const
  • protected
  • size_t numBuckets
  • TVectorltCgt bucketVector
  • H hashObject
  • private
  • CHashSet(const CHashltT, H, Cgt)
  • CHashSet operator(const CHashSet)

45
The CHashSetIterator Adaptor
  • template lttypename T, class H, class Cgt
  • class CHashSetIterator
  • friend class CHashSetltT, H, Cgt
  • public
  • // bidirectional iterator public interface
  • protected
  • const CHashSetltT, H, Cgt setPtr
  • typename CIterator bucketItr
  • size_t bucketNum
Write a Comment
User Comments (0)
About PowerShow.com