Generated on Tue Jan 28 2020 00:00:00 for Gecode by doxygen 1.8.17
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);
66  ~DynamicArray(void);
67 
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>
88  : a(a0), n(n0), x(a.template alloc<T>(n)) {}
89 
90  template<class T, class A>
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>
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>
105  a.free(x,n);
106  }
107 
108  template<class T, class A>
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
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&
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&
139  return operator [](static_cast<int>(i));
140  }
141 
142  template<class T, class A>
143  forceinline const T&
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>
158  return x;
159  }
160 
161 }}
162 
163 // STATISTICS: support-any
Post propagator for SetVar x
Definition: set.hh:784
static T * copy(T *d, const T *s, long unsigned int n)
Copy n objects starting at s to d.
Definition: heap.hpp:587
#define forceinline
Definition: config.hpp:173
Array with arbitrary number of elements.
Gecode::IntArgs i(4, 1, 2, 3, 4)
~DynamicArray(void)
Release memory.
Gecode toplevel namespace
Node * x
Pointer to corresponding Boolean expression node.
Definition: bool-expr.cpp:253
struct Gecode::@579::NNF::@61::@63 a
For atomic nodes.
T & operator[](int i)
Return element at position i (possibly resize)
IntSet * A
Position of a piece in a square board.
Heap heap
The single global heap.
Definition: heap.cpp:48
DynamicArray(A &a0, int n=32)
Initialize with size n.
const DynamicArray< T, A > & operator=(const DynamicArray< T, A > &da)
Assign array (copy elements from a)
int n
Number of negative literals for node type.
Definition: bool-expr.cpp:238
const FloatNum max
Largest allowed float value.
Definition: float.hh:848