Map, Set & Bit-Vector

50
Map, Set & Bit-Vector Discrete Mathematics and Its Applications Baojian Hua [email protected]

description

Map, Set & Bit-Vector. Discrete Mathematics and Its Applications Baojian Hua [email protected]. 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); - PowerPoint PPT Presentation

Transcript of Map, Set & Bit-Vector

Page 1: Map, Set & Bit-Vector

Map, Set & Bit-Vector

Discrete Mathematics andIts Applications

Baojian [email protected]

Page 2: Map, Set & Bit-Vector

Map

Page 3: Map, Set & Bit-Vector

Map Interfacesignature

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

Page 4: Map, Set & Bit-Vector

Interface in C#ifndef MAP_H#define MAP_H typedef struct map *map; // type maptypedef 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

Page 5: Map, Set & Bit-Vector

Implementation in C#include “map.h” struct map{ // your favorite concrete representation};

map newMap (){ // real code goes here}

Page 6: Map, Set & Bit-Vector

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

Page 7: Map, Set & Bit-Vector

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

listm

Page 8: Map, Set & Bit-Vector

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

listm

Page 9: Map, Set & Bit-Vector

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;}

listm

Page 10: Map, Set & Bit-Vector

Set

Page 11: Map, Set & Bit-Vector

Set Interfacesignature

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

Page 12: Map, Set & Bit-Vector

Interface in C#ifndef SET_H#define SET_H typedef struct set *set; // type settypedef void *poly; // type t

set newSet ();int setSize (set s);void setInsert (set s, poly x);set setUnion (set s1, set s2);…

#endif

Page 13: Map, Set & Bit-Vector

Implementation in C#include “set.h” struct set{ // your favorite concrete representation};

set newSet (){ // real code goes here}

Page 14: Map, Set & Bit-Vector

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

Page 15: Map, Set & Bit-Vector

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

lists

Page 16: Map, Set & Bit-Vector

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

lists

Page 17: Map, Set & Bit-Vector

Sample Impl’ Using Linked List// functions void setInsert (set s, poly x){ if (setExists (s, x)) return;

linkedListInsert (s->list, x); return;}

Page 18: Map, Set & Bit-Vector

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

Page 19: Map, Set & Bit-Vector

Equality TestingHow 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.

Page 20: Map, Set & Bit-Vector

Client Codeint main (){ set s1 = newSet (); set s2 = newSet (); for (…) setInsert (s1, …);

for (…) setInsert (s2, …); set s3 = setUnion (s1, s2);}

Page 21: Map, Set & Bit-Vector

Summary So Far

set

set

set

set

set

Page 22: Map, Set & Bit-Vector

Set in Java

Page 23: Map, Set & Bit-Vector

Interface in Javapublic 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

Page 24: Map, Set & Bit-Vector

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

Page 25: Map, Set & Bit-Vector

Implementation in Javapublic class Set<X> implements SetInter<X>{ // any concrete internal representation

Set () // constructor { // code goes here }

int size () { // code goes here } …}

Page 26: Map, Set & Bit-Vector

Sample Impl’ Using Linked Listimport ….linkedList;

public class Set<X> implements SetInter<X>{ private linkedList<X> list;

Set () { this.list = new LinkedList<X> (); }

}

Page 27: Map, Set & Bit-Vector

Sample Impl’ Using Linked Listimport ….linkedList;

public class Set<X> implements SetInter<X>{ private linkedList<X> list;

int size () { return this.list.size (); }

}

Page 28: Map, Set & Bit-Vector

Sample Impl’ Using Linked Listimport ….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; }}

Page 29: Map, Set & Bit-Vector

Client Codeimport ….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); }}

Page 30: Map, Set & Bit-Vector

Bit-Vector

Page 31: Map, Set & Bit-Vector

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

Page 32: Map, Set & Bit-Vector

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

Page 33: Map, Set & Bit-Vector

Implementation in C#include “bitArray.h”

// a not-so efficient onestruct bitArray{ int *array; int size;};

Page 34: Map, Set & Bit-Vector

OperationsbitArray 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;}

Page 35: Map, Set & Bit-Vector

OperationsbitArray and (bitArray ba1, bitArray ba2){ if (ba1->size != ba2->size) error (…);

bitArray ba = newBitArray (); for (…) assignOne (ba, …);

return ba;}

Page 36: Map, Set & Bit-Vector

Bit-Array in Java

Page 37: Map, Set & Bit-Vector

In Java// I omit the interface for simplicitypublic class BitArray{ private int[] array;

BitArray (int size) { this.array = new int[size]; }}

Page 38: Map, Set & Bit-Vector

Other Methodspublic 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;

}}

Page 39: Map, Set & Bit-Vector

Bit-Vector-based Set Representation

Page 40: Map, Set & Bit-Vector

Big Picture

Universe

set

setset

set

Page 41: Map, Set & Bit-Vector

Client Codeint main (){ // cook a universe set set universe = newSet ();

// cook sets s1 and s2 set s1 = newSet (); set s2 = newSet (); setUnion (universe, s1, s2);}

Page 42: Map, Set & Bit-Vector

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.

Page 43: Map, Set & Bit-Vector

Big Picture

1. Build the bit-array from the universe

{(“a”, 0), (“b”, 3), (“c”, 1”), (“d”, 2)}

{“a”} {“d”}

Page 44: Map, Set & Bit-Vector

Big Picture

1. Build the bit-array from the universebaSet1 = [0, 0, 0, 0]baSet2 = [0, 0, 0, 0]

{(“a”, 0), (“b”, 3), (“c”, 1”), (“d”, 2)}

{“a”} {“d”}

Page 45: Map, Set & Bit-Vector

Big Picture

1. Build the bit-array from the universebaSet1 = [0, 0, 0, 0]baSet2 = [0, 0, 0, 0]2. Build bit-array from setbaSet1 = [1, 0, 0, 0]baSet2 = [0, 0, 1, 0]

{(“a”, 0), (“b”, 3), (“c”, 1”), (“d”, 2)}

{“a”} {“d”}

Page 46: Map, Set & Bit-Vector

Big Picture

1. Build the bit-array from theuniversebaSet1 = [0, 0, 0, 0]baSet2 = [0, 0, 0, 0]2. Build bit-array from setbaSet1 = [1, 0, 0, 0]baSet2 = [0, 0, 1, 0]

{(“a”, 0), (“b”, 3), (“c”, 1”), (“d”, 2)}

{“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.

Page 47: Map, Set & Bit-Vector

How to Store the Universe?// Method 1: stored in separate memoryint 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}

Page 48: Map, Set & Bit-Vector

How to Store the Universe?// Method 2: shared

Universe

set

setset

set

Page 49: Map, Set & Bit-Vector

How to Make Things Shared?

In C: extern variables In C++ or Java: static fields What’s the pros and cons?

Page 50: Map, Set & Bit-Vector

Client Codeint 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!}