public static void Sum(Id i1, Id i2) { // this becomes the sum between i1 and i2 //sum(0, X) -> X; //sum(X, 0) -> X; //sum({L1,R1}, {L2, R2}) -> norm_id({sum(L1, L2), sum(R1, R2)}). if (i1.IsLeaf && i1.Value == 0) { i1.Copy(i2); return; } if (i2.IsLeaf && i2.Value == 0) { return; //i1 is the result } if (!i1.IsLeaf && !i2.IsLeaf) { Sum(i1.Left, i2.Left); Sum(i1.Right, i2.Right); i1.Normalize(); return; } throw new IdOperationException("Sum failed", i1, i2); }
protected Stamp(Id i, Event e) { Id = i; Event = e; }
public Stamp Peek() { var i = new Id(0); var ev = Event.Clone(Event); return new Stamp(i, ev); }
public Stamp() { Event = new Event(); Id = new Id(); }
public Stamp Fork() { var st = new Stamp { Event = (Event.Clone(Event)) }; var par = Id.Split(); Id = par[0]; st.Id = (par[1]); return st; }
public object Clone() { var res = new Id(this); return res; }
public void Copy(Id i) { Value = i.Value; Left = i.Left; Right = i.Right; }
protected static int StampGrow(Id i, Event e) { int cost; if (i.IsLeaf && i.Value == 1 && e.IsLeaf) { e.SetValue(e.Value + 1); return 0; } if (e.IsLeaf) { e.SetAsNode(); cost = StampGrow(i, e); return cost + 1000; } if (i.IsLeaf == false && i.Left.IsLeaf && i.Left.Value == 0) { cost = StampGrow(i.Right, e.Right); return cost + 1; } if (i.IsLeaf == false && i.Right.IsLeaf && i.Right.Value == 0) { cost = StampGrow(i.Left, e.Left); return cost + 1; } if (i.IsLeaf == false) { var el = Event.Clone(e.Left); var er = Event.Clone(e.Right); var costr = StampGrow(i.Right, e.Right); var costl = StampGrow(i.Left, e.Left); if (costl < costr) { e.SetRight(er); return costl + 1; } e.SetLeft(el); return costr + 1; } throw new StampOperationException(string.Format("Grow failed: id: {0}, event: {1}", i, e)); }
protected bool Equals(Id inId) { return Equals(Left, inId.Left) && Equals(Right, inId.Right) && Value == inId.Value; }
public Id[] Split() { var i1 = new Id(); var i2 = new Id(); if (IsLeaf && Value == 0) { i1.SetAsLeaf(0); i2.SetAsLeaf(0); return new[] { i1, i2 }; } if (IsLeaf && Value == 1) { // id = 1 i1.SetAsNode(0, 1, 0, null); i2.SetAsNode(0, 0, 1, null); return new[] { i1, i2 }; } if (!IsLeaf && (Left.IsLeaf && Left.Value == 0) && (!Right.IsLeaf || Right.Value == 1)) { // id = (0, i) var ip = Right.Split(); i1.SetAsNode(0, new Id(0), ip[0]); i2.SetAsNode(0, new Id(0), ip[1]); return new[] { i1, i2 }; } if (!IsLeaf && (!Left.IsLeaf || Left.Value == 1) && (Right.IsLeaf && Right.Value == 0)) { // id = (i, 0) var ip = Left.Split(); i1.SetAsNode(0, ip[0], new Id(0)); i2.SetAsNode(0, ip[1], new Id(0)); return new[] { i1, i2 }; } if (!IsLeaf && (!Left.IsLeaf || Left.Value == 1) && (!Right.IsLeaf || Right.Value == 1)) { // id = (i1, i2) i1.SetAsNode(0, Clone(Left), new Id(0)); i2.SetAsNode(0, new Id(0), Clone(Right)); return new[] { i1, i2 }; } throw new IdOperationException("Split failed", this, null); }
public void SetAsNode(int value, Id left, Id right) { Value = value; Left = left; Right = right; }
public void SetAsNode(int? value, int? leftValue, int? rightValue, BitArray bt) { if (value.HasValue) { Value = value.Value; } else { Value = -1; } if (leftValue.HasValue) { Left = new Id(leftValue.Value); } else { Left = new Id(); Left.Decode(bt); } if (rightValue.HasValue) { Right = new Id(rightValue.Value); } else { Right = new Id(); Right.Decode(bt); } }
public void SetAsNode() { Value = -1; Left = new Id(1); Right = new Id(0); }
public Id(Id i) { Value = i.Value; Left = Clone(i.Left); Right = Clone(i.Right); }
protected static void StampFill(Id i, Event e) { if (i.IsLeaf && i.Value == 0) { return; } if (i.IsLeaf && i.Value == 1) { e.Height(); return; } if (e.IsLeaf) { return; } if (i.IsLeaf == false && i.Left.IsLeaf && i.Left.Value == 1) { StampFill(i.Right, e.Right); e.Left.Height(); e.Left.SetMaxValue(e.Left, e.Right); e.Normalize(); return; } if (i.IsLeaf == false && i.Right.IsLeaf && i.Right.Value == 1) { StampFill(i.Left, e.Left); e.Right.Height(); e.Right.SetMaxValue(e.Right, e.Left); e.Normalize(); return; } if (i.IsLeaf == false) { StampFill(i.Left, e.Left); StampFill(i.Right, e.Right); e.Normalize(); return; } throw new StampOperationException(string.Format("Fill failed: id: {0}, event: {1}", i, e)); }
protected Stamp(Stamp s) { Event = s.Event; Id = s.Id; }
public static Id Clone(Id orig) { return (Id)(orig == null ? null : orig.Clone()); }
public static Stamp StampPeek(Stamp st) { var i = new Id(0); var ev = Event.Clone(st.Event); return new Stamp(i, ev); }
public IdOperationException(string message, Id i1, Id i2) : base(string.Format("{0} - Id1: {1} - Id2: {2}", message, i1, i2)) { Id1 = i1; Id2 = i2; }