Generated on Mon Jul 27 2020 00:00:00 for Gecode by doxygen 1.8.18
dynamic-queue.hpp
Go to the documentation of this file.
1 /* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
2 /*
3  * Main authors:
4  * Christian Schulte <schulte@gecode.org>
5  *
6  * Copyright:
7  * Christian Schulte, 2009
8  *
9  * This file is part of Gecode, the generic constraint
10  * development environment:
11  * http://www.gecode.org
12  *
13  * Permission is hereby granted, free of charge, to any person obtaining
14  * a copy of this software and associated documentation files (the
15  * "Software"), to deal in the Software without restriction, including
16  * without limitation the rights to use, copy, modify, merge, publish,
17  * distribute, sublicense, and/or sell copies of the Software, and to
18  * permit persons to whom the Software is furnished to do so, subject to
19  * the following conditions:
20  *
21  * The above copyright notice and this permission notice shall be
22  * included in all copies or substantial portions of the Software.
23  *
24  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31  *
32  */
33 
34 namespace Gecode { namespace Support {
35 
41  template<class T, class A>
42  class DynamicQueue {
43  private:
45  A& a;
47  int limit;
49  int fst;
51  int lst;
53  T* q;
55  void resize(void);
57  void move(int& i);
58  public:
60  DynamicQueue(A& a);
63 
65  bool empty(void) const;
66 
68  void reset(void);
69 
71  T pop(void);
73  void push(const T& x);
74  private:
76  static void* operator new(size_t s) throw() { (void) s; return NULL; }
78  static void operator delete(void* p) { (void) p; };
80  DynamicQueue(const DynamicQueue& s) : a(s.a) {}
82  const DynamicQueue& operator =(const DynamicQueue&) { return *this; }
83  };
84 
85 
86  template<class T, class A>
87  forceinline void
88  DynamicQueue<T,A>::move(int& i) {
89  i = (i+1) & (limit - 1);
90  }
91 
92  template<class T, class A>
93  void
94  DynamicQueue<T,A>::resize(void) {
95  assert(fst == lst);
96  T* nq = a.template alloc<T>(limit << 1);
97  int j=0;
98  for (int i = fst; i<limit; i++)
99  nq[j++] = q[i];
100  for (int i = 0; i<lst; i++)
101  nq[j++] = q[i];
102  a.template free<T>(q,limit);
103  q = nq;
104  fst = 0;
105  lst = limit;
106  limit <<= 1;
107  }
108 
109  template<class T, class A>
112  : a(a0), limit(8), fst(0), lst(0), q(a.template alloc<T>(limit)) {}
113 
114  template<class T, class A>
117  a.free(q,limit);
118  }
119 
120  template<class T, class A>
121  forceinline bool
123  return fst == lst;
124  }
125 
126  template<class T, class A>
127  forceinline void
129  fst = lst = 0;
130  }
131 
132  template<class T, class A>
133  forceinline T
135  assert(!empty());
136  T t = q[fst];
137  move(fst);
138  return t;
139  }
140 
141  template<class T, class A>
142  forceinline void
144  q[lst] = x;
145  move(lst);
146  if (fst == lst)
147  resize();
148  }
149 
150 }}
151 
152 // STATISTICS: support-any
Post propagator for SetVar x
Definition: set.hh:767
~DynamicQueue(void)
Release memory.
NodeType t
Type of node.
Definition: bool-expr.cpp:230
T pop(void)
Pop element added first from queue and return it.
Gecode toplevel namespace
void push(const T &x)
Push element x to queue.
bool empty(void) const
Test whether queue is empty.
void reset(void)
Reset queue to be empty.
DynamicQueue(A &a)
Initialize queue.
struct Gecode::@602::NNF::@65::@67 a
For atomic nodes.
Queue with arbitrary number of elements.
#define forceinline
Definition: config.hpp:185
Gecode::IntArgs i({1, 2, 3, 4})
int p
Number of positive literals for node type.
Definition: bool-expr.cpp:232