source: branches/blt4/src/objects/RpHashHelper.c @ 1932

Last change on this file since 1932 was 1386, checked in by dkearney, 15 years ago

adding a few object prototypes we can play with for future developement. the plot object is probably the most interesting. examples are located in examples/objects dirs

File size: 5.7 KB
Line 
1#include <stdio.h>
2#include <stdlib.h>
3#include <string.h>
4#include <ctype.h>
5#include "RpHashHelper.h"
6
7#ifdef __cplusplus
8extern "C" {
9#endif
10
11
12int
13Rp_HashPrint(
14    Rp_HashTable *h)
15{
16    Rp_HashEntry *hEntry = NULL;
17    Rp_HashSearch hSearch;
18    char *k = NULL;
19    char *v = NULL;
20
21    printf("hash table start\n");
22    hEntry = Rp_FirstHashEntry(h,&hSearch);
23    while (hEntry != NULL) {
24        k = (char *) Rp_GetHashKey(h,hEntry);
25        v = (char *) Rp_GetHashValue(hEntry);
26        printf("hentry = :%s->%s:\n",k,v);
27        hEntry = Rp_NextHashEntry(&hSearch);
28    }
29    printf("hash table end\n");
30    return 0;
31}
32
33int
34Rp_HashAddNode(
35    Rp_HashTable *h,
36    const char* key,
37    const void *n)
38{
39    int isNew = 0;
40    Rp_HashEntry *hEntry = NULL;
41
42    hEntry = Rp_CreateHashEntry(h,key,&isNew);
43    if (isNew == 0) {
44        // hash entry is not new
45        // warn user: two vals with the same name
46        // warn("hash entry is not new\n");
47    }
48    Rp_SetHashValue(hEntry,(ClientData)n);
49    return isNew;
50}
51
52/*
53 *  hRslt = h1 U h2
54 */
55
56int
57Rp_HashUnion(
58    Rp_HashTable *hRslt,
59    Rp_HashTable *h1,
60    Rp_HashTable *h2,
61    int (*cpyFxn)(void **to, void *from),
62    int (*cmpFxn) (void *he1, void *he2))
63{
64    Rp_HashSearch hSearch;
65    Rp_HashEntry *hEntry = NULL;
66    void *origKey = NULL;
67    void *origNode = NULL;
68    void *newNode = NULL;
69    void *searchRslt = NULL;
70    int nodesAddedCnt = 0;
71
72    Rp_HashCopy(hRslt,h1,cpyFxn);
73
74    hEntry = Rp_FirstHashEntry(h2,&hSearch);
75    while (hEntry != NULL) {
76        origKey = Rp_GetHashKey(h2,hEntry);
77        origNode = Rp_GetHashValue(hEntry);
78        searchRslt = Rp_HashSearchNode(hRslt,origKey);
79        if (searchRslt == NULL) {
80            // node not in hRslt, add it
81            cpyFxn(&newNode,origNode);
82            Rp_HashAddNode(hRslt,origKey,newNode);
83            nodesAddedCnt++;
84        }
85        hEntry = Rp_NextHashEntry(&hSearch);
86    }
87    return nodesAddedCnt;
88}
89
90/*
91 *  hRslt = h1 - h2
92 */
93
94int
95Rp_HashSubrtact(
96    Rp_HashTable *hRslt,
97    Rp_HashTable *h1,
98    Rp_HashTable *h2,
99    int (*cpyFxn)(void **to, void *from),
100    int (*cmpFxn)(void *he1, void *he2))
101{
102    Rp_HashSearch hSearch;
103    Rp_HashEntry *hEntry = NULL;
104    void *origKey = NULL;
105    void *origNode = NULL;
106    void *newNode = NULL;
107    int nodesAddedCnt = 0;
108
109    hEntry = Rp_FirstHashEntry(h1,&hSearch);
110    while (hEntry != NULL) {
111        origKey = Rp_GetHashKey(h1,hEntry);
112        origNode = Rp_GetHashValue(hEntry);
113        if (Rp_HashSearchNode(h2,origKey) == NULL) {
114            // node not in h2, add it to hRslt
115            cpyFxn(&newNode,origNode);
116            Rp_HashAddNode(hRslt,origKey,newNode);
117            nodesAddedCnt++;
118        }
119        hEntry = Rp_NextHashEntry(&hSearch);
120    }
121    return nodesAddedCnt;
122}
123
124int
125Rp_HashCompare(
126    Rp_HashTable *h1,
127    Rp_HashTable *h2,
128    int (*cmpFxn)(void *n1,void *n2))
129{
130    Rp_HashSearch hSearch;
131    Rp_HashEntry *h1Entry = NULL;
132    Rp_HashEntry *h2Entry = NULL;
133    void *h1Node = NULL;
134    void *h2Key = NULL;
135    void *h2Node = NULL;
136    int compareResult = 0;
137
138    // check the pointer values
139    if ( (h1 == NULL) && (h2 != NULL) ) {
140        return -1;
141    } else if ( (h1 != NULL) && (h2 == NULL) ) {
142        return 1;
143    } else if (h1 == h2) {
144        return 0;
145    }
146
147    // check hash table sizes
148    if (h1->numEntries < h2->numEntries) {
149        return -1;
150    } else if (h1 ->numEntries > h2->numEntries) {
151        return 1;
152    }
153
154    // equal number of entries, have to check each entry.
155
156    h2Entry = Rp_FirstHashEntry(h2,&hSearch);
157    while (h2Entry != NULL) {
158        h2Key = Rp_GetHashKey(h2,h2Entry);
159        h2Node = Rp_GetHashValue(h2Entry);
160        h1Entry = Rp_FindHashEntry(h1,h2Key);
161        if (h1Entry == NULL) {
162            compareResult = -1;
163            break;
164        } else {
165            h1Node = Rp_GetHashValue(h1Entry);
166            compareResult = cmpFxn(h1Node,h2Node);
167            if (compareResult != 0) {
168                break;
169            }
170        }
171        h2Entry = Rp_NextHashEntry(&hSearch);
172    }
173    return compareResult;
174}
175
176
177/*
178 *  forall(i) h1(i) = h2(i) : i E (nodes(h2))
179 *  elements of h2 are copied to h1 regardless of if h1
180 *  already has elements in it.
181 */
182
183int
184Rp_HashCopy(
185    Rp_HashTable *h1,
186    Rp_HashTable *h2,
187    int (*cpyFxn)(void **to,void *from))
188{
189    Rp_HashSearch hSearch;
190    Rp_HashEntry *hEntry = NULL;
191    void *origKey = NULL;
192    void *origNode = NULL;
193    void *newNode = NULL;
194
195    hEntry = Rp_FirstHashEntry(h2,&hSearch);
196    while (hEntry != NULL) {
197        origKey = Rp_GetHashKey(h2,hEntry);
198        origNode = Rp_GetHashValue(hEntry);
199        if (cpyFxn(&newNode,origNode) != 0) {
200            // error while copying
201        }
202        Rp_HashAddNode(h1,origKey,newNode);
203        hEntry = Rp_NextHashEntry(&hSearch);
204    }
205    return 0;
206}
207
208/*
209 * this function will not work for you if you put
210 * entries in the hash table with null data value
211 */
212
213void *
214Rp_HashSearchNode(
215    Rp_HashTable *h,
216    const char *key)
217{
218    Rp_HashEntry *hEntry = NULL;
219    void *n = NULL;
220    hEntry = Rp_FindHashEntry(h,key);
221    if (hEntry != NULL) {
222        n = Rp_GetHashValue(hEntry);
223    }
224    return n;
225}
226
227void *
228Rp_HashRemoveNode(
229    Rp_HashTable *h,
230    const char *key)
231{
232    Rp_HashEntry *hEntry = NULL;
233    void *n = NULL;
234    hEntry = Rp_FindHashEntry(h,key);
235    if (hEntry != NULL) {
236        n = Rp_GetHashValue(hEntry);
237        Rp_DeleteHashEntry(h,hEntry);
238    }
239    return n;
240}
241
242int charCpyFxn(
243    void **to,
244    void *from)
245{
246    size_t len = 0;
247
248    len = strlen((char*)from);
249    *to = (void*) malloc(len*sizeof(char));
250    strcpy((char*)(*to),(char*)from);
251    return 0;
252}
253
254int charCmpFxn(
255    void *to,
256    void *from)
257{
258    return strcmp(to,from);
259}
260
261#ifdef __cplusplus
262}
263#endif
264
Note: See TracBrowser for help on using the repository browser.