Difference between revisions of "ListSpace.c"

From Organic Design wiki
(remove listGetKeys, moving into serialise.h)
(remove **hash usage and **hash functions)
Line 10: Line 10:
 
// tmp: should use new pointer-lists
 
// tmp: should use new pointer-lists
 
#define MAXITEMS 100000
 
#define MAXITEMS 100000
int items = 0,hashPtr = 0;
+
int items = 0;
 
char *nssBuf;
 
char *nssBuf;
void **hashBuf = NULL;
 
  
 
// Prototypes
 
// Prototypes
Line 25: Line 24:
 
item trieGetValue(unsigned char *key);
 
item trieGetValue(unsigned char *key);
 
item trieSetValue(unsigned char *key, item value);
 
item trieSetValue(unsigned char *key, item value);
void **hash(unsigned char *key);
 
void hashTest();
 
  
  
Line 34: Line 31:
 
int listInit() {
 
int listInit() {
 
// all these are tmp and should be handled with new pointer-list
 
// all these are tmp and should be handled with new pointer-list
hashBuf = calloc(1000,sizeof(void*));
 
 
space = calloc(MAXITEMS,3*sizeof(item));
 
space = calloc(MAXITEMS,3*sizeof(item));
 
nssBuf = malloc(30);
 
nssBuf = malloc(30);
Line 94: Line 90:
  
 
// ----------------------------------------------------------------------------------------- //
 
// ----------------------------------------------------------------------------------------- //
// Trie & Hash
+
// Trie
 
// - not really part of listSpace, but needed by env's with no hash-tables
 
// - not really part of listSpace, but needed by env's with no hash-tables
 
// - uses ascii traversal by reserving first list items
 
// - uses ascii traversal by reserving first list items
 
// - trie sets/gets list items using ascii-traversal
 
// - trie sets/gets list items using ascii-traversal
// - hash sets/gets pointers - see hashTest() for example
 
  
 
item trieGetValue(unsigned char *key) {
 
item trieGetValue(unsigned char *key) {
Line 110: Line 105:
 
while(*key) subject = listTraverse(subject,*key++);
 
while(*key) subject = listTraverse(subject,*key++);
 
return listSetValue(subject,value);
 
return listSetValue(subject,value);
}
 
 
void **hash(unsigned char *key) {
 
item i,subject = 0;
 
while(*key) subject = listTraverse(subject,*key++);
 
return (i = listGetValue(subject)) ? hashBuf+i : hashBuf+listSetValue(subject,++hashPtr);
 
}
 
 
void hashTest() {
 
char* foo = "my name is mister foo, what's your's?";
 
*hash(foo) = "hello foo!";
 
*hash("pine") = "Cone";
 
printf("\thash[\"%s\"] = \"%s\"\n",foo,*hash(foo));
 
printf("\thash[\"%s\"] = \"%s\"\n","pine",*hash("pine"));
 
 
}
 
}

Revision as of 09:08, 30 October 2006

// [[[[1]]]] - nodal p2p wiki daemon // This article and all its includes are licenced under LGPL // GPL: [[[[2]]]] // SRC: [[[[3]]]] // included in [[[[4]]]][[[[5]]]]

typedef int item; item *space;

// tmp: should use new pointer-lists

  1. define MAXITEMS 100000

int items = 0; char *nssBuf;

// Prototypes int listInit(); int listExit(); item listInsert(); item listRemove(item subject); item listTraverse(item subject, item object); item listGetValue(item subject); item listSetValue(item subject, item value); void listStats(); item trieGetValue(unsigned char *key); item trieSetValue(unsigned char *key, item value);


// ----------------------------------------------------------------------------------------- // // listSpace.c

int listInit() { // all these are tmp and should be handled with new pointer-list space = calloc(MAXITEMS,3*sizeof(item)); nssBuf = malloc(30); // Reserving first list-items for ascii-hash use while (items<128) listInsert(); return EXIT_SUCCESS; }

int listExit() { free(space); }

// Create a new listItem in the space and return its index item listInsert() { // Unlink an item from the free-list and return its index if (items >= MAXITEMS) logAdd("No more items in the space!"); else return items++; return 0; }

// Move the subject onto the free-list item listRemove(item subject) { return subject; }

// 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 int i,j; for (i=1; i<=object>>1; i<<=1) subject = space[j=subject+(subject<<1)+(object&i?1:0)]?space[j]:(space[j]=listInsert()); return subject; }

// Get the value (payload key) of the subject Item item listGetValue(item subject) { return space[subject+(subject<<1)+2]; }

// Set the payload key of the subject Item to the passed value item listSetValue(item subject, item value) { return space[subject+(subject<<1)+2] = value; }


// Print listSpace statistics // - currently just counts how many nodes have payloads and displays content void listStats() { int i,j,p=0; printf("\nlistStatistics:\n"); for (i=0; i<MAXITEMS; i++) if (listGetValue(i)) p++; printf("\t%d of %d items in use:\n",p,MAXITEMS); //for (i=0; i<MAXITEMS; i++) if (j=listGetValue(i)) printf("  %d = %d\n",i,j); printf("\n\n"); }


// ----------------------------------------------------------------------------------------- // // Trie // - not really part of listSpace, but needed by env's with no hash-tables // - uses ascii traversal by reserving first list items // - trie sets/gets list items using ascii-traversal

item trieGetValue(unsigned char *key) { item subject = 0; while(*key) subject = listTraverse(subject,*key++); return listGetValue(subject); }

item trieSetValue(unsigned char *key, item value) { item subject = 0; while(*key) subject = listTraverse(subject,*key++); return listSetValue(subject,value); }