Generated on Sat Feb 7 2015 02:01:23 for Gecode by doxygen 1.8.9.1
tuple-set.hpp
Go to the documentation of this file.
1 /* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
2 /*
3  * Main authors:
4  * Mikael Lagerkvist <lagerkvist@gecode.org>
5  *
6  * Copyright:
7  * Mikael Lagerkvist, 2007
8  *
9  * Last modified:
10  * $Date: 2012-04-11 02:34:42 +0200 (Wed, 11 Apr 2012) $ by $Author: tack $
11  * $Revision: 12728 $
12  *
13  * This file is part of Gecode, the generic constraint
14  * development environment:
15  * http://www.gecode.org
16  *
17  * Permission is hereby granted, free of charge, to any person obtaining
18  * a copy of this software and associated documentation files (the
19  * "Software"), to deal in the Software without restriction, including
20  * without limitation the rights to use, copy, modify, merge, publish,
21  * distribute, sublicense, and/or sell copies of the Software, and to
22  * permit persons to whom the Software is furnished to do so, subject to
23  * the following conditions:
24  *
25  * The above copyright notice and this permission notice shall be
26  * included in all copies or substantial portions of the Software.
27  *
28  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
29  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
30  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
31  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
32  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
33  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
34  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
35  *
36  */
37 
38 #include <sstream>
39 
40 namespace Gecode {
41 
42  forceinline bool
44  assert(((excess == -1) && (domsize > 0)) ||
45  ((excess != -1) && (domsize == 0)));
46  return excess == -1;
47  }
48 
51  : arity(-1),
52  size(0),
53  tuples(NULL),
54  tuple_data(NULL),
55  data(NULL),
56  excess(0),
57  min(Int::Limits::max),
58  max(Int::Limits::min),
59  domsize(0),
60  last(NULL),
61  nullpointer(NULL)
62  {}
63 
64 
65  template<class T>
66  void
68  assert(arity != -1); // Arity has been set
69  assert(excess != -1); // Tuples may still be added
70  if (excess == 0) resize();
71  assert(excess >= 0);
72  --excess;
73  int end = size*arity;
74  for (int i = arity; i--; ) {
75  data[end+i] = t[i];
76  if (t[i] < min) min = t[i];
77  if (t[i] > max) max = t[i];
78  }
79  ++size;
80  }
81 
84  }
85 
88  : SharedHandle(ts) {}
89 
92  TupleSetI* imp = static_cast<TupleSetI*>(object());
93  assert(imp);
94  return imp;
95  }
96 
97  inline void
98  TupleSet::add(const IntArgs& tuple) {
99  TupleSetI* imp = static_cast<TupleSetI*>(object());
100  if (imp == NULL) {
101  imp = new TupleSetI;
102  object(imp);
103  }
104  assert(imp->arity == -1 ||
105  imp->arity == tuple.size());
106  imp->arity = tuple.size();
107  imp->add(tuple);
108  }
109 
110  forceinline void
112  TupleSetI* imp = static_cast<TupleSetI*>(object());
113  if (imp == NULL) {
114  imp = new TupleSetI;
115  imp->arity = 0;
116  imp->excess = -1;
117  imp->domsize = 1;
118  imp->size = 1;
119  object(imp);
120  }
121  if (!imp->finalized()) {
122  imp->finalize();
123  }
124  }
125 
126  forceinline bool
127  TupleSet::finalized(void) const {
128  TupleSetI* imp = static_cast<TupleSetI*>(object());
129  assert(imp);
130  return imp->finalized();
131  }
132 
133  forceinline int
134  TupleSet::arity(void) const {
135  TupleSetI* imp = static_cast<TupleSetI*>(object());
136  assert(imp);
137  assert(imp->arity != -1);
138  return imp->arity;
139  }
140  forceinline int
141  TupleSet::tuples(void) const {
142  TupleSetI* imp = static_cast<TupleSetI*>(object());
143  assert(imp);
144  assert(imp->finalized());
145  return imp->size-1;
146  }
149  TupleSetI* imp = static_cast<TupleSetI*>(object());
150  assert(imp);
151  assert(imp->finalized());
152  return imp->data + i*imp->arity;
153  }
154  forceinline int
155  TupleSet::min(void) const {
156  TupleSetI* imp = static_cast<TupleSetI*>(object());
157  assert(imp);
158  assert(imp->finalized());
159  return imp->min;
160  }
161  forceinline int
162  TupleSet::max(void) const {
163  TupleSetI* imp = static_cast<TupleSetI*>(object());
164  assert(imp);
165  assert(imp->finalized());
166  return imp->max;
167  }
168 
169 
170  template<class Char, class Traits, class T>
171  std::basic_ostream<Char,Traits>&
172  operator <<(std::basic_ostream<Char,Traits>& os, const TupleSet& ts) {
173  std::basic_ostringstream<Char,Traits> s;
174  s.copyfmt(os); s.width(0);
175  s << "Number of tuples: " << ts.tuples() << std::endl
176  << "Tuples:" << std::endl;
177  for (int i = 0; i < ts.tuples(); ++i) {
178  s << '\t';
179  for (int j = 0; j < ts.arity(); ++j) {
180  s.width(3);
181  s << " " << ts[i][j];
182  }
183  s << std::endl;
184  }
185  return os << s.str();
186  }
187 
188 }
189 
190 // STATISTICS: int-prop
191 
NodeType t
Type of node.
Definition: bool-expr.cpp:234
The shared handle.
Definition: core.hpp:79
int min(void) const
Minimum domain element.
Definition: tuple-set.hpp:155
void finalize(void)
Finalize tuple set.
Definition: tuple-set.hpp:111
int size(void) const
Return size of array (number of elements)
Definition: array.hpp:1662
int arity(void) const
Arity of tuple set.
Definition: tuple-set.hpp:134
int excess
Excess storage.
Definition: int.hh:2048
int * Tuple
Type of a tuple.
Definition: int.hh:2028
int tuples(void) const
Number of tuples.
Definition: tuple-set.hpp:141
Reify imp(BoolVar x)
Use implication for reification.
Definition: reify.hpp:77
Gecode::IntArgs i(4, 1, 2, 3, 4)
unsigned int size(I &i)
Size of all ranges of range iterator i.
unsigned int domsize
Domain size.
Definition: int.hh:2052
TupleSetI(void)
Initialize as empty tuple set.
Definition: tuple-set.hpp:50
Tuple operator[](int i) const
Get tuple i.
Definition: tuple-set.hpp:148
int min
Minimum and maximum in domain-values.
Definition: int.hh:2050
Data stored for a Table.
Definition: int.hh:2034
bool finalized(void) const
Is datastructure finalized.
Definition: tuple-set.hpp:43
int * data
Tuples data.
Definition: int.hh:2046
Passing integer arguments.
Definition: int.hh:607
int max(void) const
Maximum domain element.
Definition: tuple-set.hpp:162
Class represeting a set of tuples.
Definition: int.hh:2022
TupleSet(void)
Construct empty tuple set.
Definition: tuple-set.hpp:83
bool finalized(void) const
Is tuple set finalized.
Definition: tuple-set.hpp:127
void add(T t)
Add Tuple. Assumes that arity matches.
Definition: tuple-set.hpp:67
SharedHandle::Object * object(void) const
Access to the shared object.
Definition: core.hpp:2619
void finalize(void)
Finalize datastructure (disallows additions of more Tuples)
Definition: tuple-set.cpp:96
#define forceinline
Definition: config.hpp:132
int size
Number of Tuples.
Definition: int.hh:2040
Gecode toplevel namespace
void add(const IntArgs &tuple)
Add tuple to tuple set.
Definition: tuple-set.hpp:98
TupleSetI * implementation(void)
Get implementation.
Definition: tuple-set.hpp:91