Generated on Sat Feb 7 2015 02:01:31 for Gecode by doxygen 1.8.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  * Last modified:
10  * $Date: 2009-09-08 21:10:29 +0200 (Tue, 08 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);
64  ~DynamicArray(void);
65 
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>
82  : a(a0), n(n0), x(a.template alloc<T>(n)) {}
83 
84  template<class T, class A>
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>
94  a.free(x,n);
95  }
96 
97  template<class T, class A>
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
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&
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&
128  assert(n > i);
129  return x[i];
130  }
131 
132  template<class T, class A>
135  return x;
136  }
137 
138 }}
139 
140 // STATISTICS: support-any
const DynamicArray< T, A > & operator=(const DynamicArray< T, A > &da)
Assign array (copy elements from a)
static T * copy(T *d, const T *s, long unsigned int n)
Copy n objects starting at s to d.
Definition: heap.hpp:552
const FloatNum max
Largest allowed float value.
Definition: float.hh:831
Array with arbitrary number of elements.
Heap heap
The single global heap.
Definition: heap.cpp:49
DynamicArray(A &a0, int n=32)
Initialize with size n.
T & operator[](int i)
Return element at position i (possibly resize)
Gecode::IntArgs i(4, 1, 2, 3, 4)
int n
Number of negative literals for node type.
Definition: bool-expr.cpp:238
Node * x
Pointer to corresponding Boolean expression node.
Definition: bool-expr.cpp:253
#define forceinline
Definition: config.hpp:132
~DynamicArray(void)
Release memory.
Gecode toplevel namespace
struct Gecode::@518::NNF::@57::@59 a
For atomic nodes.