: : : Hi,
: : :
: : : Is there anyone here who could translate this C Code into VB? Or is it possible to link VB and C/C++ codes using the MSVisual Studio?? Thanks!
: : :
: : : #define REHASH(a, b, h) ((((h) - (a)*d) << 1) + (b))
: : :
: : : void KR(char *x, int m, char *y, int n) {
: : : int d, hx, hy, i, j;
: : :
: : : /* Preprocessing */
: : : /* computes d = 2^(m-1) with
: : : the left-shift operator */
: : : for (d = i = 1; i < m; ++i)
: : : d = (d<<1);
: : :
: : : for (hy = hx = i = 0; i < m; ++i) {
: : : hx = ((hx<<1) + x[i]);
: : : hy = ((hy<<1) + y[i]);
: : : }
: : :
: : : /* Searching */
: : : j = 0;
: : : while (j <= n-m) {
: : : if (hx == hy && memcmp(x, y + j, m) == 0)
: : : OUTPUT(j);
: : : hy = REHASH(y[j], y[j + m], hy);
: : : ++j;
: : : }
: : :
: : : }
: : :
: :
: : Well... here it goes:
: :
: : 'Function Rehash (note: d has to be supplied as well!)
: : Function Rehash(a As Integer, b As Integer, h As Integer, _
: : d As Integer) As Integer
: : Rehash = (h - a * d) * 2 + b
: : End Function
: :
: : Sub KR(ByRef x() As Byte, ByVal m As Integer, ByRef y() As Byte, _
: : ByVal n As Integer)
: : '% stands for As Integer
: : Dim d%, hx%, hy%, i%, j As Integer
: :
: : ' /* Preprocessing */
: : ' /* computes d = 2^(m-1) with
: : 'the left-shift operator */
: : d = 2 ^ (m - 1)
: : i = m - 1
: : hx = i
: : hy = hx
: : For i = 0 To m - 1
: : hx = hx * 2 + x(i)
: : hy = hy * 2 + y(i)
: : Next
: :
: : '/* Searching */
: : j = 0
: : Do While (j <= n-m)
: : 'I am not really sure if I got this one right... Sure hope so
: : If (hx = hy And CompareArrays(x, y, j, m)) Then
: : 'OUTPUT(j); '//Don't know what this does
: : End If
: : hy = Rehash(CInt(y(j)), CInt(y(j + m)), hy, d)
: : j = j + 1
: : Loop
: : End Sub
: :
: : Function CompareArrays(x() As Byte, y() As Byte, j As Integer, _
: : m As Integer) As Boolean
: : Dim i As Integer
: : Dim bSame As Boolean
: :
: : bSame = True
: : For i = j To m - 1
: : If (x(i) <> y(i)) Then
: : bSame = False
: : Exit For
: : End If
: : Next i
: :
: : CompareArrays = bSame
: : End Function
: :
: : I think this is correct, though I am not able to test it because I don't know what paramaters to supply to the function.
: : Greets...
: : Richard
:
: Just a quick note:
:
: memcmp(x, y + j, m)
:
: means basically "compare the m bytes starting from the address x with the same number of bytes starting from the address y + j". In other words, given that x and y are 0-based arrays of bytes, so the loop for CompareArrays should be:
:
:
: for i = 0 to m - 1
: If (x(i) <> y(i + j)) Then
: bSame = False
: Exit For
: End If
: next i
:
:
: It would be *very* wise to pass any parameter other than pointers in the original C code (j and m in the CompareArrays function) as ByVal, just in case (C has no idea of what ByVal and ByRef mean - it calls them respectively "copying the data", or "copying the pointer").
: Also, I'd declare them as Longs - not that it probably matters,however.
:
Thanks so much for your replies :) I'll try it out!!