c to java

A

ak

Hi folks,

I try to convert following c code to java.
Any help is appreciated.


struct decode {
struct decode *branch[2];
int leaf;
} first_decode[32], second_decode[512];

memset( first_decode, 0, sizeof first_decode);
memset(second_decode, 0, sizeof second_decode);

void doSomething(int count) {
struct decode *decode, *dindex;

while (count--) {
decode = first_decode;
for (i=0; i < 64; i++ ) {
for (dindex=decode; dindex->branch[0];)
dindex = dindex->branch[getbits(1)];
leaf = dindex->leaf;
decode = second_decode;
}
}

void makeSomething(struct decode *dest, const uchar *source, int level) {
static struct decode *free;
static int leaf;
int i, next;

if (level==0) {
free = dest;
leaf = 0;
}
free++;

for(i=next=0; i <= leaf && next < 16; )
i += source[next++];

if(level < next) {
dest->branch[0] = free;
make_decoder(free,source,level+1);
dest->branch[1] = free;
make_decoder(free,source,level+1);
}
else
dest->leaf = source[16 + leaf++];
}
 
A

ak

It seems to be really difficult job.

struct decode looks like a tree node.
So we have the tree in array (first_decode and second_decode).



----- Original Message -----
From: "Gordon Beaton" <[email protected]>
Newsgroups: comp.lang.java.programmer
Sent: Saturday, October 11, 2003 12:18 PM
Subject: Re: c to java
 
A

ak

I know such trivial things.

the problem ist, I must implement '++' operation for decode;

void makeSomething(struct decode *dest, const uchar *source, int level) {
static struct decode *free;
if (level==0) {
free = dest;
leaf = 0;
}
free++;
}



Tor Iver Wilhelmsen said:
ak said:
struct decode {
struct decode *branch[2];
int leaf;
} first_decode[32], second_decode[512];

Make this a class. Pointers are implicit when operating with variables
of non-primitive types.
memset( first_decode, 0, sizeof first_decode);
memset(second_decode, 0, sizeof second_decode);

These are implicit in Java.
for (dindex=decode; dindex->branch[0];)
dindex = dindex->branch[getbits(1)];
leaf = dindex->leaf;

Java uses the dot operator "." everywhere, not the -> variety.
void makeSomething(struct decode *dest, const uchar *source, int level)
{

uchar-pointer = String
static struct decode *free;
static int leaf;

Java doesn't have static method-local variables, you may want to move
them out.
 
T

Tor Iver Wilhelmsen

ak said:
struct decode {
struct decode *branch[2];
int leaf;
} first_decode[32], second_decode[512];

Make this a class. Pointers are implicit when operating with variables
of non-primitive types.
memset( first_decode, 0, sizeof first_decode);
memset(second_decode, 0, sizeof second_decode);

These are implicit in Java.
for (dindex=decode; dindex->branch[0];)
dindex = dindex->branch[getbits(1)];
leaf = dindex->leaf;

Java uses the dot operator "." everywhere, not the -> variety.
void makeSomething(struct decode *dest, const uchar *source, int level) {

uchar-pointer = String
static struct decode *free;
static int leaf;

Java doesn't have static method-local variables, you may want to move
them out.
 
M

Marco Schmidt

ak:
the problem ist, I must implement '++' operation for decode;

void makeSomething(struct decode *dest, const uchar *source, int level) {
static struct decode *free;
if (level==0) {
free = dest;
leaf = 0;
}
free++;
}

There are no pointer arithmetics in Java. Define an index into an
array of Decode objects and increase the index. Or use an Iterator on
a list of Decode objects.

Regards,
Marco
 
A

ak

the problem ist, I must implement '++' operation for decode;
No, you need to learn that Java has arrays but not pointer arithmetic. Thank you, I know it alredy.
Use an int index instead.
There are 2 arrays, so I must track, which array is current.
 
J

Jeffrey D. Smith

ak said:
There are 2 arrays, so I must track, which array is current.

Then you've answered your question. Use a field
that tracks the current instance (where the C
code is using a local static). Don't know what
a field is? A field holds a value, like a reference
or a primitive type. A field can be static (a single
location in a class type), or it can be an instance
field (a location for each instance of a class).

Set the field to the current array. When you change
arrays (or whatever you're looking at), assign a
new value to the field.

'++' works fine on integer values. Use an integer
value as an index into the array of "decode" things.
 
M

Marco Schmidt

ak:
Thanks, for many advices!

Some lines of code would be really helpfull!

For what? I thought you knew the "trivial things"?

If you create a class for that decode struct (e.g. class Decode), then
an array is Decode[], a list type could be ArrayList. So e.g.

Decode[] firstDecode = new Decode[32];
Decode[] secondDecode = new Decode[512];

Then you can define

int index1, index2;

and set them to 0.

Instead of struct decode *decode, *dindex; you define more int values
and initialize them according to what is in index1 / index2.

BTW, that C code is really ugly. Maybe you should do more than just a
straight port.

Regards,
Marco
 
A

ak

Instead of struct decode *decode, *dindex; you define more int values
and initialize them according to what is in index1 / index2.

Yeah, now we came to my problem!

if I define
class Decode {
Decode [] branch = new Decode[2];
int leaf;
}

then I can port easy make_decoder:

void make_decoder(struct decode *dest, const uchar *source, int level) {
static struct decode *free; /* Next unused node */
static int leaf; /* number of leaves already added */
int i, next;

if (level==0) {
free = dest;
leaf = 0;
}
free++;

if (level < next) {
dest->branch[0] = free;
make_decoder(free,source,level+1);
dest->branch[1] = free;
make_decoder(free,source,level+1);
} else
dest->leaf = source[16 + leaf++];
}

to

void make_decoder(Decode [] dest, int destIndex, char source[], int level) {
int i, next;

if(level == 0) {
free = destIndex;
leaf = 0;
}
free++;

if(level < next) {
dest[destIndex].branch[0] = dest[free];
make_decoder(dest, free, source, level + 1);
dest[destIndex].branch[1] = dest[free];
make_decoder(dest, free, source, level + 1);
}
else
dest[destIndex].leaf = source[16 + leaf++];
}

but have big problem decompress:

void decompress(ushort *outbuf, int count) {
struct decode *decode, *dindex;
int i, leaf, len, sign, diff, diffbuf[64];
static int carry, pixel, base[2];

while (count--) {
decode = first_decode;
for (i=0; i < 64; i++ ) {

//especially hier:
for (dindex=decode; dindex->branch[0]; )
dindex = dindex->branch[getbits(1)];
leaf = dindex->leaf;
decode = second_decode;
}
}

if I define
class Decode {
int [] branch = new int[2];
int leaf;
}

then I can port decompress, but can't port make_decoder;

void decompress(short [] outbuf, int count) {
Decode [] decode;
int dindex = 0;

while (count--) {
decode = first_decode;
for (i=0; i < 64; i++ ) {

for (dindex=0; decode[dindex].branch[0] != 0; )
dindex = decode[dindex].branch[getbits(1)];
leaf = decode[dindex].leaf;
decode = second_decode;
}
}
Any ideas?

Thanks in advice

Andrei
 
A

ak

if I define
class Decode {
int [] branch = new int[2];
int leaf;
}

then I can port decompress, but can't port make_decoder;

void decompress(short [] outbuf, int count) {
Decode [] decode;
int dindex = 0;

while (count--) {
decode = first_decode;
for (i=0; i < 64; i++ ) {

for (dindex=0; decode[dindex].branch[0] != 0; )
dindex = decode[dindex].branch[getbits(1)];
leaf = decode[dindex].leaf;
decode = second_decode;
}
}

ok, I think I solved it:

class Decode {
Decode [] branch = new Decode[2];
int leaf;
}

void decompress(short [] outbuf, int count) {
Decode [] decode;
Decode dindex;

for(i = 0; i < 64; i++) {
for(dindex = first_decode[0]; dindex.branch[0] != null;)
dindex = dindex.branch[getbits(1)];
leaf = dindex.leaf;
decode = second_decode;
}
}

Many thanks to all who posted!

Andrei
 
A

ak

Tor Iver Wilhelmsen said:
ak said:
struct decode {
struct decode *branch[2];
int leaf;
} first_decode[32], second_decode[512];

Make this a class. Pointers are implicit when operating with variables
of non-primitive types.
memset( first_decode, 0, sizeof first_decode);
memset(second_decode, 0, sizeof second_decode);

These are implicit in Java.

Only when you create it!

theese arrays are reused, so I must do it explicite:
for(int j = 0; j < first_decode.length; j++) {
first_decode[j] = null;
}
 
W

Wojtek

Tor Iver Wilhelmsen said:
ak said:
struct decode {
struct decode *branch[2];
int leaf;
} first_decode[32], second_decode[512];

Make this a class. Pointers are implicit when operating with variables
of non-primitive types.
memset( first_decode, 0, sizeof first_decode);
memset(second_decode, 0, sizeof second_decode);

These are implicit in Java.

Only when you create it!

theese arrays are reused, so I must do it explicite:
for(int j = 0; j < first_decode.length; j++) {
first_decode[j] = null;
}

Alright, then re-create it:
first_decode = new decode[first_decode.length];

BTW, using an underscore is a C-ish thing. It should be called
firstDecode and secondDecode.
 
A

ak

Alright, then re-create it:
first_decode = new decode[first_decode.length];
yes, thats better.
BTW, using an underscore is a C-ish thing. It should be called
firstDecode and secondDecode.

I keep it now to easy comparing with original.

Andrei Kouznetsov
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,677
Members
48,796
Latest member
Greg L.

Latest Threads

Top