2a7f3679d955f7390557360fa3cab56f59fec5cd
[pspp-builds.git] / src / subclist.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., 59 Temple Place - Suite 330, Boston, MA
21 02111-1307, USA. */
22
23
24 #include "subclist.h"
25 #include <stdlib.h>
26
27 /* I call these objects `lists' but they are in fact simple dynamic arrays */
28
29 #define CHUNKSIZE 16
30
31 /* Create a  list */
32 void
33 subc_list_double_create(subc_list_double *l)
34 {
35   l->data = (double *) malloc(CHUNKSIZE * sizeof (double));
36   l->sz = CHUNKSIZE;
37   l->n_data = 0;
38 }
39
40 /* Push a value onto the list */
41 void
42 subc_list_double_push(subc_list_double *l, double d)
43 {
44   l->data[l->n_data++] = d;
45
46   if (l->n_data >= l->sz ) 
47     {
48       l->sz += CHUNKSIZE;
49       l->data = realloc(l->data, l->sz * sizeof(double));
50     }
51
52 }
53
54 /* Return the number of items in the list */
55 int 
56 subc_list_double_count(const subc_list_double *l)
57 {
58   return l->n_data;
59 }
60
61
62 /* Index into the list (array) */
63 double
64 subc_list_double_at(const subc_list_double *l, int idx)
65 {
66   return l->data[idx];
67 }
68
69 /* Free up the list */
70 void
71 subc_list_double_destroy(subc_list_double *l)
72 {
73   free(l->data);
74 }