github.com/ledgerwatch/erigon-lib@v1.0.0/sais/utils.c (about)

     1  #include "utils.h"
     2  
     3  int lcp_kasai(const unsigned char *T, int *SA, int *LCP, int *FTR, int *INV, int sa_size, int n)
     4  {
     5      for (int i = 0, j = 0; i < sa_size; i++)
     6      {
     7          if ((SA[i] & 1) == 0)
     8              FTR[j++] = SA[i] >> 1;
     9      }
    10  
    11      for (int i = 0; i < n; i++)
    12          INV[FTR[i]] = i;
    13  
    14      for (int i = 0, k = 0; i < n; i++, k ? k-- : 0)
    15      {
    16          if (INV[i] == n - 1)
    17          {
    18              k = 0;
    19              continue;
    20          }
    21  
    22          int j = FTR[INV[i] + 1];
    23  
    24          while (i + k < n && j + k < n && (int)T[(i + k) * 2] != 0 &&
    25                 (int)T[(j + k) * 2] != 0 && T[(i + k) * 2 + 1] == T[(j + k) * 2 + 1])
    26              k++;
    27  
    28          LCP[INV[i]] = k;
    29      }
    30  
    31      return 0;
    32  }