src/or/consdiff.c
static int *
lcs_lengths(smartlist_slice_t *slice1, smartlist_slice_t *slice2,
            int direction)
{
  size_t a_size = sizeof(int) * (slice2->len+1);

  /* Resulting lcs lengths. */
  int *result = tor_malloc_zero(a_size);
  /* Copy of the lcs lengths from the last iteration. */
  int *prev = tor_malloc(a_size);

  tor_assert(direction == 1 || direction == -1);

  int si = slice1->offset;
  if (direction == -1) {
    si += (slice1->len-1);
  }

  for (int i = 0; i < slice1->len; ++i, si+=direction) {

    const char *line1 = smartlist_get(slice1->list, si);
    /* Store the last results. */
    memcpy(prev, result, a_size);

    int sj = slice2->offset;
    if (direction == -1) {
      sj += (slice2->len-1);
    }

    for (int j = 0; j < slice2->len; ++j, sj+=direction) {

      const char *line2 = smartlist_get(slice2->list, sj);
      if (!strcmp(line1, line2)) {
        /* If the lines are equal, the lcs is one line longer. */
        result[j + 1] = prev[j] + 1;
      } else {
        /* If not, see what lcs parent path is longer. */
        result[j + 1] = MAX(result[j], prev[j + 1]);
      }
    }
  }
  tor_free(prev);
  return result;
}
/** Helper: Compute the longest common subsequence lengths for the two slices.
 * Used as part of the diff generation to find the column at which to split
 * slice2 while still having the optimal solution.
 * If direction is -1, the navigation is reversed. Otherwise it must be 1.
 * The length of the resulting integer array is that of the second slice plus
 * one.
 */
Compute the longest common subsequence lengths for the two slices.
Used as part of the diff generation to find the column at which to split
slice2 while still having the optimal solution.
If direction is -1, the navigation is reversed. Otherwise it must be 1.
The length of the resulting integer array is that of the second slice plus
one.
Compute the longest common subsequence lengths for the two slices . Used as part of the diff generation to find the column at which to split slice2 while still having the optimal solution . If direction is - 1 the navigation is reversed . Otherwise it must be 1 . The length of the resulting integer array is that of the second slice plus one .
static int *
lcs_lengths(smartlist_slice_t *slice1, smartlist_slice_t *slice2,
            int direction)
{
  size_t a_size = sizeof(int) * (slice2->len+1);
  int *result = tor_malloc_zero(a_size);
  int *prev = tor_malloc(a_size);
  tor_assert(direction == 1 || direction == -1);
  int si = slice1->offset;
  if (direction == -1) {
    si += (slice1->len-1);
  }
  for (int i = 0; i < slice1->len; ++i, si+=direction) {
    const char *line1 = smartlist_get(slice1->list, si);
    memcpy(prev, result, a_size);
    int sj = slice2->offset;
    if (direction == -1) {
      sj += (slice2->len-1);
    }
    for (int j = 0; j < slice2->len; ++j, sj+=direction) {
      const char *line2 = smartlist_get(slice2->list, sj);
      if (!strcmp(line1, line2)) {
        result[j + 1] = prev[j] + 1;
      } else {
        result[j + 1] = MAX(result[j], prev[j + 1]);
      }
    }
  }
  tor_free(prev);
  return result;
}
static int * lcs_lengths ( smartlist_slice_t * slice1 , smartlist_slice_t * slice2 , int direction ) { size_t a_size = sizeof ( int ) * ( slice2 -> len + 1 ) ; int * result = tor_malloc_zero ( a_size ) ; int * prev = tor_malloc ( a_size ) ; tor_assert ( direction == 1 || direction == -1 ) ; int si = slice1 -> offset ; if ( direction == -1 ) { si += ( slice1 -> len - 1 ) ; } for ( int i = 0 ; i < slice1 -> len ; ++ i , si += direction ) { const char * line1 = smartlist_get ( slice1 -> list , si ) ; memcpy ( prev , result , a_size ) ; int sj = slice2 -> offset ; if ( direction == -1 ) { sj += ( slice2 -> len - 1 ) ; } for ( int j = 0 ; j < slice2 -> len ; ++ j , sj += direction ) { const char * line2 = smartlist_get ( slice2 -> list , sj ) ; if ( ! strcmp ( line1 , line2 ) ) { result [ j + 1 ] = prev [ j ] + 1 ; } else { result [ j + 1 ] = MAX ( result [ j ] , prev [ j + 1 ] ) ; } } } tor_free ( prev ) ; return result ; }
Helper: Compute the longest common subsequence lengths for the two slices.
Helper : Compute the longest common subsequence lengths for the two slices .
/* Resulting lcs lengths. */ /* Copy of the lcs lengths from the last iteration. */ /* Store the last results. */ /* If the lines are equal, the lcs is one line longer. */ /* If not, see what lcs parent path is longer. */