aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/main/java/com/google/devtools/build/lib/graph/Node.java
blob: 9db2a4c3e77530ca518e49fa1b6d0387f8294ecf (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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
// Copyright 2014 Google Inc. All rights reserved.
//
// 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.google.devtools.build.lib.graph;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;

/**
 * <p>A generic directed-graph Node class.  Type parameter T is the type
 * of the node's label.
 *
 * <p>Each node is identified by a label, which is unique within the graph
 * owning the node.
 *
 * <p>Nodes are immutable, that is, their labels cannot be changed.  However,
 * their predecessor/successor lists are mutable.
 *
 * <p>Nodes cannot be created directly by clients.
 *
 * <p>Clients should not confuse nodes belonging to two different graphs!  (Use
 * Digraph.checkNode() to catch such errors.)  There is no way to find the
 * graph to which a node belongs; it is intentionally not represented, to save
 * space.
 */
public final class Node<T> {

  private static final int ARRAYLIST_THRESHOLD = 6;
  private static final int INITIAL_HASHSET_CAPACITY = 12;

  // The succs and preds set representation changes depending on its size.
  // It is implemented using the following collections:
  // - null for size = 0.
  // - Collections$SingletonList for size = 1.
  // - ArrayList(6) for size = [2..6].
  // - HashSet(12) for size > 6.
  // These numbers were chosen based on profiling.

  private final T label;

  /**
   * A duplicate-free collection of edges from this node.  May be null,
   * indicating the empty set.
   */
  private Collection<Node<T>> succs = null;

  /**
   * A duplicate-free collection of edges to this node.  May be null,
   * indicating the empty set.
   */
  private Collection<Node<T>> preds = null;

  private final int hashCode;

  /**
   * Only Digraph.createNode() can call this!
   */
  Node(T label, int hashCode) {
    if (label == null) { throw new NullPointerException("label"); }
    this.label = label;
    this.hashCode = hashCode;
  }

  /**
   * Returns the label for this node.
   */
  public T getLabel() {
    return label;
  }

  /**
   * Returns a duplicate-free collection of the nodes that this node links to.
   */
  public Collection<Node<T>> getSuccessors() {
    if (succs == null) {
      return Collections.emptyList();
    } else {
      return Collections.unmodifiableCollection(succs);
    }
  }

  /**
   * Equivalent to {@code !getSuccessors().isEmpty()} but possibly more
   * efficient.
   */
  public boolean hasSuccessors() {
    return succs != null;
  }

  /**
   * Equivalent to {@code getSuccessors().size()} but possibly more efficient.
   */
  public int numSuccessors() {
    return succs == null ? 0 : succs.size();
  }

  /**
   * Removes all edges to/from this node.
   * Private: breaks graph invariant!
   */
  void removeAllEdges() {
    this.succs = null;
    this.preds = null;
  }

  /**
   * Returns an (unordered, possibly immutable) set of the nodes that link to
   * this node.
   */
  public Collection<Node<T>> getPredecessors() {
    if (preds == null) {
      return Collections.emptyList();
    } else {
      return Collections.unmodifiableCollection(preds);
    }
  }

  /**
   * Equivalent to {@code getPredecessors().size()} but possibly more
   * efficient.
   */
  public int numPredecessors() {
    return preds == null ? 0 : preds.size();
  }

  /**
   * Equivalent to {@code !getPredecessors().isEmpty()} but possibly more
   * efficient.
   */
  public boolean hasPredecessors() {
    return preds != null;
  }

  /**
   * Adds 'value' to either the predecessor or successor set, updating the
   * appropriate field as necessary.
   * @return {@code true} if the set was modified; {@code false} if the set
   * was not modified
   */
  private boolean add(boolean predecessorSet, Node<T> value) {
    final Collection<Node<T>> set = predecessorSet ? preds : succs;
    if (set == null) {
      // null -> SingletonList
      return updateField(predecessorSet, Collections.singletonList(value));
    }
    if (set.contains(value)) {
      // already exists in this set
      return false;
    }
    int previousSize = set.size();
    if (previousSize == 1) {
      // SingletonList -> ArrayList
      Collection<Node<T>> newSet =
        new ArrayList<Node<T>>(ARRAYLIST_THRESHOLD);
      newSet.addAll(set);
      newSet.add(value);
      return updateField(predecessorSet, newSet);
    } else if (previousSize < ARRAYLIST_THRESHOLD) {
      // ArrayList
      set.add(value);
      return true;
  } else if (previousSize == ARRAYLIST_THRESHOLD) {
      // ArrayList -> HashSet
      Collection<Node<T>> newSet =
        new HashSet<Node<T>>(INITIAL_HASHSET_CAPACITY);
      newSet.addAll(set);
      newSet.add(value);
      return updateField(predecessorSet, newSet);
    } else {
      // HashSet
      set.add(value);
      return true;
    }
  }

  /**
   * Removes 'value' from either 'preds' or 'succs', updating the appropriate
   * field as necessary.
   * @return {@code true} if the set was modified; {@code false} if the set
   * was not modified
   */
  private boolean remove(boolean predecessorSet, Node<T> value) {
    final Collection<Node<T>> set = predecessorSet ? preds : succs;
    if (set == null) {
      // null
      return false;
    }

    int previousSize = set.size();
    if (previousSize == 1) {
      if (set.contains(value)) {
        // -> null
        return updateField(predecessorSet, null);
      } else {
        return false;
      }
    }
    // now remove the value
    if (set.remove(value)) {
      // may need to change representation
      if (previousSize == 2) {
        // -> SingletonList
        List<Node<T>> list =
          Collections.singletonList(set.iterator().next());
        return updateField(predecessorSet, list);

      } else if (previousSize == 1 + ARRAYLIST_THRESHOLD) {
        // -> ArrayList
        Collection<Node<T>> newSet =
          new ArrayList<Node<T>>(ARRAYLIST_THRESHOLD);
        newSet.addAll(set);
        return updateField(predecessorSet, newSet);
      }
      return true;
    }
    return false;
  }

  /**
   * Update either the {@link #preds} or {@link #succs} field to point to the
   * new set.
   * @return {@code true}, because the set must have been updated
   */
  private boolean updateField(boolean predecessorSet,
      Collection<Node<T>> newSet) {
    if (predecessorSet) {
      preds = newSet;
    } else {
      succs = newSet;
    }
    return true;
  }


  /**
   * Add 'to' as a successor of 'this' node.  Returns true iff
   * the graph changed.  Private: breaks graph invariant!
   */
  boolean addSuccessor(Node<T> to) {
    return add(false, to);
  }

  /**
   * Add 'from' as a predecessor of 'this' node.  Returns true iff
   * the graph changed.  Private: breaks graph invariant!
   */
  boolean addPredecessor(Node<T> from) {
    return add(true, from);
  }

  /**
   * Remove edge: fromNode.succs = {n | n in fromNode.succs && n != toNode}
   * Private: breaks graph invariant!
   */
  boolean removeSuccessor(Node<T> to) {
    return remove(false, to);
  }

  /**
   * Remove edge: toNode.preds = {n | n in toNode.preds && n != fromNode}
   * Private: breaks graph invariant!
   */
  boolean removePredecessor(Node<T> from) {
    return remove(true, from);
  }

  @Override
  public String toString() {
    return "node:" + label;
  }

  @Override
  public int hashCode() {
    return hashCode; // Fast, deterministic.
  }

  @Override
  public boolean equals(Object that) {
    return this == that; // Nodes are unique for a given label
  }
}