main page
modules
namespaces
classes
files
Gecode home
Generated on Mon Jul 27 2020 00:00:00 for Gecode by
doxygen
1.8.18
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
* 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
#include <algorithm>
35
36
namespace
Gecode
{
namespace
Support {
37
43
template
<
class
T,
class
A>
44
class
DynamicArray
{
45
private
:
47
A& a;
49
int
n;
51
T* x;
53
void
resize(
int
n
);
54
public
:
56
DynamicArray
(A& a0,
int
n = 32);
58
DynamicArray
(A& a0,
unsigned
int
n);
60
DynamicArray
(
const
DynamicArray<T,A>
& da);
62
~DynamicArray
(
void
);
63
65
const
DynamicArray<T,A>
&
operator =
(
const
DynamicArray<T,A>
& da);
66
68
T&
operator []
(
int
i
);
70
T&
operator []
(
unsigned
int
i
);
72
const
T&
operator []
(
int
i
)
const
;
74
const
T&
operator []
(
unsigned
int
i
)
const
;
75
77
operator
T*(void);
78
};
79
80
81
template
<
class
T,
class
A>
82
forceinline
83
DynamicArray<T,A>::DynamicArray
(A& a0,
int
n0)
84
:
a
(a0),
n
(n0),
x
(
a
.template alloc<T>(
n
)) {}
85
86
template
<
class
T,
class
A>
87
forceinline
88
DynamicArray<T,A>::DynamicArray
(A& a0,
unsigned
int
n0)
89
:
a
(a0),
n
(static_cast<int>(n0)),
x
(
a
.template alloc<T>(
n
)) {}
90
91
template
<
class
T,
class
A>
92
forceinline
93
DynamicArray<T,A>::DynamicArray
(
const
DynamicArray<T,A>
& da)
94
:
a
(da.
a
),
n
(da.
n
),
x
(
a
.template alloc<T>(
n
)) {
95
(void)
heap
.
copy
<T>(x,da.x,n);
96
}
97
98
template
<
class
T,
class
A>
99
forceinline
100
DynamicArray<T,A>::~DynamicArray
(
void
) {
101
a
.free(
x
,
n
);
102
}
103
104
template
<
class
T,
class
A>
105
forceinline
const
DynamicArray<T,A>
&
106
DynamicArray<T,A>::operator =
(
const
DynamicArray<T,A>
& da) {
107
if
(
this
!= &da) {
108
if
(
n
< da.n) {
109
a
.free(
x
,
n
);
n
= da.n;
x
=
a
.template alloc<T>(
n
);
110
}
111
(void)
heap
.
copy
(
x
,da.x,
n
);
112
}
113
return
*
this
;
114
}
115
116
template
<
class
T,
class
A>
117
void
118
DynamicArray<T,A>::resize
(
int
i
) {
119
int
m =
std::max
(
i
+1, (3*
n
)/2);
120
x
=
a
.realloc(
x
,
n
,m);
121
n
= m;
122
}
123
124
template
<
class
T,
class
A>
125
forceinline
T&
126
DynamicArray<T,A>::operator []
(
int
i
) {
127
if
(
i
>=
n
) resize(
i
);
128
assert(
n
>
i
);
129
return
x
[
i
];
130
}
131
132
template
<
class
T,
class
A>
133
forceinline
T&
134
DynamicArray<T,A>::operator []
(
unsigned
int
i
) {
135
return
operator [](
static_cast<
int
>
(
i
));
136
}
137
138
template
<
class
T,
class
A>
139
forceinline
const
T&
140
DynamicArray<T,A>::operator []
(
int
i
)
const
{
141
assert(
n
>
i
);
142
return
x
[
i
];
143
}
144
145
template
<
class
T,
class
A>
146
forceinline
const
T&
147
DynamicArray<T,A>::operator []
(
unsigned
int
i
)
const
{
148
return
operator [](
static_cast<
int
>
(
i
));
149
}
150
151
template
<
class
T,
class
A>
152
forceinline
153
DynamicArray<T,A>::operator
T*(void) {
154
return
x
;
155
}
156
157
}}
158
159
// STATISTICS: support-any
Gecode::x
Post propagator for SetVar x
Definition:
set.hh:767
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:583
Gecode::Support::DynamicArray
Array with arbitrary number of elements.
Definition:
dynamic-array.hpp:44
Gecode::Support::DynamicArray::~DynamicArray
~DynamicArray(void)
Release memory.
Definition:
dynamic-array.hpp:100
Gecode::Support::DynamicArray::DynamicArray
DynamicArray(const DynamicArray< T, A > &da)
Copy elements from array da.
Definition:
dynamic-array.hpp:93
Gecode
Gecode toplevel namespace
Gecode::Support::DynamicArray::DynamicArray
DynamicArray(A &a0, unsigned int n)
Initialize with size n.
Definition:
dynamic-array.hpp:88
Gecode::Support::DynamicArray::operator[]
T & operator[](int i)
Return element at position i (possibly resize)
Definition:
dynamic-array.hpp:126
a
struct Gecode::@602::NNF::@65::@67 a
For atomic nodes.
Gecode::heap
Heap heap
The single global heap.
Definition:
heap.cpp:44
forceinline
#define forceinline
Definition:
config.hpp:185
Gecode::Support::DynamicArray::DynamicArray
DynamicArray(A &a0, int n=32)
Initialize with size n.
Definition:
dynamic-array.hpp:83
Gecode::Support::DynamicArray::operator=
const DynamicArray< T, A > & operator=(const DynamicArray< T, A > &da)
Assign array (copy elements from a)
Definition:
dynamic-array.hpp:106
n
int n
Number of negative literals for node type.
Definition:
bool-expr.cpp:234
Test::Int::Basic::i
Gecode::IntArgs i({1, 2, 3, 4})
Gecode::Float::Limits::max
const FloatNum max
Largest allowed float value.
Definition:
float.hh:844