PaStiX Handbook  6.3.2
extendVector.c
Go to the documentation of this file.
1 /**
2  *
3  * @file extendVector.c
4  *
5  * PaStiX analyse functions for the extend vector structure.
6  *
7  * @copyright 1998-2023 Bordeaux INP, CNRS (LaBRI UMR 5800), Inria,
8  * Univ. Bordeaux. All rights reserved.
9  *
10  * @version 6.3.2
11  * @author Pascal Henon
12  * @author Mathieu Faverge
13  * @date 2023-07-21
14  *
15  * @addtogroup blend_dev_extint
16  * @{
17  *
18  **/
19 #include <stdio.h>
20 #include <string.h>
21 #include <assert.h>
22 
23 #include "common.h"
24 #include "extendVector.h"
25 
26 /**
27  *******************************************************************************
28  *
29  * @brief Initialize the extendVector structure with the initial size given.
30  *
31  *******************************************************************************
32  *
33  * @param[inout] vec
34  * The allocated pointer to initialize.
35  *
36  * @param[in] size
37  * The initial size of the vector.
38  *
39  *******************************************************************************
40  *
41  * @return The pointer to the internal integer array.
42  *
43  *******************************************************************************/
46 {
47  vec->vecsize = size;
48  vec->eltnbr = 0;
49  vec->inttab = NULL;
50  MALLOC_INTERN(vec->inttab, size, pastix_int_t);
51  return vec->inttab;
52 }
53 
54 /**
55  *******************************************************************************
56  *
57  * @brief Free the extendVector structure.
58  *
59  *******************************************************************************
60  *
61  * @param[inout] vec
62  * The allocated pointer to free.
63  *
64  *******************************************************************************/
65 void
67 {
68  if(vec->inttab != NULL) {
69  memFree_null(vec->inttab);
70  }
71  /*memFree_null(vec);*/
72 }
73 
74 /**
75  *******************************************************************************
76  *
77  * @brief Add an element elt to the end of the vector.
78  *
79  *******************************************************************************
80  *
81  * @param[inout] vec
82  * The pointer to the extend vector. Might be extended if there is not
83  * enough space..
84  *
85  * @param[in] elt
86  * The value to store in the vector
87  *
88  *******************************************************************************/
89 void
91 {
92  vec->inttab[vec->eltnbr] = elt;
93  extendint_incr(vec);
94 }
95 
96 /**
97  *******************************************************************************
98  *
99  * @brief Return the number of element stored in the vector.
100  *
101  *******************************************************************************
102  *
103  * @param[in] vec
104  * The extend vector structure.
105  *
106  *******************************************************************************
107  *
108  * @return The number of element stored.
109  *
110  *******************************************************************************/
113 {
114  return vec->eltnbr;
115 }
116 
117 /**
118  *******************************************************************************
119  *
120  * @brief Return the element of index eltnum.
121  *
122  *******************************************************************************
123  *
124  * @param[in] vec
125  * The extend vector structure.
126  *
127  * @param[in] eltnum
128  * The index of the element to return.
129  *
130  *******************************************************************************
131  *
132  * @return The value of the eltnum^th element.
133  *
134  *******************************************************************************/
137 {
138  assert(eltnum < vec->eltnbr);
139  return vec->inttab[eltnum];
140 }
141 
142 /**
143  *******************************************************************************
144  *
145  * @brief Reallocate the vector to the given size.
146  *
147  *******************************************************************************
148  *
149  * @param[inout] vec
150  * The extend vector structure to realloc.
151  *
152  * @param[in] size
153  * The new size of the vector. If the size is smaller than the actual
154  * size of the vector, nothing is done. Otherwise the vectore is
155  * enlarged and the values are kept.
156  *
157  *******************************************************************************/
158 void
160 {
161  extendint_Clear(vec);
162 
163  if(size <= vec->vecsize) { /* there 's enough space */
164  return;
165  }
166 
167  if(vec->inttab != NULL) {
168  memFree_null(vec->inttab);
169  }
170 
171  MALLOC_INTERN(vec->inttab, size, pastix_int_t);
172  vec->vecsize = size;
173 }
174 
175 /**
176  *******************************************************************************
177  *
178  * @brief Increment the number of element stored.
179  *
180  * This is the internal function that reallocate the vector.
181  *
182  *******************************************************************************
183  *
184  * @param[inout] vec
185  * The extend vector structure to realloc.
186  *
187  *******************************************************************************/
188 void
190 {
191  vec->eltnbr++;
192  /** if the vector is not big enough, make it bigger !! **/
193  if(!(vec->eltnbr < vec->vecsize))
194  {
195  pastix_int_t *tmp;
196  tmp = vec->inttab;
197 
198  vec->vecsize = vec->vecsize + vec->vecsize/2 +1;
199 
200  MALLOC_INTERN(vec->inttab, vec->vecsize, pastix_int_t);
201  memcpy(vec->inttab, tmp, sizeof(pastix_int_t)*vec->eltnbr);
202  memFree_null(tmp);
203  }
204 }
205 
206 /**
207  *******************************************************************************
208  *
209  * @brief Cleanup the vector.
210  *
211  * Set the number of elements to 0. Does NµOT free the sturcture.
212  *
213  *******************************************************************************
214  *
215  * @param[inout] vec
216  * The extend vector to clear.
217  *
218  *******************************************************************************/
219 void
221 {
222  vec->eltnbr = 0;
223 }
224 
225 /**
226  *@}
227  */
BEGIN_C_DECLS typedef int pastix_int_t
Definition: datatypes.h:51
pastix_int_t * inttab
Definition: extendVector.h:32
pastix_int_t eltnbr
Definition: extendVector.h:31
pastix_int_t vecsize
Definition: extendVector.h:30
pastix_int_t extendint_Size(const ExtendVectorINT *)
Return the number of element stored in the vector.
Definition: extendVector.c:112
void extendint_incr(ExtendVectorINT *)
Increment the number of element stored.
Definition: extendVector.c:189
void extendint_Clear(ExtendVectorINT *)
Cleanup the vector.
Definition: extendVector.c:220
pastix_int_t extendint_Read(const ExtendVectorINT *, pastix_int_t)
Return the element of index eltnum.
Definition: extendVector.c:136
pastix_int_t * extendint_Init(ExtendVectorINT *, pastix_int_t)
Initialize the extendVector structure with the initial size given.
Definition: extendVector.c:45
void extendint_Add(ExtendVectorINT *, pastix_int_t)
Add an element elt to the end of the vector.
Definition: extendVector.c:90
void extendint_ToSize(ExtendVectorINT *, pastix_int_t)
Reallocate the vector to the given size.
Definition: extendVector.c:159
void extendint_Exit(ExtendVectorINT *)
Free the extendVector structure.
Definition: extendVector.c:66
The extend integer array structure.
Definition: extendVector.h:29