Debian Codegraph @

libidu/dynvec.c

raw symbols SCIP
1 /* dynvec.c -- dynamically growable vectors
2 Copyright (C) 1995-2020 Free Software Foundation, Inc.
3 Written by Greg McGary <[email protected]>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 3, or (at your option)
8 any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 #include <config.h>
20 #include <stdlib.h>
21 #include <xalloc.h>
22 #include "dynvec.h"
23
24 struct dynvec *
25 make_dynvec (int n)
26 {
27 struct dynvec *dv = xmalloc (sizeof *dv);
28 dv->dv_vec = xnmalloc (n, sizeof *dv->dv_vec);
29 dv->dv_capacity = n;
30 dv->dv_fill = 0;
31 return dv;
32 }
33
34 void
35 dynvec_free (struct dynvec *dv)
36 {
37 free (dv->dv_vec);
38 free (dv);
39 }
40
41 void
42 dynvec_freeze (struct dynvec *dv)
43 {
44 if (dv->dv_fill == dv->dv_capacity)
45 return;
46 dv->dv_capacity = dv->dv_fill;
47 dv->dv_vec = xnrealloc (dv->dv_vec, dv->dv_capacity, sizeof *dv->dv_vec);
48 }
49
50 void
51 dynvec_append (struct dynvec *dv, void *element)
52 {
53 if (dv->dv_fill == dv->dv_capacity)
54 {
55 dv->dv_capacity *= 2;
56 dv->dv_vec = xnrealloc (dv->dv_vec, dv->dv_capacity, sizeof *dv->dv_vec);
57 }
58 dv->dv_vec[dv->dv_fill++] = element;
59 }