Scippy

SCIP

Solving Constraint Integer Programs

stpvector.h
Go to the documentation of this file.
1 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2 /* */
3 /* This file is part of the program and library */
4 /* SCIP --- Solving Constraint Integer Programs */
5 /* */
6 /* Copyright (C) 2002-2022 Konrad-Zuse-Zentrum */
7 /* fuer Informationstechnik Berlin */
8 /* */
9 /* SCIP is distributed under the terms of the ZIB Academic License. */
10 /* */
11 /* You should have received a copy of the ZIB Academic License */
12 /* along with SCIP; see the file COPYING. If not visit scip.zib.de. */
13 /* */
14 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
15 
16 /**@file stpvector.h
17  * @brief header only, simple implementation of an STL like vector
18  * @author Daniel Rehfeldt
19  *
20  * STP vector (basically a much simplified replication of the C++ std::vector)
21  * Usage: define with
22  * STP_Vectype(template) myvec = NULL;
23  *
24  * for example:
25  * STP_Vectype(int) myvec = NULL;
26  *
27  * Vector needs to be freed!
28  *
29  */
30 
31 
32 /*---+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8----+----9----+----0----+----1----+----2*/
33 
34 
35 #ifndef APPLICATIONS_STP_SRC_STPVECTOR_H_
36 #define APPLICATIONS_STP_SRC_STPVECTOR_H_
37 
38 #include "scip/scip.h"
39 
40 #ifdef __cplusplus
41 extern "C" {
42 #endif
43 
44 #define STP_Vectype(type) type*
45 
46 
47 /** internal method */
48 #define vecinternalSetCapacity(vec, _size_) \
49  do \
50  { \
51  assert(_size_ >= 0); \
52  assert(vec); \
53  ((int*) (vec))[-2] = (_size_); \
54  } while( 0 )
55 
56 
57 /** internal method */
58 #define vecinternalSetSize(vec, _size_) \
59  do \
60  { \
61  assert(_size_ >= 0); \
62  assert(vec); \
63  ((int*) (vec))[-1] = (_size_); \
64  } while( 0 )
65 
66 
67 /** internal method */
68 #define vecinternalIncrementSize(vec) \
69  do \
70  { \
71  assert(vec); \
72  ((int*) (vec))[-1]++; \
73  } while( 0 )
74 
75 
76 /** internal method */
77 #define vecinternalDecrementSize(vec) \
78  do \
79  { \
80  assert(vec); \
81  assert(((int*) (vec))[-1] >= 1); \
82  ((int*) (vec))[-1]--; \
83  } while( 0 )
84 
85 
86 /** internal method */
87 #define vecinternalGetCapacity(vec) \
88  ((vec) ? ((int*) (vec))[-2] : (int) 0)
89 
90 
91 /** internal method */
92 #define vecinternalGetSize(vec) \
93  ((vec) ? ((int*) (vec))[-1] : (int) 0)
94 
95 
96 /** internal method */
97 #define vecinternalComputeNBytes(size, vec) \
98  (size * sizeof(*(vec)) + (sizeof(int) * 2))
99 
100 
101 /** internal method */
102 #define vecinternalIncreaseCapacity(scip, vec, cap) \
103  do \
104  { \
105  const int _nbytes_new_ = vecinternalComputeNBytes((cap), (vec)); \
106  assert(_nbytes_new_ >= ((int) sizeof(int) * 2)); \
107  if( !(vec) ) \
108  { \
109  char* _p_; \
110  SCIP_CALL_ABORT( SCIPallocBlockMemoryArray((scip), &_p_, _nbytes_new_) ); \
111  (vec) = (void*) (&_p_[sizeof(int) * 2]); \
112  vecinternalSetCapacity((vec), (cap)); \
113  vecinternalSetSize((vec), 0); \
114  } \
115  else \
116  { \
117  const int _cap_old_ = vecinternalGetCapacity((vec)); \
118  const int _nbytes_old_ = vecinternalComputeNBytes(_cap_old_, (vec)); \
119  char* _p_ = &((char*) (vec))[(int) sizeof(int) * (-2)]; \
120  assert(_nbytes_old_ < _nbytes_new_); \
121  SCIP_CALL_ABORT( SCIPreallocBlockMemoryArray((scip), &_p_, _nbytes_old_, _nbytes_new_) ); \
122  (vec) = (void*)(&_p_[sizeof(int) * 2]); \
123  vecinternalSetCapacity((vec), (cap)); \
124  } \
125  } while( 0 )
126 
127 
128 /** gets _cap_acity of the vector */
129 #define StpVecGetcapacity(vec) \
130  (vecinternalGetCapacity(vec))
131 
132 
133 /** removes all elements from vector */
134 #define StpVecClear(vec) \
135  vecinternalSetSize(vec, (int)0)
136 
137 
138 /** gets _size_ of the vector (number of elements) */
139 #define StpVecGetSize(vec) \
140  vecinternalGetSize(vec)
141 
142 
143 /** is the vector empty? */
144 #define StpVecIsEmpty(vec) \
145  (vecinternalGetSize(vec) == 0)
146 
147 
148 /** frees vector */
149 #define StpVecFree(scip, vec) \
150  do \
151  { \
152  if( (vec) ) \
153  { \
154  char* _p_ = &((char*) (vec))[(int) sizeof(int) * (-2)]; \
155  const int _nbytes_ = vecinternalComputeNBytes(vecinternalGetCapacity((vec)), (vec)); \
156  SCIPfreeBlockMemoryArray((scip), &_p_, _nbytes_); \
157  (vec) = NULL; \
158  } \
159  } while( 0 )
160 
161 
162 /** adds (appends) element */
163 #define StpVecPushBack(scip, vec, value) \
164  do \
165  { \
166  const int _cap_ = vecinternalGetCapacity((vec)); \
167  const int _size_ = vecinternalGetSize((vec)); \
168  if( _cap_ <= _size_ ) \
169  { \
170  vecinternalIncreaseCapacity((scip), (vec), ((_cap_ == 0) ? 2 : _cap_ * 2)); \
171  } \
172  vec[_size_] = (value); \
173  vecinternalIncrementSize((vec)); \
174  } while( 0 )
175 
176 
177 /** remove last element */
178 #define StpVecPopBack(vec) \
179  vecinternalDecrementSize((vec))
180 
181 /** reserves space */
182 #define StpVecReserve(scip, vec, _size_) \
183  do \
184  { \
185  const int _cap_ = vecinternalGetCapacity(vec); \
186  if( _cap_ < _size_ ) \
187  { \
188  vecinternalIncreaseCapacity(scip, (vec), _size_); \
189  } \
190  } while( 0 )
191 
192 
193 
194 #ifdef __cplusplus
195 }
196 #endif
197 
198 
199 #endif /* APPLICATIONS_STP_SRC_STPVECTOR_H_ */
SCIP callable library.