I am trying to convert some old 'C' code to C#, but having a problem with the syntax t = &(*t)->next[u];
and (*t)->end = 1;
The relevant part of original code is as follows
typedef struct Trie Trie;
struct Trie {
int end;
Trie *next[26];
};
void trie_insert(Trie **t, const char *p)
{
while (*p) {
unsigned u = trie_index(*p++);
if (u < 26) {
if (*t == NULL) {
*t = calloc(1, sizeof(**t));
}
t = &(*t)->next[u]; // how to convert this??
}
}
if (*t == NULL) {
*t = calloc(1, sizeof(**t));
}
(*t)->end = 1;
}
Trie *trie_from(const char **data)
{
Trie *t = NULL;
while (*data) {
trie_insert(&t, *data++);
}
return t;
}
and my converted code (so far) looks like this
class Trie
{
int end { get; set; }
public Trie[] next;
public Trie()
{
next = new Trie[26];
}
public Trie(string[] data)
{
Trie[] t = null;
foreach (string cc in data)
{
insert(t, cc);
}
}
void insert(Trie[] t, String p)
{
foreach (char c in p) {
UInt32 u = index(c);
if (u < 26)
{
if (t[0] == null)
{
t[0] = new Trie();
}
t = &(*t)->next[u]; // how to convert this line??
}
}
if (t[0] == null)
{
t[0] = new Trie();
}
(*t)->end = 1; // and this line??
}
any help with these lines (and anything else I have done wrongly!) would be most welcome.
There's also a Trie comparison function in the C Code:
int trie_cmp(const void* pa, const void* pb)
{
const void* const* a = pa;
const void* const* b = pb;
return (*a > *b) - (*a < *b);
}
which I have converted as follows:
public int CompareTo(Object obj)
{
Trie that = (Trie)obj;
for (int n=0; n< 26; n++)
{
Trie t1 = this.next[n];
Trie t2 = that.next[n];
if (t1 == null && t2 == null)
continue;
if (t1 == null)
return -1;
if (t2 == null)
return +1;
if (t1.end || t2.end) {
int foo = 1; // debug point
}
return (t1.CompareTo(t2));
}
return 0;
}
but debugging shows that every Trie referred to is NULL (the code never gets beyond if (t1 == null && t2 == null) continue;
)
If it helps to know, the data used for the Trie structure is a large array of 5-letter words (always 5 letters).
I did try a free version of a commercial C++ to C# converter, which wasn't helpful.