This page is for an old version of Hexaly Optimizer. We recommend that you update your version and read the documentation for the latest stable release.

LSPMap Class

class localsolver.modeler.LSPMap

An LSPMap is a data structure mapping keys to values. A map is both an associative table and an array. Setting a value to an existing key will overwrite any value previously set for that key.

When used as an array-like structure, adding a new unkeyed element to an LSPMap with LSPMap.append() automatically assigns an integer key to the element equal to the largest integer key already present plus one, or zero if no integer key exists.

When possible, this class tries to behave like a python dict with some particularities that reflect the behavior of the modeler and LSP files: For instance, this class never throws KeyError. In LSP querying a non-existent key never fails, but always returns nil (turned into None in Python). In addition, setting None (nil) for a value to a key, removes the key and the previous value from the map.

Also note that the iterators returned by this class follow the fail-fast convention: if the map is structurally modified after the creation of an iterator (by using the methods clear, add, __setitem__, __delitem__), the iterator will throw an exception. In other words, you can’t modify a map while you are iterating over it.

Since:10.0

Summary

Methods
clear Erases all elements from the map.
append Adds the given value to the map.
Special methods
__len__ Returns the number of elements in the map.
__contains__ Returns true if the given key is defined in the map, false otherwise.
__setitem__ Sets the value associated with the given key.
__getitem__ Returns the value associated with the given key.
__delitem__ Unsets the given key in the map if present.
__iter__ Returns an iterator to the contents of this map.

Instance methods

LSPMap.clear()

Erases all elements from the map. After this call, len(myMap) returns zero.

LSPMap.append(value)

Adds the given value to the map. The associated key corresponds to the largest integer key in the map plus one, or zero if no integer key exists. This function accepts the folling types as input:

Parameters:value – Value to add in the map.

Special operators and methods

LSPMap.__len__()

Returns the number of elements in the map. This method is automatically called by python when using special constructs like len(myMap).

Returns:The number of elements in the map.
Return type:int
LSPMap.__contains__(key)

Returns true if the given key is defined in the map, false otherwise. This method is automatically called by python when using special constructs like key in myMap.

Parameters:key (any supported type among NoneType, int, float, str, LSExpression, LSPMap, LSPFunction, LSPModule) – Key to search.
LSPMap.__setitem__(key, value)

Sets the value associated with the given key. This method accepts the following types for the key and for the value:

This method is automatically called by python when using special constructs like myMap[key] = value.

Setting an existing key with the value None has the same effect as calling __delitem__() on the key. Note that None cannot be used as a key.

Parameters:
  • key – Key to set.
  • value – Value to set.
LSPMap.__getitem__(key)

Returns the value associated with the given key. If the key doesn’t exist in the map, None is returned. This method can return values of the following types:

This method is automatically called by python when using special constructs like value = myMap[key].

Parameters:key (any supported type among NoneType, int, float, str, LSExpression, LSPMap, LSPFunction, LSPModule) – Key to search
LSPMap.__delitem__(key)

Unsets the given key in the map if present. If the key doesn’t exist in the map, do nothing. This method is automatically called by python when using special constructs like del myMap[key].

After calling this method, key in myMap returns false and myMap[key] returns None.

Parameters:key (any supported type among NoneType, int, float, str, LSExpression, LSPMap, LSPFunction, LSPModule) – Key to unset.
LSPMap.__iter__()

Returns an iterator to the contents of this map. The iterator yields the (key, value) pairs contained in the map.