-
Notifications
You must be signed in to change notification settings - Fork 8
/
fastcci_subcatcount.cc
149 lines (122 loc) · 2.75 KB
/
fastcci_subcatcount.cc
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
#include <stdio.h>
#include <stdlib.h>
#if !defined(__APPLE__)
#include <malloc.h>
#endif
#include <string.h>
#include "fastcci.h"
// the graph
int *cat;
tree_type *tree;
// depth buffer
const int maxdepth = 30;
int *cbuf[maxdepth+1];
// mask/unmask buffer
char *mask;
int *unmask;
int nmask;
inline void count(int v, int d) {
// lock current category
cbuf[d][v] = 0;
// Iterate over subcategories
int c = cat[v], cend = tree[c];
c += 2;
while (c < cend) {
int w = tree[c];
cbuf[d][v] += cbuf[d-1][w];
c++;
}
}
inline int rcount(int v, int d) {
if (d==0) return 1;
// lock current category
mask[v] = 1;
// remember for unmasking
unmask[nmask++] = v;
// Iterate over subcategories
int count = 0;
int c = cat[v], cend = tree[c];
c += 2;
while (c < cend) {
int w = tree[c];
if (mask[w]==0)
count += rcount(w, d-1);
c++;
}
}
int main() {
int maxcat = readFile("../fastcci.cat", cat);
maxcat /= sizeof(int);
mask = (char*)malloc(maxcat);
memset(mask, 0, maxcat);
unmask = (int*)malloc(maxcat * sizeof(int));
for (int i=0; i<=maxdepth; ++i)
{
cbuf[i] = (int*)malloc(maxcat * sizeof(*cbuf));
memset(cbuf[i], 0, maxcat * sizeof(int));
}
readFile("../fastcci.tree", tree);
#if 0
// recursion
for (int d=1; d<=maxdepth; ++d)
{
printf("Analyzing depth %d recursively...\n", d);
for (int v=0; v<maxcat; ++v)
if (cat[v]>-1)
{
// count
nmask = 0;
cbuf[d][v] = rcount(v,d);
// unmask
while (nmask--) { mask[unmask[nmask]] = 0; }
}
}
#else
// initialize generate depth buffer 0
for (int v=0; v<maxcat; ++v) cbuf[0][v] = 1;
for (int d=1; d<=maxdepth; ++d)
{
printf("Analyzing depth %d iteratively...\n", d);
for (int v=0; v<maxcat; ++v)
if (cat[v]>-1)
count(v, d);
}
#endif
// generate histograms
int maxsize[maxdepth+1];
int maxmax = 0;
for (int d=0; d<=maxdepth; ++d)
{
// find maximum size and mean (median?)
maxsize[d] = 0;
int n = 0;
double sum = 0.0;
for (int v=0; v<maxcat; ++v)
if (cat[v]>-1)
{
if (cbuf[d][v]>maxsize[d])
maxsize[d] = cbuf[d][v];
n++;
sum += cbuf[d][v];
}
if (maxsize[d]>maxmax) maxmax = maxsize[d];
//printf("maxsize[%d] = %d\n", d, maxsize[d]);
printf("%d %d\n", d, int(sum/double(n)));
}
return 0;
// output histogram
int *hist = (int*)malloc((maxmax+1) * sizeof(int));
for (int d=0; d<=maxdepth; ++d)
{
memset(hist, 0, (maxsize[d]+1) * sizeof(*hist));
for (int v=0; v<maxcat; ++v)
if (cat[v]>-1)
hist[cbuf[d][v]]++;
for (int i=0; i<=maxsize[d]; ++i)
printf("%d %d\n", i, hist[i]);
}
free(cat);
free(tree);
free(cbuf);
return 0;
}