Ejemplo n.º 1
0
        public static ListDiff Merge(ListDiff left, ListDiff right)
        {
            List <int> keys = left.Operations.Keys.ToList <int>();

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

            ListDiff diff = new ListDiff();

            foreach (int 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);
        }
Ejemplo n.º 2
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");
                }
            }
        }
Ejemplo n.º 3
0
        public static ListDiff PositionDiff(TagList a, TagList b)
        {
            ListDiff diff = new ListDiff();

            int n = Math.Max(a.Count, b.Count);

            for (int i = 0; i < n; i++)
            {
                Tag firstValue  = (i >= a.Count) ? null : a[i];
                Tag secondValue = (i >= b.Count) ? null : b[i];

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

            return(diff);
        }
Ejemplo n.º 4
0
 public static ChangePositionArrayOperation Merge(ChangePositionArrayOperation left, ChangePositionArrayOperation right)
 {
     return(new ChangePositionArrayOperation(ListDiff.Merge(left.Diff, right.Diff)));
 }
Ejemplo n.º 5
0
 public ChangePositionArrayOperation(ListDiff diff)
 {
     Diff = diff;
 }
Ejemplo n.º 6
0
 public ChangePositionArrayOperation(ListDiff diff)
 {
     Diff = diff;
 }
Ejemplo n.º 7
0
        public static ListDiff PositionDiff(TagList a, TagList b)
        {
            ListDiff diff = new ListDiff();

            int n = Math.Max(a.Count, b.Count);
            for (int i = 0; i < n; i++) {
                Tag firstValue = (i >= a.Count) ? null : a[i];
                Tag secondValue = (i >= b.Count) ? null : b[i];

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

            return diff;
        }
Ejemplo n.º 8
0
        public static ListDiff Merge(ListDiff left, ListDiff right)
        {
            List<int> keys = left.Operations.Keys.ToList<int>();
            foreach (var kv in right.Operations)
                if (!left.Operations.ContainsKey(kv.Key))
                    keys.Add(kv.Key);

            ListDiff diff = new ListDiff();
            foreach (int 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;
        }