Difference between revisions of "ListSpace.c"

From Organic Design wiki
(add function prototypes)
Line 1: Line 1:
 +
int listInsert();
 +
int listTraverse(int subject, int object);
 +
int listGetValue(int subject);
 +
void listSetValue(int subject, int value);
 +
 
// 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 8: Line 13:
 
// - 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)
int listTraverse( int subject, int object ) {
+
int listTraverse(int subject, int object) {
 
object += 2; // tmp: can't traverse items 0 or 1
 
object += 2; // tmp: can't traverse items 0 or 1
 
int i;
 
int i;
Line 19: 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;
 
int *addr = space + subject * 3 + 2;
 
return *addr;
 
return *addr;
Line 25: Line 30:
  
 
// 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;
 
int *addr = space + subject * 3 + 2;
 
*addr = value;
 
*addr = value;
 
}
 
}

Revision as of 09:08, 1 July 2006

int listInsert(); int listTraverse(int subject, int object); int listGetValue(int subject); void listSetValue(int subject, int value);

// 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) { int *addr = space + subject * 3 + 2; return *addr; }

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