Explanation of the code field requires its own post. There also needs to be an internal code value used within the internal code for each program element. If would be most convenient for this to be the index of the table entry so that execution of the program is as fast as possible. For readability of the C++ source, it would be nice to also have an enumeration defined for each table entry. Take the example:
enum {CODE1, CODE2, CODE3, NCODES};
char *table[NCODES] = {"100", "200", "300"};
Then table[CODE1] would be used for table[0], and so on. The problem comes in when new entries are added to the table. To add a new element in the table, say "150" between "100" and "200", the corresponding value must be added to the enumeration, the new value, say CODE1_5, must be added between CODE1 and CODE2. If both entries are not added in the correct positions, the program will not function correctly.
I don't like the idea of this because it makes the program harder to maintain, especially when the table grow very large. There's a number of kluge ways to use the C pre-processor to solve this, but I don't like them either. The best way I've come up with is to have an entry conversion array that is allocated and set up when the program starts running with some checks to make sure everything is defined properly. This takes the burden array from the programmer, especially when the enumeration and table get very large (and they will get large for this project). And it won't affect the execution of the program since it won't be used by the Run Time.
This array would take an enumeration value as the index and would return the index into the table and would be allocated statically or dynamically as in:
int entry[NCODES]; -or- int *entry = new int[NCODES];
As part of the checks, all the values in the array would be initialized to -1. There would be a loop that scans through the table to initialize the values in the array with a check to make sure a table entry does not contain the same enumeration value:
int i;
bool error = false;
for (int i = 0; i < NCODES; i++) {
int code = table[i].code;
if (entry[code] == -1) {
entry[code] = i;
} else {
"error: table entries have same code"
error = true;
}
}
After the entry array is setup, it is scanned to make sure all entries have been initialized:
for (int i = 0; i < NCODES; i++) {
if (entry[i] == -1) {
"error: code does not have table entry"
error = true;
}
}
Nether of loops terminate upon finding an error so that all possible errors are reported. After both loops, if the error flag is set, then the program will terminate.
Saturday, January 9, 2010
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment
All comments and feedback welcomed, whether positive or negative.
(Anonymous comments are allowed, but comments with URL links or unrelated comments will be removed.)