From e95850476a9640ded203100b1e45cde01c3d6dab Mon Sep 17 00:00:00 2001 From: bdemsky Date: Fri, 25 Aug 2006 08:49:04 +0000 Subject: [PATCH] more files --- Robust/src/Runtime/Queue.c | 45 +++++ Robust/src/Runtime/Queue.h | 24 +++ Robust/src/Runtime/SimpleHash.c | 281 ++++++++++++++++++++++++++++++++ Robust/src/Runtime/SimpleHash.h | 85 ++++++++++ Robust/src/Runtime/mem.h | 16 ++ 5 files changed, 451 insertions(+) create mode 100644 Robust/src/Runtime/Queue.c create mode 100644 Robust/src/Runtime/Queue.h create mode 100755 Robust/src/Runtime/SimpleHash.c create mode 100755 Robust/src/Runtime/SimpleHash.h create mode 100644 Robust/src/Runtime/mem.h diff --git a/Robust/src/Runtime/Queue.c b/Robust/src/Runtime/Queue.c new file mode 100644 index 00000000..bc8643e2 --- /dev/null +++ b/Robust/src/Runtime/Queue.c @@ -0,0 +1,45 @@ +#include "mem.h" +#include "Queue.h" + +struct Queue * createQueue() { + return RUNMALLOC(sizeof(struct Queue)); +} + +int isEmpty(struct Queue *queue) { + return queue->head==NULL; +} + +struct QueueItem * addNewItem(struct Queue * queue, void * ptr) { + struct QueueItem * item=RUNMALLOC(sizeof(struct QueueItem)); + item->objectptr=ptr; + item->queue=queue; + if (queue->head==NULL) { + queue->head=item; + queue->tail=item; + } else { + item->next=queue->head; + queue->head->prev=item; + queue->head=item; + } + return item; +} + +void removeItem(struct Queue * queue, struct QueueItem * item) { + struct QueueItem * prev=item->prev; + struct QueueItem * next=item->next; + if (queue->head==item) + queue->head=next; + else + prev->next=next; + if (queue->tail==item) + queue->tail=prev; + else + next->prev=prev; + RUNFREE(item); +} + +struct QueueItem * getTail(struct Queue * queue) { + return queue->tail; +} + + diff --git a/Robust/src/Runtime/Queue.h b/Robust/src/Runtime/Queue.h new file mode 100644 index 00000000..d19d2cb7 --- /dev/null +++ b/Robust/src/Runtime/Queue.h @@ -0,0 +1,24 @@ +#ifndef QUEUE_H +#define QUEUE_H + +struct Queue { + struct QueueItem * head; + struct QueueItem * tail; +}; + +struct QueueItem { + void * objectptr; + struct Queue * queue; + struct QueueItem * next; + struct QueueItem * prev; + struct QueueItem * nextqueue; +}; + +struct Queue * createQueue(); +struct QueueItem * addNewItem(struct Queue * queue, void * ptr); +void removeItem(struct Queue * queue, struct QueueItem * item); +int isEmpty(struct Queue *queue); +struct QueueItem * getTail(struct Queue * queue); + + +#endif diff --git a/Robust/src/Runtime/SimpleHash.c b/Robust/src/Runtime/SimpleHash.c new file mode 100755 index 00000000..2ab3b3cf --- /dev/null +++ b/Robust/src/Runtime/SimpleHash.c @@ -0,0 +1,281 @@ +#include "SimpleHash.h" +#include + +/* SIMPLE HASH ********************************************************/ +struct SimpleIterator* SimpleHashcreateiterator(struct SimpleHash * thisvar) { + return allocateSimpleIterator(thisvar->listhead,thisvar->listtail,thisvar->tailindex/*,thisvar*/); +} + +void SimpleHashiterator(struct SimpleHash *thisvar, struct SimpleIterator * it) { + it->cur=thisvar->listhead; + it->index=0; + it->tailindex=thisvar->tailindex; + it->tail=thisvar->listtail; +} + +struct SimpleHash * noargallocateSimpleHash() { + return allocateSimpleHash(100); +} + +struct SimpleHash * allocateSimpleHash(int size) { + struct SimpleHash *thisvar=(struct SimpleHash *)RUNMALLOC(sizeof(struct SimpleHash)); + if (size <= 0) { + printf("Negative Hashtable size Exception\n"); + exit(-1); + } + thisvar->size = size; + thisvar->bucket = (struct SimpleNode **) calloc(sizeof(struct SimpleNode *)*size,1); + /* Set allocation blocks*/ + thisvar->listhead=(struct ArraySimple *) calloc(sizeof(struct ArraySimple),1); + thisvar->listtail=thisvar->listhead; + thisvar->tailindex=0; + /*Set data counts*/ + thisvar->numelements = 0; + return thisvar; +} + +void freeSimpleHash(struct SimpleHash *thisvar) { + struct ArraySimple *ptr=thisvar->listhead; + RUNFREE(thisvar->bucket); + while(ptr) { + struct ArraySimple *next=ptr->nextarray; + RUNFREE(ptr); + ptr=next; + } + RUNFREE(thisvar); +} + +inline int SimpleHashcountset(struct SimpleHash * thisvar) { + return thisvar->numelements; +} + +int SimpleHashfirstkey(struct SimpleHash *thisvar) { + struct ArraySimple *ptr=thisvar->listhead; + int index=0; + while((index==ARRAYSIZE)||!ptr->nodes[index].inuse) { + if (index==ARRAYSIZE) { + index=0; + ptr=ptr->nextarray; + } else + index++; + } + return ptr->nodes[index].key; +} + +int SimpleHashremove(struct SimpleHash *thisvar, int key, int data) { + unsigned int hashkey = (unsigned int)key % thisvar->size; + + struct SimpleNode **ptr = &thisvar->bucket[hashkey]; + int i; + + while (*ptr) { + if ((*ptr)->key == key && (*ptr)->data == data) { + struct SimpleNode *toremove=*ptr; + *ptr=(*ptr)->next; + + toremove->inuse=0; /* Marked as unused */ + + thisvar->numelements--; + return 1; + } + ptr = &((*ptr)->next); + } + + return 0; +} + +void SimpleHashaddAll(struct SimpleHash *thisvar, struct SimpleHash * set) { + struct SimpleIterator it; + SimpleHashiterator(set, &it); + while(hasNext(&it)) { + int keyv=key(&it); + int data=next(&it); + SimpleHashadd(thisvar,keyv,data); + } +} + +int SimpleHashadd(struct SimpleHash * thisvar,int key, int data) { + /* Rehash code */ + unsigned int hashkey; + struct SimpleNode **ptr; + + if (thisvar->numelements>=thisvar->size) { + int newsize=2*thisvar->size+1; + struct SimpleNode ** newbucket = (struct SimpleNode **) calloc(sizeof(struct SimpleNode *)*newsize,1); + int i; + for(i=thisvar->size-1;i>=0;i--) { + struct SimpleNode *ptr; + for(ptr=thisvar->bucket[i];ptr!=NULL;) { + struct SimpleNode * nextptr=ptr->next; + unsigned int newhashkey=(unsigned int)ptr->key % newsize; + ptr->next=newbucket[newhashkey]; + newbucket[newhashkey]=ptr; + ptr=nextptr; + } + } + thisvar->size=newsize; + RUNFREE(thisvar->bucket); + thisvar->bucket=newbucket; + } + + hashkey = (unsigned int)key % thisvar->size; + ptr = &thisvar->bucket[hashkey]; + + /* check that thisvar key/object pair isn't already here */ + /* TBD can be optimized for set v. relation */ + + while (*ptr) { + if ((*ptr)->key == key && (*ptr)->data == data) { + return 0; + } + ptr = &((*ptr)->next); + } + if (thisvar->tailindex==ARRAYSIZE) { + thisvar->listtail->nextarray=(struct ArraySimple *) calloc(sizeof(struct ArraySimple),1); + thisvar->tailindex=0; + thisvar->listtail=thisvar->listtail->nextarray; + } + + *ptr = &thisvar->listtail->nodes[thisvar->tailindex++]; + (*ptr)->key=key; + (*ptr)->data=data; + (*ptr)->inuse=1; + + thisvar->numelements++; + return 1; +} + +bool SimpleHashcontainskey(struct SimpleHash *thisvar,int key) { + unsigned int hashkey = (unsigned int)key % thisvar->size; + + struct SimpleNode *ptr = thisvar->bucket[hashkey]; + while (ptr) { + if (ptr->key == key) { + /* we already have thisvar object + stored in the hash so just return */ + return true; + } + ptr = ptr->next; + } + return false; +} + +bool SimpleHashcontainskeydata(struct SimpleHash *thisvar, int key, int data) { + unsigned int hashkey = (unsigned int)key % thisvar->size; + + struct SimpleNode *ptr = thisvar->bucket[hashkey]; + while (ptr) { + if (ptr->key == key && ptr->data == data) { + /* we already have thisvar object + stored in the hash so just return*/ + return true; + } + ptr = ptr->next; + } + return false; +} + +int SimpleHashcount(struct SimpleHash *thisvar,int key) { + unsigned int hashkey = (unsigned int)key % thisvar->size; + int count = 0; + + struct SimpleNode *ptr = thisvar->bucket[hashkey]; + while (ptr) { + if (ptr->key == key) { + count++; + } + ptr = ptr->next; + } + return count; +} + +struct SimpleHash * SimpleHashimageSet(struct SimpleHash *thisvar, int key) { + struct SimpleHash * newset=allocateSimpleHash(2*SimpleHashcount(thisvar,key)+4); + unsigned int hashkey = (unsigned int)key % thisvar->size; + + struct SimpleNode *ptr = thisvar->bucket[hashkey]; + while (ptr) { + if (ptr->key == key) { + SimpleHashadd(newset,ptr->data,ptr->data); + } + ptr = ptr->next; + } + return newset; +} + +int SimpleHashget(struct SimpleHash *thisvar, int key, int *data) { + unsigned int hashkey = (unsigned int)key % thisvar->size; + + struct SimpleNode *ptr = thisvar->bucket[hashkey]; + while (ptr) { + if (ptr->key == key) { + *data = ptr->data; + return 1; /* success */ + } + ptr = ptr->next; + } + + return 0; /* failure */ +} + +int SimpleHashcountdata(struct SimpleHash *thisvar,int data) { + int count = 0; + struct ArraySimple *ptr = thisvar->listhead; + while(ptr) { + if (ptr->nextarray) { + int i; + for(i=0;inodes[i].data == data + &&ptr->nodes[i].inuse) { + count++; + } + } else { + int i; + for(i=0;itailindex;i++) + if (ptr->nodes[i].data == data + &&ptr->nodes[i].inuse) { + count++; + } + } + ptr = ptr->nextarray; + } + return count; +} + +inline struct SimpleIterator * noargallocateSimpleIterator() { + return (struct SimpleIterator*)RUNMALLOC(sizeof(struct SimpleIterator)); +} + +inline struct SimpleIterator * allocateSimpleIterator(struct ArraySimple *start, struct ArraySimple *tl, int tlindex) { + struct SimpleIterator *thisvar=(struct SimpleIterator*)RUNMALLOC(sizeof(struct SimpleIterator)); + thisvar->cur = start; + thisvar->index=0; + thisvar->tailindex=tlindex; + thisvar->tail=tl; + return thisvar; +} + +inline int hasNext(struct SimpleIterator *thisvar) { + if (thisvar->cur==thisvar->tail && + thisvar->index==thisvar->tailindex) + return 0; + while((thisvar->index==ARRAYSIZE)||!thisvar->cur->nodes[thisvar->index].inuse) { + if (thisvar->index==ARRAYSIZE) { + thisvar->index=0; + thisvar->cur=thisvar->cur->nextarray; + } else + thisvar->index++; + } + if (thisvar->cur->nodes[thisvar->index].inuse) + return 1; + else + return 0; +} + +inline int next(struct SimpleIterator *thisvar) { + return thisvar->cur->nodes[thisvar->index++].data; +} + +inline int key(struct SimpleIterator *thisvar) { + return thisvar->cur->nodes[thisvar->index].key; +} diff --git a/Robust/src/Runtime/SimpleHash.h b/Robust/src/Runtime/SimpleHash.h new file mode 100755 index 00000000..edb1f66a --- /dev/null +++ b/Robust/src/Runtime/SimpleHash.h @@ -0,0 +1,85 @@ +#ifndef SIMPLEHASH_H +#define SIMPLEHASH_H + +#ifndef bool +#define bool int +#endif + +#ifndef true +#define true 1 +#endif + +#ifndef false +#define false 0 +#endif + +#include "mem.h" + +/* SimpleHash *********************************************************/ + +struct SimpleHash * noargallocateSimpleHash(); +struct SimpleHash * allocateSimpleHash(int size); +void SimpleHashaddChild(struct SimpleHash *thisvar, struct SimpleHash * child); +void freeSimpleHash(struct SimpleHash *); + + +int SimpleHashadd(struct SimpleHash *, int key, int data); +int SimpleHashremove(struct SimpleHash *,int key, int data); +bool SimpleHashcontainskey(struct SimpleHash *,int key); +bool SimpleHashcontainskeydata(struct SimpleHash *,int key, int data); +int SimpleHashget(struct SimpleHash *,int key, int* data); +int SimpleHashcountdata(struct SimpleHash *,int data); +void SimpleHashaddParent(struct SimpleHash *,struct SimpleHash* parent); +int SimpleHashfirstkey(struct SimpleHash *); +struct SimpleIterator* SimpleHashcreateiterator(struct SimpleHash *); +void SimpleHashiterator(struct SimpleHash *, struct SimpleIterator * it); +int SimpleHashcount(struct SimpleHash *, int key); +void SimpleHashaddAll(struct SimpleHash *, struct SimpleHash * set); +struct SimpleHash * SimpleHashimageSet(struct SimpleHash *, int key); + +struct SimpleHash { + int numelements; + int size; + struct SimpleNode **bucket; + struct ArraySimple *listhead; + struct ArraySimple *listtail; + int tailindex; +}; + +inline int SimpleHashcountset(struct SimpleHash * thisvar); + +/* SimpleHashException *************************************************/ + + +/* SimpleIterator *****************************************************/ +#define ARRAYSIZE 100 + +struct SimpleNode { + struct SimpleNode *next; + int data; + int key; + int inuse; +}; + +struct ArraySimple { + struct SimpleNode nodes[ARRAYSIZE]; + struct ArraySimple * nextarray; +}; + + +struct SimpleIterator { + struct ArraySimple *cur, *tail; + int index,tailindex; +}; + +inline struct SimpleIterator * noargallocateSimpleIterator(); + +inline struct SimpleIterator * allocateSimpleIterator(struct ArraySimple *start, struct ArraySimple *tl, int tlindex); + +inline int hasNext(struct SimpleIterator *thisvar); + +inline int next(struct SimpleIterator *thisvar); + +inline int key(struct SimpleIterator *thisvar); + +#endif diff --git a/Robust/src/Runtime/mem.h b/Robust/src/Runtime/mem.h new file mode 100644 index 00000000..a2212edd --- /dev/null +++ b/Robust/src/Runtime/mem.h @@ -0,0 +1,16 @@ +#ifndef MEMH +#define MEMH +#include +#include + +#ifdef BOEHM_GC +#include "gc.h" +#define FREEMALLOC(x) GC_malloc(x) +#define RUNMALLOC(x) GC_malloc(x) +#define RUNFREE(x) +#else +#define FREEMALLOC(x) calloc(1,x) +#define RUNMALLOC(x) calloc(1,x) +#define RUNFREE(x) free(x) +#endif +#endif -- 2.34.1