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