problem with compression algorithm LZ78



Hell_o
:)
I've got problem with LZ78 compression

I've got code:



typedef struct {
unsigned short int addr;
unsigned short int index;
} AddrPos;

- src - in data bufor
- len - number of data to encode
- buf - out data bufor
unsigned int compZL(char *src, unsigned int len, char *buf)
{
assert(src && buf);
map<std::string, AddrPos> dict;
map<std::string, AddrPos>::iterator iter;
string str = "";
AddrPos last;
unsigned short int offset = 0, index = 1;
unsigned short int *header = (unsigned short int*)buf;
unsigned int i, size = 0;

*header++ = (unsigned short int)len;
buf += 2 * sizeof(short int);
for(i = 0 ; i < len ; i++)
{
str += *src;
iter = dict.find(str);
if ( iter == dict.end())
{ //if not in dictionary

last.addr = index++;
last.index = offset;
dict[str] =
last; /*here I've got a
problem*/

*buf++ = *src;
*((unsigned short int*)buf) = offset;
buf += 2;
str = "";
size++;
offset = 0;
}
else offset = iter->second.addr;
src++;
}

if (!str.empty()) {
*buf++ = *(--src);
if (str.length() == 1) offset = 0;

*((unsigned short int*)buf) = offset;
size++;
}

size = size * 3 + 4;
*header = (unsigned short int)size;
return size;
}

Unfortunately i can compress data less then 330B [and change "short
int" to "long" didn't help ]
<b>
What should I change to encode larger data?
</b>

Thx for any help.
Liriann.
.



Relevant Pages