Difference between revisions of "ListSpace.c"
(move list-info-printing into listStatistics() in here) |
m |
||
Line 1: | Line 1: | ||
#define MAXITEMS 10000 | #define MAXITEMS 10000 | ||
+ | #define ROOT 0 | ||
+ | typedef int item; | ||
int *space; | int *space; | ||
int items = 0; | int items = 0; | ||
Line 12: | Line 14: | ||
// - object is also a listItem reference and its binary address is used as the traversal path | // - object is also a listItem reference and its binary address is used as the traversal path | ||
// - subject and object (all list-item references are ints starting at item 0) | // - subject and object (all list-item references are ints starting at item 0) | ||
− | + | item listTraverse(item subject, item object) { | |
object += 2; // tmp: can't traverse items 0 or 1 | object += 2; // tmp: can't traverse items 0 or 1 | ||
− | int | + | item *pitem; |
+ | int i; | ||
for (i = 1; i <= object >> 1; i <<= 1) { | for (i = 1; i <= object >> 1; i <<= 1) { | ||
− | + | pitem = space+subject*3+(object&i?1:0); | |
− | subject = * | + | subject = *pitem ? *pitem : (*pitem = listInsert()); |
} | } | ||
return subject; | return subject; | ||
Line 23: | Line 26: | ||
// Get the value (payload key) of the subject Item | // Get the value (payload key) of the subject Item | ||
− | + | item listGetValue(item subject) { | |
return space[subject*3+2]; | return space[subject*3+2]; | ||
} | } | ||
// Set the payload key of the subject Item to the passed value | // Set the payload key of the subject Item to the passed value | ||
− | void listSetValue( | + | void listSetValue(item subject, item value) { |
space[subject*3+2] = value; | space[subject*3+2] = value; | ||
} | } | ||
// - treat each character in text as a list-item-index to traverse from root | // - treat each character in text as a list-item-index to traverse from root | ||
− | + | item traverseText(char *text) { | |
− | int i | + | item subject = ROOT; |
+ | int i; | ||
for (i=0; i<strlen(text); i++) | for (i=0; i<strlen(text); i++) | ||
− | subject = listTraverse(subject, ( | + | subject = listTraverse(subject, (item)text[i]); |
return subject; | return subject; | ||
} | } | ||
// Allocate memory for maximum ListItems | // Allocate memory for maximum ListItems | ||
− | space = calloc(MAXITEMS*3, sizeof( | + | space = calloc(MAXITEMS*3, sizeof(item)); |
− | printf("%d bytes of memory allocated\n", MAXITEMS*3*sizeof( | + | printf("%d bytes of memory allocated\n", MAXITEMS*3*sizeof(item)); |
// Insert items 0-127 as ascii character nodes | // Insert items 0-127 as ascii character nodes |
Revision as of 12:36, 5 July 2006
- define MAXITEMS 10000
- define ROOT 0
typedef int item; int *space; int items = 0;
// Create a new listItem in the space and return its index int listInsert() { //printf("listInsert(): new item %d\n", items); return items++; }
// Start at subject listItem and traverse the object as an association to a new listItem // - object is also a listItem reference and its binary address is used as the traversal path // - subject and object (all list-item references are ints starting at item 0) item listTraverse(item subject, item object) { object += 2; // tmp: can't traverse items 0 or 1 item *pitem; int i; for (i = 1; i <= object >> 1; i <<= 1) { pitem = space+subject*3+(object&i?1:0); subject = *pitem ? *pitem : (*pitem = listInsert()); } return subject; }
// Get the value (payload key) of the subject Item item listGetValue(item subject) { return space[subject*3+2]; }
// Set the payload key of the subject Item to the passed value void listSetValue(item subject, item value) { space[subject*3+2] = value; }
// - treat each character in text as a list-item-index to traverse from root item traverseText(char *text) { item subject = ROOT; int i; for (i=0; i<strlen(text); i++) subject = listTraverse(subject, (item)text[i]); return subject; }
// Allocate memory for maximum ListItems space = calloc(MAXITEMS*3, sizeof(item)); printf("%d bytes of memory allocated\n", MAXITEMS*3*sizeof(item));
// Insert items 0-127 as ascii character nodes // - this allows us to temporarily use list-space as a dictionary // since C/C++ doesn't inherently have one for (i=0; i<128; i++) listInsert();
// Print listSpace statistics
// - currently just counts how many nodes have payloads and displays content
void listStatistics() {
int i,j,p=0;
for (i=0; i<MAXITEMS; i++) if (listGetValue(i)) p++;
printf("%d items in use:\n", p);
for (i=0; i<MAXITEMS; i++) if (j=listGetValue(i)) printf(" %d = %d\n",i,j);
printf("\n\n");
}