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 /** gets top element of the vector */
143 #define StpVecTop(vec) \
144  vec[StpVecGetSize(vec) - 1]
145 
146 
147 /** is the vector empty? */
148 #define StpVecIsEmpty(vec) \
149  (vecinternalGetSize(vec) == 0)
150 
151 
152 /** frees vector */
153 #define StpVecFree(scip, vec) \
154  do \
155  { \
156  if( (vec) ) \
157  { \
158  char* _p_ = &((char*) (vec))[(int) sizeof(int) * (-2)]; \
159  const int _nbytes_ = vecinternalComputeNBytes(vecinternalGetCapacity((vec)), (vec)); \
160  SCIPfreeBlockMemoryArray((scip), &_p_, _nbytes_); \
161  (vec) = NULL; \
162  } \
163  } while( 0 )
164 
165 
166 /** adds (appends) element */
167 #define StpVecPushBack(scip, vec, value) \
168  do \
169  { \
170  const int _cap_ = vecinternalGetCapacity((vec)); \
171  const int _size_ = vecinternalGetSize((vec)); \
172  if( _cap_ <= _size_ ) \
173  { \
174  vecinternalIncreaseCapacity((scip), (vec), ((_cap_ == 0) ? 2 : _cap_ * 2)); \
175  } \
176  vec[_size_] = (value); \
177  vecinternalIncrementSize((vec)); \
178  } while( 0 )
179 
180 
181 /** remove last element */
182 #define StpVecPopBack(vec) \
183  vecinternalDecrementSize((vec))
184 
185 /** reserves space */
186 #define StpVecReserve(scip, vec, _size_) \
187  do \
188  { \
189  const int _cap_ = vecinternalGetCapacity(vec); \
190  if( _cap_ < _size_ ) \
191  { \
192  vecinternalIncreaseCapacity(scip, (vec), _size_); \
193  } \
194  } while( 0 )
195 
196 
197 
198 #ifdef __cplusplus
199 }
200 #endif
201 
202 
203 #endif /* APPLICATIONS_STP_SRC_STPVECTOR_H_ */
SCIP callable library.