forked from jhamman/DHSVM
-
Notifications
You must be signed in to change notification settings - Fork 0
/
LookupTable.c
executable file
·86 lines (68 loc) · 2.58 KB
/
LookupTable.c
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
/*
* SUMMARY: LookupTable.c
* USAGE: Part of DHSVM - initializes and allows lookup for tables with
* regularly spaced indices
* AUTHOR: Bart Nijssen
* ORG: University of Washington, Department of Civil Engineering
* E-MAIL: [email protected]
* ORIG-DATE: 18-Feb-97 at 17:46:13
* DESCRIPTION: initializes and allows lookup for tables with
* regularly spaced indices
* DESCRIP-END.
* FUNCTIONS: init_float_table()
* float float_lookup(float x, FLOATTABLE *table)
* COMMENTS:
* $Id: LookupTable.c,v 1.4 2003/07/01 21:26:19 olivier Exp $
*/
#include <stdio.h>
#include <stdlib.h>
#include "lookuptable.h"
#include "DHSVMerror.h"
/*****************************************************************************
Function name: InitFloatTable()
Purpose : Initialize a table structure
Required :
unsigned long Size - Number of entries in the lookup table
float Offset - Value of key for first entry in the table
float Delta - Key interval
float (*Function)(float) - Function used to fill the table entries
FLOATTABLE *Table - pointer to structure that holds the table
Returns : void
Modifies : FLOATTABLE *table
Comments :
*****************************************************************************/
void InitFloatTable(unsigned long Size, float Offset, float Delta,
float (*Function) (float), FLOATTABLE * Table)
{
int i;
float x;
Table->Size = Size;
Table->Offset = Offset;
Table->Delta = Delta;
Table->Data = calloc(Table->Size, sizeof(float));
if (Table->Data == NULL)
ReportError("InitFloatTable", 1);
for (i = 0, x = Table->Offset + .5 * Table->Delta; i < Table->Size;
i++, x += Table->Delta)
Table->Data[i] = Function(x);
}
/*****************************************************************************
Function name: FloatLookup()
Purpose : Lookup a table entry corresponding to key x
Required :
float x - key to be looked up
FLOATTABLE *Table - Table structure that contains the entries
Returns : float
Modifies : None
Comments :
*****************************************************************************/
float FloatLookup(float x, FLOATTABLE * Table)
{
int i;
i = (int) (((x - Table->Offset) / Table->Delta));
if (i < 0 || i >= Table->Size) {
sprintf(errorstr, "FloatLookup: attempting lookup of value %f \n", x);
ReportError(errorstr, 47);
}
return Table->Data[i];
}