OrderedMap

Usage

use OrderedMap;

or

import OrderedMap;

This module contains the implementation of the orderedMap type which is a container that stores key-value associations.

orderedMaps are not parallel safe by default, but can be made parallel safe by setting the param formal parSafe to true in any orderedMap constructor. When constructed from another orderedMap, the new orderedMap will inherit the parallel safety mode of its originating orderedMap.

OrderedSet supports searching for a certain key, insertion and deletion in O(logN).

record orderedMap
type keyType

Type of orderedMap keys.

type valType

Type of orderedMap values.

param parSafe = false

If true, this orderedMap will perform parallel safe operations.

var comparator: record = defaultComparator

The comparator used to compare keys

proc init(type keyType, type valType, param parSafe = false, comparator: record = defaultComparator)

Initializes an empty orderedMap containing keys and values of given types.

Arguments
  • keyType – The type of the keys of this orderedMap.

  • valType – The type of the values of this orderedMap.

  • parSafe : bool – If true, this orderedMap will use parallel safe operations.

  • comparator – The comparator used to compare keys.

proc init=(const ref other: orderedMap(?kt, ?vt))

Initialize this orderedMap with a copy of each of the elements contained in the orderedMap other. This orderedMap will inherit the parSafe value of the orderedMap other.

Arguments

other – An orderedMap to initialize this orderedMap with.

proc clear()

Clears the contents of this orderedMap.

Warning

Clearing the contents of this orderedMap will invalidate all existing references to the elements contained in this orderedMap.

proc size

The current number of keys contained in this orderedMap.

proc isEmpty(): bool

Returns true if this orderedMap contains zero keys.

Returns

true if this orderedMap is empty.

Return type

bool

proc contains(const k: keyType): bool

Returns true if the given key is a member of this orderedMap, and false otherwise.

Arguments

k : keyType – The key to test for membership.

Returns

Whether or not the given key is a member of this orderedMap.

Return type

bool

proc update(other: orderedMap(keyType, valType, ?p))

Updates this orderedMap with the contents of the other, overwriting the values for already-existing keys.

Arguments

other – The other orderedMap

proc ref this(k: keyType) ref

Get the value mapped to the given key, or add the mapping if key does not exist.

Arguments

k : keyType – The key to access

Returns

Reference to the value mapped to the given key.

proc getBorrowed(k: keyType)

Get a borrowed reference to the element at position k.

proc getReference(k: keyType) ref

Get a reference to the element at position k. This method is not available for non-nilable types.

proc getValue(k: keyType)

Get a copy of the element stored at position k. This method is only available when a orderedMap’s valType is a non-nilable class.

proc getAndRemove(k: keyType)

Remove the element at position k from the orderedMap and return its value

iter these() const ref

Iterates over the keys of this orderedMap. This is a shortcut for keys.

Yields

A reference to one of the keys contained in this orderedMap.

iter keys() const ref

Iterates over the keys of this orderedMap.

Yields

A reference to one of the keys contained in this orderedMap.

iter items() const ref

Iterates over the key-value pairs of this orderedMap.

Yields

A tuple of references to one of the key-value pairs contained in this orderedMap.

iter values() ref

Iterates over the values of this orderedMap.

Yields

A reference to one of the values contained in this orderedMap.

proc writeThis(ch: channel) throws

Writes the contents of this orderedMap to a channel. The format looks like:

{k1: v1, k2: v2, .... , kn: vn}
Arguments

ch – A channel to write to.

proc add(in k: keyType, in v: valType): bool

Adds a key-value pair to the orderedMap. Method returns false if the key already exists in the orderedMap.

Arguments
  • k : keyType – The key to add to the orderedMap

  • v : valueType – The value that maps to k

Returns

true if k was not in the orderedMap and added with value v. false otherwise.

Return type

bool

proc set(k: keyType, in v: valType): bool

Sets the value associated with a key. Method returns false if the key does not exist in the orderedMap.

Arguments
  • k : keyType – The key whose value needs to change

  • v : valueType – The desired value to the key k

Returns

true if k was in the orderedMap and its value is updated with v. false otherwise.

Return type

bool

proc addOrSet(in k: keyType, in v: valType)

If the orderedMap doesn’t contain a value at position k add one and set it to v. If the orderedMap already contains a value at position k, update it to the value v.

proc remove(k: keyType): bool

Removes a key-value pair from the orderedMap, with the given key.

Arguments

k – The key to remove from the orderedMap

Returns

false if k was not in the orderedMap. true if it was and removed.

Return type

bool

proc toArray(): [] (keyType, valType)

Returns a new 0-based array containing a copy of key-value pairs as tuples.

Returns

A new DefaultRectangular array.

Return type

[] (keyType, valType)

proc keysToArray(): [] keyType

Returns a new 0-based array containing a copy of keys. Array is sorted using the comparator.

Returns

A new DefaultRectangular array.

Return type

[] keyType

proc valuesToArray(): [] valType

Returns a new 0-based array containing a copy of values. Array is not guaranteed to be in any particular order.

Returns

A new DefaultRectangular array.

Return type

[] valType

proc type orderedMap.=(ref lhs: orderedMap(?kt, ?vt, ?ps), const ref rhs: orderedMap(kt, vt, ps))

Replace the content of this orderedMap with the other’s.

Warning

This will invalidate any references to elements previously contained in lhs.

Arguments
  • lhs – The orderedMap to assign to.

  • rhs – The orderedMap to assign from.

proc type orderedMap.==(const ref a: orderedMap(?kt, ?vt, ?ps), const ref b: orderedMap(kt, vt, ps)): bool

Returns true if the contents of two orderedMaps are the same.

Arguments
  • a – A orderedMap to compare.

  • b – A orderedMap to compare.

Returns

true if the contents of two orderedMaps are equal.

Return type

bool

proc type orderedMap.!=(const ref a: orderedMap(?kt, ?vt, ?ps), const ref b: orderedMap(kt, vt, ps)): bool

Returns true if the contents of two orderedMaps are not the same.

Arguments
  • a – A orderedMap to compare.

  • b – A orderedMap to compare.

Returns

true if the contents of two orderedMaps are not equal.

Return type

bool

proc type orderedMap.+(a: orderedMap(?keyType, ?valueType, ?parSafe), b: orderedMap(keyType, valueType, parSafe))

Returns a new orderedMap containing the keys and values in either a or b.

proc type orderedMap.+=(ref a: orderedMap(?keyType, ?valueType, ?parSafe), b: orderedMap(keyType, valueType, parSafe))

Sets the left-hand side orderedMap to contain the keys and values in either a or b.

proc type orderedMap.|(a: orderedMap(?keyType, ?valueType, ?parSafe), b: orderedMap(keyType, valueType, parSafe))

Returns a new orderedMap containing the keys and values in either a or b.

proc type orderedMap.|=(ref a: orderedMap(?keyType, ?valueType, ?parSafe), b: orderedMap(keyType, valueType, parSafe))

Sets the left-hand side map to contain the keys and values in either a or b.

proc type orderedMap.&(a: orderedMap(?keyType, ?valueType, ?parSafe), b: orderedMap(keyType, valueType, parSafe))

Returns a new orderedMap containing the keys that are in both a and b.

proc type orderedMap.&=(ref a: orderedMap(?keyType, ?valueType, ?parSafe), b: orderedMap(keyType, valueType, parSafe))

Sets the left-hand side orderedMap to contain the keys that are in both a and b.

Warning

This will invalidate any references to elements previously contained in lhs.

proc type orderedMap.-(a: orderedMap(?keyType, ?valueType, ?parSafe), b: orderedMap(keyType, valueType, parSafe))

Returns a new orderedMap containing the keys that are only in a, but not b.

proc type orderedMap.-=(ref a: orderedMap(?keyType, ?valueType, ?parSafe), b: orderedMap(keyType, valueType, parSafe))

Sets the left-hand side orderedMap to contain the keys that are in the left-hand orderedMap, but not the right-hand orderedMap.

proc type orderedMap.^(a: orderedMap(?keyType, ?valueType, ?parSafe), b: orderedMap(keyType, valueType, parSafe))

Returns a new orderedMap containing the keys that are in either a or b, but not both.

proc type orderedMap.^=(ref a: orderedMap(?keyType, ?valueType, ?parSafe), b: orderedMap(keyType, valueType, parSafe))

Sets the left-hand side orderedMap to contain the keys that are in either the left-hand orderedMap or the right-hand orderedMap, but not both.