main page
modules
namespaces
classes
files
Gecode home
Generated on Sat May 25 2013 18:00:41 for Gecode by
doxygen
1.8.3.1
gecode
support
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
* Last modified:
10
* $Date: 2009-09-08 21:10:29 +0200 (Tue, 08 Sep 2009) $ by $Author: schulte $
11
* $Revision: 9692 $
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
namespace
Gecode {
namespace
Support {
39
45
template
<
class
T,
class
A>
46
class
DynamicQueue
{
47
private
:
49
A& a;
51
int
limit;
53
int
fst;
55
int
lst;
57
T* q;
59
void
resize(
void
);
61
void
move(
int
&
i
);
62
public
:
64
DynamicQueue
(A& a);
66
~DynamicQueue
(
void
);
67
69
bool
empty
(
void
)
const
;
70
72
void
reset
(
void
);
73
75
T
pop
(
void
);
77
void
push
(
const
T&
x
);
78
private
:
80
static
void
*
operator
new
(
size_t
s)
throw
() { (void) s;
return
NULL; }
82
static
void
operator
delete
(
void
*
p
) { (void)
p
; };
84
DynamicQueue
(
const
DynamicQueue
& s) : a(s.a) {}
86
const
DynamicQueue
& operator =(
const
DynamicQueue
&) {
return
*
this
; }
87
};
88
89
90
template
<
class
T,
class
A>
91
forceinline
void
92
DynamicQueue<T,A>::move
(
int
&
i
) {
93
i = (i+1) & (limit - 1);
94
}
95
96
template
<
class
T,
class
A>
97
void
98
DynamicQueue<T,A>::resize(
void
) {
99
assert(fst == lst);
100
T* nq =
a
.template alloc<T>(limit << 1);
101
int
j=0;
102
for
(
int
i = fst; i<limit; i++)
103
nq[j++] = q[i];
104
for
(
int
i = 0; i<lst; i++)
105
nq[j++] = q[i];
106
a
.template free<T>(q,limit);
107
q = nq;
108
fst = 0;
109
lst = limit;
110
limit <<= 1;
111
}
112
113
template
<
class
T,
class
A>
114
forceinline
115
DynamicQueue<T,A>::DynamicQueue
(A& a0)
116
:
a
(a0), limit(8), fst(0), lst(0), q(
a
.template alloc<T>(limit)) {}
117
118
template
<
class
T,
class
A>
119
forceinline
120
DynamicQueue<T,A>::~DynamicQueue
(
void
) {
121
a
.free(q,limit);
122
}
123
124
template
<
class
T,
class
A>
125
forceinline
bool
126
DynamicQueue<T,A>::empty
(
void
)
const
{
127
return
fst == lst;
128
}
129
130
template
<
class
T,
class
A>
131
forceinline
void
132
DynamicQueue<T,A>::reset
(
void
) {
133
fst = lst = 0;
134
}
135
136
template
<
class
T,
class
A>
137
forceinline
T
138
DynamicQueue<T,A>::pop
(
void
) {
139
assert(!empty());
140
T
t
= q[fst];
141
move(fst);
142
return
t
;
143
}
144
145
template
<
class
T,
class
A>
146
forceinline
void
147
DynamicQueue<T,A>::push
(
const
T&
x
) {
148
q[lst] =
x
;
149
move(lst);
150
if
(fst == lst)
151
resize();
152
}
153
154
}}
155
156
// STATISTICS: support-any