From: Maximilian Szengel Date: Wed, 6 Jun 2012 10:54:07 +0000 (+0000) Subject: Better proofs X-Git-Tag: initial-import-from-subversion-38251~13247 X-Git-Url: https://git.librecmc.org/?a=commitdiff_plain;h=40c65ed83cc3ab82c87e65e3291b95fcd214fd02;p=oweals%2Fgnunet.git Better proofs --- diff --git a/src/regex/regex.c b/src/regex/regex.c index 551610baa..72ed0f81c 100644 --- a/src/regex/regex.c +++ b/src/regex/regex.c @@ -817,6 +817,7 @@ automaton_create_proofs (struct GNUNET_REGEX_Automaton *a) char *R_last[a->state_count][a->state_count]; char *R_cur[a->state_count][a->state_count]; char *temp; + int length; k = 0; n = a->state_count; @@ -849,16 +850,17 @@ automaton_create_proofs (struct GNUNET_REGEX_Automaton *a) } } + if (NULL != R_last[i][j] && 1 < strlen (R_last[i][j])) + { + temp = R_last[i][j]; + GNUNET_asprintf (&R_last[i][j], "(%s)", R_last[i][j]); + GNUNET_free (temp); + } + if (i == j) { if (NULL == R_last[i][j]) GNUNET_asprintf (&R_last[i][j], ""); - else if (1 < strlen (R_last[i][j])) - { - temp = R_last[i][j]; - GNUNET_asprintf (&R_last[i][j], "(%s)", R_last[i][j]); - GNUNET_free (temp); - } } } } @@ -870,33 +872,51 @@ automaton_create_proofs (struct GNUNET_REGEX_Automaton *a) { for (j = 0; j < n; j++) { + // 0*R = R*0 = 0 + // 0+R = R+0 = R if (NULL == R_last[i][k] || NULL == R_last[k][j]) { if (NULL != R_last[i][j]) R_cur[i][j] = GNUNET_strdup (R_last[i][j]); } - else if (NULL == R_last[i][j] || - 0 == strcmp (R_last[i][j], R_last[i][k])) - { - if ((R_last[k][k][0] == '(' && - R_last[k][k][strlen (R_last[k][k]) - 1] == ')') || - (1 == strlen (R_last[k][k]))) - GNUNET_asprintf (&R_cur[i][j], "(%s%s*%s)", R_last[i][k], - R_last[k][k], R_last[k][j]); - else - GNUNET_asprintf (&R_cur[i][j], "(%s(%s)*%s)", R_last[i][k], - R_last[k][k], R_last[k][j]); - } else { - if ((R_last[k][k][0] == '(' && - R_last[k][k][strlen (R_last[k][k]) - 1] == ')') || - (1 == strlen (R_last[k][k]))) - GNUNET_asprintf (&R_cur[i][j], "(%s|%s%s*%s)", R_last[i][j], - R_last[i][k], R_last[k][k], R_last[k][j]); + // R(k)ij = R(k-1)ij + R(k-1)ik (R(k-1)kk)* R(k-1)kj + length = + snprintf (NULL, 0, "(%s|%s(%s)*%s)", R_last[i][j], R_last[i][k], + R_last[k][k], R_last[k][j]) + 1; + char *left = GNUNET_malloc (length); + char *right = GNUNET_malloc (length); + + if (NULL != R_last[i][j]) + strcat (left, R_last[i][j]); + + if (NULL != R_last[i][k]) + strcat (right, R_last[i][k]); + + if (NULL != R_last[k][k] && 0 != strcmp (R_last[k][k], "")) + { + strcat (right, "("); + strcat (right, R_last[k][k]); + strcat (right, ")*"); + } + + if (NULL != R_last[k][j]) + strcat (right, R_last[k][j]); + + if (0 == strcmp (left, right) || 0 == strcmp (left, "") || + 0 == strcmp (right, "")) + { + if (0 == strcmp (left, "")) + GNUNET_asprintf (&R_cur[i][j], "%s", right); + else + GNUNET_asprintf (&R_cur[i][j], "%s", left); + } else - GNUNET_asprintf (&R_cur[i][j], "(%s|%s(%s)*%s)", R_last[i][j], - R_last[i][k], R_last[k][k], R_last[k][j]); + GNUNET_asprintf (&R_cur[i][j], "(%s|%s)", left, right); + + GNUNET_free_non_null (left); + GNUNET_free_non_null (right); } } } @@ -917,10 +937,12 @@ automaton_create_proofs (struct GNUNET_REGEX_Automaton *a) } } - // assign proofs + // assign proofs and hashes for (i = 0; i < n; i++) { states[i]->proof = GNUNET_strdup (R_last[a->start->marked][i]); + GNUNET_CRYPTO_hash (states[i]->proof, strlen (states[i]->proof), + &states[i]->hash); } // cleanup diff --git a/src/regex/test_regex_iterate_api.c b/src/regex/test_regex_iterate_api.c index 51ebbcd88..6c5b0e55b 100644 --- a/src/regex/test_regex_iterate_api.c +++ b/src/regex/test_regex_iterate_api.c @@ -61,10 +61,14 @@ main (int argc, char *argv[]) error = 0; /*regex = "ab(c|d)+c*(a(b|c)+d)+(bla)+"; */ + /*regex = "ab(c|d)+c*(a(b|c)+d)+(bla)(bla)*"; */ /*regex = "z(abc|def)?xyz"; */ - regex = "1*0(0|1)*"; + /*regex = "1*0(0|1)*"; */ /*regex = "a+X*y+c|p|R|Z*K*y*R+w|Y*6+n+h*k*w+V*F|W*B*e*"; */ - + /*regex = "abcd:(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)"; */ + /*regex = "abc(1|0)*def"; */ + /*regex = "ab|ac"; */ + regex = "(ab)(ab)*"; dfa = GNUNET_REGEX_construct_dfa (regex, strlen (regex)); GNUNET_REGEX_automaton_save_graph (dfa, "dfa.dot"); GNUNET_REGEX_iterate_all_edges (dfa, key_iterator, NULL);