Esempio n. 1
0
 public virtual int Compare(Variant lhs, Variant rhs)
 {
     try
     {
         Variant   result  = new Variant();
         Variant[] @params = new Variant[2];
         @params[0] = lhs;
         @params[1] = rhs;
         int hr = mClosure.FuncCall(0, null, result, @params, null);
         if (hr < 0)
         {
             Error.ThrowFrom_tjs_error(hr, null);
         }
         bool ret = result.AsBoolean();
         return(ret ? -1 : 1);
     }
     catch (VariantException)
     {
         return(0);
     }
     catch (TjsException)
     {
         return(0);
     }
 }
 /// <exception cref="TjsException"></exception>
 /// <exception cref="VariantException"></exception>
 public virtual void Randomize(Variant[] param)
 {
     if (param.Length == 0)
     {
         // parametor not given
         if (mRandomBits128 != null)
         {
             // another random generator is given
             //tjs_uint8 buf[32];
             //unsigned long tmp[32];
             ByteBuffer buf = ByteBuffer.AllocateDirect(32);
             mRandomBits128.GetRandomBits128(buf, 0);
             mRandomBits128.GetRandomBits128(buf, 16);
             int[] tmp = new int[32];
             for (int i = 0; i < 32; i++)
             {
                 long num = (long)buf.Get(i) + ((long)buf.Get(i) << 8) + ((long)buf.Get(1) << 16)
                            + ((long)buf.Get(i) << 24);
                 tmp[i] = (int)(num > int.MaxValue ? num - unchecked ((long)(0x100000000L)) : num);
             }
             if (mGenerator != null)
             {
                 mGenerator = null;
             }
             mGenerator = new MersenneTwister(tmp);
         }
         else
         {
             if (mGenerator != null)
             {
                 mGenerator = null;
             }
             mGenerator = new MersenneTwister(Runtime.CurrentTimeMillis());
         }
     }
     else
     {
         if (param.Length >= 1)
         {
             if (param[0].IsObject())
             {
                 MersenneTwisterData data = null;
                 try
                 {
                     // may be a reconstructible information
                     VariantClosure clo = param[0].AsObjectClosure();
                     if (clo.mObject == null)
                     {
                         throw new TjsException(Error.NullAccess);
                     }
                     string  state;
                     Variant val = new Variant();
                     data = new MersenneTwisterData();
                     // get state array
                     //TJSThrowFrom_tjs_error
                     int hr = clo.PropGet(Interface.MEMBERMUSTEXIST, "state", val, null);
                     if (hr < 0)
                     {
                         Error.ThrowFrom_tjs_error(hr, null);
                     }
                     state = val.AsString();
                     if (state.Length != MT_N * 8)
                     {
                         throw new TjsException(Error.NotReconstructiveRandomizeData);
                     }
                     int p = 0;
                     for (int i = 0; i < MT_N; i++)
                     {
                         long n = 0;
                         int  tmp;
                         for (int j = 0; j < 8; j++)
                         {
                             int c = state[p + j];
                             tmp = -1;
                             if (c >= '0' && c <= '9')
                             {
                                 n = c - '0';
                             }
                             else
                             {
                                 if (c >= 'a' && c <= 'f')
                                 {
                                     n = c - 'a' + 10;
                                 }
                                 else
                                 {
                                     if (c >= 'A' && c <= 'F')
                                     {
                                         n = c - 'A' + 10;
                                     }
                                 }
                             }
                             if (tmp == -1)
                             {
                                 throw new TjsException(Error.NotReconstructiveRandomizeData);
                             }
                             else
                             {
                                 n <<= 4;
                                 n  += tmp;
                             }
                         }
                         p += 8;
                         data.state.Put(i, n & unchecked ((long)(0xffffffffL)));
                     }
                     // get other members
                     hr = clo.PropGet(Interface.MEMBERMUSTEXIST, "left", val, null);
                     if (hr < 0)
                     {
                         Error.ThrowFrom_tjs_error(hr, null);
                     }
                     data.left = val.AsInteger();
                     hr        = clo.PropGet(Interface.MEMBERMUSTEXIST, "next", val, null);
                     data.next = val.AsInteger();
                     if (mGenerator != null)
                     {
                         mGenerator = null;
                     }
                     mGenerator = new MersenneTwister(data);
                 }
                 catch (VariantException)
                 {
                     data = null;
                     throw new TjsException(Error.NotReconstructiveRandomizeData);
                 }
                 catch (TjsException)
                 {
                     data = null;
                     throw new TjsException(Error.NotReconstructiveRandomizeData);
                 }
                 data = null;
             }
             else
             {
                 // 64bitじゃなくて、32bit にしてしまっている。实用上问题あれば修正。
                 int   n   = param[0].AsInteger();
                 int[] tmp = new int[1];
                 tmp[0] = n;
                 if (mGenerator != null)
                 {
                     mGenerator = null;
                 }
                 mGenerator = new MersenneTwister(tmp);
             }
         }
     }
 }