2 * implement arrays for dc
4 * Copyright (C) 1994, 1997, 1998 Free Software Foundation, Inc.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2, or (at your option)
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, you can either send email to this
18 * program's author (see below) or write to:
20 * The Free Software Foundation, Inc.
21 * 59 Temple Place, Suite 330
22 * Boston, MA 02111 USA
25 /* This module is the only one that knows what arrays look like. */
29 #include <stdio.h> /* "dc-proto.h" wants this */
31 /* get size_t definition from "almost ANSI" compiling environments. */
36 #include "dc-regdef.h"
38 /* what's most useful: quick access or sparse arrays? */
39 /* I'll go with sparse arrays for now */
43 struct dc_array *next;
47 /* initialize the arrays */
49 dc_array_init DC_DECLVOID()
53 /* store value into array_id[Index] */
55 dc_array_set DC_DECLARG((array_id, Index, value))
56 int array_id DC_DECLSEP
58 dc_data value DC_DECLEND
61 struct dc_array *prev=NULL;
62 struct dc_array *newentry;
64 cur = dc_get_stacked_array(array_id);
65 while (cur && cur->Index < Index){
69 if (cur && cur->Index == Index){
70 if (cur->value.dc_type == DC_NUMBER)
71 dc_free_num(&cur->value.v.number);
72 else if (cur->value.dc_type == DC_STRING)
73 dc_free_str(&cur->value.v.string);
75 dc_garbage(" in array", array_id);
78 newentry = dc_malloc(sizeof *newentry);
79 newentry->Index = Index;
80 newentry->value = value;
83 prev->next = newentry;
85 dc_set_stacked_array(array_id, newentry);
89 /* retrieve a dup of a value from array_id[Index] */
90 /* A zero value is returned if the specified value is unintialized. */
92 dc_array_get DC_DECLARG((array_id, Index))
93 int array_id DC_DECLSEP
98 for (cur=dc_get_stacked_array(array_id); cur; cur=cur->next)
99 if (cur->Index == Index)
100 return dc_dup(cur->value);
101 return dc_int2data(0);
104 /* free an array chain */
106 dc_array_free DC_DECLARG((a_head))
107 struct dc_array *a_head DC_DECLEND
109 struct dc_array *cur;
110 struct dc_array *next;
112 for (cur=a_head; cur; cur=next) {
114 if (cur->value.dc_type == DC_NUMBER)
115 dc_free_num(&cur->value.v.number);
116 else if (cur->value.dc_type == DC_STRING)
117 dc_free_str(&cur->value.v.string);
119 dc_garbage("in stack", -1);