forked from bxshi/voting
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vote_counter.c
206 lines (168 loc) · 3.76 KB
/
vote_counter.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
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
//
// vote_counter.c
// assign1
//
// Created by Baoxu Shi on 13-8-30.
// Copyright (c) 2013年 Baoxu Shi. All rights reserved.
//
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include "constants.h"
#include "uthash.h"
#define HASH_NUM 256
#define HASH_MASK 0xff //HASH_MASK should <= HASH_NUM
//initialize hash table
struct vote_result *vr = NULL;
struct vote_history *vh = NULL;
pthread_mutex_t vr_lock;
pthread_mutex_t vh_lock;
struct result_chunk
{
struct vote_result *vr;
pthread_mutex_t vr_lock;
struct vote_history *vh;
pthread_mutex_t vh_lock;
};
struct result_chunk *rc_list;
void init_mutex()
{
int i;
rc_list = malloc(sizeof(struct result_chunk) * HASH_NUM);
for(i = 0; i< HASH_NUM; i++)
{
rc_list[i].vr = NULL;
rc_list[i].vh = NULL;
pthread_mutex_init(&(rc_list[i].vr_lock), NULL);
pthread_mutex_init(&(rc_list[i].vh_lock), NULL);
}
pthread_mutex_init(&vr_lock, NULL);
pthread_mutex_init(&vh_lock, NULL);
}
int hash_locator(u_int32_t uid)
{
return uid & HASH_MASK;
}
//maybe copy pointer is faster than copy int?
void add_vote(u_int32_t vote)
{
struct vote_result *p;
struct result_chunk *rc;
rc = &rc_list[hash_locator(vote)];
pthread_mutex_lock(&(rc->vr_lock));
HASH_FIND_INT(rc->vr, &vote, p);
if(!p)
{
p = malloc(sizeof(struct vote_result));
p->uid = vote;
p->count = 1;
HASH_ADD_INT(rc->vr, uid, p);
}
else
{
p->count++;
}
pthread_mutex_unlock(&(rc->vr_lock));
}
void remove_vote(u_int32_t vote)
{
struct vote_result *p;
struct result_chunk *rc;
rc = &rc_list[hash_locator(vote)];
pthread_mutex_lock(&(rc->vr_lock));
HASH_FIND_INT(rc->vr, &vote, p);
if(!p)
{
printf("[error]");
}
else
{
p->count--;
}
pthread_mutex_unlock(&(rc->vr_lock));
};
short check_dup(u_int32_t uid, u_int32_t vote)
{
struct vote_history *p;
struct result_chunk *rc;
rc = &rc_list[hash_locator(uid)];
pthread_mutex_lock(&(rc->vh_lock));
HASH_FIND_INT(rc->vh, &uid, p);
if(!p)
{
//first vote
p = malloc(sizeof(struct vote_history));
p->uid = uid;
p->vote = vote;
HASH_ADD_INT(rc->vh, uid, p);
pthread_mutex_unlock(&(rc->vh_lock));
return FALSE;
}
else
{
//dup vote
if(p->vote!=0)
{
//remove vote
remove_vote(p->vote);
p->vote = 0;
}
pthread_mutex_unlock(&(rc->vh_lock));
return TRUE;
}
}
void count_vote(unsigned int uid, unsigned int vid, int vote_cnt, int dup_vote)
{
int cnt;
cnt = 0;
while(cnt<vote_cnt)
{
if(dup_vote)
{
if(!check_dup(uid, vid))
{
//not dup
add_vote(vid);
}
}
else
{
add_vote(vid);
}
cnt++;
}
}
int sort_func(struct vote_result *a, struct vote_result *b)
{
return b->count - a->count;
};
void get_vote_result(int rank)
{
struct vote_result *p, *tmp, *maxp;
int i, j, count, uid, hash_cnt;
j = 0;
while(rank>j)
{
count = 0;
hash_cnt = -1;
maxp = NULL;
for(i=0; i<HASH_NUM; i++)
{
if(rc_list[i].vr)
{
HASH_ITER(hh,rc_list[i].vr,p,tmp)
{
if((p->count) > count)
{
uid = p->uid;
count = p->count;
maxp = p;
hash_cnt = i;
}
}
}
}
maxp->count = 0;
printf("%d,%d,%d\n", ++j, uid, count);
}
}