Struct seax_util::compiler_tools::forktable::ForkTable [] [src]

pub struct ForkTable<'a, K, V> where
    K: Eq + Hash,
    K: 'a,
    V: 'a, 
{ /* fields omitted */ }

An associative map data structure for representing scopes.

A ForkTable functions similarly to a standard associative map data structure (such as a HashMap), but with the ability to fork children off of each level of the map. If a key exists in any of a child's parents, the child will 'pass through' that key. If a new value is bound to a key in a child level, that child will overwrite the previous entry with the new one, but the previous key -> value mapping will remain in the level it is defined. This means that the parent level will still provide the previous value for that key.

This is an implementation of the ForkTable data structure for representing scopes. The ForkTable was initially described by Max Clive. This implemention is based primarily by the Scala reference implementation written by Hawk Weisman for the Decaf compiler, which is available here.

Methods

impl<'a, K, V> ForkTable<'a, K, V> where
    K: Eq + Hash
[src]

Returns a reference to the value corresponding to the key.

If the key is defined in this level of the table, or in any of its' parents, a reference to the associated value will be returned.

The key may be any borrowed form of the map's key type, but Hash and Eq on the borrowed form must match those for the key type.

Arguments

  • key - the key to search for

Return Value

  • Some(&V) if an entry for the given key exists in the table, or None if there is no entry for that key.

Examples

let mut table: ForkTable<isize,&str> = ForkTable::new();
assert_eq!(table.get(&1), None);
table.insert(1, "One");
assert_eq!(table.get(&1), Some(&"One"));
assert_eq!(table.get(&2), None);
let mut level_1: ForkTable<isize,&str> = ForkTable::new();
level_1.insert(1, "One");

let mut level_2: ForkTable<isize,&str> = level_1.fork();
assert_eq!(level_2.get(&1), Some(&"One"));

Returns a mutable reference to the value corresponding to the key.

If the key is defined in this level of the table, a reference to the associated value will be returned.

Note that only keys defined in this level of the table can be accessed as mutable. This is because otherwise it would be necessary for each level of the table to hold a mutable reference to its parent.

The key may be any borrowed form of the map's key type, but Hash and Eq on the borrowed form must match those for the key type.

Arguments

  • key - the key to search for

Return Value

  • Some(&mut V) if an entry for the given key exists in the table, or None if there is no entry for that key.

Examples

let mut table: ForkTable<isize,&str> = ForkTable::new();
assert_eq!(table.get_mut(&1), None);
table.insert(1isize, "One");
assert_eq!(table.get_mut(&1), Some(&mut "One"));
assert_eq!(table.get_mut(&2), None);
let mut level_1: ForkTable<isize,&str> = ForkTable::new();
level_1.insert(1, "One");

let mut level_2: ForkTable<isize,&str> = level_1.fork();
assert_eq!(level_2.get_mut(&1), None);

Removes a key from the map, returning the value at the key if the key was previously in the map.

If the removed value exists in a lower level of the table, it will be whited out at this level. This means that the entry will be 'removed' at this level and this table will not provide access to it, but the mapping will still exist in the level where it was defined. Note that the key will not be returned if it is defined in a lower level of the table.

The key may be any borrowed form of the map's key type, but Hash and Eq on the borrowed form must match those for the key type.

Arguments

  • key - the key to remove

Return Value

  • Some(V) if an entry for the given key exists in the table, or None if there is no entry for that key.

Examples

let mut table: ForkTable<isize,&str> = ForkTable::new();
table.insert(1, "One");

assert_eq!(table.remove(&1), Some("One"));
assert_eq!(table.contains_key(&1), false);
let mut level_1: ForkTable<isize,&str> = ForkTable::new();
level_1.insert(1, "One");
assert_eq!(level_1.contains_key(&1), true);

let mut level_2: ForkTable<isize,&str> = level_1.fork();
assert_eq!(level_2.chain_contains_key(&1), true);
assert_eq!(level_2.remove(&1), None);
assert_eq!(level_2.chain_contains_key(&1), false);

Inserts a key-value pair from the map.

If the key already had a value present in the map, that value is returned. Otherwise, None is returned.

If the key is currently whited out (i.e. it was defined in a lower level of the map and was removed) then it will be un-whited out and added at this level.

Arguments

  • k - the key to add
  • v - the value to associate with that key

Return Value

  • Some(V) if a previous entry for the given key exists in the table, or None if there is no entry for that key.

Examples

Simply inserting an entry:

let mut table: ForkTable<isize,&str> = ForkTable::new();
assert_eq!(table.get(&1), None);
table.insert(1, "One");
assert_eq!(table.get(&1), Some(&"One"));

Overwriting the value associated with a key:

let mut table: ForkTable<isize,&str> = ForkTable::new();
assert_eq!(table.get(&1), None);
assert_eq!(table.insert(1, "one"), None);
assert_eq!(table.get(&1), Some(&"one"));

assert_eq!(table.insert(1, "One"), Some("one"));
assert_eq!(table.get(&1), Some(&"One"));

Returns true if this level contains a value for the specified key.

The key may be any borrowed form of the map's key type, but Hash and Eq on the borrowed form must match those for the key type.

Arguments

  • k - the key to search for

Return Value

  • true if the given key is defined in this level of the table, false if it does not.

Examples

let mut table: ForkTable<isize,&str> = ForkTable::new();
assert_eq!(table.contains_key(&1), false);
table.insert(1, "One");
assert_eq!(table.contains_key(&1), true);
let mut level_1: ForkTable<isize,&str> = ForkTable::new();
assert_eq!(level_1.contains_key(&1), false);
level_1.insert(1, "One");
assert_eq!(level_1.contains_key(&1), true);

let mut level_2: ForkTable<isize,&str> = level_1.fork();
assert_eq!(level_2.contains_key(&1), false);

Returns true if the key is defined in this level of the table, or in any of its' parents and is not whited out.

The key may be any borrowed form of the map's key type, but Hash and Eq on the borrowed form must match those for the key type.

Arguments

  • k - the key to search for

Return Value

  • true if the given key is defined in the table, false if it does not.

Examples

let mut table: ForkTable<isize,&str> = ForkTable::new();
assert_eq!(table.chain_contains_key(&1), false);
table.insert(1, "One");
assert_eq!(table.chain_contains_key(&1), true);
let mut level_1: ForkTable<isize,&str> = ForkTable::new();
assert_eq!(level_1.chain_contains_key(&1), false);
level_1.insert(1, "One");
assert_eq!(level_1.chain_contains_key(&1), true);

let mut level_2: ForkTable<isize,&str> = level_1.fork();
assert_eq!(level_2.chain_contains_key(&1), true);

Forks this table, returning a new ForkTable<K,V>.

This level of the table will be set as the child's parent. The child will be created with an empty backing HashMap and no keys whited out.

Note that the new ForkTable<K,V> has a lifetime bound ensuring that it will live at least as long as the parent ForkTable.

Constructs a new ForkTable<K,V>

Wrapper for the backing map's values() function.

Provides an iterator visiting all values in arbitrary order. Iterator element type is &'b V.

Wrapper for the backing map's keys() function.

Provides an iterator visiting all keys in arbitrary order. Iterator element type is &'b K.

Trait Implementations

impl<'a, K: Debug, V: Debug> Debug for ForkTable<'a, K, V> where
    K: Eq + Hash,
    K: 'a,
    V: 'a, 
[src]

Formats the value using the given formatter.

impl<'a, 'b, K, Q: ?Sized, V> Index<&'b Q> for ForkTable<'a, K, V> where
    K: Borrow<Q>,
    K: Eq + Hash,
    Q: Eq + Hash
[src]

Allows table[&key] indexing syntax.

This is just a wrapper for get(&key)

let mut table: ForkTable<isize,&str> = ForkTable::new();
table.insert(1, "One");
assert_eq!(table[&1], "One");

The returned type after indexing

The method for the indexing (container[index]) operation

impl<'a, 'b, K, Q: ?Sized, V> IndexMut<&'b Q> for ForkTable<'a, K, V> where
    K: Borrow<Q>,
    K: Eq + Hash,
    Q: Eq + Hash
[src]

Allows mutable table[&key] indexing syntax.

This is just a wrapper for get_mut(&key)

let mut table: ForkTable<isize,&str> = ForkTable::new();
table.insert(1, "One");
table[&1] = "one";
assert_eq!(table[&1], "one")

The method for the mutable indexing (container[index]) operation