Difference between revisions of "ListSpace.c"

From Organic Design wiki
m
m
Line 1: Line 1:
 +
#define MAXITEMS 10000
 +
int *space;
 +
int items = 0;
 +
 
// Create a new listItem in the space and return its index
 
// Create a new listItem in the space and return its index
 
int listInsert() {
 
int listInsert() {
Line 11: Line 15:
 
object += 2; // tmp: can't traverse items 0 or 1
 
object += 2; // tmp: can't traverse items 0 or 1
 
int i;
 
int i;
for ( i = 1; i <= object >> 1; i <<= 1 ) {
+
for (i = 1; i <= object >> 1; i <<= 1) {
int *addr = space + subject * 3 + (object & i ? 1 : 0);
+
int *addr = space+subject*3+(object&i?1:0);
 
subject = *addr ? *addr : (*addr = listInsert());
 
subject = *addr ? *addr : (*addr = listInsert());
 
}
 
}
Line 20: Line 24:
 
// Get the value (payload key) of the subject Item
 
// Get the value (payload key) of the subject Item
 
int listGetValue(int subject) {
 
int listGetValue(int subject) {
int *addr = space + subject * 3 + 2;
+
return space[subject*3+2];
return *addr;
 
 
}
 
}
  
 
// 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(int subject, int value) {
 
void listSetValue(int subject, int value) {
int *addr = space + subject * 3 + 2;
+
space[subject*3+2] = value;
*addr = value;
 
 
}
 
}
  

Revision as of 11:14, 4 July 2006

  1. define MAXITEMS 10000

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) int listTraverse(int subject, int object) { object += 2; // tmp: can't traverse items 0 or 1 int i; for (i = 1; i <= object >> 1; i <<= 1) { int *addr = space+subject*3+(object&i?1:0); subject = *addr ? *addr : (*addr = listInsert()); } return subject; }

// Get the value (payload key) of the subject Item int listGetValue(int subject) { return space[subject*3+2]; }

// Set the payload key of the subject Item to the passed value void listSetValue(int subject, int value) { space[subject*3+2] = value; }

// - treat each character in text as a list-item-index to traverse from root int traverseText(char *text) { int i,subject = 0; for (i=0; i<strlen(text); i++) subject = listTraverse(subject, (int)text[i]); return subject; }

// Allocate memory for maximum ListItems space = calloc(MAXITEMS*3, sizeof(int)); printf("%d bytes of memory allocated\n", MAXITEMS*3*sizeof(int));

// 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();