1 / 50

Map, Set & Bit-Vector

Map, Set & Bit-Vector. Discrete Mathematics and Its Applications Baojian Hua bjhua@ustc.edu.cn. Map. Map Interface. signature type map type key type value map newMap (); void mapInsert (map m, key k, value v); value mapLookup (map m, key k);

rhea
Download Presentation

Map, Set & Bit-Vector

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Map, Set & Bit-Vector Discrete Mathematics and Its Applications Baojian Hua bjhua@ustc.edu.cn

  2. Map

  3. Map Interface signature type map type key type value map newMap (); void mapInsert (map m, key k, value v); value mapLookup (map m, key k); void mapDelete (map m, key k); … end

  4. Interface in C #ifndef MAP_H #define MAP_H typedef struct map *map; // type map typedef void *poly; // type key, value map newMap (); void mapInsert (map m, poly k, poly v); poly mapLookup (map m, poly k); void mapDelete (map m, poly k); … #endif

  5. Implementation in C #include “map.h” struct map { // your favorite concrete representation }; map newMap () { // real code goes here } …

  6. Sample Impl’ Using Linked List #include “linkedList.h” #include “map.h” struct map { linkedList list; };

  7. m list Sample Impl’ Using Linked List // functions map newMap () { map m = malloc (sizeof (*m)); m->list = newLinkedList (); return m; }

  8. Sample Impl’ Using Linked List // functions void mapInsert (map m, poly k, poly v) { linkList list = m->list; linkedListInsert (list, newTuple (k, v)); return; } m list

  9. Sample Impl’ Using Linked List // functions poly mapLookup (map m, poly k) { linkList list = s->list; while(…) { // scan the list and lookup key k // return v, if found } return NULL; } m list

  10. Set

  11. Set Interface signature type set // set type type t // element type set newSet (); int setSize (set s); void setInsert (set s, t x); set setUnion (set s1, set s2); … end

  12. Interface in C #ifndef SET_H #define SET_H typedef struct set *set; // type set typedef void *poly; // type t set newSet (); int setSize (set s); void setInsert (set s, poly x); set setUnion (set s1, set s2); … #endif

  13. Implementation in C #include “set.h” struct set { // your favorite concrete representation }; set newSet () { // real code goes here } …

  14. Sample Impl’ Using Linked List #include “linkedList.h” #include “set.h” struct set { linkedList list; };

  15. s list Sample Impl’ Using Linked List // functions set newSet () { set s = malloc (sizeof (*s)); s->list = newLinkedList (); return s; }

  16. Sample Impl’ Using Linked List // functions int setSize (set s) { linkList l = s->list; return linkedListSize (l); } s list

  17. Sample Impl’ Using Linked List // functions void setInsert (set s, poly x) { if (setExists (s, x)) return; linkedListInsert (s->list, x); return; }

  18. Sample Impl’ Using Linked List // functions int setExist (set s, poly x) { return linkedListExists (s->list, x); }

  19. Equality Testing How to do equality testing on “polymorphic” data? 1. “equals” function pointer as argument. int linkedListExist (linkedList list, poly x, tyEq equals); 2. “equals” function pointers in data. int linkedListExist (linkedList list, poly x) { for (…p…) (p->data)->equals (p->data, x); } // As we can see next in C++ or Java.

  20. Client Code int main () { set s1 = newSet (); set s2 = newSet (); for (…) setInsert (s1, …); for (…) setInsert (s2, …); set s3 = setUnion (s1, s2); }

  21. Summary So Far set set set set set

  22. Set in Java

  23. Interface in Java public interface SetInter // the type “set” { int size (); // “Object” is very polymorphic… void insert (Object x); SetInter union (SetInter s); … } // Follow this, all the stuffs are essentially // same with those in C

  24. Or Using Generic // Type “set”, with type argument “X” public interface SetInter<X> { int size (); void insert (X x); SetInter<X> union (SetInter<X> s); … } // We’ll discuss this strategy in following // slides

  25. Implementation in Java public class Set<X> implements SetInter<X> { // any concrete internal representation Set () // constructor { // code goes here } int size () { // code goes here } … }

  26. Sample Impl’ Using Linked List import ….linkedList; public class Set<X> implements SetInter<X> { private linkedList<X> list; Set () { this.list = new LinkedList<X> (); } }

  27. Sample Impl’ Using Linked List import ….linkedList; public class Set<X> implements SetInter<X> { private linkedList<X> list; int size () { return this.list.size (); } }

  28. Sample Impl’ Using Linked List import ….linkedList; public class Set<X> implements SetInter<X> { private linkedList<X> list; void insert (X x) { if (exists (x)) // equality testing? return; this.list.insert (x); return; } }

  29. Client Code import ….Set; public class Main { public static void main (String[] args) { SetInter<String> s1 = new Set<String> (); SetInter<String> s2 = new Set<String> (); s1.size (); s1.union (s2); } }

  30. Bit-Vector

  31. Bit-Vector Interface interface type bitArray bitArray newBitArray (int size); void assignOne (bitArray ba, int index); bitArray and (bitArray ba1, bitArray ba2); … end

  32. Interface in C #ifndef BITARRAY_H #define BITARRAY_H typedef struct bitArray *bitArray; bitArray newBitArray (int size); void assignOne (bitArray ba, int index); bitArray and (bitArray ba1, bitArray ba2); … #endif

  33. Implementation in C #include “bitArray.h” // a not-so efficient one struct bitArray { int *array; int size; };

  34. Operations bitArray newBitArray (int i) { bitArray ba = malloc (sizeof (*ba)); ba->array = malloc (sizeof (*(ba->array)) * i); for (int k=0; k<i; k++) (ba->array)[i] = 0; ba->size = i; return ba; }

  35. Operations bitArray and (bitArray ba1, bitArray ba2) { if (ba1->size != ba2->size) error (…); bitArray ba = newBitArray (); for (…) assignOne (ba, …); return ba; }

  36. Bit-Array in Java

  37. In Java // I omit the interface for simplicity public class BitArray { private int[] array; BitArray (int size) { this.array = new int[size]; } }

  38. Other Methods public class BitArray { private int[] array; BitArray and (BitArray ba2) { if (this.size () != ba2.size ()) throw new Error (…); BitArray ba = new BitArray (this.size()); … return ba; } }

  39. Bit-Vector-based Set Representation

  40. Big Picture Universe set set set set

  41. Client Code int main () { // cook a universe set set universe = newSet (); // cook sets s1 and s2 set s1 = newSet (); set s2 = newSet (); setUnion (universe, s1, s2); }

  42. What does the Universe Look Like? Universe is a set of (element, index) tuple. For instance: Universe = {(“a”, 0), (“b”, 3), (“c”, 1”), (“d”, 2)} Question: How to build such kind of universe from the input set element? Answer: associate every set element e a unique (and continuous) integer i (i will be used as an index in the bit-vector. Details leave to you.

  43. Big Picture {(“a”, 0), (“b”, 3), (“c”, 1”), (“d”, 2)} 1. Build the bit-array from the universe {“a”} {“d”}

  44. Big Picture {(“a”, 0), (“b”, 3), (“c”, 1”), (“d”, 2)} 1. Build the bit-array from the universe baSet1 = [0, 0, 0, 0] baSet2 = [0, 0, 0, 0] {“a”} {“d”}

  45. Big Picture {(“a”, 0), (“b”, 3), (“c”, 1”), (“d”, 2)} 1. Build the bit-array from the universe baSet1 = [0, 0, 0, 0] baSet2 = [0, 0, 0, 0] 2. Build bit-array from set baSet1 = [1, 0, 0, 0] baSet2 = [0, 0, 1, 0] {“a”} {“d”}

  46. Big Picture {(“a”, 0), (“b”, 3), (“c”, 1”), (“d”, 2)} 1. Build the bit-array from the universe baSet1 = [0, 0, 0, 0] baSet2 = [0, 0, 0, 0] 2. Build bit-array from set baSet1 = [1, 0, 0, 0] baSet2 = [0, 0, 1, 0] {“a”} {“d”} 3. Bit-vector operations baSet3 = or (baSet1, baSet2) baSet3 = [1, 0, 1, 0] 4. Turn baSet3 to ordinary set How? Leave it to you.

  47. How to Store the Universe? // Method 1: stored in separate memory int main () { // cook a universe set set universe = newSet (); // cook two sets s1 and s2 set s1 = newSet (); set s2 = newSet (); setUnion (universe, s1, s2); // ugly }

  48. How to Store the Universe? // Method 2: shared Universe set set set set

  49. How to Make Things Shared? • In C: extern variables • In C++ or Java: static fields • What’s the pros and cons?

  50. Client Code int main () { // cook a universe set set universe = newUniverse (); // cook two sets s1 and s2 set s1 = newSet (); set s2 = newSet (); setUnion (s1, s2); // hummm, no universe AT ALL! }

More Related