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