-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathint_bucket.c
More file actions
149 lines (127 loc) · 3.15 KB
/
int_bucket.c
File metadata and controls
149 lines (127 loc) · 3.15 KB
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 <math.h>
#include <stdint.h>
#include <string.h>
#include <malloc.h>
#include "int_bucket.h"
struct int_bucket {
uint32_t *occurrences;
uint32_t *integers;
uint32_t size;
uint32_t n_elements;
};
uint32_t int_bucket_check_pos(const struct int_bucket *h, const uint32_t n)
{
uint32_t a,b,i;
i = 0;
if(h && h->n_elements > 0)
{
a = 0;
b = h->n_elements-1;
i = (b+a)/2;
while(b > a)
{
if (n > h->integers[i])
a = i+1;
if (n < h->integers[i])
b = i ? i-1 : 0;
if (n == h->integers[i])
a = b = i;
i = (b+a)/2;
}
if (n > (h->integers[i]))
i++;
}
return i;
}
int int_bucket_init(struct int_bucket *ib,const uint32_t size)
{
if(ib)
{
ib->size = size;
ib->occurrences = (uint32_t *) malloc(ib->size * sizeof(uint32_t));
ib->integers = (uint32_t *) malloc(ib->size * sizeof(uint32_t));
ib->n_elements = 0;
}
return ib->occurrences == NULL && ib->integers == NULL ? -1 : 0;
}
struct int_bucket * int_bucket_new(const uint32_t size)
{
struct int_bucket * ib = NULL;
ib = (struct int_bucket *) malloc(sizeof(struct int_bucket));
if (ib)
int_bucket_init(ib,size);
return ib;
}
void int_bucket_destroy(struct int_bucket **ib)
{
if(*ib)
{
if(*ib != NULL && (*ib)->occurrences != NULL)
free(((*ib)->occurrences));
if(*ib != NULL && (*ib)->integers != NULL)
free(((*ib)->integers));
free(*ib);
*ib=NULL;
}
}
uint32_t int_bucket_length(const struct int_bucket *ib)
{
return ib->n_elements;
}
int int_bucket_insert(struct int_bucket * ib,const uint32_t n,const uint32_t occurr)
{
uint32_t i;
if(ib)
{
i = int_bucket_check_pos(ib,n);
if(i>= ib->n_elements || ib->integers[i] != n)
{
if((ib->n_elements + 1) >= ib->size)
{
ib->size += INT_BUCKET_INC_SIZE;
ib->occurrences = (uint32_t *) realloc(ib->occurrences,sizeof(uint32_t) * ib->size);
ib->integers = (uint32_t *) realloc(ib->integers,sizeof(uint32_t) * ib->size);
}
memmove(ib->integers + i+1,ib->integers + i,sizeof(uint32_t) * (ib->n_elements -i));
memmove(ib->occurrences + i+1,ib->occurrences + i,sizeof(uint32_t) * (ib->n_elements -i));
ib->integers[i] = n;
ib->occurrences[i] = occurr;
ib->n_elements++;
} else
ib->occurrences[i] += occurr;
return 0;
}
return -1;
}
void int_bucket_sum(struct int_bucket *dst, const struct int_bucket *op)
{
uint32_t i;
if (dst && op)
for (i = 0; i<op->n_elements;i++)
int_bucket_insert(dst,op->integers[i],op->occurrences[i]);
}
double int_bucket_occurr_norm(const struct int_bucket *ib)
{
double sum = 0;
uint32_t i;
sum = 0;
for (i = 0; ib && i<ib->n_elements; i++)
{
// fprintf(stderr,"Element %d with occurrence %d\n",ib->integers[i],ib->occurrences[i]);
sum += (ib->occurrences[i])*(ib->occurrences[i]);
}
return sqrt(sum);
}
struct sparse_vector * int_bucket_2_sparse_vector(const struct int_bucket *ib)
{
struct sparse_vector * v;
uint32_t i;
// fprintf(stderr,"Start cloning...\n");
v = sparse_vector_new(int_bucket_length(ib));
for (i = 0; ib && i<ib->n_elements; i++)
{
// fprintf(stderr,"Inserting %d with value %d\n",ib->integers[i],ib->occurrences[i]);
sparse_vector_set_element(v,ib->integers[i],ib->occurrences[i]);
}
return v;
}