summaryrefslogtreecommitdiff
path: root/platform/util/src/com/intellij/util/diff/DiffTree.java
blob: 680b15b7d1de8866c0666574c55e8be1665eebf9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/*
 * Copyright 2000-2014 JetBrains s.r.o.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.intellij.util.diff;

import com.intellij.openapi.util.Ref;
import com.intellij.util.ThreeState;

import java.util.ArrayList;
import java.util.List;

/**
 * @author max
 */
public class DiffTree<OT, NT> {
  private static final int CHANGE_PARENT_VERSUS_CHILDREN_THRESHOLD = 20;

  private final FlyweightCapableTreeStructure<OT> myOldTree;
  private final FlyweightCapableTreeStructure<NT> myNewTree;
  private final ShallowNodeComparator<OT, NT> myComparator;
  private final DiffTreeChangeBuilder<OT, NT> myConsumer;
  private final List<Ref<OT[]>> myOldChildrenLists = new ArrayList<Ref<OT[]>>();
  private final List<Ref<NT[]>> myNewChildrenLists = new ArrayList<Ref<NT[]>>();

  private DiffTree(final FlyweightCapableTreeStructure<OT> oldTree,
                   final FlyweightCapableTreeStructure<NT> newTree,
                   final ShallowNodeComparator<OT, NT> comparator,
                   final DiffTreeChangeBuilder<OT, NT> consumer) {
    myOldTree = oldTree;
    myNewTree = newTree;
    myComparator = comparator;
    myConsumer = consumer;
  }

  public static <OT, NT> void diff(final FlyweightCapableTreeStructure<OT> oldTree,
                                   final FlyweightCapableTreeStructure<NT> newTree,
                                   final ShallowNodeComparator<OT, NT> comparator,
                                   final DiffTreeChangeBuilder<OT, NT> consumer) {
    new DiffTree<OT, NT>(oldTree, newTree, comparator, consumer).build(oldTree.getRoot(), newTree.getRoot(), 0);
  }

  private enum CompareResult {
    EQUAL, // 100% equal
    DRILL_DOWN_NEEDED, // element types are equal, but elements are composite
    TYPE_ONLY, // only element types are equal
    NOT_EQUAL, // 100% different
  }

  private void build(OT oldN, NT newN, int level) {
    OT oldNode = myOldTree.prepareForGetChildren(oldN);
    NT newNode = myNewTree.prepareForGetChildren(newN);

    if (level >= myNewChildrenLists.size()) {
      myNewChildrenLists.add(new Ref<NT[]>());
      myOldChildrenLists.add(new Ref<OT[]>());
    }

    final Ref<OT[]> oldChildrenR = myOldChildrenLists.get(level);
    int oldSize = myOldTree.getChildren(oldNode, oldChildrenR);
    final OT[] oldChildren = oldChildrenR.get();

    final Ref<NT[]> newChildrenR = myNewChildrenLists.get(level);
    int newSize = myNewTree.getChildren(newNode, newChildrenR);
    final NT[] newChildren = newChildrenR.get();

    compareLevel(level, oldNode, oldSize, oldChildren, newNode, newSize, newChildren);
    disposeLevel(oldChildren, oldSize, newChildren, newSize);
  }

  private void compareLevel(int level, OT oldNode, int oldSize, OT[] oldChildren, NT newNode, int newSize, NT[] newChildren) {
    if (Math.abs(oldSize - newSize) > CHANGE_PARENT_VERSUS_CHILDREN_THRESHOLD) {
      myConsumer.nodeReplaced(oldNode, newNode);
      return;
    }

    final ShallowNodeComparator<OT, NT> comparator = myComparator;
    if (oldSize == 0 && newSize == 0) {
      if (!comparator.hashCodesEqual(oldNode, newNode) || !comparator.typesEqual(oldNode, newNode)) {
        myConsumer.nodeReplaced(oldNode, newNode);
      }
      return;
    }

    while (oldSize > 0 && newSize > 0) {
      OT oldChild1 = oldChildren[oldSize-1];
      NT newChild1 = newChildren[newSize-1];

      CompareResult c11 = looksEqual(comparator, oldChild1, newChild1);

      if (c11 != CompareResult.EQUAL && c11 != CompareResult.DRILL_DOWN_NEEDED) {
        break;
      }
      if (c11 == CompareResult.DRILL_DOWN_NEEDED) {
        build(oldChild1, newChild1, level + 1);
      }
      oldSize--;
      newSize--;
    }

    int oldIndex = 0;
    int newIndex = 0;
    while (oldIndex < oldSize || newIndex < newSize) {
      OT oldChild1 = oldIndex < oldSize ? oldChildren[oldIndex] : null;
      OT oldChild2 = oldIndex < oldSize-1 ? oldChildren[oldIndex+1] : null;
      NT newChild1 = newIndex < newSize ? newChildren[newIndex] : null;
      NT newChild2 = newIndex < newSize-1 ? newChildren[newIndex+1] : null;

      CompareResult c11 = looksEqual(comparator, oldChild1, newChild1);
      if (c11 == CompareResult.EQUAL || c11 == CompareResult.DRILL_DOWN_NEEDED) {
        if (c11 == CompareResult.DRILL_DOWN_NEEDED) {
          build(oldChild1, newChild1, level+1);
        }
        oldIndex++;
        newIndex++;
        continue;
      }
      if (c11 == CompareResult.TYPE_ONLY) {
        CompareResult c21 = looksEqual(comparator, oldChild2, newChild1);
        if (c21 == CompareResult.EQUAL || c21 == CompareResult.DRILL_DOWN_NEEDED) {
          myConsumer.nodeDeleted(oldNode, oldChild1);
          oldIndex++;
          continue;
        }
        CompareResult c12 = looksEqual(comparator, oldChild1, newChild2);
        if (c12 == CompareResult.EQUAL || c12 == CompareResult.DRILL_DOWN_NEEDED) {
          myConsumer.nodeInserted(oldNode, newChild1, newIndex);
          newIndex++;
          continue;
        }
        myConsumer.nodeReplaced(oldChild1, newChild1);
        oldIndex++;
        newIndex++;
        continue;
      }

      CompareResult c12 = looksEqual(comparator, oldChild1, newChild2);
      if (c12 == CompareResult.EQUAL || c12 == CompareResult.DRILL_DOWN_NEEDED || c12 == CompareResult.TYPE_ONLY) {
        myConsumer.nodeInserted(oldNode, newChild1, newIndex);
        newIndex++;
        continue;
      }

      CompareResult c21 = looksEqual(comparator, oldChild2, newChild1);
      if (c21 == CompareResult.EQUAL || c21 == CompareResult.DRILL_DOWN_NEEDED || c21 == CompareResult.TYPE_ONLY) {
        myConsumer.nodeDeleted(oldNode, oldChild1);
        oldIndex++;
        continue;
      }

      if (oldChild1 == null) {
        myConsumer.nodeInserted(oldNode, newChild1, newIndex);
        newIndex++;
        continue;
      }
      if (newChild1 == null) {
        myConsumer.nodeDeleted(oldNode, oldChild1);
        oldIndex++;
        continue;
      }

      myConsumer.nodeReplaced(oldChild1, newChild1);
      oldIndex++;
      newIndex++;
    }
  }

  private CompareResult looksEqual(ShallowNodeComparator<OT, NT> comparator, OT oldChild1, NT newChild1) {
    if (oldChild1 == null || newChild1 == null) {
      return oldChild1 == newChild1 ? CompareResult.EQUAL : CompareResult.NOT_EQUAL;
    }
    if (!comparator.typesEqual(oldChild1, newChild1)) return CompareResult.NOT_EQUAL;
    ThreeState ret = comparator.deepEqual(oldChild1, newChild1);
    if (ret == ThreeState.YES) return CompareResult.EQUAL;
    if (ret == ThreeState.UNSURE) return CompareResult.DRILL_DOWN_NEEDED;
    return CompareResult.TYPE_ONLY;
  }

  private void disposeLevel(final OT[] oldChildren, final int oldSize, final NT[] newChildren, final int newSize) {
    myOldTree.disposeChildren(oldChildren, oldSize);
    myNewTree.disposeChildren(newChildren, newSize);
  }
}