File: PlaceMarking.C

package info (click to toggle)
maria 1.3.5-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 4,068 kB
  • sloc: cpp: 43,408; yacc: 8,080; ansic: 436; sh: 404; lisp: 395; makefile: 228; perl: 21
file content (324 lines) | stat: -rw-r--r-- 8,578 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
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
231
232
233
234
235
236
237
238
239
240
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
// Place marking class -*- c++ -*-

#ifdef __GNUC__
# pragma implementation
#endif // __GNUC__
#include "PlaceMarking.h"
#include "Place.h"
#include "BitBuffer.h"
#include "Marking.h"
#include "CardType.h"

#include <assert.h>
#include <string.h>

/** @file PlaceMarking.C
 * Assignment of a place to a multi-set
 */

/* Copyright  1999-2002 Marko Mkel (msmakela@tcs.hut.fi).

   This file is part of MARIA, a reachability analyzer and model checker
   for high-level Petri nets.

   MARIA is free software; you can redistribute it and/or modify it
   under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2, or (at your option)
   any later version.

   MARIA is distributed in the hope that it will be useful, but
   WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   General Public License for more details.

   The GNU General Public License is often shipped with GNU software, and
   is generally kept in a file called COPYING or LICENSE.  If you do not
   have a copy of the license, write to the Free Software Foundation,
   59 Temple Place, Suite 330, Boston, MA 02111 USA. */

#ifndef NDEBUG
void
PlaceMarking::setPlace (const class Place* place)
{
  assert (!place || !myPlace);
  if (place)
    setType (&place->getType ());
  myPlace = place;
}

void
PlaceMarking::setType (const class Type* type)
{
  assert (!myPlace && !myType);
  myType = type;
}
#endif // NDEBUG

bool
PlaceMarking::add (class Value& value, card_t amount)
{
  assert (myType->isConstrained (value));
  assert (amount > 0);
  std::pair<iterator, bool> result =
    myTokens.insert (TokenMap::value_type (&value, amount));
  if (!result.second) {
    delete &value;
    if (amount >= CARD_T_MAX - result.first->second)
      return false;
    result.first->second += amount;
  }
  return true;
}

void
PlaceMarking::remove (const class Value& value, card_t amount)
{
  assert (myType->isConstrained (value));
  assert (amount > 0);
  iterator i = find (&value);
  assert (i != end ());
  assert (i->second >= amount);
  i->second -= amount;
}

bool
PlaceMarking::encode (class BitPacker& buf,
		      const class Valuation* valuation) const
{
  assert (myPlace && !myPlace->isConstant ());
  if (myPlace->isImplicit ()) {
    if (!valuation)
      return true;
    const class Marking* marking = myPlace->getInitMarking ();
    assert (!!marking);
    class PlaceMarking p;
    p.setPlace (myPlace);
    return marking->add (p, 1, *valuation) && p == *this;
  }

  /** Number of distinct tokens */
  size_t distinct = size ();
  /** Array of sorted cardinality,value pairs */
  class card_value* histogram = distinct ? new class card_value[distinct] : 0;
  /** Total cardinality */
  card_t total = distinct ? sort (histogram) : 0;

  if (total == CARD_T_MAX) {
  capacity:
    delete[] histogram;
    return false;
  }

  // Encode the total number of tokens
  if (const class Constraint* c = myPlace->getCapacity ()) {
    card_t n;
    if (!CardType::convert (n, total, *c))
      goto capacity;
    if (unsigned cap = myPlace->getCapacityBits ()) {
      if (cap > 2 && !myPlace->isNonempty ()) {
	if (!total)
	  goto empty;
	buf.append (1, 1);
      }
      buf.append (n, cap);
    }
    if (!total)
      goto none;
  }
  else {
    assert (!myPlace->isNonempty ());
    // variable-length code:
    // 0		0
    // 1..8		10xxx
    // 9..264		110xxxxxxxx
    // 265..65800	1110xxxxxxxxxxxxxxxx
    // 65801..		1111xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
    if (!total) {
    empty:
      buf.append (0, 1);
      goto none;
    }
    else {
      buf.append (1, 1);
      if (total <= 8)
	buf.append (0, 1), buf.append (total - 1, 3);
      else {
	buf.append (1, 1);
	if (total <= 8 + 256)
	  buf.append (0, 1), buf.append (total - (1 + 8), 8);
	else {
	  buf.append (1, 1);
	  if (total <= 8 + 256 + 65536)
	    buf.append (0, 1), buf.append (total - (1 + 8 + 256), 16);
	  else
	    buf.append (1, 1), buf.append (total, CARD_T_BIT);
	}
      }
    }
  }

  // Encode the number of distinct tokens
  assert (distinct && distinct <= total);
  if (total > 1)
    buf.append (distinct - 1, log2 (total));

  // Encode each distinct token in descending order of multiplicity
  while (distinct--) {
    // Encode the multiplicity (distinct has been decremented by one)
    assert (total >= distinct + 1);
    /** Lower multiplicity bound, inclusive */
    const card_t low = (total + distinct) / (distinct + 1);
    /** Higher multiplicity bound, inclusive */
    const card_t high = total - distinct;
    assert (low <= high);
    // Update the total cardinality of remaining tokens
    assert (total >= histogram[distinct].card);
    total -= histogram[distinct].card;

    assert (histogram[distinct].card >= low &&
	    histogram[distinct].card <= high);
    // Finally encode the multiplicity
    if (high != low)
      buf.append (histogram[distinct].card - low, log2 (high - low + 1));
    // Encode the value
    buf.append (*histogram[distinct].value);
  }

 none:
  assert (!total);
  delete[] histogram;
  return true;
}

void
PlaceMarking::decode (class BitUnpacker& buf)
{
  assert (!!myPlace);
  if (myPlace->isImplicit () || myPlace->isConstant ())
    return;
  /** Total cardinality */
  card_t total;

  // Decode the total number of tokens
  if (const class Constraint* c = myPlace->getCapacity ()) {
    if (unsigned cap = myPlace->getCapacityBits ()) {
      if (cap > 2 && !myPlace->isNonempty () && !buf.extract (1))
	return;
      total = CardType::convert (buf.extract (cap), *c);
    }
    else
      total = CardType::convert (0, *c);
    if (!total)
      return;
  }
  else {
    if (!buf.extract (1))		// 0: 0
      return;
    else if (!buf.extract (1))		// 10xxx: 1..8
      total = 1 + buf.extract (3);
    else if (!buf.extract (1))		// 110xxxxxxxx: 9..264
      total = (1 + 8) + buf.extract (8);
    else if (!buf.extract (1))		// 1110xxxxxxxxxxxxxxxx: 265..65800
      total = (1 + 8 + 256) + buf.extract (16);
    else {				// 1111...: 65801..
      total = buf.extract (CARD_T_BIT);
      assert (total > 8 + 256 + 65536);
    }
  }

  /** Number of distinct tokens */
  card_t distinct = total > 1 ? buf.extract (log2 (total)) + 1 : 1;

  // Decode each distinct token in descending order of multiplicity
  while (distinct--) {
    // Decode the multiplicity (distinct has been decremented by one)
    assert (total >= distinct + 1);
    /** Lower multiplicity bound, inclusive */
    const card_t low = (total + distinct) / (distinct + 1);
    /** Higher multiplicity bound, inclusive */
    const card_t high = total - distinct;
    assert (low <= high);
    /** Multiplicity */
    const card_t count = low != high
      ? buf.extract (log2 (high - low + 1)) + low
      : low;
    // Update the total cardinality of remaining tokens
    assert (total >= count);
    total -= count;

    // Decode the value
    if (!add (*buf.extract (myPlace->getType ()), count))
      assert (false);
  }

  assert (!total);
}

#include "Printer.h"
void
PlaceMarking::display (const class Printer& printer) const
{
  if (empty ())
    return;
  if (myPlace) {
    printer.print (myPlace->getName ());
    printer.delimiter (':')++;
  }
  bool comma = false;
  for (const_iterator i = myTokens.begin (); i != myTokens.end (); i++) {
    if (!i->second)
      continue;

    if (!comma) {
      if (myPlace)
	printer.linebreak ();
      comma = true;
    }
    else
      printer.delimiter (',');

    if (i->second > 1) {
      printer.print (i->second);
      printer.delimiter ('#');
    }
    i->first->display (printer);
  }
  if (myPlace)
    printer--;
}

card_t
PlaceMarking::sort (class PlaceMarking::card_value* histogram) const
{
  assert (histogram && !empty ());
  card_t total = 0;
  unsigned num = 0, pos = 0;
  for (const_iterator i = myTokens.begin ();
       i != myTokens.end ();
       i++, num++) {
    card_t card = i->second;
    if (!card) continue;
    if (total > CARD_T_MAX - card)
      return CARD_T_MAX;
    total += card;
    if (num) {
      for (unsigned low = 0, high = num; low != high; ) {
	pos = (low + high) >> 1;
	assert (pos < num && pos < high);
	if (histogram[pos].card > card)
	  high = pos;
	else if (++pos == high)
	  break;
	else {
	  assert (high > pos);
	  low = pos;
	}
      }
      memmove (histogram + pos + 1, histogram + pos,
	       (num - pos) * sizeof *histogram);
    }
    histogram[pos] = card_value (card, i->first);
  }
  assert (num == size ());
  return total;
}