Skip to content

Commit

Permalink
Thaw code for VarDeque, VarHash, BigInteger, Complex, Rat, FatRat
Browse files Browse the repository at this point in the history
  • Loading branch information
sorear committed Oct 16, 2011
1 parent 6dd5b76 commit 8035d26
Show file tree
Hide file tree
Showing 3 changed files with 131 additions and 12 deletions.
21 changes: 19 additions & 2 deletions lib/BigInteger.cs
Expand Up @@ -2343,8 +2343,25 @@ static void DivModUnsigned (uint[] u, uint[] v, out uint[] q, out uint[] r)
Niecza.Serialization.FreezeBuffer fb) { Niecza.Serialization.FreezeBuffer fb) {
fb.Byte((byte) Niecza.Serialization.SerializationCode.BigInteger); fb.Byte((byte) Niecza.Serialization.SerializationCode.BigInteger);
fb.Int(sign * data.Length); fb.Int(sign * data.Length);
foreach (uint x in data) if (sign != 0)
fb.Int((int)x); 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;
} }
} }
} }
73 changes: 69 additions & 4 deletions lib/Serialize.cs
Expand Up @@ -322,6 +322,10 @@ public class FreezeBuffer {
data[wpointer++] = (byte)(x ); data[wpointer++] = (byte)(x );
} }


public void Double(double x) {
Long(BitConverter.DoubleToInt64Bits(x));
}

public void String(string s) { public void String(string s) {
if (s == null) { if (s == null) {
Int(-1); Int(-1);
Expand Down Expand Up @@ -437,7 +441,7 @@ public class FreezeBuffer {
Int((int)o); Int((int)o);
break; break;
case 5: case 5:
Long(BitConverter.DoubleToInt64Bits((double)o)); Double((double)o);
break; break;
case 6: case 6:
String(((Type)o).AssemblyQualifiedName); String(((Type)o).AssemblyQualifiedName);
Expand Down Expand Up @@ -502,6 +506,10 @@ class ThawBuffer {
return (((long)Int()) << 32) | (long)(uint)Int(); return (((long)Int()) << 32) | (long)(uint)Int();
} }


public double Double() {
return BitConverter.Int64BitsToDouble(Long());
}

public string String() { public string String() {
int l = Int(); int l = Int();


Expand Down Expand Up @@ -547,27 +555,84 @@ class ThawBuffer {
switch(tag) { switch(tag) {
case SerializationCode.Null: case SerializationCode.Null:
return null; return null;
case SerializationCode.SelfRef:
i = Int();
return unit.bynum[i];
case SerializationCode.ForeignRef: case SerializationCode.ForeignRef:
i = Int(); i = Int();
j = Int(); j = Int();
return unit_map[i].bynum[j]; return unit_map[i].bynum[j];
case SerializationCode.SelfRef:
i = Int();
return unit.bynum[i];
case SerializationCode.NewUnitRef: case SerializationCode.NewUnitRef:
return LoadNewUnit(); return LoadNewUnit();

case SerializationCode.RuntimeUnit: case SerializationCode.RuntimeUnit:
return RuntimeUnit.Thaw(this); return RuntimeUnit.Thaw(this);
//SubInfo,
//STable,
case SerializationCode.StashEnt: case SerializationCode.StashEnt:
return StashEnt.Thaw(this); 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: case SerializationCode.ReflectObj:
return ReflectObj.Thaw(this); return ReflectObj.Thaw(this);
//P6opaque,
//Frame,
//Cursor,

//String,
//ArrP6any,
//ArrVariable,
//Boolean,
//Int,
//Double,
//Type,

case SerializationCode.SimpleVariable: case SerializationCode.SimpleVariable:
case SerializationCode.SimpleVariable_1: case SerializationCode.SimpleVariable_1:
case SerializationCode.SimpleVariable_2: case SerializationCode.SimpleVariable_2:
case SerializationCode.SimpleVariable_3: case SerializationCode.SimpleVariable_3:
return SimpleVariable.Thaw(this, return SimpleVariable.Thaw(this,
(int)tag - (int)SerializationCode.SimpleVariable); (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: default:
throw new ThawException("unexpected object tag " + tag); throw new ThawException("unexpected object tag " + tag);
} }
Expand Down
49 changes: 43 additions & 6 deletions lib/Utils.cs
Expand Up @@ -141,6 +141,13 @@ public sealed class VarDeque : IFreeze {
if (index == data.Length) index = 0; 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 { struct VarHashLink {
Expand Down Expand Up @@ -457,6 +464,14 @@ public struct KEnum : IEnumerator<string> {
fb.ObjRef(kv.Value); 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 { public class SubscriberSet {
Expand Down Expand Up @@ -728,14 +743,20 @@ public sealed class Complex : IFreeze {


void IFreeze.Freeze(FreezeBuffer fb) { void IFreeze.Freeze(FreezeBuffer fb) {
fb.Byte((byte)SerializationCode.Complex); fb.Byte((byte)SerializationCode.Complex);
fb.Long(BitConverter.DoubleToInt64Bits(re)); fb.Double(re);
fb.Long(BitConverter.DoubleToInt64Bits(im)); 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 sealed class Rat : IFreeze {
public readonly BigInteger num; public BigInteger num;
public readonly ulong den; public ulong den;


public Rat(BigInteger num, ulong den) { public Rat(BigInteger num, ulong den) {
this.num = num; this.den = den; this.num = num; this.den = den;
Expand All @@ -746,11 +767,19 @@ public sealed class Rat : IFreeze {
fb.ObjRef(num); fb.ObjRef(num);
fb.Long((long)den); 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 sealed class FatRat : IFreeze {
public readonly BigInteger num; public BigInteger num;
public readonly BigInteger den; public BigInteger den;


public FatRat(BigInteger num, BigInteger den) { public FatRat(BigInteger num, BigInteger den) {
this.num = num; this.den = den; this.num = num; this.den = den;
Expand All @@ -761,6 +790,14 @@ public sealed class FatRat : IFreeze {
fb.ObjRef(num); fb.ObjRef(num);
fb.ObjRef(den); 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 { public sealed class RatApproxer {
Expand Down

0 comments on commit 8035d26

Please sign in to comment.