main page
modules
namespaces
classes
files
Gecode home
Generated on Thu Mar 13 2014 04:39:36 for Gecode by
doxygen
1.8.1.2
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: 2009-09-09 05:10:29 +1000 (Wed, 09 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
#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
(
const
DynamicArray<T,A>
& da);
64
~DynamicArray
(
void
);
65
67
const
DynamicArray<T,A>
&
operator =
(
const
DynamicArray<T,A>
& da);
68
70
T&
operator []
(
int
i
);
72
const
T&
operator []
(
int
)
const
;
73
75
operator
T*(void);
76
};
77
78
79
template
<
class
T,
class
A>
80
forceinline
81
DynamicArray<T,A>::DynamicArray
(A& a0,
int
n0)
82
:
a
(a0), n(n0), x(
a
.template alloc<T>(n)) {}
83
84
template
<
class
T,
class
A>
85
forceinline
86
DynamicArray<T,A>::DynamicArray
(
const
DynamicArray<T,A>
& da)
87
:
a
(da.
a
), n(da.n), x(
a
.template alloc<T>(n)) {
88
(void)
heap
.
copy
<T>(x,da.x,n);
89
}
90
91
template
<
class
T,
class
A>
92
forceinline
93
DynamicArray<T,A>::~DynamicArray
(
void
) {
94
a
.free(x,n);
95
}
96
97
template
<
class
T,
class
A>
98
forceinline
const
DynamicArray<T,A>
&
99
DynamicArray<T,A>::operator =
(
const
DynamicArray<T,A>
& da) {
100
if
(
this
!= &da) {
101
if
(n < da.n) {
102
a
.free(x,n); n = da.n; x =
a
.template alloc<T>(n);
103
}
104
(void)
heap
.
copy
(x,da.x,n);
105
}
106
return
*
this
;
107
}
108
109
template
<
class
T,
class
A>
110
void
111
DynamicArray<T,A>::resize
(
int
i
) {
112
int
m
=
std::max
(i+1, (3*n)/2);
113
x =
a
.realloc(x,n,m);
114
n =
m
;
115
}
116
117
template
<
class
T,
class
A>
118
forceinline
T&
119
DynamicArray<T,A>::operator []
(
int
i) {
120
if
(i >= n) resize(i);
121
assert(n > i);
122
return
x[
i
];
123
}
124
125
template
<
class
T,
class
A>
126
forceinline
const
T&
127
DynamicArray<T,A>::operator []
(
int
i)
const
{
128
assert(n > i);
129
return
x[
i
];
130
}
131
132
template
<
class
T,
class
A>
133
forceinline
134
DynamicArray<T,A>::operator
T*(void) {
135
return
x;
136
}
137
138
}}
139
140
// STATISTICS: support-any