Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Thaw code for VarDeque, VarHash, BigInteger, Complex, Rat, FatRat

  • Loading branch information...
commit 8035d26ceb7394591f147ae0ad92cccaa51c0839 1 parent 6dd5b76
@sorear authored
Showing with 131 additions and 12 deletions.
  1. +19 −2 lib/BigInteger.cs
  2. +69 −4 lib/Serialize.cs
  3. +43 −6 lib/Utils.cs
View
21 lib/BigInteger.cs
@@ -2343,8 +2343,25 @@ static void DivModUnsigned (uint[] u, uint[] v, out uint[] q, out uint[] r)
Niecza.Serialization.FreezeBuffer fb) {
fb.Byte((byte) Niecza.Serialization.SerializationCode.BigInteger);
fb.Int(sign * data.Length);
- foreach (uint x in data)
- fb.Int((int)x);
+ if (sign != 0)
+ foreach (uint x in data)
+ fb.Int((int)x);
+ }
+
+ internal static BigInteger Thaw(
+ Niecza.Serialization.ThawBuffer tb) {
+ int s_l = tb.Int();
+ short sign;
+ int ct;
+ if (s_l > 0) { sign = 1; ct = s_l; }
+ else if (s_l == 0) { sign = 0; ct = 0; }
+ else { sign = -1; ct = -s_l; }
+ uint[] d = new uint[ct];
+ for (int i = 0; i < ct; i++)
+ d[i] = (uint)tb.Int();
+ BigInteger r = new BigInteger(sign, sign == 0 ? ZERO : d);
+ tb.Register(r);
+ return r;
}
}
}
View
73 lib/Serialize.cs
@@ -322,6 +322,10 @@ public class FreezeBuffer {
data[wpointer++] = (byte)(x );
}
+ public void Double(double x) {
+ Long(BitConverter.DoubleToInt64Bits(x));
+ }
+
public void String(string s) {
if (s == null) {
Int(-1);
@@ -437,7 +441,7 @@ public class FreezeBuffer {
Int((int)o);
break;
case 5:
- Long(BitConverter.DoubleToInt64Bits((double)o));
+ Double((double)o);
break;
case 6:
String(((Type)o).AssemblyQualifiedName);
@@ -502,6 +506,10 @@ class ThawBuffer {
return (((long)Int()) << 32) | (long)(uint)Int();
}
+ public double Double() {
+ return BitConverter.Int64BitsToDouble(Long());
+ }
+
public string String() {
int l = Int();
@@ -547,27 +555,84 @@ class ThawBuffer {
switch(tag) {
case SerializationCode.Null:
return null;
- case SerializationCode.SelfRef:
- i = Int();
- return unit.bynum[i];
+
case SerializationCode.ForeignRef:
i = Int();
j = Int();
return unit_map[i].bynum[j];
+ case SerializationCode.SelfRef:
+ i = Int();
+ return unit.bynum[i];
case SerializationCode.NewUnitRef:
return LoadNewUnit();
+
case SerializationCode.RuntimeUnit:
return RuntimeUnit.Thaw(this);
+ //SubInfo,
+ //STable,
case SerializationCode.StashEnt:
return StashEnt.Thaw(this);
+ case SerializationCode.Rat:
+ return Rat.Thaw(this);
+ case SerializationCode.FatRat:
+ return FatRat.Thaw(this);
+ case SerializationCode.Complex:
+ return Complex.Thaw(this);
+ case SerializationCode.BigInteger:
+ return BigInteger.Thaw(this);
+ case SerializationCode.VarDeque:
+ return VarDeque.Thaw(this);
+ case SerializationCode.VarHash:
+ return VarHash.Thaw(this);
+ //case SerializationCode.DispatchEnt:
+ // return DispatchEnt.Thaw(this);
+ //case SerializationCode.RxFrame:
+ // return RxFrame.Thaw(this);
+ //case SerializationCode.P6how:
+ // return P6how.Thaw(this);
+
case SerializationCode.ReflectObj:
return ReflectObj.Thaw(this);
+ //P6opaque,
+ //Frame,
+ //Cursor,
+
+ //String,
+ //ArrP6any,
+ //ArrVariable,
+ //Boolean,
+ //Int,
+ //Double,
+ //Type,
+
case SerializationCode.SimpleVariable:
case SerializationCode.SimpleVariable_1:
case SerializationCode.SimpleVariable_2:
case SerializationCode.SimpleVariable_3:
return SimpleVariable.Thaw(this,
(int)tag - (int)SerializationCode.SimpleVariable);
+ //SubstrLValue,
+ //TiedVariable,
+ //SubViviHook,
+ //ArrayViviHook,
+ //NewArrayViviHook,
+ //HashViviHook,
+ //NewHashViviHook,
+ //LADNone, // no-args
+ //LADNull,
+ //LADDot,
+ //LADDispatcher,
+ //LADImp,
+ //LADStr, // string
+ //LADStrNoCase,
+ //LADMethod,
+ //LADParam,
+ //LADOpt, // LAD
+ //LADPlus,
+ //LADStar,
+ //LADSequence, // LAD[]
+ //LADAny,
+ //LADCC, // CC
default:
throw new ThawException("unexpected object tag " + tag);
}
View
49 lib/Utils.cs
@@ -141,6 +141,13 @@ public sealed class VarDeque : IFreeze {
if (index == data.Length) index = 0;
}
}
+ internal static VarDeque Thaw(ThawBuffer tb) {
+ VarDeque r = new VarDeque();
+ tb.Register(r);
+ int c = tb.Int();
+ for (int i = 0; i < c; i++) r.Push((Variable) tb.ObjRef());
+ return r;
+ }
}
struct VarHashLink {
@@ -457,6 +464,14 @@ public struct KEnum : IEnumerator<string> {
fb.ObjRef(kv.Value);
}
}
+ internal static VarHash Thaw(ThawBuffer tb) {
+ VarHash r = new VarHash();
+ tb.Register(r);
+ int c = tb.Int();
+ for (int i = 0; i < c; i++)
+ r[tb.String()] = (Variable) tb.ObjRef();
+ return r;
+ }
}
public class SubscriberSet {
@@ -728,14 +743,20 @@ public sealed class Complex : IFreeze {
void IFreeze.Freeze(FreezeBuffer fb) {
fb.Byte((byte)SerializationCode.Complex);
- fb.Long(BitConverter.DoubleToInt64Bits(re));
- fb.Long(BitConverter.DoubleToInt64Bits(im));
+ fb.Double(re);
+ fb.Double(im);
+ }
+ internal static Complex Thaw(ThawBuffer tb) {
+ // NOTE this deferral only works because we don't call ObjRef
+ Complex r = new Complex(tb.Double(), tb.Double());
+ tb.Register(r);
+ return r;
}
}
public sealed class Rat : IFreeze {
- public readonly BigInteger num;
- public readonly ulong den;
+ public BigInteger num;
+ public ulong den;
public Rat(BigInteger num, ulong den) {
this.num = num; this.den = den;
@@ -746,11 +767,19 @@ public sealed class Rat : IFreeze {
fb.ObjRef(num);
fb.Long((long)den);
}
+ private Rat() {}
+ internal static Rat Thaw(ThawBuffer tb) {
+ Rat r = new Rat();
+ tb.Register(r);
+ r.num = (BigInteger)tb.ObjRef();
+ r.den = (ulong)tb.Long();
+ return r;
+ }
}
public sealed class FatRat : IFreeze {
- public readonly BigInteger num;
- public readonly BigInteger den;
+ public BigInteger num;
+ public BigInteger den;
public FatRat(BigInteger num, BigInteger den) {
this.num = num; this.den = den;
@@ -761,6 +790,14 @@ public sealed class FatRat : IFreeze {
fb.ObjRef(num);
fb.ObjRef(den);
}
+ private FatRat() {}
+ internal static FatRat Thaw(ThawBuffer tb) {
+ FatRat r = new FatRat();
+ tb.Register(r);
+ r.num = (BigInteger)tb.ObjRef();
+ r.den = (BigInteger)tb.ObjRef();
+ return r;
+ }
}
public sealed class RatApproxer {
Please sign in to comment.
Something went wrong with that request. Please try again.