Map.opApply

'foreach' iteration over key/value pairs currently in the map.

Note: It is possible to have interruptible iterations, see documentation for mixin of IteratorClass

See also: BucketSet.Iterator, MapIterator.IteratorClass

Notes: - During iteration it is forbidden to call clear() or redistribute() or remove map elements. If elements are added, the iteration may or may not include these elements. - If V or K (or both) are a static array, the corresponding iteration variable is a dynamic array of the same base type and slices the key or value of the element in the map. (The reason is that static array 'ref' parameters are forbidden in D.) In this case it is not recommended to do a 'ref' iteration over key or value. To modify a value during iteration, copy the new value contents into the array content. Example:

// Using the StandardKeyHashingMap subclass because Map is an
// abstract class (inherits abstract toHash()).

alias int[7]  ValueType;
alias char[4] KeyType;

auto map = new StandardKeyHashingMap!(ValueType, KeyType);

// Side note: Use the '[x, y, ...]' array literal only with
// constants or in code locations that are not executed repeatedly
// because for variables it invokes a buffer allocation, leading
// to a memory leak condition when it is done very often.

const int[7] new_value = [2, 3, 5, 7, 11, 13, 17];

foreach (key, val; map)
{
    // - key is of type char[] and slices the key of the current
    //   map element so key.length is guaranteed to be 4,
    // - val is of type int[] and slices the value of the current
    //   map element so val.length is guaranteed to be 7.

    // Modify the value by copying into the static array
    // referenced by val.

    val[] = new_value[];

    // It is also possible to modify single val array elements.

    val[2] += val[5] * 4711;
}

DO NOT do that with the key or modify it in-place in any way!

- It is not recommended to specify 'ref' for the key iteration variable. If you do it anyway, DO NOT modify the key in-place!

  1. int opApply(MapIterator.Dg dg)
    class Map(size_t V, K)
    int
    opApply
  2. int opApply(MapIterator.Dgi dgi)

Meta