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 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285
|
/* ϤΥåȤ륳
*
* Copyright (C) 2006 HANAOKA Toshiyuki
* Copyright (C) 2006-2007 TABATA Yusuke
*
* Special Thanks: Google Summer of Code Program 2006
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include "input_set.h"
#define HASH_SIZE 1024
struct int_map_node {
int key;
int val;
struct int_map_node *next;
};
struct int_map {
/**/
int nr;
struct int_map_node *hash_head;
/**/
int array_size;
struct int_map_node **array;
};
struct input_set {
/**/
struct input_line *lines;
struct input_line *buckets[HASH_SIZE];
/**/
struct int_map *feature_freq;
};
static int
line_hash(const int *ar, int nr)
{
int i;
unsigned h = 0;
for (i = 0; i < nr; i++) {
h += ar[i];
}
return (h % HASH_SIZE);
}
static struct input_line *
find_same_line(struct input_set *is, int *features, int nr)
{
struct input_line *il;
int h = line_hash(features, nr);
for (il = is->buckets[h]; il; il = il->next_in_hash) {
int i;
if (il->nr_features != nr) {
continue;
}
for (i = 0; i < nr; i++) {
if (il->features[i] != features[i]) {
break;
}
}
if (i >= nr) {
return il;
}
}
return NULL;
}
static struct input_line *
add_line(struct input_set *is, int *features, int nr)
{
int i, h;
struct input_line *il;
il = malloc(sizeof(struct input_line));
il->nr_features = nr;
il->features = malloc(sizeof(int) * nr);
for (i = 0; i < nr; i++) {
il->features[i] = features[i];
}
il->weight = 0;
il->negative_weight = 0;
/* link */
il->next_line = is->lines;
is->lines = il;
/**/
h = line_hash(features, nr);
il->next_in_hash = is->buckets[h];
is->buckets[h] = il;
return il;
}
static void
add_feature_count(struct int_map *im, int nr, int *features, int weight)
{
int i;
for (i = 0; i < nr; i++) {
int f = features[i];
int v = int_map_peek(im, f);
int_map_set(im, f, v + weight);
}
}
/* input_setϤIJä */
void
input_set_set_features(struct input_set *is, int *features,
int nr, int weight)
{
struct input_line *il;
int abs_weight = abs(weight);
/**/
il = find_same_line(is, features, nr);
if (!il) {
il = add_line(is, features, nr);
}
/**/
if (weight > 0) {
il->weight += weight;
} else {
il->negative_weight += abs_weight;
}
/**/
add_feature_count(is->feature_freq, nr, features, abs_weight);
}
struct input_set *
input_set_create(void)
{
int i;
struct input_set *is;
is = malloc(sizeof(struct input_set));
is->lines = NULL;
/**/
for (i = 0; i < HASH_SIZE; i++) {
is->buckets[i] = NULL;
}
/**/
is->feature_freq = int_map_new();
/**/
return is;
}
struct input_line *
input_set_get_input_line(struct input_set *is)
{
return is->lines;
}
struct input_set *
input_set_filter(struct input_set *is,
double pos, double neg)
{
struct input_set *new_is = input_set_create();
struct input_line *il;
for (il = is->lines; il; il = il->next_line) {
if (il->weight > pos ||
il->negative_weight > neg) {
input_set_set_features(new_is, il->features,
il->nr_features, il->weight);
input_set_set_features(new_is, il->features,
il->nr_features, -il->negative_weight);
}
}
return new_is;
}
void
input_set_output_feature_freq(FILE *fp, struct input_set *is)
{
int i;
struct int_map *im = is->feature_freq;
fprintf(fp, "0 %d\n", im->array_size);
int_map_flatten(im);
for (i = 0; i < im->array_size; i++) {
if (!im->array[i]) {
fprintf(fp, "0 0\n");
} else {
fprintf(fp, "%d %d\n", im->array[i]->key,
im->array[i]->val);
}
}
}
struct int_map *
int_map_new(void)
{
int i;
struct int_map *im = malloc(sizeof(struct int_map));
im->nr = 0;
im->hash_head = malloc(sizeof(struct int_map_node) * HASH_SIZE);
for (i = 0; i < HASH_SIZE; i++) {
im->hash_head[i].next = NULL;
}
return im;
}
static int
node_index(int idx)
{
return idx % HASH_SIZE;
}
static struct int_map_node *
find_int_map_node(struct int_map *im, int idx)
{
struct int_map_node *node;
int h = node_index(idx);
for (node = im->hash_head[h].next; node; node = node->next) {
if (node->key == idx) {
return node;
}
}
return NULL;
}
int
int_map_peek(struct int_map *im, int idx)
{
struct int_map_node *node = find_int_map_node(im, idx);
if (node) {
return node->val;
}
return 0;
}
void
int_map_set(struct int_map *im, int idx, int val)
{
struct int_map_node *node = find_int_map_node(im, idx);
int h;
if (node) {
node->val = val;
return ;
}
/**/
node = malloc(sizeof(struct int_map_node));
node->key = idx;
node->val = val;
h = node_index(idx);
node->next = im->hash_head[h].next;
im->hash_head[h].next = node;
/**/
im->nr ++;
}
void
int_map_flatten(struct int_map *im)
{
int i;
struct int_map_node *node;
int max_n = 0;
/* */
im->array_size = im->nr * 2;
im->array = malloc(sizeof(struct int_map_node *) *
im->array_size);
for (i = 0; i < im->array_size; i++) {
im->array[i] = NULL;
}
/* Ǥ֤Ƥ */
for (i = 0; i < HASH_SIZE; i++) {
for (node = im->hash_head[i].next; node; node = node->next) {
int n = 0;
while (1) {
int h;
h = node->key + n;
h %= im->array_size;
if (!im->array[h]) {
im->array[h] = node;
break;
}
/**/
n++;
}
if (n > max_n) {
max_n = n;
}
}
}
/**/
printf(" max_collision=%d\n", max_n);
}
|