Array and Inline help.

Previous topic - Next topic

Hatonastick

Yup, it's me again.  Sorry.  I'm having a lot of trouble trying to work out how to pass a 32-bit GLB array to a C++ function that requires an array.  This is to make the Mersenne Twister PRNG complete, and help me with another project once I understand how.

Currently I have this (this isn't all the code, this is just the part that is relevant):
Code (glbasic) Select
INLINE
// Initialize by an array, where init_key is the array for initializing keys
// and key_length is its length.
// slight change for C++, 2004/2/26
void init_by_array(unsigned long init_key[], int key_length)
{
int i, j, k;
init_genrand(19650218UL);
i=1; j=0;
k = (N>key_length ? N : key_length);
for (; k; k--)
{
// Non-linear.
mt[i] = (mt[i] ^ ((mt[i-1] ^ (mt[i-1] >> 30)) * 1664525UL))
+ init_key[j] + j;
// For WORDSIZE > 32 machines.
mt[i] &= 0xffffffffUL;
i++; j++;
if (i>=N)
{
mt[0] = mt[N-1]; i=1;
}
        if (j>=key_length)
        {
        j=0;
        }
    }
for (k=N-1; k; k--)
{
// Non-linear.
mt[i] = (mt[i] ^ ((mt[i-1] ^ (mt[i-1] >> 30)) * 1566083941UL))
- i;
// For WORDSIZE > 32 machines.
mt[i] &= 0xffffffffUL;
i++;
if (i>=N)
{
mt[0] = mt[N-1]; i=1;
}
}
// MSB is 1; assuring non-zero initial array.
mt[0] = 0x80000000UL;
}
ENDINLINE

// Initializes mt[N] with a seed in the form of an array ie. for greater than
// 32-bit seed initialization.
FUNCTION InitPRNGArray: a%[], n%
INLINE
init_by_array((unsigned long)a, (int) n);
ENDINLINE
ENDFUNCTION


And this is the error message I'm getting:
Code (glbasic) Select
compiling:
C:\DOCUME~1\Jazz\LOCALS~1\Temp\glbasic\gpc_temp0.cpp: In function `DGInt __GLBASIC__::InitPRNGArray(__GLBASIC__::DGNatArray&, DGNat)':
C:\DOCUME~1\Jazz\LOCALS~1\Temp\glbasic\gpc_temp0.cpp:228: error: conversion from `__GLBASIC__::DGNatArray' to `long unsigned int' is ambiguous
C:/Program Files/GLBasic/Compiler/platform/Include/glb.h:371: note: candidates are: __GLBASIC__::DGNatArray::operator DGInt()
C:/Program Files/GLBasic/Compiler/platform/Include/glb.h:372: note:                 __GLBASIC__::DGNatArray::operator DGNat()
*** FATAL ERROR - Please post this output in the forum


Suggestions needed (Please!), because everything I've tried has failed.

Kitty Hello


Hatonastick

No, I'm afraid not.  Most of the other INLINE stuff has been straight forward, but this I can't work out and I'm sure it's something simple.  I'm guessing it's a type issue.  Maybe I'll have to do the conversion in C/C++.  Will have another look at it tomorrow.

Kitty Hello

Code (glbasic) Select



FUNCTION foo: // or put in a seperate file
ENDFUNCTION


INLINE

// define these somehere!!!
int N, mt[8];
void init_genrand(int){}



// Initialize by an array, where init_key is the array for initializing keys
// and key_length is its length.
// slight change for C++, 2004/2/26
void init_by_array(DGNatArray& init_key, int key_length)
{
int i, j, k;
init_genrand(19650218UL);
i=1; j=0;
k = (N>key_length ? N : key_length);
for (; k; k--)
{
// Non-linear.
mt[i] = (mt[i] ^ ((mt[i-1] ^ (mt[i-1] >> 30)) * 1664525UL))
+ init_key(j) + j;
// For WORDSIZE > 32 machines.
mt[i] &= 0xffffffffUL;
i++; j++;
if (i>=N)
{
mt[0] = mt[N-1]; i=1;
}
        if (j>=key_length)
        {
        j=0;
        }
    }
for (k=N-1; k; k--)
{
// Non-linear.
mt[i] = (mt[i] ^ ((mt[i-1] ^ (mt[i-1] >> 30)) * 1566083941UL))
- i;
// For WORDSIZE > 32 machines.
mt[i] &= 0xffffffffUL;
i++;
if (i>=N)
{
mt[0] = mt[N-1]; i=1;
}
}
// MSB is 1; assuring non-zero initial array.
mt[0] = 0x80000000UL;
}
ENDINLINE

// Initializes mt[N] with a seed in the form of an array ie. for greater than
// 32-bit seed initialization.
FUNCTION InitPRNGArray: a%[], n%
INLINE



init_by_array(a, n);
ENDINLINE
ENDFUNCTION


OK, fixxored.

Hatonastick

Ok thanks mate, you are as always a life saver. :)

Just a question on the use of BOUNDS().  With the code you have listed there, could I do this:
Code (glbasic) Select
// Initializes mt[N] with a seed in the form of an array ie. for greater than
// 32-bit seed initialization.
FUNCTION InitPRNGArray: a%[]
INLINE
init_by_array(a, (int)BOUNDS(a,0));
ENDINLINE
ENDFUNCTION

Or would that be a Bad Thing(TM)?

Kitty Hello


Hatonastick

Ok thanks again mate. :)