-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcntTable.c
88 lines (77 loc) · 2.25 KB
/
cntTable.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include "murmur3.h"
#include "gtf.h"
//These are just modified hashTable methods, since cntTableElements have a cnt field.
cntTable *initCntTable(uint64_t size) {
hashTable *ht = initHT(size);
assert(ht);
cntTable *ct = calloc(1, sizeof(cntTable));
assert(ct);
ct->ht = ht;
ct->cnts = calloc(1, sizeof(uint32_t));
assert(ct->cnts);
return ct;
}
void destroyCntTable(cntTable *ct) {
free(ct->cnts);
destroyHT(ct->ht);
free(ct);
}
void initCnts(cntTable *ct) {
if(ct->ht->l) {
ct->cnts = calloc(ct->ht->l, sizeof(uint32_t));
assert(ct->cnts);
}
}
//There's no way to distinguish between "doesn't exist" and "has no counts"!
uint32_t str2cnt(cntTable *ct, char *s) {
if(!s) return -1;
uint64_t h = hashString(s);
hashTableElement *curr = ct->ht->elements[h%ct->ht->m];
while(curr) {
if(strcmp(ct->ht->str[curr->val], s) == 0) return ct->cnts[curr->val];
curr = curr->next;
}
return 0;
}
void incCntTable(cntTable *ct, char *s) {
assert(strExistsHT(ct->ht, s));
int32_t val = str2valHT(ct->ht, s);
assert(val >= 0);
ct->cnts[val]++;
}
void nodes2cnt(cntTable *ct, GTFnode *n, hashTable *ht, int32_t key) {
int i;
GTFentry *e = n->starts;
while(e) {
for(i=0; i<e->nAttributes; i++) {
if(e->attrib[i]->key == key) {
if(!strExistsHT(ct->ht, val2strHT(ht, e->attrib[i]->val))) {
addHTelement(ct->ht, val2strHT(ht, e->attrib[i]->val)); //ignore the return value
}
break;
}
}
e = e->right;
}
if(n->right) nodes2cnt(ct, n->right, ht, key);
if(n->left) nodes2cnt(ct, n->left, ht, key);
}
//Hmm, in hindsight, perhaps the hashTable structure should have held key:value pairs...
cntTable *makeCntTable(GTFtree *t, hashTable *ht, char *name) {
uint64_t i;
int32_t val = str2valHT(ht, name);
cntTable *ct = NULL;
if(val<0) { //No such name in the hash table!
return ct;
}
ct = initCntTable(100);
for(i=0; i<t->n_targets; i++) {
nodes2cnt(ct, (GTFnode*) t->chroms[i]->tree, ht, val);
}
initCnts(ct);
return ct;
}