Generated on Thu Jan 16 2025 00:00:00 for Gecode by doxygen 1.14.0
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
34namespace Gecode { namespace Support {
35
41 template<class T, class A>
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:
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>
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
~DynamicQueue(void)
Release memory.
void push(const T &x)
Push element x to queue.
bool empty(void) const
Test whether queue is empty.
DynamicQueue(A &a)
Initialize queue.
T pop(void)
Pop element added first from queue and return it.
void reset(void)
Reset queue to be empty.
Support algorithms and datastructures
Gecode toplevel namespace
Post propagator for SetVar x
Definition set.hh:773
Gecode::FloatVal a(-8, 5)
Gecode::IntArgs i({1, 2, 3, 4})
#define forceinline
Definition config.hpp:194