summaryrefslogtreecommitdiff
path: root/dc/array.c
blob: afcdb14da376e18b9900ef689e3e77cd62d15372 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/*
 * implement arrays for dc
 *
 * Copyright (C) 1994, 1997, 1998, 2000, 2006 Free Software Foundation, Inc.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, you can either send email to this
 * program's author (see below) or write to:
 *
 *    The Free Software Foundation, Inc.
 *    51 Franklin Street, Fifth Floor
 *    Boston, MA 02110-1301  USA
 */

/* This module is the only one that knows what arrays look like. */

#include "config.h"

#include <stdio.h>	/* "dc-proto.h" wants this */
#ifdef HAVE_STDLIB_H
/* get size_t definition from "almost ANSI" compiling environments. */
#include <stdlib.h>
#endif
#include "dc.h"
#include "dc-proto.h"
#include "dc-regdef.h"

/* what's most useful: quick access or sparse arrays? */
/* I'll go with sparse arrays for now */
struct dc_array {
	int Index;
	dc_data value;
	struct dc_array *next;
};


/* initialize the arrays */
void
dc_array_init DC_DECLVOID()
{
}

/* store value into array_id[Index] */
void
dc_array_set DC_DECLARG((array_id, Index, value))
	int array_id DC_DECLSEP
	int Index DC_DECLSEP
	dc_data value DC_DECLEND
{
	struct dc_array *cur;
	struct dc_array *prev = NULL;

	cur = dc_get_stacked_array(array_id);
	while (cur != NULL  &&  cur->Index < Index){
		prev = cur;
		cur = cur->next;
	}
	if (cur != NULL  &&  cur->Index == Index){
		if (cur->value.dc_type == DC_NUMBER)
			dc_free_num(&cur->value.v.number);
		else if (cur->value.dc_type == DC_STRING)
			dc_free_str(&cur->value.v.string);
		else
			dc_garbage(" in array", array_id);
		cur->value = value;
	}else{
		struct dc_array *newentry = dc_malloc(sizeof *newentry);
		newentry->Index = Index;
		newentry->value = value;
		newentry->next = cur;
		if (prev != NULL)
			prev->next = newentry;
		else
			dc_set_stacked_array(array_id, newentry);
	}
}

/* retrieve a dup of a value from array_id[Index] */
/* A zero value is returned if the specified value is unintialized. */
dc_data
dc_array_get DC_DECLARG((array_id, Index))
	int array_id DC_DECLSEP
	int Index DC_DECLEND
{
	struct dc_array *cur = dc_get_stacked_array(array_id);

	while (cur != NULL  &&  cur->Index < Index)
		cur = cur->next;
	if (cur !=NULL  &&  cur->Index == Index)
		return dc_dup(cur->value);
	return dc_int2data(0);
}

/* free an array chain */
void
dc_array_free DC_DECLARG((a_head))
	struct dc_array *a_head DC_DECLEND
{
	struct dc_array *cur;
	struct dc_array *next;

	for (cur=a_head; cur!=NULL; cur=next) {
		next = cur->next;
		if (cur->value.dc_type == DC_NUMBER)
			dc_free_num(&cur->value.v.number);
		else if (cur->value.dc_type == DC_STRING)
			dc_free_str(&cur->value.v.string);
		else
			dc_garbage("in stack", -1);
		free(cur);
	}
}


/*
 * Local Variables:
 * mode: C
 * tab-width: 4
 * End:
 * vi: set ts=4 :
 */