45
Sets and Maps Sets and Maps Andy Wang Andy Wang Data Structures, Data Structures, Algorithms, and Generic Algorithms, and Generic Programming Programming

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

Embed Size (px)

Citation preview

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

Sets and MapsSets and Maps

Andy WangAndy Wang

Data Structures, Algorithms, and Data Structures, Algorithms, and Generic ProgrammingGeneric Programming

Page 2: Sets and Maps Andy Wang Data Structures, Algorithms, and Generic Programming

SetsSets

Abstract associative containersAbstract associative containersSetSet

Stores objectsStores objectsUnimodal: duplicate objects not allowsUnimodal: duplicate objects not allows

MultiSetMultiSetStores objectsStores objectsMultimodal: duplicate objects OKMultimodal: duplicate objects OKAlso known as bagsAlso known as bags

Page 3: Sets and Maps Andy Wang Data Structures, Algorithms, and Generic Programming

MapsMaps

Abstract associative containersAbstract associative containersMapMap

Stores (key, object) pairsStores (key, object) pairsUnimodal: duplicate keys not allowsUnimodal: duplicate keys not allowsAKA: table, associative arrayAKA: table, associative array

MultiMapMultiMapStores (key, object) pairsStores (key, object) pairsMultimodal: duplicate keys OKMultimodal: duplicate keys OK

Page 4: Sets and Maps Andy Wang Data Structures, Algorithms, and Generic Programming

Client NeedsClient Needs

Insert an objectInsert an objectRemove a specified objectRemove a specified objectRemove all copies of an objectRemove all copies of an object Inspect an objectInspect an object Iterate through all objectsIterate through all objectsOptionalOptional

Performance constraintsPerformance constraints Iteration order constraintsIteration order constraints

Page 5: Sets and Maps Andy Wang Data Structures, Algorithms, and Generic Programming

Client Does Not NeedClient Does Not Need

Implementation detailsImplementation detailsData structure used to implement the Data structure used to implement the

containercontainer

Page 6: Sets and Maps Andy Wang Data Structures, Algorithms, and Generic Programming

Example Set ClientsExample Set Clients

InventoryInventory

struct StockItem {struct StockItem {

// barcode, name, amount// barcode, name, amount

};};

void print_inventory(std::ostream&os, const set<StockItem>& inventory) {void print_inventory(std::ostream&os, const set<StockItem>& inventory) {

set<StockItem>::Iterator;set<StockItem>::Iterator;

for (I = inventory.Begin(); I != inventory.End(); ++I) {for (I = inventory.Begin(); I != inventory.End(); ++I) {

os << *I;os << *I;

}}

}}

Page 7: Sets and Maps Andy Wang Data Structures, Algorithms, and Generic Programming

Example Set ClientsExample Set Clients

Customer accountsCustomer accounts

class Customer {class Customer {

// ssn, account_number, last_name, first_name…// ssn, account_number, last_name, first_name…

};};

int main() {int main() {

set<Customer> customers;set<Customer> customers;

}}

Page 8: Sets and Maps Andy Wang Data Structures, Algorithms, and Generic Programming

Example Set ClientsExample Set Clients

Exceptional instancesExceptional instances

set<String> dictionary;set<String> dictionary;

set<String> wordset;set<String> wordset;

set<String> unknown = wordset – dictionary;set<String> unknown = wordset – dictionary;

for (set<String>::Iterator I = unknown.Begin(); I != unknown.End(); ++I) {for (set<String>::Iterator I = unknown.Begin(); I != unknown.End(); ++I) {

std::cout << *I << endl; // output possible mispelled wordsstd::cout << *I << endl; // output possible mispelled words

}}

Page 9: Sets and Maps Andy Wang Data Structures, Algorithms, and Generic Programming

Example Set ClientsExample Set Clients

Instances in commonInstances in common

set<widget> S1, S2;set<widget> S1, S2;

set<widget> S = S1 * S2;set<widget> S = S1 * S2;

// S contains the widgets that are in both S1 and S2// S contains the widgets that are in both S1 and S2

Page 10: Sets and Maps Andy Wang Data Structures, Algorithms, and Generic Programming

Example Set ClientsExample Set Clients

Password serverPassword server

struct User {struct User {

String username;String username;

unsigned long signature;unsigned long signature;

};};

class PWServer {class PWServer {

set<User> users;set<User> users;

……

};};

Page 11: Sets and Maps Andy Wang Data Structures, Algorithms, and Generic Programming

Example Set ClientsExample Set Clients

Password serverPassword server

int PWServer::CheckPW(const String uid, const String pw) {int PWServer::CheckPW(const String uid, const String pw) {

String uid_pw(uid + pw);String uid_pw(uid + pw);

unsigned long hash = secure_hash_function(uid_pw);unsigned long hash = secure_hash_function(uid_pw);

User u(uid, hash);User u(uid, hash);

set<User>::Iterator I = users.Includes(u);set<User>::Iterator I = users.Includes(u);

if ((*I).username == u.username) && (*I).signature == user.signature)) {if ((*I).username == u.username) && (*I).signature == user.signature)) {

return 1;return 1;

}}

return 0;return 0;

}}

Page 12: Sets and Maps Andy Wang Data Structures, Algorithms, and Generic Programming

Example Set ClientsExample Set Clients

Any map clientAny map client typedef set<pair<K, V> > map<K, V>;typedef set<pair<K, V> > map<K, V>;

Page 13: Sets and Maps Andy Wang Data Structures, Algorithms, and Generic Programming

Example Map ClientsExample Map Clients

Password serverPassword servertypedef String username;typedef String username;typedef unsigned long signature;typedef unsigned long signature;

class PWServer {class PWServer {map <username, signature> users;map <username, signature> users;……

};};

int PWServer::CheckPW(const String & uid, const String &pw) {int PWServer::CheckPW(const String & uid, const String &pw) {unsigned long hash = secure_hash_function(uid + pw);unsigned long hash = secure_hash_function(uid + pw);map<username, signature>:: Iterator I = users.Includes(uid);map<username, signature>:: Iterator I = users.Includes(uid);if (I.Valid() && (*I).key == uid && (*I).value = signature) {if (I.Valid() && (*I).key == uid && (*I).value = signature) {

return 1;return 1;}}return 0;return 0;

}}

Page 14: Sets and Maps Andy Wang Data Structures, Algorithms, and Generic Programming

Example Map ClientsExample Map Clients

Internet routerInternet routermap <DestIPNumber, NextHopIPNumber> routemap;map <DestIPNumber, NextHopIPNumber> routemap;

DictionaryDictionarymap <Word, Definition> dictionary;map <Word, Definition> dictionary;

Keyword indexKeyword indexmap <Word, List<page_numbers> > concordance;map <Word, List<page_numbers> > concordance;

Page 15: Sets and Maps Andy Wang Data Structures, Algorithms, and Generic Programming

Set Tools (Sorted)Set Tools (Sorted)

Sorted setsSorted sets CSet<T, C> adaptorCSet<T, C> adaptor

Classic choices for C: sorted list, binary search tree, or red-Classic choices for C: sorted list, binary search tree, or red-black treeblack tree

Adaptation of sorted associative containerAdaptation of sorted associative container Search operations: LowerBound(), UpperBound(), Search operations: LowerBound(), UpperBound(),

and Includes() and Includes() Sorted order traversalSorted order traversal Operators: union, intersection, difference, subset Operators: union, intersection, difference, subset Modality determined by adapteeModality determined by adaptee

Page 16: Sets and Maps Andy Wang Data Structures, Algorithms, and Generic Programming

Map Tools (Sorted)Map Tools (Sorted)

Sorted mapsSorted mapsCMap and CMultiMap adaptorsCMap and CMultiMap adaptorsAdaptation of sorted associative containerAdaptation of sorted associative containerSearch operations: LowerBound(), Search operations: LowerBound(),

UpperBound(), and Includes()UpperBound(), and Includes()Sorted order traversalSorted order traversalGeneric set algorithms applyGeneric set algorithms applyModality determined by adaptorModality determined by adaptor

Page 17: Sets and Maps Andy Wang Data Structures, Algorithms, and Generic Programming

Set and Map Tools (Sorted)Set and Map Tools (Sorted)

Client usageClient usage CSet<char, TUSList<char> > S1;CSet<char, TUSList<char> > S1;

Sorted listSorted listUnimodalUnimodal

CSet<char, TMSList<char, TGreaterThan<char> > > S2;CSet<char, TMSList<char, TGreaterThan<char> > > S2;

Sorted list in reverse orderSorted list in reverse orderMultimodalMultimodal

CMap<String, int, TUSList<TAssociation<String, int> > > S3CMap<String, int, TUSList<TAssociation<String, int> > > S3

Sorted listSorted listUnimodalUnimodal

Page 18: Sets and Maps Andy Wang Data Structures, Algorithms, and Generic Programming

Set and Map Tools (Sorted)Set and Map Tools (Sorted)

typedef String key_type;typedef String key_type;

typedef int data_type;typedef int data_type;

typedef TAssociation<key_type, data_type> pair_type;typedef TAssociation<key_type, data_type> pair_type;

typedef special_class predicate_type;typedef special_class predicate_type;

typedef TMBST<pair_type, predicate_type> container_type;typedef TMBST<pair_type, predicate_type> container_type;

CMultiMap<key_type, container_type> M1;CMultiMap<key_type, container_type> M1;

Binary search treeBinary search treeMultimodalMultimodalSpecial predicate classSpecial predicate class

Page 19: Sets and Maps Andy Wang Data Structures, Algorithms, and Generic Programming

Set and Map Tools (Unsorted)Set and Map Tools (Unsorted)

Unsorted sets and mapsUnsorted sets and mapsCHashSet, CHashMultiSet adaptorsCHashSet, CHashMultiSet adaptorsCHashMap, CHashMultiMap adaptorsCHashMap, CHashMultiMap adaptorsAdaption as vector of containersAdaption as vector of containersSearch operation: Includes()Search operation: Includes()Random order traversalRandom order traversalModality determined by adaptorModality determined by adaptor

Page 20: Sets and Maps Andy Wang Data Structures, Algorithms, and Generic Programming

The CSet AdaptorThe CSet Adaptortemplate <typename T, class C>template <typename T, class C>class CSet {class CSet {

friend class CSetIterator<K, V, C>;friend class CSetIterator<K, V, C>;

public:public:typedef T value_type;typedef T value_type;

typedef C container_type;typedef C container_type;typedef CSetIterator<T, C> Iterator;typedef CSetIterator<T, C> Iterator;

// constructors// constructorsCSet() : c() { }CSet() : c() { }CSet(const CSet<T, C>& S) : c(S.c) { }CSet(const CSet<T, C>& S) : c(S.c) { }~CSet() { Clear(); }~CSet() { Clear(); }CSet<T, C>&operator=(const CSet<T, C>& S) {CSet<T, C>&operator=(const CSet<T, C>& S) {

if (this != &S) {if (this != &S) {c = S.c;c = S.c;

}}return *this;return *this;

}}

Page 21: Sets and Maps Andy Wang Data Structures, Algorithms, and Generic Programming

The CSet AdaptorThe CSet Adaptor// element operations// element operations

Iterator Insert(const value_type& t) {Iterator Insert(const value_type& t) {

CSet<T, C>::Iterator I;CSet<T, C>::Iterator I;

I.i = c.Insert(t);I.i = c.Insert(t);

return I;return I;

} }

int Insert(Iterator& I, const value_type& t) { int Insert(Iterator& I, const value_type& t) {

return c.Insert(I.i, t); return c.Insert(I.i, t);

}}

size_t Remove(const value_type& t) { return c.Remove(t); }size_t Remove(const value_type& t) { return c.Remove(t); }

int Remove(Iterator& I) { return c.Remove(I.i); }int Remove(Iterator& I) { return c.Remove(I.i); }

void Clear() { c.Clear(); }void Clear() { c.Clear(); }

Page 22: Sets and Maps Andy Wang Data Structures, Algorithms, and Generic Programming

The CSet AdaptorThe CSet Adaptor// locator operations// locator operations

Iterator LowerBound(const value_type& t) const {Iterator LowerBound(const value_type& t) const {

CSet<T, C>::Iterator I;CSet<T, C>::Iterator I;

I.i = c.LowerBound(t);I.i = c.LowerBound(t);

return I;return I;

}}

Iterator UpperBound(const value_type& t) const {Iterator UpperBound(const value_type& t) const {

CSet<T, C>::Iterator I;CSet<T, C>::Iterator I;

I.i = c.UpperBound(t);I.i = c.UpperBound(t);

return I;return I;

}}

Iterator Includes(const value_type& t) const {Iterator Includes(const value_type& t) const {

CSet<T, C>::Iterator I;CSet<T, C>::Iterator I;

I.i = c.Includes(t);I.i = c.Includes(t);

return I;return I;

}}

Page 23: Sets and Maps Andy Wang Data Structures, Algorithms, and Generic Programming

The CSet AdaptorThe CSet AdaptorIterator Begin() const {Iterator Begin() const {

CSet<T, C>::Iterator I;CSet<T, C>::Iterator I;

I.i = c.Begin(t);I.i = c.Begin(t);

return I;return I;

}}

Iterator End() const {Iterator End() const {

CSet<T, C>::Iterator I;CSet<T, C>::Iterator I;

I.i = c.End(t);I.i = c.End(t);

return I;return I;

}}

Iterator rBegin() const {Iterator rBegin() const {

CSet<T, C>::Iterator I;CSet<T, C>::Iterator I;

I.i = c.Begin(t);I.i = c.Begin(t);

return I;return I;

}}

Iterator rEnd() const {Iterator rEnd() const {

CSet<T, C>::Iterator I;CSet<T, C>::Iterator I;

I.i = c.rEnd(t);I.i = c.rEnd(t);

return I;return I;

}}

Page 24: Sets and Maps Andy Wang Data Structures, Algorithms, and Generic Programming

The CSet AdaptorThe CSet Adaptor// size operations// size operations

int Empty() const { return c.Empty(); }int Empty() const { return c.Empty(); }

size_t Size() const { return c.Size(); }size_t Size() const { return c.Size(); }

protected:protected:

C c;C c;

};};

Page 25: Sets and Maps Andy Wang Data Structures, Algorithms, and Generic Programming

Global Operators on CSetGlobal Operators on CSet// containment operators// containment operators

bool operator<=(const CSet<T, C>& S1, const CSet<T, C>& S2) {bool operator<=(const CSet<T, C>& S1, const CSet<T, C>& S2) {

return g_subset_of(S1.Begin(), S1.End(), S2.Begin(), S2.End();return g_subset_of(S1.Begin(), S1.End(), S2.Begin(), S2.End();

}}

bool operator<(const CSet<T, C>& S1, const CSet<T, C>& S2) {bool operator<(const CSet<T, C>& S1, const CSet<T, C>& S2) {

return (S1.Size() < S1.Size() && S1 <= S2);return (S1.Size() < S1.Size() && S1 <= S2);

}}

bool operator>=(const CSet<T, C>& S1, const CSet<T, C>& S2) {bool operator>=(const CSet<T, C>& S1, const CSet<T, C>& S2) {

return S2 <= S1;return S2 <= S1;

}}

bool operator>(const CSet<T, C>& S1, const CSet<T, C>& S2) {bool operator>(const CSet<T, C>& S1, const CSet<T, C>& S2) {

return S2 < S1;return S2 < S1;

}}

Page 26: Sets and Maps Andy Wang Data Structures, Algorithms, and Generic Programming

Global Operators on CSetGlobal Operators on CSet// union// union

CSet<T, C> operator+(const CSet<T, C>& S1, const CSet<T, C>& S2) {CSet<T, C> operator+(const CSet<T, C>& S1, const CSet<T, C>& S2) {

CSet<T, C> S;CSet<T, C> S;

InsertIterator<CSet<T, C> > I(S);InsertIterator<CSet<T, C> > I(S);

g_set_union(S1.Begin(), S1.End(), S2.Begin(), S2.End(), I);g_set_union(S1.Begin(), S1.End(), S2.Begin(), S2.End(), I);

return S;return S;

}}

// difference// difference

CSet<T, C> operator-(const CSet<T, C>& S1, const CSet<T, C>& S2) {CSet<T, C> operator-(const CSet<T, C>& S1, const CSet<T, C>& S2) {

CSet<T, C> S;CSet<T, C> S;

InsertIterator<CSet<T, C> > I(S);InsertIterator<CSet<T, C> > I(S);

g_set_difference(S1.Begin(), S1.End(), S2.Begin(), S2.End(), I);g_set_difference(S1.Begin(), S1.End(), S2.Begin(), S2.End(), I);

return S;return S;

}}

Page 27: Sets and Maps Andy Wang Data Structures, Algorithms, and Generic Programming

Global Operators on CSetGlobal Operators on CSetCSet<T, C> operator*(const CSet<T, C>& S1, const CSet<T, C>& S2) {CSet<T, C> operator*(const CSet<T, C>& S1, const CSet<T, C>& S2) {

CSet<T, C> S;CSet<T, C> S;

InsertIterator<CSet<T, C> > I(S);InsertIterator<CSet<T, C> > I(S);

g_set_intersection(S1.Begin(), S1.End(), S2.Begin(), S2.End(), I);g_set_intersection(S1.Begin(), S1.End(), S2.Begin(), S2.End(), I);

return S;return S;

}}

bool operator==(const CSet<T, C>& S1, const CSet<T, C>& S2) {bool operator==(const CSet<T, C>& S1, const CSet<T, C>& S2) {

if (S1.Size() != S2.Size()) {if (S1.Size() != S2.Size()) {

return 0;return 0;

}}

CSet<T, C>::Iterator I1(S1), I2(S2);CSet<T, C>::Iterator I1(S1), I2(S2);

while (I1.Valid() {while (I1.Valid() {

if (*(I1++) != *(I2++)) {if (*(I1++) != *(I2++)) {

return 0;return 0;

}}

}}

return 1;return 1;

}}

Page 28: Sets and Maps Andy Wang Data Structures, Algorithms, and Generic Programming

Global Operators on CSetGlobal Operators on CSetbool operator!=(const CSet<T, C>& S1, const CSet<T, C>& S2) {bool operator!=(const CSet<T, C>& S1, const CSet<T, C>& S2) {

return !(S1 == S2);return !(S1 == S2);

}}

template<typename T, class C>template<typename T, class C>

std::ostream operator<<(std::ostream& os, const CSet<T, C>& S) {std::ostream operator<<(std::ostream& os, const CSet<T, C>& S) {

CSet<T, C>::Iterator I;CSet<T, C>::Iterator I;

for (I = S.Begin(); I != S.End(); ++I) {for (I = S.Begin(); I != S.End(); ++I) {

os << *I;os << *I;

}}

return os;return os;

}}

Page 29: Sets and Maps Andy Wang Data Structures, Algorithms, and Generic Programming

The CSetIterator AdaptorThe CSetIterator Adaptortemplate <typename T, class C>template <typename T, class C>

class CSetIterator {class CSetIterator {

friend class CSet<T, C>;friend class CSet<T, C>;

public:public:

// constructors// constructors

CSetIterator() : i() { }CSetIterator() : i() { }

CSetIterator(const CSet<T, C>& S) : i() { i = S.c.Begin(); }CSetIterator(const CSet<T, C>& S) : i() { i = S.c.Begin(); }

CSetIterator(const CSetIterator<T, C>& I) : i(I.i) { }CSetIterator(const CSetIterator<T, C>& I) : i(I.i) { }

// initializers// initializers

void Initialize(const CSet<T, C>& S) { i.Initialize(S.c); }void Initialize(const CSet<T, C>& S) { i.Initialize(S.c); }

void rInitialize(cocnst CSet<T, C>& S) { i.rInitialize(S.c); }void rInitialize(cocnst CSet<T, C>& S) { i.rInitialize(S.c); }

// informationals// informationals

value_type& Retrieve() const { return i.Retrieve(); }value_type& Retrieve() const { return i.Retrieve(); }

int Valid() const { return i.Valid(); }int Valid() const { return i.Valid(); }

Page 30: Sets and Maps Andy Wang Data Structures, Algorithms, and Generic Programming

The CSetIterator AdaptorThe CSetIterator Adaptor// operators// operators

int operator==(const CSetIterator<T, C>& I2) const { return i == I2.i; }int operator==(const CSetIterator<T, C>& I2) const { return i == I2.i; }

int operator!=(const CSetIterator<T, C>& I2) const { return i != I2.i; }int operator!=(const CSetIterator<T, C>& I2) const { return i != I2.i; }

value_type& operator*() const { return *i; }value_type& operator*() const { return *i; }

CSetIterator<T, C>& operator=(const CSetIterator<T, C> &I) {CSetIterator<T, C>& operator=(const CSetIterator<T, C> &I) {

i = I.i;i = I.i;

return *this;return *this;

}}

CSetIterator<T, C>& operator++() {CSetIterator<T, C>& operator++() {

++i;++i;

return *this;return *this;

}}

CSetIterator<T, C>& operator++(int) {CSetIterator<T, C>& operator++(int) {

CSetIterator<T, C> I = *this;CSetIterator<T, C> I = *this;

CSetIterator<T, C>::operator++();CSetIterator<T, C>::operator++();

return I;return I;

}}

Page 31: Sets and Maps Andy Wang Data Structures, Algorithms, and Generic Programming

The CSetIterator AdaptorThe CSetIterator AdaptorCSetIterator<T, C>& operator--() {CSetIterator<T, C>& operator--() {

--i;--i;

return *this;return *this;

}}

CSetIterator<T, C>& operator--(int) {CSetIterator<T, C>& operator--(int) {

CSetIterator<T, C> I = *this;CSetIterator<T, C> I = *this;

CSetIterator<T, C>::operator--();CSetIterator<T, C>::operator--();

return I;return I;

}}

protected:protected:

typename C::Iterator i;typename C::Iterator i;

};};

Page 32: Sets and Maps Andy Wang Data Structures, Algorithms, and Generic Programming

The CMap AdaptorThe CMap Adaptortemplate <typename K, class V, class C>template <typename K, class V, class C>

class CMap {class CMap {

friend class CMapIterator<K, V, C>;friend class CMapIterator<K, V, C>;

public:public:

typedef K key_type;typedef K key_type;

typedef C container_type;typedef C container_type;

typedef typename C::value_type value_type;typedef typename C::value_type value_type;

typedef CMapIterator<K, V, C> Iterator;typedef CMapIterator<K, V, C> Iterator;

// proper type// proper type

CMap() : c() { }CMap() : c() { }

CMap(const CMap<K, V, C>& M) : c(M.c) { }CMap(const CMap<K, V, C>& M) : c(M.c) { }

~CMap() { c.Clear(); }~CMap() { c.Clear(); }

CMap& operator=(const CMap<K, V, C>& M) {CMap& operator=(const CMap<K, V, C>& M) {

if (this != M) {if (this != M) {

c = M.c;c = M.c;

}}

return *this;return *this;

}}

Page 33: Sets and Maps Andy Wang Data Structures, Algorithms, and Generic Programming

The CMap AdaptorThe CMap Adaptor

// associative array operator: unimodal// associative array operator: unimodal

V& operator[](const K& k) {V& operator[](const K& k) {

TAssociation<K, V> p;TAssociation<K, V> p;

p.key = k;p.key = k;

typename C::Iterator i = c.LowerBound(p);typename C::Iterator i = c.LowerBound(p);

if (!i.Valid() || p != *i) { // if not in map or not identical if (!i.Valid() || p != *i) { // if not in map or not identical keykey

c.Insert(i, p);c.Insert(i, p);

}}

return (*i).value;return (*i).value;

}}

Page 34: Sets and Maps Andy Wang Data Structures, Algorithms, and Generic Programming

The CMap AdaptorThe CMap AdaptorIterator Insert(const K& k, const V& v) {Iterator Insert(const K& k, const V& v) {

TAssociation<K, V> p;TAssociation<K, V> p;

Iterator I;Iterator I;

I.i = c.LowerBound(p);I.i = c.LowerBound(p);

if (!I.Valid() || p != *I) { // not foundif (!I.Valid() || p != *I) { // not found

if (c.Insert(I.i, p) {if (c.Insert(I.i, p) {

return I;return I;

} else {} else {

return End();return End();

}}

} }

(*I).value = v; // overwrite if found(*I).value = v; // overwrite if found

return I;return I;

} }

int Insert(Iterator& I, const K& k, const V& v) {int Insert(Iterator& I, const K& k, const V& v) {

TAssociation<K, V> p(k, v);TAssociation<K, V> p(k, v);

return c.Insert(I.i, p);return c.Insert(I.i, p);

}}

Page 35: Sets and Maps Andy Wang Data Structures, Algorithms, and Generic Programming

The CMap AdaptorThe CMap Adaptorunsigned int Remove(const K& k) {unsigned int Remove(const K& k) {

TAssociation<K, V> p;TAssociation<K, V> p;

p.key = k;p.key = k;

return c.Remove(p);return c.Remove(p);

}}

int Remove(Iterator& I) { return c.Remove(I.i); }int Remove(Iterator& I) { return c.Remove(I.i); }

void Clear() { return c.Clear(); }void Clear() { return c.Clear(); }

// size operations// size operations

unsigned long Size() const { return c.Size(); }unsigned long Size() const { return c.Size(); }

int Empty() const { return c.Empty(); }int Empty() const { return c.Empty(); }

Page 36: Sets and Maps Andy Wang Data Structures, Algorithms, and Generic Programming

The CMap AdaptorThe CMap Adaptor// locator operations// locator operations

Iterator Includes(const K& k) const {Iterator Includes(const K& k) const {

CMapIterator<K, V, C> I;CMapIterator<K, V, C> I;

TAssociation<K, V> p;TAssociation<K, V> p;

p.key = k;p.key = k;

I.i = c.LowerBound(p);I.i = c.LowerBound(p);

if (I.Valid() && (k == (*I).key) {if (I.Valid() && (k == (*I).key) {

return I;return I;

}}

return End();return End();

}}

Iterator LowerBound(const K& k) const { … }Iterator LowerBound(const K& k) const { … }

Iterator UpperBound(const K& k) const { … }Iterator UpperBound(const K& k) const { … }

Page 37: Sets and Maps Andy Wang Data Structures, Algorithms, and Generic Programming

The CMap AdaptorThe CMap AdaptorIterator Begin() const {Iterator Begin() const {

CMapIterator<K, V, C> I;CMapIterator<K, V, C> I;

I.i = c.Begin();I.i = c.Begin();

return I;return I;

}}

Iterator End() const {…}Iterator End() const {…}

Iterator rBegin() const {…}Iterator rBegin() const {…}

Iterator rEnd() const {…}Iterator rEnd() const {…}

protected:protected:

C c;C c;

};};

Page 38: Sets and Maps Andy Wang Data Structures, Algorithms, and Generic Programming

The CMapIteratorThe CMapIteratortemplate<typename K, class V, class C> template<typename K, class V, class C>

class CMapIterator {class CMapIterator {

friend class CMap<K, V, C>friend class CMap<K, V, C>

public:public:

// bidirectional iterator interface// bidirectional iterator interface

protected:protected:

typename C::Iterator i;typename C::Iterator i;

}}

Page 39: Sets and Maps Andy Wang Data Structures, Algorithms, and Generic Programming

The CMultiMap AdaptorThe CMultiMap Adaptortemplate<typename K, class V, class C> template<typename K, class V, class C>

class CMultiMap {class CMultiMap {

friend class CMapIterator<K, V, C>friend class CMapIterator<K, V, C>

public:public:

typedef K key_type;typedef K key_type;

typedef C container_type;typedef C container_type;

typedef typename C::value_type value_type;typedef typename C::value_type value_type;

typedef CMapIterator<K, V, C> Iterator;typedef CMapIterator<K, V, C> Iterator;

// proper type// proper type

CMultiMap() : c() { }CMultiMap() : c() { }

CMultiMap(const CMultiMap<K, V, C>& MM) : c(MM.c) { }CMultiMap(const CMultiMap<K, V, C>& MM) : c(MM.c) { }

~CMultiMap() { c.Clear(); }~CMultiMap() { c.Clear(); }

CMultiMap& operator=(const CMultiMap& MM) { … }CMultiMap& operator=(const CMultiMap& MM) { … }

Page 40: Sets and Maps Andy Wang Data Structures, Algorithms, and Generic Programming

The CMultiMap AdaptorThe CMultiMap Adaptor// operators// operators

Iterator Insert(const K& k, const V& v) {Iterator Insert(const K& k, const V& v) {

Tassociation<K, V> p(k, v);Tassociation<K, V> p(k, v);

Iterator I;Iterator I;

I.i = c.Insert(p);I.i = c.Insert(p);

return I;return I;

}}

int Insert(Iterator& I, const K& k, const V& v) {int Insert(Iterator& I, const K& k, const V& v) {

TAssociation<K, V> p(k, v);TAssociation<K, V> p(k, v);

return c.Insert(I.i, p);return c.Insert(I.i, p);

}}

unsigned int Remove(const K& k) {unsigned int Remove(const K& k) {

TAssociation<K, V> p;TAssociation<K, V> p;

p.key = k;p.key = k;

return c.Remove(p);return c.Remove(p);

}}

int Remove(Iterator& I) { return c.Remove(I.i); }int Remove(Iterator& I) { return c.Remove(I.i); }

void Clear() { return c.Clear(); }void Clear() { return c.Clear(); }

Page 41: Sets and Maps Andy Wang Data Structures, Algorithms, and Generic Programming

The CMultiMap AdaptorThe CMultiMap Adaptor// size operations// size operations

unsigned long Size() const { return c.Size(); }unsigned long Size() const { return c.Size(); }

int Empty() const { return c.Empty(); }int Empty() const { return c.Empty(); }

// locator operations// locator operations

Iterator Includes(const K& k) const {Iterator Includes(const K& k) const {

Iterator I;Iterator I;

TAssociation<K, V> p;TAssociation<K, V> p;

p.key = k;p.key = k;

I.i = c.Includes(p);I.i = c.Includes(p);

return I;return I;

}}

Iterator LowerBound(const K& k) const { … }Iterator LowerBound(const K& k) const { … }

Iterator UpperBound(const K& k) const { … }Iterator UpperBound(const K& k) const { … }

Page 42: Sets and Maps Andy Wang Data Structures, Algorithms, and Generic Programming

The CMultiMap AdaptorThe CMultiMap AdaptorIterator Begin() const {Iterator Begin() const {

CMultiMapIterator<K, V, C> I;CMultiMapIterator<K, V, C> I;

I.i = c.Begin();I.i = c.Begin();

return I;return I;

}}

Iterator End() const {…}Iterator End() const {…}

Iterator rBegin() const {…}Iterator rBegin() const {…}

Iterator rEnd() {…}Iterator rEnd() {…}

protected:protected:

C c;C c;

}}

Page 43: Sets and Maps Andy Wang Data Structures, Algorithms, and Generic Programming

The CHashSet AdaptorThe CHashSet Adaptortemplate <typename T, class H, class C>template <typename T, class H, class C>

class CHashSet {class CHashSet {

friend class CHashSetIterator<T, H, C>;friend class CHashSetIterator<T, H, C>;

public:public:

typedef T value_type;typedef T value_type;

typedef H hash_type;typedef H hash_type;

typedef C bucket_type;typedef C bucket_type;

typedef CHashSetIterator<T, H, C> Iterator;typedef CHashSetIterator<T, H, C> Iterator;

// constructor// constructor

CHashSet(size_t number_of_buckets);CHashSet(size_t number_of_buckets);

~CHashSet();~CHashSet();

// element operations// element operations

Iterator Insert(const value_type& t);Iterator Insert(const value_type& t);

int Insert(Iterator& I, const value_type& t);int Insert(Iterator& I, const value_type& t);

size_t Remove(const value_type& t);size_t Remove(const value_type& t);

int Remove(Iterator& I);int Remove(Iterator& I);

void Clear();void Clear();

Page 44: Sets and Maps Andy Wang Data Structures, Algorithms, and Generic Programming

The CHashSet AdaptorThe CHashSet Adaptor// locator operations// locator operations

Iterator Includes(const value_type& t) const;Iterator Includes(const value_type& t) const;

Iterator Begin() const;Iterator Begin() const;

Iterator End() const;Iterator End() const;

Iterator rBegin() const;Iterator rBegin() const;

Iterator rEnd() const;Iterator rEnd() const;

// size operations// size operations

int Empty() const;int Empty() const;

size_t Size() const;size_t Size() const;

protected:protected:

size_t numBuckets;size_t numBuckets;

TVector<C> bucketVector;TVector<C> bucketVector;

H hashObject;H hashObject;

private:private:

CHashSet(const CHash<T, H, C>&);CHashSet(const CHash<T, H, C>&);

CHashSet& operator=(const CHashSet&);CHashSet& operator=(const CHashSet&);

};};

Page 45: Sets and Maps Andy Wang Data Structures, Algorithms, and Generic Programming

The CHashSetIterator AdaptorThe CHashSetIterator Adaptortemplate <typename T, class H, class C>template <typename T, class H, class C>

class CHashSetIterator {class CHashSetIterator {

friend class CHashSet<T, H, C>;friend class CHashSet<T, H, C>;

public:public:

// bidirectional iterator public interface// bidirectional iterator public interface

protected:protected:

const CHashSet<T, H, C> *setPtr;const CHashSet<T, H, C> *setPtr;

typename C::Iterator bucketItr;typename C::Iterator bucketItr;

size_t bucketNum;size_t bucketNum;

};};