-
Notifications
You must be signed in to change notification settings - Fork 2
/
context.c
152 lines (111 loc) · 2.88 KB
/
context.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
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
#include "context.h"
#include <stdlib.h>
#include <string.h>
#include <assert.h>
struct ctx *ctx_enlarge(struct ctx *c, size_t size, size_t elems)
{
c = realloc(c, size * sizeof(struct ctx));
if (c == NULL) {
abort();
}
memset(c + elems, 0, (size - elems) * sizeof(struct ctx));
return c;
}
struct item *ctx_query_tag_item(struct ctx *c, size_t tag)
{
for (size_t i = 0; i < c->items; ++i) {
if (c->arr[i].tag == tag) {
return &(c->arr[i]);
}
}
return NULL;
}
size_t ctx_query_tag_index(struct ctx *c, size_t tag)
{
for (size_t i = 0; i < c->items; ++i) {
if (c->arr[i].tag == tag) {
return i;
}
}
return (size_t)-1;
}
void ctx_add_tag(struct ctx *c, size_t tag)
{
assert(!ctx_query_tag_item(c, tag));
c->items++;
c->arr = realloc(c->arr, c->items * sizeof(struct item));
if (c->arr == NULL) {
abort();
}
c->arr[c->items - 1].tag = tag;
c->arr[c->items - 1].freq = 1;
}
int item_compar(const void *l, const void *r)
{
const struct item *li = l;
const struct item *ri = r;
if (li->freq > ri->freq) {
return -1;
}
if (li->freq < ri->freq) {
return +1;
}
return 0;
}
/* sort ctx->items[] according to item.freq */
void ctx_sort(struct ctx *ctx)
{
#if 0
qsort(ctx->arr, ctx->items, sizeof(struct item), item_compar);
if (ctx->items > 1) {
assert(ctx->arr[0].freq >= ctx->arr[1].freq);
}
#else
(void)ctx;
#endif
}
void ctx_item_inc_freq(struct ctx *ctx, size_t tag)
{
struct item *item = ctx_query_tag_item(ctx, tag);
item->freq++;
}
void ctx_encode_tag_without_update_ac(struct bio *bio, struct ac *ac, struct ctx *ctx, size_t tag)
{
struct model model;
model_create(&model, ctx->items);
for (size_t i = 0; i < ctx->items; ++i) {
model.table[i].freq = ctx->arr[i].freq;
}
count_cum_freqs(model.table, model.count);
model.total = calc_total_freq(model.table, model.count);
size_t item_index = ctx_query_tag_index(ctx, tag);
ac_encode_symbol_model(ac, bio, item_index, &model);
model_destroy(&model);
}
float ctx_encode_tag_without_update_ac_query_prob(struct ctx *ctx, size_t tag)
{
struct model model;
model_create(&model, ctx->items);
for (size_t i = 0; i < ctx->items; ++i) {
model.table[i].freq = ctx->arr[i].freq;
}
count_cum_freqs(model.table, model.count);
model.total = calc_total_freq(model.table, model.count);
size_t item_index = ctx_query_tag_index(ctx, tag);
float prob = ac_encode_symbol_model_query_prob(item_index, &model);
model_destroy(&model);
return prob;
}
size_t ctx_decode_tag_without_update_ac(struct bio *bio, struct ac *ac, struct ctx *ctx)
{
struct model model;
model_create(&model, ctx->items);
for (size_t i = 0; i < ctx->items; ++i) {
model.table[i].freq = ctx->arr[i].freq;
}
count_cum_freqs(model.table, model.count);
model.total = calc_total_freq(model.table, model.count);
size_t item_index = ac_decode_symbol_model(ac, bio, &model);
model_destroy(&model);
return ctx->arr[item_index].tag;
}