blob: 6d5aaedee24737dbdccf9ef0f4e100db51c7ef47 (
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
|
% Copyright The Numerical Algorithms Group Limited 1992-94. All rights reserved.
% !! DO NOT MODIFY THIS FILE BY HAND !! Created by ht.awk.
\newcommand{\GeneralSparseTableXmpTitle}{GeneralSparseTable}
\newcommand{\GeneralSparseTableXmpNumber}{9.30}
%
% =====================================================================
\begin{page}{GeneralSparseTableXmpPage}{9.30 GeneralSparseTable}
% =====================================================================
\beginscroll
%
Sometimes when working with tables there is a natural value to use
as the entry in all but a few cases.
The \spadtype{GeneralSparseTable} constructor can be used to provide any
table type with a default value for entries.
See \downlink{`Table'}{TableXmpPage}\ignore{Table} for general information about tables.
\showBlurb{GeneralSparseTable}
Suppose we launched a fund-raising campaign to raise fifty thousand dollars.
To record the contributions, we want a table with strings as keys
(for the names) and integer entries (for the amount).
In a data base of cash contributions, unless someone
has been explicitly entered, it is reasonable to assume they have made
a zero dollar contribution.
\xtc{
This creates a keyed access file with default entry \spad{0}.
}{
\spadpaste{patrons: GeneralSparseTable(String, Integer, KeyedAccessFile(Integer), 0) := table() ; \bound{patrons}}
}
\xtc{
Now \spad{patrons} can be used just as any other table.
Here we record two gifts.
}{
\spadpaste{patrons."Smith" := 10500 \free{patrons}\bound{smith}}
}
\xtc{
}{
\spadpaste{patrons."Jones" := 22000 \free{smith}\bound{jones}}
}
\xtc{
Now let us look up the size of the contributions from Jones and Stingy.
}{
\spadpaste{patrons."Jones" \free{jones}}
}
\xtc{
}{
\spadpaste{patrons."Stingy" \free{jones}}
}
\xtc{
Have we met our seventy thousand dollar goal?
}{
\spadpaste{reduce(+, entries patrons) \free{jones}}
}
\noOutputXtc{
So the project is cancelled and we can delete the data base:
}{
\spadpaste{)system rm -r kaf*.sdata \free{patrons}}
}
\endscroll
\autobuttons
\end{page}
%
|