-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrevDist.c
More file actions
86 lines (72 loc) · 1.49 KB
/
Copy pathrevDist.c
File metadata and controls
86 lines (72 loc) · 1.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include <stdio.h>
#include <stdlib.h>
#include <R.h>
#include <Rdefines.h>
#include <Rmath.h>
#include <math.h>
//Algorithmes de calcul de distance de chaîne de caractère
SEXP orderDist(SEXP vA, SEXP vB){
//input : two integer vector
int n;
int i = 0;
int* a;
int* b;
long res = 0;
int* compTab;
SEXP resR;
n=length(vA);
a = INTEGER(vA);
b = INTEGER(vB);
compTab=Calloc(n,int);
PROTECT(resR=allocVector(INTSXP,1));
for(i=0;i<n;i++) compTab[b[i]-1]=i;
for(i=0;i<n;i++) res+= abs(compTab[a[i]-1]-i);
INTEGER(resR)[0]=res;
Free(compTab);
UNPROTECT(1);
return resR;
}
SEXP LevDist(SEXP vA, SEXP vB){
//LevensteinDistance
//input : two integer vector
int ncols;
int nrows;
int i = 0;
int j = 0;
int* a;
int* b;
long** Mat;
long del;
long ins;
long sub;
long cost;
SEXP resR;
nrows=length(vA);
ncols=length(vB);
a = INTEGER(vA);
b = INTEGER(vB);
Mat=Calloc(nrows,long*);
for(i=0;i<=nrows;i++){
*Mat = Calloc(ncols,long);
Mat[i][0]=i;
}
for(j=0;j<=ncols;j++) Mat[0][j]=j;
PROTECT(resR=allocVector(INTSXP,1));
printf("ready2");
for(i=1;i<=nrows;i++){
for(j=1;j<=ncols;j++){
cost = (a[i-1]==b[j-1]) ? 0 : 1;
del=Mat[i-1][j]+1;
ins=Mat[i][j-1]+1;
sub=Mat[i-1][j-1]+cost;
if(del>=ins && del>=sub) Mat[i][j]=del;
if(ins>=del && ins>=sub) Mat[i][j]=ins;
if(sub>=ins && sub>=del) Mat[i][j]=sub;
}
}
INTEGER(resR)[0]=Mat[nrows][ncols];
for(i=0;i<=nrows;i++) Free(Mat[i]);
Free(Mat);
UNPROTECT(1);
return resR;
}