liquid_feedback_core
changeset 353:31ce1877320b
Extended "lf_update_suggestion_order" to create a candidate structure
author | jbe |
---|---|
date | Sat Mar 16 18:19:15 2013 +0100 (2013-03-16) |
parents | 98c14d8d07f1 |
children | c23bb5ebcdec |
files | lf_update_suggestion_order.c |
line diff
1.1 --- a/lf_update_suggestion_order.c Sat Mar 16 17:22:01 2013 +0100 1.2 +++ b/lf_update_suggestion_order.c Sat Mar 16 18:19:15 2013 +0100 1.3 @@ -29,32 +29,44 @@ 1.4 #define COL_PREFERENCE 2 1.5 #define COL_SUGGESTION_ID 3 1.6 1.7 -static int candidate_count; 1.8 -static char **candidates; 1.9 +struct candidate { 1.10 + char *key; 1.11 + double score; 1.12 + int seat; 1.13 +}; 1.14 + 1.15 +static int compare_candidate(struct candidate *c1, struct candidate *c2) { 1.16 + return strcmp(c1->key, c2->key); 1.17 +} 1.18 1.19 -static void register_candidate(char **candidate, VISIT visit, int level) { 1.20 +static int candidate_count; 1.21 +static struct candidate *candidates; 1.22 + 1.23 +static void register_candidate(char **candidate_key, VISIT visit, int level) { 1.24 if (visit == postorder || visit == leaf) { 1.25 - candidates[candidate_count++] = *candidate; 1.26 + struct candidate *candidate; 1.27 + candidate = candidates + (candidate_count++); 1.28 + candidate->key = *candidate_key; 1.29 + candidate->score = 0.0; 1.30 + candidate->seat = 0; 1.31 } 1.32 } 1.33 1.34 -static int ptrstrcmp(char **s1, char **s2) { 1.35 - return strcmp(*s1, *s2); 1.36 -} 1.37 - 1.38 -static int candidate_number(char *candidate) { 1.39 - char **addr; 1.40 - addr = bsearch(&candidate, candidates, candidate_count, sizeof(char *), (void *)ptrstrcmp); 1.41 - if (!addr) { 1.42 +static struct candidate *candidate_by_key(char *candidate_key) { 1.43 + struct candidate *candidate; 1.44 + struct candidate compare; 1.45 + compare.key = candidate_key; 1.46 + candidate = bsearch(&compare, candidates, candidate_count, sizeof(struct candidate), (void *)compare_candidate); 1.47 + if (!candidate) { 1.48 fprintf(stderr, "Candidate not found (should not happen)\n"); 1.49 abort(); 1.50 } 1.51 - return addr - candidates; 1.52 + return candidate; 1.53 } 1.54 1.55 struct ballot_section { 1.56 int count; 1.57 - int *candidates; 1.58 + struct candidate **candidates; 1.59 }; 1.60 1.61 struct ballot { 1.62 @@ -90,7 +102,7 @@ 1.63 old_member_id = member_id; 1.64 } 1.65 printf("Candidate count: %i\n", candidate_count); 1.66 - candidates = malloc(candidate_count * sizeof(char *)); 1.67 + candidates = malloc(candidate_count * sizeof(struct candidate)); 1.68 if (!candidates) { 1.69 fprintf(stderr, "Insufficient memory\n"); 1.70 abort(); 1.71 @@ -105,36 +117,32 @@ 1.72 abort(); 1.73 } 1.74 ballot = ballots; 1.75 - for (i=0; i<=tuple_count; i++) { 1.76 + for (i=0; i<tuple_count; i++) { 1.77 char *member_id, *suggestion_id; 1.78 int weight, preference; 1.79 - if (i<tuple_count) { 1.80 - member_id = PQgetvalue(res, i, COL_MEMBER_ID); 1.81 - suggestion_id = PQgetvalue(res, i, COL_SUGGESTION_ID); 1.82 - weight = (int)strtol(PQgetvalue(res, i, COL_WEIGHT), (char **)NULL, 10); 1.83 - if (weight <= 0) { 1.84 - fprintf(stderr, "Unexpected weight value\n"); 1.85 - abort(); 1.86 - } 1.87 - preference = (int)strtol(PQgetvalue(res, i, COL_PREFERENCE), (char **)NULL, 10); 1.88 - if (preference < 1 || preference > 4) { 1.89 - fprintf(stderr, "Unexpected preference value\n"); 1.90 - abort(); 1.91 - } 1.92 - preference--; 1.93 - ballot->weight = weight; 1.94 - ballot->sections[preference].count++; 1.95 + member_id = PQgetvalue(res, i, COL_MEMBER_ID); 1.96 + suggestion_id = PQgetvalue(res, i, COL_SUGGESTION_ID); 1.97 + weight = (int)strtol(PQgetvalue(res, i, COL_WEIGHT), (char **)NULL, 10); 1.98 + if (weight <= 0) { 1.99 + fprintf(stderr, "Unexpected weight value\n"); 1.100 + abort(); 1.101 } 1.102 - if (i==tuple_count || (old_member_id && strcmp(old_member_id, member_id))) { 1.103 - ballot++; 1.104 + preference = (int)strtol(PQgetvalue(res, i, COL_PREFERENCE), (char **)NULL, 10); 1.105 + if (preference < 1 || preference > 4) { 1.106 + fprintf(stderr, "Unexpected preference value\n"); 1.107 + abort(); 1.108 } 1.109 + preference--; 1.110 + ballot->weight = weight; 1.111 + ballot->sections[preference].count++; 1.112 + if (old_member_id && strcmp(old_member_id, member_id)) ballot++; 1.113 old_member_id = member_id; 1.114 } 1.115 for (i=0; i<ballot_count; i++) { 1.116 int j; 1.117 for (j=0; j<4; j++) { 1.118 if (ballots[i].sections[j].count) { 1.119 - ballots[i].sections[j].candidates = malloc(ballots[i].sections[j].count * sizeof(int)); 1.120 + ballots[i].sections[j].candidates = malloc(ballots[i].sections[j].count * sizeof(struct candidate *)); 1.121 if (!ballots[i].sections[j].candidates) { 1.122 fprintf(stderr, "Insufficient memory\n"); 1.123 abort(); 1.124 @@ -155,7 +163,7 @@ 1.125 abort(); 1.126 } 1.127 preference--; 1.128 - ballot->sections[preference].candidates[candidates_in_sections[preference]++] = candidate_number(suggestion_id); 1.129 + ballot->sections[preference].candidates[candidates_in_sections[preference]++] = candidate_by_key(suggestion_id); 1.130 } 1.131 if (i==tuple_count || (old_member_id && strcmp(old_member_id, member_id))) { 1.132 ballot++;