main page
modules
namespaces
classes
files
Gecode home
Generated on Tue Jan 28 2020 00:00:00 for Gecode by
doxygen
1.8.17
gecode
support
dynamic-array.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, 2002
8
*
9
* Last modified:
10
* $Date: 2016-06-18 14:20:49 +0200 (Sat, 18 Jun 2016) $ by $Author: schulte $
11
* $Revision: 15118 $
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 <algorithm>
39
40
namespace
Gecode
{
namespace
Support {
41
47
template
<
class
T,
class
A>
48
class
DynamicArray
{
49
private
:
51
A& a;
53
int
n;
55
T* x;
57
void
resize(
int
n
);
58
public
:
60
DynamicArray
(A& a0,
int
n
= 32);
62
DynamicArray
(A& a0,
unsigned
int
n
);
64
DynamicArray
(
const
DynamicArray<T,A>
& da);
66
~DynamicArray
(
void
);
67
69
const
DynamicArray<T,A>
&
operator =
(
const
DynamicArray<T,A>
& da);
70
72
T&
operator []
(
int
i
);
74
T&
operator []
(
unsigned
int
i
);
76
const
T&
operator []
(
int
i
)
const
;
78
const
T&
operator []
(
unsigned
int
i
)
const
;
79
81
operator
T*(void);
82
};
83
84
85
template
<
class
T,
class
A>
86
forceinline
87
DynamicArray<T,A>::DynamicArray
(A& a0,
int
n0)
88
:
a
(a0),
n
(n0),
x
(
a
.template alloc<T>(
n
)) {}
89
90
template
<
class
T,
class
A>
91
forceinline
92
DynamicArray<T,A>::DynamicArray
(
A
& a0,
unsigned
int
n0)
93
:
a
(a0),
n
(static_cast<int>(n0)),
x
(
a
.template alloc<T>(
n
)) {}
94
95
template
<
class
T,
class
A>
96
forceinline
97
DynamicArray<T,A>::DynamicArray
(
const
DynamicArray<T,A>
& da)
98
:
a
(da.
a
),
n
(da.
n
),
x
(
a
.template alloc<T>(
n
)) {
99
(void)
heap
.
copy
<T>(
x
,da.x,
n
);
100
}
101
102
template
<
class
T,
class
A>
103
forceinline
104
DynamicArray<T,A>::~DynamicArray
(
void
) {
105
a
.free(
x
,
n
);
106
}
107
108
template
<
class
T,
class
A>
109
forceinline
const
DynamicArray<T,A>
&
110
DynamicArray<T,A>::operator =
(
const
DynamicArray<T,A>
& da) {
111
if
(
this
!= &da) {
112
if
(
n
< da.n) {
113
a
.free(
x
,
n
);
n
= da.n;
x
=
a
.template alloc<T>(
n
);
114
}
115
(void)
heap
.
copy
(
x
,da.x,
n
);
116
}
117
return
*
this
;
118
}
119
120
template
<
class
T,
class
A>
121
void
122
DynamicArray<T,A>::resize
(
int
i
) {
123
int
m =
std::max
(
i
+1, (3*
n
)/2);
124
x
=
a
.realloc(
x
,
n
,m);
125
n
= m;
126
}
127
128
template
<
class
T,
class
A>
129
forceinline
T&
130
DynamicArray<T,A>::operator []
(
int
i
) {
131
if
(
i
>=
n
) resize(
i
);
132
assert(
n
>
i
);
133
return
x
[
i
];
134
}
135
136
template
<
class
T,
class
A>
137
forceinline
T&
138
DynamicArray<T,A>::operator []
(
unsigned
int
i
) {
139
return
operator [](
static_cast<
int
>
(
i
));
140
}
141
142
template
<
class
T,
class
A>
143
forceinline
const
T&
144
DynamicArray<T,A>::operator []
(
int
i
)
const
{
145
assert(
n
>
i
);
146
return
x
[
i
];
147
}
148
149
template
<
class
T,
class
A>
150
forceinline
const
T&
151
DynamicArray<T,A>::operator []
(
unsigned
int
i
)
const
{
152
return
operator [](
static_cast<
int
>
(
i
));
153
}
154
155
template
<
class
T,
class
A>
156
forceinline
157
DynamicArray<T,A>::operator
T*(void) {
158
return
x
;
159
}
160
161
}}
162
163
// STATISTICS: support-any
Gecode::x
Post propagator for SetVar x
Definition:
set.hh:784
Gecode::Heap::copy
static T * copy(T *d, const T *s, long unsigned int n)
Copy n objects starting at s to d.
Definition:
heap.hpp:587
forceinline
#define forceinline
Definition:
config.hpp:173
Gecode::Support::DynamicArray
Array with arbitrary number of elements.
Definition:
dynamic-array.hpp:48
Test::Int::Basic::i
Gecode::IntArgs i(4, 1, 2, 3, 4)
Gecode::Support::DynamicArray::~DynamicArray
~DynamicArray(void)
Release memory.
Definition:
dynamic-array.hpp:104
Gecode
Gecode toplevel namespace
x
Node * x
Pointer to corresponding Boolean expression node.
Definition:
bool-expr.cpp:253
a
struct Gecode::@579::NNF::@61::@63 a
For atomic nodes.
Gecode::Support::DynamicArray::operator[]
T & operator[](int i)
Return element at position i (possibly resize)
Definition:
dynamic-array.hpp:130
QueenArmies::A
IntSet * A
Position of a piece in a square board.
Definition:
queen-armies.cpp:50
Gecode::heap
Heap heap
The single global heap.
Definition:
heap.cpp:48
Gecode::Support::DynamicArray::DynamicArray
DynamicArray(A &a0, int n=32)
Initialize with size n.
Definition:
dynamic-array.hpp:87
Gecode::Support::DynamicArray::operator=
const DynamicArray< T, A > & operator=(const DynamicArray< T, A > &da)
Assign array (copy elements from a)
Definition:
dynamic-array.hpp:110
n
int n
Number of negative literals for node type.
Definition:
bool-expr.cpp:238
Gecode::Float::Limits::max
const FloatNum max
Largest allowed float value.
Definition:
float.hh:848