Android Question How to convert from C code to b4j ???

adaw

Member
Hi guys,
I have C code, and I want to convert it to b4j...

this is the C code :
B4X:
void preQsBc (char *x, int m, int qsBc[]) {
    int i;
   
    for (i = 0; i < ASIZE; ++i)
        qsBc[i] = m + 1;
    for (i = 0; i < m; ++i)
        qsBc[x[i]] = m - i;
}

typedef struct patternScanOrder {
    int loc;
    char c;
} pattern;

int freq[ASIZE];

/* Construct an ordered pattern from a string. */
void orderPattern(char *x, int m, int (*pcmp)(), pattern *pat) {

    int i;
   
    for (i=0  i <= m; ++i) {
        pat[i].loc = i;
        pat[i].c = x[i];
    }
    qsort(pat, m, sizeof(pattern), pcmp);
}

/* Optimal Mismatch pattern comparison function. */
int optimalPcmp(pattern *pat1, pattern *pat2) {
    float fx;
   
    fx = freq[pat1->c] - freq[pat2->c];
    return(fx ? (fx > 0 ? 1 : -1) : (pat2->loc - pat1->loc));
}

/* Find the next leftward matching shift for the first ploc pattern elements after a current shift or lshift. *\
int matchShift (char *x, int m, int ploc, int lshift, pattern *pat) {
   
    int i, j;
   
    for (; lshift < m; ++lshift) {
        i = ploc;
        while (--i >= 0) {
            if ((j = (pat[i].loc - lshift)) < 0)
                continue;
            if (pat[i].c != x[j])
                break;
        }
        if (i < 0)
            break;
    }
    return(lshift);
}

/* Constructs the good-suffix shift table from an ordered string. */
void preAdaptedGs(char *x, int m, int adaptedGs[], pattern *pat) {
   
    int lshift, i, ploc
   
    adaptedGs[0] = lshift = 1;
    for (ploc = 1; ploc <= m; ++ploc) {
        lshift = matchShift(x, m, ploc, lshift, pat);
        adaptedGs[ploc] = lshift;
    }
    for (ploc = 0; ploc <= m; ++ploc) {
        lshift = adaptedGs[ploc];
        while (lshift < m) {
            i = pat[ploc].loc - lshift;
            if (i < 0 || pat[ploc].c != x[i])
                break;
            ++lshift;
            lshift = matchShift(x, m, ploc, lshift, pat);
        }
        adaptedGs[ploc] = lshift;
    }
}

/* Optimal Mismatch string matching algoritm. */
void OM (char *x, int m, char *y, int n) {
    int i, j, adaptedGs[XSIZE], qsBc[ASIZE];
    pattern pat[XSIZE];
   
    /* Preprocessing */
    orderPattern(x, m, optimalPcmp, pat);
    preQsBc(x, m, qsBc);
    preAdaptedGs(x, m, adaptedGs, pat);
   
    /* Searching */
    j = 0;
    while (j <= n-m) {
        i = 0;
        while (i < m && pat[i].c == y[j + pat[i].loc])
            ++i;
        if (i >= m)
            OUTPUT(j);
        j += MAX(adaptedGs[i], qsBc[y[j+m]]);
    }
}

thanks
 

adaw

Member
1. You have posted your question in B4A forum.
2. You shouldn't ask us to port your code. You should try to do it yourself and if you have any specific question then post the question.

I'm sorry ...
can you help me to convert this code :

B4X:
/* Optimal Mismatch pattern comparison function. */
int optimalPcmp(pattern *pat1, pattern *pat2) {
    float fx;
   
    fx = freq[pat1->c] - freq[pat2->c];
    return(fx ? (fx > 0 ? 1 : -1) : (pat2->loc - pat1->loc));
}

please help me to convert this code only...
I really confused...
 
Upvote 0

adaw

Member
It is incomplete. What is pattern?

pattern is a type. Pattern is name of type.
I try to convert it . this is a code that I made :
B4X:
Sub optimalPcmp(m As String, n As String) As Pattern
    Dim pat1, pat2 As Pattern
    pat1.Initialize
    pat2.Initialize
    fx = Freq(pat1.c, pat2.c)
    Return If fx <> 0 Then
                If fx > 0 Then fx = 1 : fx = -1
    : fx = pat2.Loc - pat1.Loc
    End If
End Sub

but I get a warning when I compile it...
the warning is :
Object reference not set to an instance of an object
 
Upvote 0

ilan

Expert
Licensed User
Longtime User

/* Optimal Mismatch pattern comparison function. */
int optimalPcmp(pattern *pat1, pattern *pat2) {
float fx;

fx = freq[pat1->c] - freq[pat2->c];return(fx ? (fx > 0 ? 1 : -1) : (pat2->loc - pat1->loc));}

according to your code fx is a float but you try to return a pattern this wont work!

B4X:
Sub optimalPcmp(m As String, n As String) As Pattern
 
Last edited:
Upvote 0

ilan

Expert
Licensed User
Longtime User
another mistake is that you intialize 2 new objects:

B4X:
Dim pat1, pat2 As Pattern

and then without giving them any values you use them and try to get something

B4X:
Dim pat1, pat2 As Pattern
    pat1.Initialize
    pat2.Initialize
    fx = Freq(pat1.c, pat2.c)

if you intialize an object it will probably be NULL or 0 or "" (depends on the object type) so you cannot use it before you give it a value.

B4X:
fx = Freq(pat1.c, pat2.c)
 
Upvote 0

wonder

Expert
Licensed User
Longtime User
B4X:
'In Process_Globals
Type Pattern(loc As Int, c As Char)

'==========================================================

'Optimal Mismatch pattern comparison function.
Sub optimalPcmp(pat1 As Pattern, pat2 As Pattern) As Int
    Dim fx = freq(Asc(pat1.c)) - freq(Asc(pat2.c)) As Float
    If fx = 0 Then Return pat2.loc - pat1.loc
    If fx > 0 Then Return 1
    Return -1   
End Sub

This, perhaps?
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…