12.2.1 Implementation
A bag is an unordered collection of elements. An element can be added to the bag,
deleted from the bag, and looked up in the bag. The multiplicity of an element is the
number of times it occurs in the bag. Looking up an element in the bag returns the
multiplicity of that element. An element is not in the bag if and only if its multiplicity
is zero. You can also count the total number of elements in the bag. Unlike the bag
data type in the modeling library, the bag implementation is not a value type.
12.2.2 Model
The bag model has an action vocabulary that corresponds to the methods of the bag
implementation. The model program is shown in Figure 12.2. The following action
trace is an example of a correct behavior of the bag:
Add("elem1")
Add("elem1")
Count_Start()
Count_Finish(2)
Lookup_Start("elem1")
Lookup_Finish(2)
Lookup_Start("elem2")
Lookup_Finish(0)
Delete("elem1")
Lookup_Start("elem1")
Lookup_Finish(1)
The content variable is the only state variable of the model program. In the
initial state, the value of content is the empty bag. The bag model program defines
an infinite state machine because the input parameter domains of the actions are
unbounded and the size of the bag is unbounded. One way to constrain the set of
Systems with Complex State 195
using System.Collections.Generic;
namespace BagImpl
{
public class BagImpl
{
Dictionary
table = new Dictionary();
int count = 0;
public int Lookup(string element)
{
int c = 0;
table.
Pages:
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265