Change license from GPLv2+ to GPLv3+.
[pspp-builds.git] / src / language / lexer / subcommand-list.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 2004 Free Software Foundation, Inc.
3
4    This program is free software: you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation, either version 3 of the License, or
7    (at your option) any later version.
8
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this program.  If not, see <http://www.gnu.org/licenses/>. */
16
17
18 #include <config.h>
19 #include "subcommand-list.h"
20 #include <stdlib.h>
21 #include "xalloc.h"
22
23 /* I call these objects `lists' but they are in fact simple dynamic arrays */
24
25 #define CHUNKSIZE 16
26
27 /* Create a  list */
28 void
29 subc_list_double_create(subc_list_double *l)
30 {
31   l->data = xnmalloc (CHUNKSIZE, sizeof *l->data);
32   l->sz = CHUNKSIZE;
33   l->n_data = 0;
34 }
35
36 void
37 subc_list_int_create(subc_list_int *l)
38 {
39   l->data = xnmalloc (CHUNKSIZE, sizeof *l->data);
40   l->sz = CHUNKSIZE;
41   l->n_data = 0;
42 }
43
44 /* Push a value onto the list */
45 void
46 subc_list_double_push(subc_list_double *l, double d)
47 {
48   l->data[l->n_data++] = d;
49
50   if (l->n_data >= l->sz )
51     {
52       l->sz += CHUNKSIZE;
53       l->data = xnrealloc (l->data, l->sz, sizeof *l->data);
54     }
55
56 }
57
58 void
59 subc_list_int_push(subc_list_int *l, int d)
60 {
61   l->data[l->n_data++] = d;
62
63   if (l->n_data >= l->sz )
64     {
65       l->sz += CHUNKSIZE;
66       l->data = xnrealloc (l->data, l->sz, sizeof *l->data);
67     }
68
69 }
70
71 /* Return the number of items in the list */
72 int
73 subc_list_double_count(const subc_list_double *l)
74 {
75   return l->n_data;
76 }
77
78 int
79 subc_list_int_count(const subc_list_int *l)
80 {
81   return l->n_data;
82 }
83
84
85 /* Index into the list (array) */
86 double
87 subc_list_double_at(const subc_list_double *l, int idx)
88 {
89   return l->data[idx];
90 }
91
92 int
93 subc_list_int_at(const subc_list_int *l, int idx)
94 {
95   return l->data[idx];
96 }
97
98 /* Free up the list */
99 void
100 subc_list_double_destroy(subc_list_double *l)
101 {
102   free(l->data);
103 }
104
105 void
106 subc_list_int_destroy(subc_list_int *l)
107 {
108   free(l->data);
109 }