Presentation is loading. Please wait.

Presentation is loading. Please wait.

Maps Rem Collier Room A1.02 School of Computer Science and Informatics

Similar presentations


Presentation on theme: "Maps Rem Collier Room A1.02 School of Computer Science and Informatics"— Presentation transcript:

1 Maps Rem Collier Room A1.02 School of Computer Science and Informatics
University College Dublin, Ireland

2 The Map ADT Maps are often referred to as associative stores.
They store key-value pairs known as entries. Each key in a map must be unique. Adding an entry with a key that is already in the map will cause the existing entry to be replaced. Main Operations: When an entry is inserted we say that it is put into the map We can get a value by giving its key. We can remove an entry by giving the corresponding key. Applications: Student database: key = student id, value = student record Variable tables (compilers): key = variable, value = type DNS Servers: key = domain, value = IP address

3 Map ADT: Operations The Map ADT has the following operations:
size() Returns the number of entries isEmpty() Test whether there are entries get(k) If the map contains an entry e with key equal to k, then return the value of e, otherwise return null. put(k,v) If the map does not have an entry with key equal to k then add entry (k,v) to the map; otherwise update the existing entry to associate k with v and return the old value. remove(k) Remove the entry with key k and return the value; if no entry exists, return null. keys() Return an iterator of the keys stored in the map. values() Return an iterator of the values stored in the map. entries() Return an iterator of the entries stored in the map.

4 Entry and Iterator ADTs
An Entry can be modeled as an ADT that supports the following operations: key(): Returns the key part of the entry Value(): Returns the value part of the entry RECAP: An Iterator is an ADT that encapsulates the notion of “walking along the elements of a sequence”. To achieve this, we can identify two basic operations: hasNext(): Are there more items? next(): Returns next element (if there is one) Iterators are most commonly associated with lists, but can also be used with other ADTs such as trees and graphs…

5 Entry and Map Interfaces
public interface Entry<K, V> { public K key(); public V value(); } public interface Map<K, V> { public int size(); public boolean isEmpty(); public V get(K k); public V put(K k, V v); public V remove(K k); public Iterator<K> keys(); public Iterator<V> values(); public Iterator<Entry<K,V>> entries();

6 List-based Maps

7 Implementing a Map The easiest way to implement a map is to view it as a list of entries. We can utilize the pre-existing List ADT. Our Map implementation creates and manipulates a List object. This is known as the Adaptor Pattern. For example, a map of student numbers to student names may take the form: {“ ”, “Rem Collier”} {“ ”, “Arthur Cater”} {“ ”, “Joe Carthy”}

8 Implementing a Map The easiest way to implement a map is to view it as a list of entries. We can utilize the pre-existing List ADT. Our Map implementation creates and manipulates a List object. This is known as the Adaptor Pattern. Core Map Operations: put(k,v): Search the list for the entry with key, k. If one exists, replace it with entry (k,v), else add a new entry. get(k): Search the list for the entry with key, k, and return the corresponding value. remove(k): Search the list for position of the entry with key, k. If Such a position exists, remove the item at that position All three operations make use of some form of search operation that locates the relevant entry (if one exists).

9 Insert: List Operations
replace(p,e): Replace the element at position p with e, returning the element formerly at p. insertFirst(e): Insert a new element e into S as the first element and return the position of e. insertLast(e): Insert a new element e into S as the last element and return the position of e. insertBefore(p,e): Insert a new element e into S before position p and return the position of e. insertAfter(p,e): Insert a new element e into S after position p and return the position of e. remove(p): Remove from S the element at position p. isEmpty(): Returns true if the vector is empty, or false otherwise size(): Returns the number of elements in the vector first(): Return the position of the first element of S; a list empty error occurs if S is empty. last(): Return the position of the last element of S; a list empty error occurs if S is empty. prev(p): Return the position of the element of S preceding the one at p; a boundary violation error occurs if p is the first position. next(p): Return the position of the element of S following the one at p; a boundary violation error occurs if p is the last position.

10 List-based Map Lets start with the find(…) operation:
Algorithm find(k): Input: A key, k Output: The Position, of the Entry with key, k, or null if S.isEmpty() return null P  S.first() while P <> S.last() do if P.element().key() = k then return P P  S.next(P) return null

11 List-based Map The get(k) operation: P  find(k) Algorithm get(k):
Input: A key, k Output: A Position, P, or null P  find(k) if (P = null) then return null return P.element().value()

12 List-based Map The put(k, v) operation: Algorithm put(k, v):
Input: A key, k, and a value, v Output: The previous value associated with k, or null Entry E  new Entry(k, v) P  find(k) if (P = null) then S.insertLast(E) return null temp  list.replace(P, E) return temp.value()

13 List-based Map The remove(k) operation: Algorithm remove(k):
Input: A key, k Output: The value associated with k, or null P  find(k) if (P = null) then return null list.remove(P) return P.element().value()

14 List-based Map So, what about performance?
get(), put() and remove() depend on find() This has a O(n) running-time! This is the best case running-time for linked-list searches. This means that we cannot improve on O(n)… There must be something better than this!!! Operation Time size() O(1) isEmpty() get(k) O(n) put(k,v) remove(k) keys() values() entries()


Download ppt "Maps Rem Collier Room A1.02 School of Computer Science and Informatics"

Similar presentations


Ads by Google