Generated on Thu Mar 7 2013 10:21:29 for Gecode by doxygen 1.8.3.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: 2009-09-30 21:59:39 +1000 (Wed, 30 Sep 2009) $ by $Author: tack $
11  * $Revision: 9784 $
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  assert(imp);
114  if (!imp->finalized()) {
115  imp->finalize();
116  }
117  }
118 
119  forceinline bool
120  TupleSet::finalized(void) const {
121  TupleSetI* imp = static_cast<TupleSetI*>(object());
122  assert(imp);
123  return imp->finalized();
124  }
125 
126  forceinline int
127  TupleSet::arity(void) const {
128  TupleSetI* imp = static_cast<TupleSetI*>(object());
129  assert(imp);
130  assert(imp->arity != -1);
131  return imp->arity;
132  }
133  forceinline int
134  TupleSet::tuples(void) const {
135  TupleSetI* imp = static_cast<TupleSetI*>(object());
136  assert(imp);
137  assert(imp->finalized());
138  return imp->size-1;
139  }
142  TupleSetI* imp = static_cast<TupleSetI*>(object());
143  assert(imp);
144  assert(imp->finalized());
145  return imp->data + i*imp->arity;
146  }
147  forceinline int
148  TupleSet::min(void) const {
149  TupleSetI* imp = static_cast<TupleSetI*>(object());
150  assert(imp);
151  assert(imp->finalized());
152  return imp->min;
153  }
154  forceinline int
155  TupleSet::max(void) const {
156  TupleSetI* imp = static_cast<TupleSetI*>(object());
157  assert(imp);
158  assert(imp->finalized());
159  return imp->max;
160  }
161 
162 
163  template<class Char, class Traits, class T>
164  std::basic_ostream<Char,Traits>&
165  operator <<(std::basic_ostream<Char,Traits>& os, const TupleSet& ts) {
166  std::basic_ostringstream<Char,Traits> s;
167  s.copyfmt(os); s.width(0);
168  s << "Number of tuples: " << ts.tuples() << std::endl
169  << "Tuples:" << std::endl;
170  for (int i = 0; i < ts.tuples(); ++i) {
171  s << '\t';
172  for (int j = 0; j < ts.arity(); ++j) {
173  s.width(3);
174  s << " " << ts[i][j];
175  }
176  s << std::endl;
177  }
178  return os << s.str();
179  }
180 
181 }
182 
183 // STATISTICS: int-prop
184