Original Post
If any of you have the time could you possibly help me convert some C code to its equivalent in Java plz, i really dont know much about c at all but have to try and implement a java version of some c code. below i have listed the c code and also what i have done so far, think its ok apart from the init_arguments method. thanks hope this doesnt seem like too much code, but thanks to all of you who take the time to help, greatly appreciated, even if you can just help with a single method that will be great! (as i know there is quite a few lines here) =) C Code first: ================================================ My Java Code So Far: [Edited by - ug02070 on October 10, 2005 10:17:26 AM]
/* Program to produce suffix array. */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <assert.h>
typedef unsigned int boolean;
#define FALSE 0 /* For boolean expressions */
#define TRUE 1 /* For boolean expressions */
#define DUMP_STRING_SIZE 40 /* Used for dumping long strings
(for debug purposes */
unsigned int Order = 0; /* Order used for comparison */
void
usage (void)
{
fprintf (stderr,
"Usage: ident_language [options]\n"
"\n"
"options:\n"
" -o n\tOrder=n\n"
);
exit (2);
}
void
init_arguments (int argc, char *argv[])
{
extern char *optarg;
extern int optind;
int opt;
boolean order_found;
/* set defaults */
order_found = FALSE;
/* get the argument options */
while ((opt = getopt (argc, argv, "o:")) != -1)
switch (opt)
{
case 'o':
order_found = TRUE;
sscanf (optarg, "%u", &Order);
assert (Order > 0);
fprintf (stderr, "Order = %d\n", Order);
break;
default:
usage ();
break;
}
if (!order_found)
usage ();
for (; optind < argc; optind++)
usage ();
}
void
SA_init_sarray (unsigned int *sarray, unsigned int size)
/* Initializes the suffix array's index. */
{
unsigned int p;
for (p = 0; p < size; p++)
sarray = p;
}
void
SA_check_sarray (char *string, unsigned int string_size, unsigned int *sarray)
/* Checks that the suffix array is sorted */
{
unsigned int p, i, prev_i;
char cc;
prev_i = sarray [0];
for (p = 1; p < string_size; p++)
{
i = sarray ;
if (strcmp (string + prev_i, string + i) > 0)
{
fprintf (stderr, "Error in suffix array at index %d :\n", p);
cc = string [prev_i + 40];
string [prev_i + 40] = '\0';
fprintf (stderr, "position %6d, string = %s\n",
prev_i, string + prev_i);
string [prev_i + 40] = cc;
cc = string [i + 40];
string [i + 40] = '\0';
fprintf (stderr, "position %6d, string = %s\n",
i, string + i);
string [i + 40] = cc;
}
prev_i = i;
}
}
static char *SA_String;
int
SA_qsort_compare (const void *index1, const void *index2)
/* Comparison function used by qsort. */
{
int p1, p2, cmp;
/*fprintf (stderr, "Comparing %p with %p\n", index1, index2);*/
p1 = *((unsigned int *) index1);
p2 = *((unsigned int *) index2);
/*fprintf (stderr, "Value %d with %d\n", p1, p2);*/
cmp = strcmp (SA_String + p1, SA_String + p2);
if (!cmp)
{
if (p1 > p2)
return (1);
if (p1 < p2)
return (-1);
return 0;
}
return cmp;
}
unsigned int *
SA_make_sarray (char *string, unsigned int string_size)
/* Builds the suffix array in memory. */
{
unsigned int *sarray;
SA_String = string;
sarray = (unsigned int *) malloc (string_size * sizeof (unsigned int));
SA_init_sarray (sarray, string_size);
qsort (sarray, string_size, sizeof (unsigned int), SA_qsort_compare);
SA_check_sarray (string, string_size, sarray);
return (sarray);
}
void
SA_dump_string ( FILE *fp, char *string, unsigned int p, boolean full_dump)
/* Dumps the string to FP. */
{
int cc, len;
len = strlen (string + p);
if (full_dump)
fprintf (fp, "position = %d, string = ", p);
if (len < DUMP_STRING_SIZE)
fprintf (fp, "%s\n", string + p);
else
{
cc = string [p + DUMP_STRING_SIZE];
string [p + DUMP_STRING_SIZE] = '\0';
fprintf (stderr, "%s\n", string + p);
string [p + DUMP_STRING_SIZE] = cc;
}
}
void
SA_dump_sarray (FILE *fp, char *string, unsigned int string_size,
unsigned int *sarray)
/* Dumps out the suffix array to file FP (fro debug purposes). */
{
unsigned int p, i;
for (p = 0; p < string_size; p++)
{
i = sarray ;
fprintf (fp, "%6d : %6d ", p, i);
SA_dump_string (fp, string, i, TRUE);
}
}
unsigned int SA_compute_measure
(char *string1, unsigned int string_size1, unsigned int *sarray1,
char *string2, unsigned int string_size2, unsigned int *sarray2,
unsigned int order)
/* Computes a measure of similarity between the two strings
using their respective suffix arrays. */
{
unsigned int p1, p2, i1, i2, measure;
int result;
p2 = 0;
measure = 0;
for (p1 = 0; p1 < string_size1;)
{
/* We are now at the start of a "new" group of prefixes as
determined by the Order length */
i2 = sarray2 [p2];
if (strlen (string1 + i1) < order)
continue;
while ((result = strncmp (string2 + i2, string1 + i1, order)) < 0)
{
/*
fprintf (stderr, "Skipping suffix array at index %d,%d :\n",
p1, p2);
SA_dump_string (stderr, string1, i1, TRUE);
SA_dump_string (stderr, string2, i2, TRUE);
*/
p2++;
if (p2 >= string_size2)
break;
i2 = sarray2 [p2];
}
if (!result) /* Prefixes up to length order are equal */
{
measure++;
fprintf (stderr, "Prefixes match at index %d,%d :\n",
p1, p2);
SA_dump_string (stderr, string1, i1, TRUE);
SA_dump_string (stderr, string2, i2, TRUE);
}
/* Skip through suffix array 1 until we reach the start of
a new group of prefixes */
old_p1 = p1;
old_i1 = i1;
for (;;)
{
p1++;
if (p1 > string_size1)
break;
i1 = sarray [p1];
if ($$$result = strncmp (string1 + old_i1, string1 + i1, order)) < 0)
}
while (
{
}
/*
fprintf (stderr, "Position %d,%d :\n", p1, p2);
SA_dump_string (stderr, string1, i1, TRUE);
SA_dump_string (stderr, string2, i2, TRUE);
*/
}
return (measure);
}
int
main (int argc, char *argv[])
{
char string1 [128], string2 [128];
unsigned int *sarray1, *sarray2, string_size1, string_size2, measure;
init_arguments (argc, argv);
/*
string = (char *) malloc (array_size * sizeof (char));
sarray = (unsigned int *) malloc (array_size * sizeof (unsigned int));
*/
/* Read in the input file */
/*string_size = fread (string, sizeof (char), array_size+1, stdin);*/
fprintf (stdout, "Enter string1: ");
fscanf (stdin, "%s", string1);
string_size1 = strlen (string1) + 1;
fprintf (stdout, "Enter string2: ");
fscanf (stdin, "%s", string2);
string_size2 = strlen (string2) + 1;
sarray1 = SA_make_sarray (string1, string_size1);
sarray2 = SA_make_sarray (string2, string_size2);
/*dump_sarray (string, string_size, sarray);*/
measure = SA_compute_measure (string1, string_size1, sarray1,
string2, string_size2, sarray2, Order);
fprintf (stderr, "Measure = %d\n", measure);
exit (1);
}
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
public class SArray
{
// Builds the suffix array in memory
public static int[] SA_make_sarray(String string, int stringSize)
{
String SA_string;
int[] sarray;
//SA_string = aString;
sarray = new int[stringSize];
SA_init_sarray(sarray, stringSize);
SA_check_sarray(string, stringSize, sarray);
return sarray;
}
//Initializes the suffix array's index.
public static void SA_init_sarray(int[] sarray, int size)
{
for(int p = 0; p < size; p++)
{
sarray = p;
}
}
public static void SA_check_sarray(String string, int stringSize, int[] sarray)
{
int p;
int i = 0;
int prev_i = 0;
char cc;
char[] charArray = string.toCharArray();
prev_i = sarray[0];
for(p = 1; p < stringSize; p++)
{
i = sarray;
if ((string + prev_i).compareTo((string + i)) > 0)
{
System.out.println ("Error in suffix array at index : " + p);
}
cc = charArray [prev_i + 40];
charArray [prev_i + 40] = '\0';
System.out.println ("position " + prev_i + ", string = " + (string + prev_i));
charArray [prev_i + 40] = cc;
cc = charArray [i + 40];
charArray [i + 40] = '\0';
System.out.println ("position " + i + ", string = " + (string + i));
charArray [i + 40] = cc;
}
prev_i = i;
}
public static void init_arguments (String args[])
{
char optarg = '?';
int optind;
int opt = 0;
boolean order_found;
/* set defaults */
order_found = false;
/* get the argument options */
while (opt != -1)
{
opt = args.toString().indexOf("o:" + 1);
}
switch (opt)
{
case 'o':
order_found = true;
//sscanf (optarg, "%u", &Order); get integer out of optarg and place in Order
int Order = optarg;
if (Order > 0)
{
System.out.println("Error, order greater than 0...Exiting");
System.exit(0);
}
System.out.println("Order = " + Order);
break;
default:
usage ();
break;
}
if (!order_found)
usage ();
//for (; optind < args; optind++)
//usage ();
}
public static void usage()
{
System.out.println("Usage: ident_language [options]\n");
System.out.println("options:\n");
System.out.println("-o n\tOrder=n\n");
System.exit(0);
}
public static void main (String args[]) throws IOException
{
String string1;
String string2;
int[] sarray1, sarray2;
int string_size1, string_size2, measure;
init_arguments (args);
/*
string = (char *) malloc (array_size * sizeof (char));
sarray = (unsigned int *) malloc (array_size * sizeof (unsigned int));
*/
/* Read in the input file */
/*string_size = fread (string, sizeof (char), array_size+1, stdin);*/
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter string1: ");
string1 = br.readLine();
string_size1 = string1.length() + 1;
System.out.println("Enter string2: ");
string2 = br.readLine();
string_size2 = string2.length() + 1;
sarray1 = SA_make_sarray (string1, string_size1);
sarray2 = SA_make_sarray (string2, string_size2);
}
}