I've learned LZ77 in theory, know I wanna practice, my first attempt was to code a C++ function that finds the best match when comparing the sliding window with the look ahead buffer, this is what i came up with: (btw it must return a array of int, 0->contains distance to run backwards from the lookahead buffer 1->contains how long the match is, it cant be less then 2)
Code:
#include <iostream>
#include <string>
using namespace std;
int * find_match(char * sliding_window, char * look_ahead)
{
int distance = 0;
int lenght = 0;
// for loop runs every character pointed in var sliding_window
// it searches for a character match longer than 2
for(int x = 0; *(sliding_window+x) != '\0'; x++)
{
if(sliding_window[x] == look_ahead[0])
if(sliding_window[x+1] == look_ahead[1])
if(sliding_window[x+2] == look_ahead[2])
{
//if reached here, then it means a match longer than 2 bytes has been found
// distance = ???? dont know how to calculate distance
lenght++; //previously 2, but 2 has already been checked if it matches, and it does
while(sliding_window[x+lenght] == look_ahead[lenght])
lenght++;
}
if(lenght > 0) // if it's bigger than 0, then it means it's already found a match
break;
}
if(distance != 0 && lenght != 0)
{
int return_array[] = {distance,lenght};
return return_array;
}
else
{
return 0;
}
}
int main()
{
char * str1 = "Just testing my function.";
char * str2 = "I'm just testing.";
int lenght_distance[] = find_match(str1, str2);
cout << "distance: " << lenght_distance[0];
return 0;
}
Somebody help me at making this function work, it's awful so far. And I can't call it on main without bumping into error report.