Exemplo n.º 1
0
        public static CompoundDiff Diff(TagCompound first, TagCompound second)
        {
            CompoundDiff diff = new CompoundDiff();

            foreach (var kv in first)
            {
                Tag firstValue  = kv.Value;
                Tag secondValue = second.ContainsKey(kv.Key) ? second[kv.Key] : null;

                DiffOperation op = DiffValue(firstValue, secondValue);
                if (op != null)
                {
                    diff.Operations[kv.Key] = op;
                }
            }

            foreach (var kv in second)
            {
                if (!first.ContainsKey(kv.Key))
                {
                    diff.Operations[kv.Key] = new ChangeOperation(kv.Value);
                }
            }

            return(diff);
        }
Exemplo n.º 2
0
        public static CompoundDiff IdDiff(TagList a, TagList b)
        {
            CompoundDiff diff = new CompoundDiff();

            for (int i = 0; i < a.Count; i++)
            {
                object        id = Id.GetId(a[i]);
                DiffOperation op = DiffValue(a[i], Id.FindObjectWithId(b, id));
                if (op != null)
                {
                    diff.Operations[id] = op;
                }
            }

            for (int i = 0; i < b.Count; i++)
            {
                object id      = Id.GetId(b[i]);
                object aObject = Id.FindObjectWithId(a, id);
                if (aObject != null)
                {
                    diff.Operations[id] = new ChangeOperation(b[i]);
                }
            }

            return(diff);
        }
Exemplo n.º 3
0
        public static CompoundDiff Merge(CompoundDiff left, CompoundDiff right)
        {
            // Find combined key list
            List <object> keys = left.Operations.Keys.ToList <object>();

            foreach (var kv in right.Operations)
            {
                if (!left.Operations.ContainsKey(kv.Key))
                {
                    keys.Add(kv.Key);
                }
            }

            // Merge the operations for each key.
            CompoundDiff diff = new CompoundDiff();

            foreach (object key in keys)
            {
                if (left.Operations.ContainsKey(key) && right.Operations.ContainsKey(key))
                {
                    diff.Operations[key] = DiffOperation.Merge(left.Operations[key], right.Operations[key]);
                }
                else if (left.Operations.ContainsKey(key))
                {
                    diff.Operations[key] = left.Operations[key];
                }
                else if (right.Operations.ContainsKey(key))
                {
                    diff.Operations[key] = right.Operations[key];
                }
            }

            return(diff);
        }
Exemplo n.º 4
0
        public static TagCompound Merge(TagCompound parent, TagCompound left, TagCompound right)
        {
            CompoundDiff leftDiff  = Diff(parent, left);
            CompoundDiff rightDiff = Diff(parent, right);
            CompoundDiff diff      = CompoundDiff.Merge(leftDiff, rightDiff);

            TagCompound res = parent.DeepCopy();

            diff.Apply(res);
            return(res);
        }
Exemplo n.º 5
0
        private static DiffOperation DiffValue(Tag first, Tag second)
        {
            if (first == null && second == null)
            {
                return(null);
            }
            else if (second == null)
            {
                return(new RemoveOperation());
            }
            else if (first == null)
            {
                return(new ChangeOperation(second));
            }
            else if (first.TagType != second.TagType)
            {
                return(new ChangeOperation(second));
            }
            else
            {
                switch (first.TagType)
                {
                case TagType.Byte:
                case TagType.Short:
                case TagType.Int:
                case TagType.Long:
                case TagType.Float:
                case TagType.Double:
                case TagType.String:
                case TagType.ByteArray:
                case TagType.IntArray:
                    return(first.DataEqual(second) ? null : new ChangeOperation(second));

                case TagType.Compound:
                    CompoundDiff compDiff = Diff(first as TagCompound, second as TagCompound);
                    return(compDiff.IsEmpty ? null : new ChangeObjectOperation(compDiff));

                case TagType.List:
                    if (AreIdArrays(first as TagList, second as TagList))
                    {
                        CompoundDiff subDiff = IdDiff(first as TagList, second as TagList);
                        return(subDiff.IsEmpty ? null : new ChangeIdArrayOperation(subDiff));
                    }
                    else
                    {
                        ListDiff subDiff = PositionDiff(first as TagList, second as TagList);
                        return(subDiff.IsEmpty ? null : new ChangePositionArrayOperation(subDiff));
                    }

                default:
                    throw new Exception("Unexpected tag type");
                }
            }
        }
Exemplo n.º 6
0
        public static TagCompound MergeDetailed(TagCompound parent, TagCompound left, TagCompound right,
                                                out CompoundDiff leftDiff, out CompoundDiff rightDiff, out CompoundDiff mergedDiff)
        {
            leftDiff   = Diff(parent, left);
            rightDiff  = Diff(parent, right);
            mergedDiff = CompoundDiff.Merge(leftDiff, rightDiff);

            TagCompound res = parent.DeepCopy();

            mergedDiff.Apply(res);
            return(res);
        }
Exemplo n.º 7
0
        public static CompoundDiff Merge(CompoundDiff left, CompoundDiff right)
        {
            // Find combined key list
            List<object> keys = left.Operations.Keys.ToList<object>();
            foreach (var kv in right.Operations)
                if (!left.Operations.ContainsKey(kv.Key))
                    keys.Add(kv.Key);

            // Merge the operations for each key.
            CompoundDiff diff = new CompoundDiff();
            foreach (object key in keys) {
                if (left.Operations.ContainsKey(key) && right.Operations.ContainsKey(key))
                    diff.Operations[key] = DiffOperation.Merge(left.Operations[key], right.Operations[key]);
                else if (left.Operations.ContainsKey(key))
                    diff.Operations[key] = left.Operations[key];
                else if (right.Operations.ContainsKey(key))
                    diff.Operations[key] = right.Operations[key];
            }

            return diff;
        }
Exemplo n.º 8
0
 public static ChangeIdArrayOperation Merge(ChangeObjectOperation left, ChangeObjectOperation right)
 {
     return(new ChangeIdArrayOperation(CompoundDiff.Merge(left.Diff, right.Diff)));
 }
Exemplo n.º 9
0
 public ChangeIdArrayOperation(CompoundDiff diff)
 {
     Diff = diff;
 }
Exemplo n.º 10
0
 public ChangeObjectOperation(CompoundDiff diff)
 {
     Diff = diff;
 }
Exemplo n.º 11
0
 public ChangeObjectOperation(CompoundDiff diff)
 {
     Diff = diff;
 }
Exemplo n.º 12
0
 public ChangeIdArrayOperation(CompoundDiff diff)
 {
     Diff = diff;
 }
Exemplo n.º 13
0
        public static TagCompound MergeDetailed(TagCompound parent, TagCompound left, TagCompound right, 
            out CompoundDiff leftDiff, out CompoundDiff rightDiff, out CompoundDiff mergedDiff)
        {
            leftDiff = Diff(parent, left);
            rightDiff = Diff(parent, right);
            mergedDiff = CompoundDiff.Merge(leftDiff, rightDiff);

            TagCompound res = parent.DeepCopy();

            mergedDiff.Apply(res);
            return res;
        }
Exemplo n.º 14
0
        public static CompoundDiff IdDiff(TagList a, TagList b)
        {
            CompoundDiff diff = new CompoundDiff();

            for (int i = 0; i < a.Count; i++) {
                object id = Id.GetId(a[i]);
                DiffOperation op = DiffValue(a[i], Id.FindObjectWithId(b, id));
                if (op != null)
                    diff.Operations[id] = op;
            }

            for (int i = 0; i < b.Count; i++) {
                object id = Id.GetId(b[i]);
                object aObject = Id.FindObjectWithId(a, id);
                if (aObject != null)
                    diff.Operations[id] = new ChangeOperation(b[i]);
            }

            return diff;
        }
Exemplo n.º 15
0
        public static CompoundDiff Diff(TagCompound first, TagCompound second)
        {
            CompoundDiff diff = new CompoundDiff();

            foreach (var kv in first) {
                Tag firstValue = kv.Value;
                Tag secondValue = second.ContainsKey(kv.Key) ? second[kv.Key] : null;

                DiffOperation op = DiffValue(firstValue, secondValue);
                if (op != null)
                    diff.Operations[kv.Key] = op;
            }

            foreach (var kv in second) {
                if (!first.ContainsKey(kv.Key))
                    diff.Operations[kv.Key] = new ChangeOperation(kv.Value);
            }

            return diff;
        }