File: CardType.C

package info (click to toggle)
maria 1.3.4-5
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 3,956 kB
  • ctags: 5,485
  • sloc: cpp: 43,267; yacc: 8,080; sh: 460; ansic: 436; lisp: 395; makefile: 288; perl: 21
file content (267 lines) | stat: -rw-r--r-- 6,921 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
// Unsigned integer (cardinality) type class -*- c++ -*-

#ifdef __GNUC__
# pragma implementation
#endif // __GNUC__
#include "CardType.h"
#include "LeafValue.h"
#include "Constraint.h"
#include "Range.h"

/** @file CardType.C
 * Unsigned integer data type
 */

/* Copyright  2000-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. */

class Value&
CardType::getFirstValue () const
{
  const class Value* v = myConstraint ? &myConstraint->getFirstValue () : NULL;
  assert (!v || &v->getType () == this);
  return v
    ? *static_cast<class LeafValue*>(v->copy ())
    : *new class LeafValue (*this, 0);
}

class Value&
CardType::getLastValue () const
{
  const class Value* v = myConstraint ? &myConstraint->getLastValue () : NULL;
  assert (!v || &v->getType () == this);
  return v
    ? *static_cast<class LeafValue*>(v->copy ())
    : *new class LeafValue (*this, CARD_T_MAX);
}

card_t
CardType::do_getNumValues () const
{
  return myConstraint ? myConstraint->getNumValues () : CARD_T_MAX;
}

card_t
CardType::convert (const class Value& value) const
{
  assert (value.getKind () == Value::vLeaf);
  card_t v = card_t (static_cast<const class LeafValue&>(value));
  if (myConstraint) {
    card_t number;
    if (!convert (number, v, *myConstraint))
      assert (false);
    return number;
  }
  else
    return v;
}

class Value*
CardType::convert (card_t number) const
{
  assert (number < static_cast<const class Type*>(this)->getNumValues ());
  return new class LeafValue (*this, myConstraint
			      ? convert (number, *myConstraint)
			      : number);
}

bool
CardType::convert (card_t& number, card_t value, const class Constraint& c)
{
  number = 0;

  for (Constraint::const_iterator i = c.begin (); !(i == c.end ()); i++) {
    const class Range* r = *i;
    card_t lowLimit =
      card_t (static_cast<const class LeafValue&>(r->getLow ()));
    card_t highLimit =
      card_t (static_cast<const class LeafValue&>(r->getHigh ()));

    assert (lowLimit <= highLimit);

    if (value < lowLimit || value > highLimit) {
      if (card_t diff = highLimit - lowLimit + 1)
	number += diff;
      else
	return false;
    }
    else {
      number += value - lowLimit;
      assert (number <= c.getNumValues ());
      return true;
    }
  }

  return false;
}

card_t
CardType::convert (card_t number, const class Constraint& c)
{
  for (Constraint::const_iterator i = c.begin (); !(i == c.end ()); i++) {
    const class Range* r = *i;
    card_t lowLimit =
      card_t (static_cast<const class LeafValue&>(r->getLow ()));
    card_t highLimit =
      card_t (static_cast<const class LeafValue&>(r->getHigh ()));

    assert (lowLimit <= highLimit);

    card_t diff = highLimit - lowLimit;
    if (number <= diff)
      return lowLimit + number;
    else
      number -= diff, number--;
  }

  assert (false);
  return 0;
}

#ifdef EXPR_COMPILE
# include "StringBuffer.h"

void
CardType::compileDefinition (class StringBuffer& out,
			     unsigned indent) const
{
  out.indent (indent);
  out.append ("unsigned");
}

void
CardType::do_compileConversion (class StringBuffer& out,
				unsigned indent,
				const char* value,
				const char* number,
				bool add) const
{
  out.indent (indent);
  out.append (number);
  out.append (add ? "+=" : "=");
  out.append (value);
  out.append (";\n");
}

void
CardType::compileConv (class StringBuffer& out,
		       unsigned indent,
		       const class Constraint& c,
		       const char* value,
		       const char* number)
{
  bool next = false;

  for (Constraint::const_iterator i = c.end ();; next = true) {
    const class Range* range = *--i;
    const class Value& low = range->getLow ();
    const class Value& high = range->getHigh ();
    const bool notfirst = i != c.begin ();
    card_t num;
    if (!convert (num,
		  card_t (static_cast<const class LeafValue&>(low)), c))
      assert (false);
    if (notfirst) {
      out.indent (indent);
      if (next)
	out.append ("else ");
      out.append ("if (");
      if (&low == &high)
	low.compileEqual (out, next ? indent + 9 : indent + 4,
			  value, true, true, true);
      else
	low.compileOrder (out, next ? indent + 9 : indent + 4,
			  value, false, true, true, true);
      out.append (")\n");
      indent += 2;
    }
    else if (next)
      out.indent (indent), out.append ("else\n"), indent += 2;

    out.indent (indent);
    if (&low == &high) {
      out.append (number), out.append ("="), out.append (num);
      out.append (";\n");
    }
    else {
      out.append (number), out.append ("=");
      out.append ("(card_t) ("), out.append (value);
      out.append (" - ");
      low.compile (out);
      out.append (")");
      if (num)
	out.append ("+"), out.append (num);
      out.append (";\n");
    }

    if (notfirst || next)
      indent -= 2;
    if (!notfirst)
      break;
  }
}

void
CardType::compileReverseConv (class StringBuffer& out,
			      unsigned indent,
			      const class Constraint& c,
			      const char* number,
			      const char* value)
{
  bool next = false;
  for (Constraint::const_iterator i = c.end (); i != c.begin (); next = true) {
    const class Range* range = *--i;
    const class Value& low = range->getLow ();
    const class Value& high = range->getHigh ();
    card_t num;
    if (!convert (num,
		  card_t (static_cast<const class LeafValue&>(low)), c))
      assert (false);
    if (num) {
      out.indent (indent);
      if (next)
	out.append ("else ");
      out.append ("if ("), out.append (number);
      out.append (&low == &high ? "==" : ">="), out.append (num);
      out.append (")");
    }
    else if (next)
      out.indent (indent), out.append ("else");
    if (num || next)
      out.append (" {\n"), indent += 2;

    low.compileInit (value, indent, out);

    if (&low != &high) {
      out.indent (indent);
      out.append (value);
      out.append ("+=");
      out.append (number);
      if (num)
	out.append ("-"), out.append (num);
      out.append (";\n");
    }

    if (num || next)
      out.indent (indent -= 2), out.append ("}\n");
  }
}

#endif // EXPR_COMPILE