Many .NET data types
support enumeration by means of IEnumerable, and this can act as an interface
between the data types used formodeling and system data types. Here is an example:
static Set
ConvertToSet(IEnumerable list)
{
return new Set(list);
}
The order of elements in the enumeration doesn??™t matter, and duplicate elements
will be ignored.
Note that sets themselves implement the IEnumerable interface, so they can be
used iteratively:
static int CountOdd(Set s)
{
int result = 0;
foreach(int i in s)
if (i % 2 == 1) result += 1;
return result;
}
You can expect iteration over sets to occur in an arbitrary order.
Set properties and queries
There are several properties defined for sets. You can use IsEmpty to determine if the
set has no elements. The Count property returns the number of elements contained
in a set.
You can use the Contains method to see whether a given element is in the set.
Set cities = new Set("Athens", "Rome", "Athens"};
Assert.IsFalse(cities.IsEmpty);
Assert.AreEqual(cities.Count, 2);
Assert.IsTrue(cities.Contains("Rome"));
Assert.IsFalse(cities.Contains("New York"));
162 Modeling Systems with Structured State
Union, intersection, and difference
Any two sets of the same type can be combined by union, intersection, and difference.
Union produces the set with all of the elements.
Pages:
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230