Difference between revisions of "LT.c"

From Organic Design wiki
(listTraverse function from listSpace.c)
 
(simplify code for example)
Line 4: Line 4:
 
// - if key is 0 the subject is returned unchanged, key 1 and 2 are the two single-iteration traversals
 
// - if key is 0 the subject is returned unchanged, key 1 and 2 are the two single-iteration traversals
 
item listTraverse(item subject, item key) {
 
item listTraverse(item subject, item key) {
if (key++) {
+
if (key++ > 0) {
 
int i,j;
 
int i,j;
for (i=1; i<=key>>1; i<<=1)
+
for (i = 1; i <= key >> 1; i <<= 1) {
subject = space[j=subject+(subject<<1)+(key&i?1:0)]?space[j]:(space[j]=listInsert());
+
j = 3 * subject + (key & i ? 1 : 0);
 +
subject = space[j];
 +
if (subject) space[j]; else space[j] = listInsert();
 +
}
 
}
 
}
 
return subject;
 
return subject;
 
}
 
}

Revision as of 04:00, 9 November 2006

// Start at subject listItem and traverse the key as an association to a new listItem // - key is also a listItem reference and its binary address is used as the traversal path // - subject and key (all list-item references are ints starting at item 0) // - if key is 0 the subject is returned unchanged, key 1 and 2 are the two single-iteration traversals item listTraverse(item subject, item key) { if (key++ > 0) { int i,j; for (i = 1; i <= key >> 1; i <<= 1) { j = 3 * subject + (key & i ? 1 : 0); subject = space[j]; if (subject) space[j]; else space[j] = listInsert(); } } return subject; }