Hash Tables LAST TODAY NEXT Client vs library interface Hashing - - PowerPoint PPT Presentation

hash tables last today next client vs library interface
SMART_READER_LITE
LIVE PREVIEW

Hash Tables LAST TODAY NEXT Client vs library interface Hashing - - PowerPoint PPT Presentation

Hash Tables LAST TODAY NEXT Client vs library interface Hashing Making Implementing hash table dictionaries Genericity code generic Hashing fruit You are the client entries key hash value operations apple


slide-1
SLIDE 1

Hash Tables

slide-2
SLIDE 2

LAST Hashing Genericity TODAY

  • Client vs library interface
  • Implementing hash table dictionaries

NEXT Making code generic

slide-3
SLIDE 3

Hashing fruit — You are the client

entries key hash value

  • perations

0xA0: “apple”, 20 0xB0: “banana”, 10 0xC0: “pumpkin”, 50 0xD0: “banana”, 20 0xE0: “lemon”, 5 “apple” “banana” “pumpkin” “lemon” “grape” “berry” “lime”

  • 1290151091
  • 514151789

207055587

  • 1189657311
  • 581390202

2086736531

  • 665562942
  • 1. insert 0xA0
  • 2. insert 0xB0
  • 3. insert 0xC0
  • 4. lookup “apple”
  • 5. lookup “lime”

🍏 🍔 🍍 🎄 🍈 🍌

  • 6. insert 0xD0
  • 7. insert 0xE0
slide-4
SLIDE 4

// typedef ______* hdict_t; hdict_t hdict_new(int capacity) /*@requires capacity > 0; @*/ /*@ensures \result != NULL; @*/ ; entry hdict_lookup(hdict_t H, key k) /*@requires H != NULL; @*/ // typedef ______ key; // typedef ______* entry; void hdict_insert(hdict_t H, entry x) key entry_key(entry x) /*@requires x != NULL; @*/ ; int key_hash(key k); bool key_equiv(key k1, key k2); /*@requires H != NULL && x != NULL; @*/

Library Interface Client Interface

slide-5
SLIDE 5

Library interface

// typedef ______* hdict_t; hdict_t hdict_new(int capacity) /*@requires capacity > 0; @*/ /*@ensures \result != NULL; @*/ ; entry hdict_lookup(hdict_t H, key k) /*@requires H != NULL; @*/ void hdict_insert(hdict_t H, entry x) /*@requires H != NULL && x != NULL; @*/

client-side types What about postconditions?

// typedef ______* entry; // typedef ______ key; key entry_key(entry x) /*@requires x != NULL; @*/ ; int key_hash(key k); bool key_equiv(key k1, key k2);

slide-6
SLIDE 6

Library interface

// typedef ______* hdict_t; hdict_t hdict_new(int capacity) /*@requires capacity > 0; @*/ /*@ensures \result != NULL; @*/ ; entry hdict_lookup(hdict_t H, key k) /*@requires H != NULL; @*/ /*@ensures \result == NULL || key_equiv(entry_key(\result), k); @*/ ; void hdict_insert(hdict_t H, entry x) /*@requires H != NULL && x != NULL; @*/

// typedef ______* entry; // typedef ______ key; key entry_key(entry x) /*@requires x != NULL; @*/ ; int key_hash(key k); bool key_equiv(key k1, key k2);

slide-7
SLIDE 7

Library interface

// typedef ______* hdict_t; hdict_t hdict_new(int capacity) /*@requires capacity > 0; @*/ /*@ensures \result != NULL; @*/ ; entry hdict_lookup(hdict_t H, key k) /*@requires H != NULL; @*/ /*@ensures \result == NULL || key_equiv(entry_key(\result), k); @*/ ; void hdict_insert(hdict_t H, entry x) /*@requires H != NULL && x != NULL; @*/ /*@ensures hdict_lookup(H, entry_key(x)) == x; @*/ ;

// typedef ______* entry; // typedef ______ key; key entry_key(entry x) /*@requires x != NULL; @*/ ; int key_hash(key k); bool key_equiv(key k1, key k2);

slide-8
SLIDE 8

Code organization

Client implementation (HOW) Client interface (WHAT)

Library implementation (HOW)

Library interface (WHAT) Client application

slide-9
SLIDE 9

Library code

Client interface (WHAT)

Library implementation (HOW)

Library interface (WHAT)

/************************/ /*** Client interface ***/ /************************/ /************************/ /** Library interface ***/ /************************/ /**************************/ /* Library implementation */ /**************************/

slide-10
SLIDE 10

cc0 -d lib/*.c0 produce.c0 hdict.c0 produce-test.c0

Need to compile in this order:

coin hdict.c0 hdict.c0:18.1-18.4:error:identifier 'key' at top level

client implementation

slide-11
SLIDE 11

Client Implementation:

using the dictionary (produce-test.c0) fulfilling the client interface (produce.c0)

slide-12
SLIDE 12

Library Implementation: structure definitions, data structure invariants

slide-13
SLIDE 13

typedef struct chain_node chain; struct chain_node { ____ data; // != NULL; contains both key and value ____ next; };

1 2 3 4

entry chain*

slide-14
SLIDE 14

typedef struct chain_node chain; struct chain_node { ____ data; // != NULL; contains both key and value ____ next; }; typedef struct hdict_header hdict; struct hdict_header { int size; // 0 <= size _______ table; // \length(table) == capacity int capacity; // 0 < capacity };

1 2 3 4

chain*[] entry chain*

slide-15
SLIDE 15

Data (representation) structure invariant

bool is_array_expected_length(chain*[] table, int length) { //@assert \length(table) == length; return true; } bool is_hdict(hdict* H) { return H != NULL && H->capacity > 0 && H->size >= 0 && is_array_expected_length(H->table, H->capacity); /* && other properties*/ }

Can the client violate is_hdict?

slide-16
SLIDE 16

hdict* hdict_new(int capacity) //@requires capacity > 0; //@ensures is_hdict(\result); { hdict* H = _____________ H->size = 0; H->capacity = capacity; H->table = ____________________________ return H; }

typedef struct chain_node chain; struct chain_node { entry data; chain* next; }; typedef struct hdict_header hdict; struct hdict_header { int size; chain*[] table; int capacity; };

alloc_array(chain*, capacity); alloc(hdict);

slide-17
SLIDE 17

entry hdict_lookup(hdict* H, key k) //@requires is_hdict(H); //@ensures \result == NULL || key_equiv(entry_key(\result), k); { int i = index_of_key(H, k); for (chain* p = H->table[i]; p != NULL; p = p->next) { if (key_equiv(entry_key(p->data), k)) return p->data; } return NULL; }

slide-18
SLIDE 18

Hash sets

  • Insert is adding an member to a set
  • lookup is a membership check
  • (key = entry = set member)