Mixing Vectors and Dictionaries, and Reference Semantics

When working with vectors and dictionaries, you can build simple or complex structures. For example, you can build vectors of vectors, or vectors of dictionaries, or any other mix.

For instance, you might want to store a sequence of previous stock prices by ticker symbol. This declaration and function creates such a set of stored prices, keyed by symbols:
dictionary(string, vector(money)) previousPrices;
integer addPrice(string symbol, money price) 
{
  vector(money) prices := previousPrices[symbol];
  if (isnull(prices)) {
    prices := new vector(money);
    previousPrices[symbol] := prices;
  }
  push_back(prices, price);
}
The example relies on reference semantics of containers. For instance, this assignment returns a reference to the vector, not a copy of the vector:
vector(money) prices := previousPrices[symbol];
Because it is a reference, the value inserted by push_back is in the vector the next time it is read from the dictionary.
Reference semantics does permit aliasing, that is, alternative names for the same entity. For instance, this results in the program printing "aliased!":
dictionary(integer, integer) d0 := new dictionary(integer, integer);
dictionary(integer, integer) d1 := d0;
d1[0] := 1;
if (d0[0] = 1) print('aliased!');