Also, unlike the .NET dictionary type, the value null may be used as a key in a
map.
Appendix A.2.3 provides a list of map operations provided by the NModel library.
Maps appear often in models with structured state. Maps may be used to represent
dynamic functions, or key/value relationships that evolve during the run of the
Systems with Complex State 163
system. For example, a server may have an operation that assigns integer priorities
to its active clients:
static Map
priority = Map.EmptyMap;
[Action]
static void SetPriority(string client, int p)
{
priority = priority.Override(client, p);
}
The Override method produces a new map by substituting a key/value pair. If the
key given as the argument is not in the map, then a new key/value pair is added. In
this example we set the variable priority to contain the value of the new map.
Creating maps
If your map contains five or fewer key/value pairs, you can use a map constructor
that takes keys and values as arguments:
Map cityId = new Map("Athens", 1, "Rome", 2,
"Paris", 3, "New York", 4);
Assert.AreEqual(cityId["Paris"], 3);
A common way to build a map is to start with the empty map and add elements
programmatically:
Map idCity = Map.EmptyMap;
string[] cities =
new string[]{"Athens", "Rome", "Paris", "New York"};
for(int i = 0; i < cities.
Pages:
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232