liquid_feedback_core

changeset 355:accffcefeff7

Code cleanup in "lf_update_suggestion_order" (own block for preparation of the ballot data)
author jbe
date Sat Mar 16 18:29:55 2013 +0100 (2013-03-16)
parents c23bb5ebcdec
children 4698b68e0942
files lf_update_suggestion_order.c
line diff
     1.1 --- a/lf_update_suggestion_order.c	Sat Mar 16 18:23:06 2013 +0100
     1.2 +++ b/lf_update_suggestion_order.c	Sat Mar 16 18:29:55 2013 +0100
     1.3 @@ -75,105 +75,110 @@
     1.4  };
     1.5  
     1.6  static void process_initiative(PGresult *res) {
     1.7 -  void *candidate_tree = NULL;
     1.8    int ballot_count = 0;
     1.9 -  int tuple_count, i;
    1.10 -  char *old_member_id = NULL;
    1.11 -  struct ballot *ballots, *ballot;
    1.12 -  int candidates_in_sections[4] = {0, };
    1.13 -  candidate_count = 0;
    1.14 -  tuple_count = PQntuples(res);
    1.15 -  for (i=0; i<=tuple_count; i++) {
    1.16 -    char *member_id, *suggestion_id;
    1.17 -    if (i<tuple_count) {
    1.18 -      member_id = PQgetvalue(res, i, COL_MEMBER_ID);
    1.19 -      suggestion_id = PQgetvalue(res, i, COL_SUGGESTION_ID);
    1.20 -      if (!candidate_tree || !tfind(suggestion_id, &candidate_tree, (void *)strcmp)) {
    1.21 -        candidate_count++;
    1.22 -        if (!tsearch(suggestion_id, &candidate_tree, (void *)strcmp)) {
    1.23 -          fprintf(stderr, "Insufficient memory\n");
    1.24 -          abort();
    1.25 +  int i;
    1.26 +  struct ballot *ballots;
    1.27 +  {
    1.28 +    void *candidate_tree = NULL;
    1.29 +    int tuple_count;
    1.30 +    char *old_member_id = NULL;
    1.31 +    struct ballot *ballot;
    1.32 +    int candidates_in_sections[4] = {0, };
    1.33 +    candidate_count = 0;
    1.34 +    tuple_count = PQntuples(res);
    1.35 +    for (i=0; i<=tuple_count; i++) {
    1.36 +      char *member_id, *suggestion_id;
    1.37 +      if (i<tuple_count) {
    1.38 +        member_id = PQgetvalue(res, i, COL_MEMBER_ID);
    1.39 +        suggestion_id = PQgetvalue(res, i, COL_SUGGESTION_ID);
    1.40 +        if (!candidate_tree || !tfind(suggestion_id, &candidate_tree, (void *)strcmp)) {
    1.41 +          candidate_count++;
    1.42 +          if (!tsearch(suggestion_id, &candidate_tree, (void *)strcmp)) {
    1.43 +            fprintf(stderr, "Insufficient memory\n");
    1.44 +            abort();
    1.45 +          }
    1.46          }
    1.47        }
    1.48 -    }
    1.49 -    if (i==tuple_count || (old_member_id && strcmp(old_member_id, member_id))) {
    1.50 -      ballot_count++;
    1.51 +      if (i==tuple_count || (old_member_id && strcmp(old_member_id, member_id))) {
    1.52 +        ballot_count++;
    1.53 +      }
    1.54 +      old_member_id = member_id;
    1.55      }
    1.56 -    old_member_id = member_id;
    1.57 -  }
    1.58 -  printf("Candidate count: %i\n", candidate_count);
    1.59 -  candidates = malloc(candidate_count * sizeof(struct candidate));
    1.60 -  if (!candidates) {
    1.61 -    fprintf(stderr, "Insufficient memory\n");
    1.62 -    abort();
    1.63 -  }
    1.64 -  candidate_count = 0;
    1.65 -  twalk(candidate_tree, (void *)register_candidate);
    1.66 -  while (candidate_tree) tdelete(*(void **)candidate_tree, &candidate_tree, (void *)strcmp);
    1.67 -  printf("Ballot count: %i\n", ballot_count);
    1.68 -  ballots = calloc(ballot_count, sizeof(struct ballot));
    1.69 -  if (!ballots) {
    1.70 -    fprintf(stderr, "Insufficient memory\n");
    1.71 -    abort();
    1.72 -  }
    1.73 -  ballot = ballots;
    1.74 -  for (i=0; i<tuple_count; i++) {
    1.75 -    char *member_id, *suggestion_id;
    1.76 -    int weight, preference;
    1.77 -    member_id = PQgetvalue(res, i, COL_MEMBER_ID);
    1.78 -    suggestion_id = PQgetvalue(res, i, COL_SUGGESTION_ID);
    1.79 -    weight = (int)strtol(PQgetvalue(res, i, COL_WEIGHT), (char **)NULL, 10);
    1.80 -    if (weight <= 0) {
    1.81 -      fprintf(stderr, "Unexpected weight value\n");
    1.82 +    printf("Candidate count: %i\n", candidate_count);
    1.83 +    candidates = malloc(candidate_count * sizeof(struct candidate));
    1.84 +    if (!candidates) {
    1.85 +      fprintf(stderr, "Insufficient memory\n");
    1.86        abort();
    1.87      }
    1.88 -    preference = (int)strtol(PQgetvalue(res, i, COL_PREFERENCE), (char **)NULL, 10);
    1.89 -    if (preference < 1 || preference > 4) {
    1.90 -      fprintf(stderr, "Unexpected preference value\n");
    1.91 +    candidate_count = 0;
    1.92 +    twalk(candidate_tree, (void *)register_candidate);
    1.93 +    while (candidate_tree) tdelete(*(void **)candidate_tree, &candidate_tree, (void *)strcmp);
    1.94 +    printf("Ballot count: %i\n", ballot_count);
    1.95 +    ballots = calloc(ballot_count, sizeof(struct ballot));
    1.96 +    if (!ballots) {
    1.97 +      fprintf(stderr, "Insufficient memory\n");
    1.98        abort();
    1.99      }
   1.100 -    preference--;
   1.101 -    ballot->weight = weight;
   1.102 -    ballot->sections[preference].count++;
   1.103 -    if (old_member_id && strcmp(old_member_id, member_id)) ballot++;
   1.104 -    old_member_id = member_id;
   1.105 -  }
   1.106 -  for (i=0; i<ballot_count; i++) {
   1.107 -    int j;
   1.108 -    for (j=0; j<4; j++) {
   1.109 -      if (ballots[i].sections[j].count) {
   1.110 -        ballots[i].sections[j].candidates = malloc(ballots[i].sections[j].count * sizeof(struct candidate *));
   1.111 -        if (!ballots[i].sections[j].candidates) {
   1.112 -          fprintf(stderr, "Insufficient memory\n");
   1.113 -          abort();
   1.114 -        }
   1.115 -      }
   1.116 -    }
   1.117 -  }
   1.118 -  ballot = ballots;
   1.119 -  for (i=0; i<=tuple_count; i++) {
   1.120 -    char *member_id, *suggestion_id;
   1.121 -    int preference;
   1.122 -    if (i<tuple_count) {
   1.123 +    ballot = ballots;
   1.124 +    for (i=0; i<tuple_count; i++) {
   1.125 +      char *member_id, *suggestion_id;
   1.126 +      int weight, preference;
   1.127        member_id = PQgetvalue(res, i, COL_MEMBER_ID);
   1.128        suggestion_id = PQgetvalue(res, i, COL_SUGGESTION_ID);
   1.129 +      weight = (int)strtol(PQgetvalue(res, i, COL_WEIGHT), (char **)NULL, 10);
   1.130 +      if (weight <= 0) {
   1.131 +        fprintf(stderr, "Unexpected weight value\n");
   1.132 +        abort();
   1.133 +      }
   1.134        preference = (int)strtol(PQgetvalue(res, i, COL_PREFERENCE), (char **)NULL, 10);
   1.135        if (preference < 1 || preference > 4) {
   1.136          fprintf(stderr, "Unexpected preference value\n");
   1.137          abort();
   1.138        }
   1.139        preference--;
   1.140 -      ballot->sections[preference].candidates[candidates_in_sections[preference]++] = candidate_by_key(suggestion_id);
   1.141 +      ballot->weight = weight;
   1.142 +      ballot->sections[preference].count++;
   1.143 +      if (old_member_id && strcmp(old_member_id, member_id)) ballot++;
   1.144 +      old_member_id = member_id;
   1.145 +    }
   1.146 +    for (i=0; i<ballot_count; i++) {
   1.147 +      int j;
   1.148 +      for (j=0; j<4; j++) {
   1.149 +        if (ballots[i].sections[j].count) {
   1.150 +          ballots[i].sections[j].candidates = malloc(ballots[i].sections[j].count * sizeof(struct candidate *));
   1.151 +          if (!ballots[i].sections[j].candidates) {
   1.152 +            fprintf(stderr, "Insufficient memory\n");
   1.153 +            abort();
   1.154 +          }
   1.155 +        }
   1.156 +      }
   1.157      }
   1.158 -    if (i==tuple_count || (old_member_id && strcmp(old_member_id, member_id))) {
   1.159 -      ballot++;
   1.160 -      candidates_in_sections[0] = 0;
   1.161 -      candidates_in_sections[1] = 0;
   1.162 -      candidates_in_sections[2] = 0;
   1.163 -      candidates_in_sections[3] = 0;
   1.164 +    ballot = ballots;
   1.165 +    for (i=0; i<=tuple_count; i++) {
   1.166 +      char *member_id, *suggestion_id;
   1.167 +      int preference;
   1.168 +      if (i<tuple_count) {
   1.169 +        member_id = PQgetvalue(res, i, COL_MEMBER_ID);
   1.170 +        suggestion_id = PQgetvalue(res, i, COL_SUGGESTION_ID);
   1.171 +        preference = (int)strtol(PQgetvalue(res, i, COL_PREFERENCE), (char **)NULL, 10);
   1.172 +        if (preference < 1 || preference > 4) {
   1.173 +          fprintf(stderr, "Unexpected preference value\n");
   1.174 +          abort();
   1.175 +        }
   1.176 +        preference--;
   1.177 +        ballot->sections[preference].candidates[candidates_in_sections[preference]++] = candidate_by_key(suggestion_id);
   1.178 +      }
   1.179 +      if (i==tuple_count || (old_member_id && strcmp(old_member_id, member_id))) {
   1.180 +        ballot++;
   1.181 +        candidates_in_sections[0] = 0;
   1.182 +        candidates_in_sections[1] = 0;
   1.183 +        candidates_in_sections[2] = 0;
   1.184 +        candidates_in_sections[3] = 0;
   1.185 +      }
   1.186 +      old_member_id = member_id;
   1.187      }
   1.188 -    old_member_id = member_id;
   1.189    }
   1.190 +
   1.191    free(candidates);
   1.192    for (i=0; i<ballot_count; i++) {
   1.193      int j;
   1.194 @@ -257,9 +262,7 @@
   1.195        escaped_initiative_id = escapeLiteral(db, initiative_id, strlen(initiative_id));
   1.196        if (asprintf(&cmd, "SELECT \"member_id\", \"weight\", \"preference\", \"suggestion_id\" FROM \"individual_suggestion_ranking\" WHERE \"initiative_id\" = %s ORDER BY \"member_id\", \"preference\"", escaped_initiative_id) < 0) {
   1.197          fprintf(stderr, "Could not prepare query string in memory.\n");
   1.198 -        err = 1;
   1.199 -        freemem(escaped_initiative_id);
   1.200 -        break;
   1.201 +        abort();
   1.202        }
   1.203        res2 = PQexec(db, cmd);
   1.204        free(cmd);

Impressum / About Us