Mercurial > cgi-bin > hgwebdir.cgi > VMS > C_Libraries > PriorityQueue
changeset 8:d3ca41a44a21
deprecated default brch
| author | kshalle |
|---|---|
| date | Mon, 13 Feb 2012 13:27:20 -0800 |
| parents | 500d0e2fabfb |
| children | |
| files | .brch__default PriorityQueue.c PriorityQueue.h TestPriorityQueue.c __brch__DEPRECATED_README |
| diffstat | 5 files changed, 8 insertions(+), 321 deletions(-) [+] |
line diff
1.1 --- a/.brch__default Sat Feb 11 20:27:13 2012 -0800 1.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 1.3 @@ -1,1 +0,0 @@ 1.4 -The default branch is for use with normal C code. Other branches specialize the library for use with VMS.. they may need VMS header files, and the working directory may be located at various different positions relative to the VMS implementation. 1.5 \ No newline at end of file
2.1 --- a/PriorityQueue.c Sat Feb 11 20:27:13 2012 -0800 2.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 2.3 @@ -1,116 +0,0 @@ 2.4 -/* 2.5 - * Copyright 2009 OpenSourceStewardshipFoundation.org 2.6 - * Licensed under GNU General Public License version 2 2.7 - * 2.8 - * Author: hausers@cs.tu-berlin.de 2.9 - */ 2.10 - 2.11 -#include "PriorityQueue.h" 2.12 - 2.13 -#define left(i) 2*i 2.14 -#define right(i) 2*i+1 2.15 -#define parent(i) i/2 2.16 - 2.17 -void swap (PrioQueueStruc *Q, int a, int b) { 2.18 - void *valTmp; 2.19 - int keyTmp; 2.20 - keyTmp= Q->data[a].key; 2.21 - valTmp= Q->data[a].val; 2.22 - Q->data[a].key= Q->data[b].key; 2.23 - Q->data[a].val= Q->data[b].val; 2.24 - Q->data[b].key= keyTmp; 2.25 - Q->data[b].val= valTmp; 2.26 - 2.27 -} 2.28 - 2.29 -bool increaseKey (PrioQueueStruc *Q, int i, int key) { 2.30 - if (key < Q->data[i].key) return false; // increasing not possible 2.31 - 2.32 - Q->data[i].key= key; 2.33 - while (i>0 && Q->data[parent(i)].key < Q->data[i].key) { 2.34 - swap(Q,i,parent(i)); 2.35 - i= parent(i); 2.36 - } 2.37 - return true; 2.38 -} 2.39 - 2.40 -void heapify (PrioQueueStruc *Q, int i) { 2.41 - int largest; 2.42 - int l,r; 2.43 - l= left(i); 2.44 - r= right(i); 2.45 - if (l < Q->size && Q->data[l].key > Q->data[i].key) largest= l; 2.46 - else largest= i; 2.47 - if (r < Q->size && Q->data[r].key > Q->data[largest].key) largest= r; 2.48 - if (largest != i) { 2.49 - swap(Q,i,largest); 2.50 - heapify(Q,largest); 2.51 - } 2.52 -} 2.53 - 2.54 -void enlargePrioQ (PrioQueueStruc *Q) { 2.55 - size_t oldSize, newSize; 2.56 - heapNode *oldData,*newData; 2.57 - 2.58 - oldSize= Q->size; 2.59 - newSize= 2*oldSize; 2.60 - 2.61 - Q->maxSize= newSize; 2.62 - newData= malloc(Q->maxSize*sizeof(heapNode)); 2.63 - oldData= Q->data; 2.64 - 2.65 - memcpy(newData,oldData,Q->maxSize*sizeof(heapNode)); 2.66 - Q->data= newData; 2.67 - 2.68 - free(oldData); 2.69 -} 2.70 - 2.71 - 2.72 -PrioQueueStruc* makePrioQ () { 2.73 - PrioQueueStruc *retQ; 2.74 - retQ= malloc(sizeof(PrioQueueStruc)); 2.75 - retQ->maxSize= 1024; 2.76 - retQ->data= malloc(retQ->maxSize*sizeof(heapNode)); 2.77 - retQ->size= 0; 2.78 - return retQ; 2.79 -} 2.80 - 2.81 -void* getFirstPrioQ (PrioQueueStruc *Q) { 2.82 - return Q->data[0].val; 2.83 -} 2.84 - 2.85 -void* popPrioQ (PrioQueueStruc *Q) { 2.86 - void *max; 2.87 - if (Q->size < 1) max= NULL; 2.88 - else { 2.89 - max= Q->data[0].val; 2.90 - swap(Q,0,Q->size-1); 2.91 - Q->size--; 2.92 - heapify(Q,0); 2.93 - } 2.94 - return max; 2.95 -} 2.96 - 2.97 -bool insertPrioQ (void *val, int key, PrioQueueStruc *Q) { 2.98 - if (Q->size == Q->maxSize) enlargePrioQ(Q); 2.99 - 2.100 - Q->size++; 2.101 - Q->data[Q->size-1].key= INT_MIN; 2.102 - Q->data[Q->size-1].val= val; 2.103 - 2.104 - return increaseKey(Q,Q->size-1,key); 2.105 -} 2.106 - 2.107 -void printPrioQ (PrioQueueStruc *Q) { 2.108 - int i; 2.109 - printf("{"); 2.110 - if (Q->size>0) printf("%s",(char *)Q->data[0].val); 2.111 - for (i= 1; i<Q->size; i++) printf(",%s",(char *)Q->data[i].val); 2.112 - printf("} ... "); 2.113 - 2.114 -} 2.115 - 2.116 -void freePrioQ (PrioQueueStruc *Q) { 2.117 - free(Q->data); 2.118 - free(Q); 2.119 -}
3.1 --- a/PriorityQueue.h Sat Feb 11 20:27:13 2012 -0800 3.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 3.3 @@ -1,45 +0,0 @@ 3.4 -/* 3.5 - * Copyright 2009 OpenSourceStewardshipFoundation.org 3.6 - * Licensed under GNU General Public License version 2 3.7 - * 3.8 - * Author: hausers@cs.tu-berlin.de 3.9 - */ 3.10 - 3.11 -#ifndef _PRIORITY_QUEUE_H 3.12 -#define _PRIORITY_QUEUE_H 3.13 - 3.14 -#include <stdbool.h> 3.15 - 3.16 -#include <limits.h> 3.17 -#include <stdio.h> 3.18 -#include <string.h> 3.19 - 3.20 -typedef struct _PrioQueueStruc PrioQueueStruc; 3.21 -typedef struct _heapNode heapNode; 3.22 - 3.23 -struct _heapNode { 3.24 - int key; 3.25 - void *val; 3.26 -}; 3.27 - 3.28 -struct _PrioQueueStruc { 3.29 - int size; 3.30 - int maxSize; 3.31 - 3.32 - heapNode *data; 3.33 -}; 3.34 - 3.35 -PrioQueueStruc* makePrioQ(); 3.36 -void* getFirstPrioQ (PrioQueueStruc *Q); 3.37 -void* popPrioQ (PrioQueueStruc *Q); 3.38 -bool insertPrioQ (void *val, int key, PrioQueueStruc *Q); 3.39 -void freePrioQ (PrioQueueStruc *Q); 3.40 - 3.41 -#ifdef DEBUG 3.42 -void printPrioQ (PrioQueueStruc *Q); 3.43 -void swap (PrioQueueStruc *Q, int a, int b); 3.44 -#endif 3.45 - 3.46 - 3.47 -#endif 3.48 -
4.1 --- a/TestPriorityQueue.c Sat Feb 11 20:27:13 2012 -0800 4.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 4.3 @@ -1,159 +0,0 @@ 4.4 -#include <stdbool.h> 4.5 -#include <stdio.h> 4.6 -#include <stdlib.h> 4.7 - 4.8 - 4.9 -#include "CUnit/Basic.h" 4.10 -#include "PriorityQueue.h" 4.11 - 4.12 -#ifndef DEBUG 4.13 -#define DEBUG 4.14 -#endif 4.15 - 4.16 -static PrioQueueStruc *Q; 4.17 -static char *first,*second,*third; 4.18 - 4.19 -int init_suite (void) { 4.20 - if (NULL == (Q= makePrioQ())) return -1; 4.21 - else return 0; 4.22 -} 4.23 - 4.24 -int clean_suite (void) { 4.25 - return 0; 4.26 -} 4.27 - 4.28 -void testInsertAElement (void) { 4.29 - 4.30 - first= malloc(3*sizeof(char)); 4.31 - first= "foo"; 4.32 - 4.33 - printf("SIZE = %d | ",Q->size); 4.34 - printf("MAXSIZE = %d | ",Q->maxSize); 4.35 - CU_ASSERT(true == insertPrioQ(first,0,Q)); 4.36 - printPrioQ(Q); 4.37 - printf("SIZE = %d | ",Q->size); 4.38 - printf("MAXSIZE = %d | ",Q->maxSize); 4.39 -} 4.40 - 4.41 -void testInsertOneElement (void) { 4.42 - 4.43 - first= malloc(3*sizeof(char)); 4.44 - first= "ten"; 4.45 - 4.46 - CU_ASSERT(true == insertPrioQ(first,10,Q)); 4.47 - printPrioQ(Q); 4.48 -} 4.49 - 4.50 -void testPopOneElement (void) { 4.51 - popPrioQ(Q); 4.52 -} 4.53 - 4.54 -void testInsertSorted (void) { 4.55 - second= malloc(4*sizeof(char)); 4.56 - second= "five"; 4.57 - 4.58 - CU_ASSERT(true == insertPrioQ(second,5,Q)); 4.59 - printPrioQ(Q); 4.60 -} 4.61 - 4.62 -void testInsertAndHeapify (void) { 4.63 - third= malloc(6*sizeof(char)); 4.64 - third= "twenty"; 4.65 - 4.66 - CU_ASSERT(true == insertPrioQ(third,20,Q)); 4.67 - printPrioQ(Q); 4.68 -} 4.69 - 4.70 -void testGetFirstElement (void) { 4.71 - void* elem; 4.72 - 4.73 - elem= getFirstPrioQ(Q); 4.74 - CU_ASSERT(elem == third); 4.75 - printPrioQ(Q); 4.76 -} 4.77 - 4.78 -void testSwap (void) { 4.79 - swap(Q,0,2); 4.80 - printPrioQ(Q); 4.81 - swap(Q,2,0); 4.82 - printPrioQ(Q); 4.83 -} 4.84 - 4.85 -void testPop (void) { 4.86 - void *elem; 4.87 - 4.88 - elem= popPrioQ(Q); 4.89 - CU_ASSERT(elem == third); 4.90 - printPrioQ(Q); 4.91 - printf("size = %d",Q->size); 4.92 - elem= popPrioQ(Q); 4.93 - CU_ASSERT(elem == first); 4.94 - printPrioQ(Q); 4.95 - printf("size = %d",Q->size); 4.96 - elem= popPrioQ(Q); 4.97 - CU_ASSERT(elem == second); 4.98 - printPrioQ(Q); 4.99 - printf("size = %d",Q->size); 4.100 -} 4.101 - 4.102 -void testPopEmptyQueue (void) { 4.103 - void *elem; 4.104 - CU_ASSERT (NULL == (elem= popPrioQ(Q))); 4.105 -} 4.106 - 4.107 -void testEnlargeQueue (void) { 4.108 - int i; 4.109 - char *elem; 4.110 - for (i= 0; i<1163; i++) { 4.111 - elem= malloc(5*sizeof(char)); 4.112 - sprintf(elem,"%d",i); 4.113 - CU_ASSERT(true == insertPrioQ(elem,i,Q)); 4.114 - } 4.115 -} 4.116 - 4.117 - 4.118 -void testPopEnlargedQueue (void) { 4.119 - int i; 4.120 - char *elem; 4.121 - for (i= 1162; i>= 0; i--) { 4.122 - elem= popPrioQ(Q); 4.123 - CU_ASSERT(i == atoi(elem)); 4.124 - } 4.125 - CU_ASSERT(NULL == popPrioQ(Q)); 4.126 -} 4.127 - 4.128 -int main () { 4.129 - CU_pSuite pSuite= NULL; 4.130 - 4.131 - if (CUE_SUCCESS != CU_initialize_registry()) return CU_get_error(); 4.132 - 4.133 - pSuite= CU_add_suite("Simple Test Suite",init_suite, clean_suite); 4.134 - if (NULL == pSuite) { 4.135 - CU_cleanup_registry(); 4.136 - return CU_get_error(); 4.137 - } 4.138 - 4.139 - if (NULL == CU_add_test(pSuite,"test of insert first element",testInsertOneElement) || 4.140 - NULL == CU_add_test(pSuite,"test of insert sorted",testInsertSorted) || 4.141 - NULL == CU_add_test(pSuite,"test of insert and heapify",testInsertAndHeapify) || 4.142 - NULL == CU_add_test(pSuite,"test of swap",testSwap) || 4.143 - NULL == CU_add_test(pSuite,"test of get top element",testGetFirstElement) || 4.144 - NULL == CU_add_test(pSuite,"test of pop elements",testPop) || 4.145 - NULL == CU_add_test(pSuite,"test of pop an empty queue",testPopEmptyQueue) || 4.146 - NULL == CU_add_test(pSuite,"test of again a element",testInsertAElement) || 4.147 - NULL == CU_add_test(pSuite,"test of remove the element",testPopOneElement) || 4.148 - NULL == CU_add_test(pSuite,"test of enlarge queue",testEnlargeQueue) || 4.149 - NULL == CU_add_test(pSuite,"test of pop enlarged queue",testPopEnlargedQueue)) { 4.150 - 4.151 - 4.152 - 4.153 - CU_cleanup_registry(); 4.154 - return CU_get_error(); 4.155 - } 4.156 - 4.157 - 4.158 - CU_basic_set_mode(CU_BRM_VERBOSE); 4.159 - CU_basic_run_tests(); 4.160 - CU_cleanup_registry(); 4.161 - return CU_get_error(); 4.162 -}
5.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 5.2 +++ b/__brch__DEPRECATED_README Mon Feb 13 13:27:20 2012 -0800 5.3 @@ -0,0 +1,8 @@ 5.4 + 5.5 +There are two versions of the library -- one for pure C use, the other for use inside VMS or within applications written in a VMS-based language (IE, inside a top-level function or a call descendant of a top-level function) -- but only when the VMS is the "MC_shared" version. 5.6 + 5.7 +The reason is that VMS that uses shared memory on multicores moves the SlaveVPs around among cores. But, the libC and glibC malloc stores info at the top of the stack (a "clever" hack), for a speed improvement. So, when VMS manipulates the stack pointer, and/or moves Slaves to different cores, the "free" seg faults (that was FUN to figure out ; ) So, this version of VMS implements its own malloc. 5.8 + 5.9 +It is anticipated that the MC_split version, where each core has separate data, and messages are sent between cores, can handle malloc and free to use the glibC version. 5.10 + 5.11 +For now, update to the version of the library you wish to use..
