Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Sets and Maps Andy Wang Data Structures, Algorithms, and Generic Programming."— Presentation transcript:

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

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

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

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

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

6 Example Set Clients Inventory Inventory struct StockItem { // barcode, name, amount }; void print_inventory(std::ostream&os, const set & inventory) { set<StockItem>::Iterator; for (I = inventory.Begin(); I != inventory.End(); ++I) { os << *I; }}

7 Example Set Clients Customer accounts Customer accounts class Customer { // ssn, account_number, last_name, first_name… }; int main() { set customers; }

8 Example Set Clients Exceptional instances Exceptional instances set dictionary; set wordset; set unknown = wordset – dictionary; for (set ::Iterator I = unknown.Begin(); I != unknown.End(); ++I) { std::cout << *I << endl; // output possible mispelled words }

9 Example Set Clients Instances in common Instances in common set S1, S2; set S = S1 * S2; // S contains the widgets that are in both S1 and S2

10 Example Set Clients Password server Password server struct User { String username; unsigned long signature; }; class PWServer { set users; …};

11 Example Set Clients Password server Password server int PWServer::CheckPW(const String uid, const String pw) { String uid_pw(uid + pw); unsigned long hash = secure_hash_function(uid_pw); User u(uid, hash); set ::Iterator I = users.Includes(u); if ((*I).username == u.username) && (*I).signature == user.signature)) { return 1; } return 0; }

12 Example Set Clients Any map client Any map client typedef set > map ; typedef set > map ;

13 Example Map Clients Password server Password server typedef String username; typedef unsigned long signature; class PWServer { map users; …}; int PWServer::CheckPW(const String & uid, const String &pw) { unsigned long hash = secure_hash_function(uid + pw); map :: Iterator I = users.Includes(uid); if (I.Valid() && (*I).key == uid && (*I).value = signature) { return 1; } return 0; }

14 Example Map Clients Internet router Internet router map routemap; Dictionary Dictionary map dictionary; Keyword index Keyword index map > concordance;

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

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

17 Set and Map Tools (Sorted) Client usage Client usage CSet > S1; CSet > S1; Sorted list Sorted list Unimodal Unimodal CSet > > S2; CSet > > S2; Sorted list in reverse order Sorted list in reverse order Multimodal Multimodal CMap > > S3 CMap > > S3 Sorted list Sorted list Unimodal Unimodal

18 Set and Map Tools (Sorted) typedef String key_type; typedef int data_type; typedef TAssociation pair_type; typedef special_class predicate_type; typedef TMBST container_type; CMultiMap M1; Binary search tree Binary search tree Multimodal Multimodal Special predicate class Special predicate class

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

20 The CSet Adaptor template template class CSet { friend class CSetIterator ; public: typedef T value_type; typedef C container_type; typedef C container_type; typedef CSetIterator Iterator; // constructors CSet() : c() { } CSet(const CSet & S) : c(S.c) { } ~CSet() { Clear(); } CSet &operator=(const CSet & S) { if (this != &S) { c = S.c; } return *this; }

21 The CSet Adaptor // element operations Iterator Insert(const value_type& t) { CSet ::Iterator 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 { CSet ::Iterator I; I.i = c.LowerBound(t); return I; } Iterator UpperBound(const value_type& t) const { CSet ::Iterator I; I.i = c.UpperBound(t); return I; } Iterator Includes(const value_type& t) const { CSet ::Iterator I; I.i = c.Includes(t); return I; }

23 The CSet Adaptor Iterator Begin() const { CSet ::Iterator I; I.i = c.Begin(t); return I; } Iterator End() const { CSet ::Iterator I; I.i = c.End(t); return I; } Iterator rBegin() const { CSet ::Iterator I; I.i = c.Begin(t); return I; } Iterator rEnd() const { CSet ::Iterator 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 operator & S1, const CSet & S2) { return g_subset_of(S1.Begin(), S1.End(), S2.Begin(), S2.End(); } bool operator & S1, const CSet & S2) { return (S1.Size() < S1.Size() && S1 <= S2); } bool operator>=(const CSet & S1, const CSet & S2) { return S2 <= S1; } bool operator>(const CSet & S1, const CSet & S2) { return S2 < S1; }

26 Global Operators on CSet // union CSet operator+(const CSet & S1, const CSet & S2) { CSet S; InsertIterator > I(S); g_set_union(S1.Begin(), S1.End(), S2.Begin(), S2.End(), I); return S; } // difference CSet operator-(const CSet & S1, const CSet & S2) { CSet S; InsertIterator > I(S); g_set_difference(S1.Begin(), S1.End(), S2.Begin(), S2.End(), I); return S; }

27 Global Operators on CSet CSet operator*(const CSet & S1, const CSet & S2) { CSet S; InsertIterator > I(S); g_set_intersection(S1.Begin(), S1.End(), S2.Begin(), S2.End(), I); return S; } bool operator==(const CSet & S1, const CSet & S2) { if (S1.Size() != S2.Size()) { return 0; } CSet ::Iterator I1(S1), I2(S2); while (I1.Valid() { if (*(I1++) != *(I2++)) { return 0; }} return 1; }

28 Global Operators on CSet bool operator!=(const CSet & S1, const CSet & S2) { return !(S1 == S2); } template template std::ostream operator & S) { CSet ::Iterator I; for (I = S.Begin(); I != S.End(); ++I) { os << *I; } return os; }

29 The CSetIterator Adaptor template template class CSetIterator { friend class CSet ; public: // constructors CSetIterator() : i() { } CSetIterator(const CSet & S) : i() { i = S.c.Begin(); } CSetIterator(const CSetIterator & I) : i(I.i) { } // initializers void Initialize(const CSet & S) { i.Initialize(S.c); } void rInitialize(cocnst CSet & 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 CSetIterator & I2) const { return i == I2.i; } int operator!=(const CSetIterator & I2) const { return i != I2.i; } value_type& operator*() const { return *i; } CSetIterator & operator=(const CSetIterator &I) { i = I.i; return *this; } CSetIterator & operator++() { ++i; return *this; } CSetIterator & operator++(int) { CSetIterator I = *this; CSetIterator ::operator++(); return I; }

31 The CSetIterator Adaptor CSetIterator & operator--() { --i; return *this; } CSetIterator & operator--(int) { CSetIterator I = *this; CSetIterator ::operator--(); return I; }protected: typename C::Iterator i; };

32 The CMap Adaptor template template class CMap { friend class CMapIterator ; public: typedef K key_type; typedef C container_type; typedef typename C::value_type value_type; typedef CMapIterator Iterator; // proper type CMap() : c() { } CMap(const CMap & M) : c(M.c) { } ~CMap() { c.Clear(); } CMap& operator=(const CMap & M) { if (this != M) { c = M.c; } return *this; }

33 The CMap Adaptor // associative array operator: unimodal V& operator[](const K& k) { TAssociation p; p.key = k; typename C::Iterator 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) { TAssociation 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) { TAssociation p(k, v); return c.Insert(I.i, p); }

35 The CMap Adaptor unsigned int Remove(const K& k) { TAssociation 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 { CMapIterator I; TAssociation 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 { CMapIterator I; I.i = c.Begin(); return I; } Iterator End() const {…} Iterator rBegin() const {…} Iterator rEnd() const {…} protected: C c; };

38 The CMapIterator template template class CMapIterator { friend class CMap friend class CMap public: // bidirectional iterator interface protected: typename C::Iterator i; }

39 The CMultiMap Adaptor template template class CMultiMap { friend class CMapIterator friend class CMapIterator public: typedef K key_type; typedef C container_type; typedef typename C::value_type value_type; typedef CMapIterator Iterator; // proper type CMultiMap() : c() { } CMultiMap(const CMultiMap & 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) { Tassociation p(k, v); Iterator I; I.i = c.Insert(p); return I; } int Insert(Iterator& I, const K& k, const V& v) { TAssociation p(k, v); return c.Insert(I.i, p); } unsigned int Remove(const K& k) { TAssociation 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; TAssociation 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 { CMultiMapIterator I; I.i = c.Begin(); return I; } Iterator End() const {…} Iterator rBegin() const {…} Iterator rEnd() {…} protected: C c; }

43 The CHashSet Adaptor template template class CHashSet { friend class CHashSetIterator ; public: typedef T value_type; typedef H hash_type; typedef C bucket_type; typedef CHashSetIterator 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); void Clear();

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; TVector bucketVector; H hashObject; private: CHashSet(const CHash &); CHashSet& operator=(const CHashSet&); };

45 The CHashSetIterator Adaptor template template class CHashSetIterator { friend class CHashSet ; public: // bidirectional iterator public interface protected: const CHashSet *setPtr; typename C::Iterator bucketItr; size_t bucketNum; };


Download ppt "Sets and Maps Andy Wang Data Structures, Algorithms, and Generic Programming."

Similar presentations


Ads by Google