forked from genialis/gotea
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprocessor.h
157 lines (143 loc) · 2.82 KB
/
processor.h
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#ifndef __PROCESSOR_H__
#define __PROCESSOR_H__
#include<stddef.h>
#include<unordered_set>
#include<vector>
#include<algorithm>
/* string library; hashing, serialization */
struct StrComp
{
bool operator()(const char *str1, const char *str2) const
{
return strcmp(str1,str2)<0;
}
};
struct StrHash
{
size_t operator()(const char *s) const
{
return StrHash::fakehash(s);
}
static size_t fakehash(const char *s)
{
return ((size_t*)s)[-1];
}
static size_t realhash(const char *s)
{
/*FNV-1a 32- or 64bit*/
static const size_t prime=(sizeof(size_t)==8)?1099511628211LU:16777619LU;
static const size_t offset=(sizeof(size_t)==8)?14695981039346656037LU:2166136261LU;
size_t accum=offset;
for (int i=0;s[i]!=0;i++)
{
accum^=s[i];
accum*=prime;
}
return accum;
}
};
struct PtrHash
{
size_t operator()(const char *s) const { return (size_t)s; }
};
struct StrEq
{
bool operator()(const char *str1, const char *str2) const
{
size_t len1=((size_t*)str1)[-2];
size_t len2=((size_t*)str2)[-2];
return len1==len2 && strcmp(str1,str2)==0;
}
};
struct PtrEq
{
bool operator()(const char *str1, const char *str2) const { return str1==str2; }
};
static inline constexpr int str_serialize_padsize() { return 2*sizeof(size_t); }
static inline int str_serialize_len(const char *s)
{
return strlen(s)+str_serialize_padsize();
}
static inline void str_serialize(char *buf, const char *s)
{
size_t *nums=(size_t*)buf;
nums[-1]=StrHash::realhash(s);
nums[-2]=strlen(s);
strcpy(buf,s);
}
static inline size_t strlen_bin(const char *s)
{
return ((size_t*)s)[-2];
}
/*elements must always be pushed in ascending order*/
struct quick_set_t : public std::vector<const char*>
{
quick_set_t(size_t res)
{
reserve(res);
}
bool push_gene(const char *gene)
{
if (size()==0 || back()!=gene)
{
push_back(gene);
return true;
}
return false;
}
bool has_gene(const char *gene) const
{
return std::binary_search(begin(),end(),gene,[] (const char *g1, const char *g2) -> bool { return g1<g2; });
}
};
/*struct quick_set_t : public std::unordered_set<const char*, PtrHash, PtrEq>
{
quick_set_t(size_t) {}
bool push_gene(const char *gene)
{
return insert(gene).second;
}
bool has_gene(const char *gene) const
{
return count(gene)!=0;
}
};*/
typedef double float_type;
struct int_term_t
{
union
{
struct int_term_t **ptr;
ptrdiff_t idx;
} parents,children;
int nparents,nchildren;
union
{
const char *str;
ptrdiff_t idx;
} id,name,prerender;
struct quick_set_t genes,intersect;
float_type pval,score;
int prerender_len,genes_len,intersect_len;
union
{
struct
{
unsigned dumped: 1;
unsigned type: 2;
unsigned obsolete: 1;
};
int all;
} flags;
long color;
char *genebuf;
};
struct int_link_t
{
union
{
const char *str;
ptrdiff_t idx;
} gene,term;
};
#endif /* __PROCESSOR_H__ */