Class Models for Lua

Map with Array of Keys
loop.collection.MapWithArrayOfKeys
Class of objects that store in a single table a mapping of non-integer key values to arbitrary values and a sequence of all the key values currently mapped. The array of keys is stored just like the sequence stored by UnorderedArray
instances. This class is useful for storing a map of non-integer values and an array of its keys in a single table.
Each non-integer key indexes its mapped value. Integer keys are used to store mapped keys as an array.
Behavior
Methods
add(key, value)
- Adds the mapping of value
key
tovalue
to the map. value(key [, value])
- If
value
is provided then the mapped value for keykey
is replaced by valuevalue
. Otherwise, the value mapped by keykey
is returned. valueat(index [, value])
- If
value
is provided then the mapped value for the key stored at positionindex
is replaced by valuevalue
. Otherwise, the value mapped by key stored at positionindex
is returned. remove(key)
- Removes the mapping of key from the map and the array.
removeat(index)
- Removes the mapping of key stored at position
index
from the map and the array.
Remarks
- Instances cannot store integer keys because they are reserved for storage of the key values in an array form.
- Instances cannot store the name of class members as strings. To do so, use the class operations over an empty table like in the following example:
MapWithArrayOfKeys = require "loop.collection.MapWithArrayOfKeys" map = {} MapWithArrayOfKeys.add(map, "hi", true) MapWithArrayOfKeys.add(map, "everybody,", true) MapWithArrayOfKeys.add(map, "have", true) MapWithArrayOfKeys.add(map, "a", true) MapWithArrayOfKeys.add(map, "nice", true) MapWithArrayOfKeys.add(map, "day", true) print(table.concat(map, " "))
Copyright (C) 2004-2008 Tecgraf, PUC-RioThis project is currently being maintained by Tecgraf at PUC-Rio.