Generated on Tue Jan 26 2021 00:00:00 for Gecode by doxygen 1.9.1
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);
63 
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>
84  : a(a0), n(n0), x(a.template alloc<T>(n)) {}
85 
86  template<class T, class A>
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>
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>
101  a.free(x,n);
102  }
103 
104  template<class T, class A>
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
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&
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&
135  return operator [](static_cast<int>(i));
136  }
137 
138  template<class T, class A>
139  forceinline const T&
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>
154  return x;
155  }
156 
157 }}
158 
159 // STATISTICS: support-any
int n
Number of negative literals for node type.
Definition: bool-expr.cpp:234
Node * x
Pointer to corresponding Boolean expression node.
Definition: bool-expr.cpp:249
struct Gecode::@602::NNF::@65::@67 a
For atomic nodes.
static T * copy(T *d, const T *s, long unsigned int n)
Copy n objects starting at s to d.
Definition: heap.hpp:583
Array with arbitrary number of elements.
DynamicArray(A &a0, unsigned int n)
Initialize with size n.
T & operator[](int i)
Return element at position i (possibly resize)
~DynamicArray(void)
Release memory.
DynamicArray(const DynamicArray< T, A > &da)
Copy elements from array da.
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)
Heap heap
The single global heap.
Definition: heap.cpp:44
const FloatNum max
Largest allowed float value.
Definition: float.hh:844
Gecode::IntArgs i({1, 2, 3, 4})
#define forceinline
Definition: config.hpp:185