PandA-2024.02
kmp.c
Go to the documentation of this file.
1 /*
2 Implementation based on http://www-igm.univ-mlv.fr/~lecroq/string/node8.html
3 */
4 
5 #include "kmp.h"
6 
7 void CPF(char pattern[PATTERN_SIZE], int32_t kmpNext[PATTERN_SIZE]) {
8  int32_t k, q;
9  k = 0;
10  kmpNext[0] = 0;
11 
12  c1 : for(q = 1; q < PATTERN_SIZE; q++){
13  c2 : while(k > 0 && pattern[k] != pattern[q]){
14  k = kmpNext[q];
15  }
16  if(pattern[k] == pattern[q]){
17  k++;
18  }
19  kmpNext[q] = k;
20  }
21 }
22 
23 
24 int kmp(char pattern[PATTERN_SIZE], char input[STRING_SIZE], int32_t kmpNext[PATTERN_SIZE], int32_t n_matches[1]) {
25  int32_t i, q;
26  n_matches[0] = 0;
27 
28  CPF(pattern, kmpNext);
29 
30  q = 0;
31  k1 : for(i = 0; i < STRING_SIZE; i++){
32  k2 : while (q > 0 && pattern[q] != input[i]){
33  q = kmpNext[q];
34  }
35  if (pattern[q] == input[i]){
36  q++;
37  }
38  if (q >= PATTERN_SIZE){
39  n_matches[0]++;
40  q = kmpNext[q - 1];
41  }
42  }
43  return 0;
44 }
TVMArray c2[1]
TVMArray c1[1]
#define STRING_SIZE
Definition: kmp.h:11
int input[SIZE]
Definition: hash.h:1
#define PATTERN_SIZE
Definition: kmp.h:10
int kmp(char pattern[PATTERN_SIZE], char input[STRING_SIZE], int32_t kmpNext[PATTERN_SIZE], int32_t n_matches[1])
Definition: kmp.c:24
static const uint32_t k[]
Definition: sha-256.c:22
void CPF(char pattern[PATTERN_SIZE], int32_t kmpNext[PATTERN_SIZE])
Definition: kmp.c:7

Generated on Mon Feb 12 2024 13:02:50 for PandA-2024.02 by doxygen 1.8.13