Algorithms and OOD (CSC 207 2014S) : EBoards

CSC207.01 2014S, Class 38: Implementing Dictionaries


Overview

Preliminaries

Upcoming Work

Admin

Extra Credit

Questions

Talk to us above model/view/controller, particularly for this.

See the whiteboard for details.

A dictionary ADT, continued

A slight change, to follow common terminology

Note: keys can be any type, rather than just string.

The interface

/**
 * A dictionary of values of type V keyed by values of type K.
 */
public interface Dictionary<K,V>
{
  /**
   * Confirm that a key is valid.
   */
  public boolean containsKey(K key);

  /**
   * Change the entry for a particular key.
   *
   * @pre key is a valid key for the dictionary
   * @post get(key) == value
   */
  public void set(K key, V value)
    throws NoSuchKeyException;

  /**
   * Add the entry for a particular key.
   *
   * @pre key is not a valid key for the dictionary
   * @post get(key) == value
   */
  public void add(K key, V value)
    throws Exception;

  /** 
   * Get the entry for a particular key.  
   *
   * @throws NoSuchElementException
   *   If the key is not a valid key for the dictionary.
   */
  public V get(K key)
    throws NoSuchElementException;

  /** 
   * Add a bunch of keys and values.
   *
   * @pre
   *   keys.length == values.length
   * @pre
   *   No element of keys is null.
   * @pre
   *   No two elements of keys are equal.
   * @pre
   *   No elements of keys are already valid.
   */
  public void addAll(K[] keys, V[] values)
    throws SomeRandomException;

  /**
   * Remove a value based on its key.
   */
  public void remove(K key);

  /**
   * Remove everything.
   */
  public void clear();

  /** 
   * Remove all the entries with a given value.
   */
  public void removeAll(V val);

  /**
   * Get all of the keys in the dictionary.
   */
  public Iterator<K> keys();

  /**
   * Get all of the values in the dictionary.
   */
  public Iterator<V> values();

  /**
   * Get all of the key/value pairs in the dictionary.
   */
  public Iterator<Pair<K,V>> pairs();

  /** 
   * Update all of the values.
   */
  public void map(BinaryFunction<K,V,V> update);

} // interface Dictionary<K,V>

Some design questions, major and minor

Deprecated

  /**
   * Create an array of keys.
   */
  public K[] keyArray();

  /** 
   * Create an array of values.
   */
  public V[] valuesArray();

Review: Thinking about data structures

Association lists

Techniques for improving the implementation

Binary search trees

Copyright (c) 2013-14 Samuel A. Rebelsky.

Creative Commons License

This work is licensed under a Creative Commons Attribution 3.0 Unported License. To view a copy of this license, visit http://creativecommons.org/licenses/by/3.0/ or send a letter to Creative Commons, 543 Howard Street, 5th Floor, San Francisco, California, 94105, USA.