Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: aa5024239dd04e120714671eb6612a406e271089 (plain) (blame)
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
/*******************************************************************************
 * Copyright (c) 2006, 2015 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.ui.internal.texteditor.rulers;

import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Set;

import org.eclipse.core.runtime.Assert;

/**
 * A directed acyclic graph. See http://en.wikipedia.org/wiki/Directed_acyclic_graph
 * @param <E> type of the vertices
 *
 * @since 3.3
 */
public final class DAG<E> {
	/**
	 * Multimap, supports <code>null</code> key, but not <code>null</code> values.
	 * @param <K> key type
	 * @param <V> values type
	 */
	private static final class MultiMap<K, V> {
		private final Map<K, Set<V>> fMap= new LinkedHashMap<>();

		/**
		 * Adds <code>val</code> to the values mapped to by <code>key</code>. If
		 * <code>val</code> is <code>null</code>, <code>key</code> is added to the key set of
		 * the multimap.
		 *
		 * @param key the key
		 * @param val the value
		 */
		public void put(K key, V val) {
			Set<V> values= fMap.get(key);
			if (values == null) {
				values= new LinkedHashSet<>();
				fMap.put(key, values);
			}
			if (val != null)
				values.add(val);
		}

		/**
		 * Returns all mappings for the given key, an empty set if there are no mappings.
		 *
		 * @param key the key
		 * @return the mappings for <code>key</code>
		 */
		public Set<V> get(K key) {
			Set<V> values= fMap.get(key);
			return values == null ? Collections.emptySet() : values;
		}

		public Set<K> keySet() {
			return fMap.keySet();
		}

		/**
		 * Removes all mappings for <code>key</code> and removes <code>key</code> from the key
		 * set.
		 *
		 * @param key the key to remove
		 * @return the removed mappings
		 */
		public Set<V> removeAll(K key) {
			Set<V> values= fMap.remove(key);
			return values == null ? Collections.emptySet() : values;
		}

		/**
		 * Removes a mapping from the multimap, but does not remove the <code>key</code> from the
		 * key set.
		 *
		 * @param key the key
		 * @param val the value
		 */
		public void remove(K key, V val) {
			Set<V> values= fMap.get(key);
			if (values != null)
				values.remove(val);
		}

		@Override
		public String toString() {
			return fMap.toString();
		}
	}

	private final MultiMap<E, E> fOut= new MultiMap<>();
	private final MultiMap<E, E> fIn= new MultiMap<>();

	/**
	 * Adds a directed edge from <code>origin</code> to <code>target</code>. The vertices are not
	 * required to exist prior to this call - if they are not currently contained by the graph, they are
	 * automatically added.
	 *
	 * @param origin the origin vertex of the dependency
	 * @param target the target vertex of the dependency
	 * @return <code>true</code> if the edge was added, <code>false</code> if the
	 *         edge was not added because it would have violated the acyclic nature of the
	 *         receiver.
	 */
	public boolean addEdge(E origin, E target) {
		Assert.isLegal(origin != null);
		Assert.isLegal(target != null);

		if (hasPath(target, origin))
			return false;

		fOut.put(origin, target);
		fOut.put(target, null);
		fIn.put(target, origin);
		fIn.put(origin, null);
		return true;
	}

	/**
	 * Adds a vertex to the graph. If the vertex does not exist prior to this call, it is added with
	 * no incoming or outgoing edges. Nothing happens if the vertex already exists.
	 *
	 * @param vertex the new vertex
	 */
	public void addVertex(E vertex) {
		Assert.isLegal(vertex != null);
		fOut.put(vertex, null);
		fIn.put(vertex, null);
	}

	/**
	 * Removes a vertex and all its edges from the graph.
	 *
	 * @param vertex the vertex to remove
	 */
	public void removeVertex(E vertex) {
		Set<E> targets= fOut.removeAll(vertex);
		for (Iterator<E> it= targets.iterator(); it.hasNext();)
			fIn.remove(it.next(), vertex);
		Set<E> origins= fIn.removeAll(vertex);
		for (Iterator<E> it= origins.iterator(); it.hasNext();)
			fOut.remove(it.next(), vertex);
	}

	/**
	 * Returns the sources of the receiver. A source is a vertex with no incoming edges. The
	 * returned set's iterator traverses the nodes in the order they were added to the graph.
	 *
	 * @return the sources of the receiver
	 */
	public Set<E> getSources() {
		return computeZeroEdgeVertices(fIn);
	}

	/**
	 * Returns the sinks of the receiver. A sink is a vertex with no outgoing edges. The returned
	 * set's iterator traverses the nodes in the order they were added to the graph.
	 *
	 * @return the sinks of the receiver
	 */
	public Set<E> getSinks() {
		return computeZeroEdgeVertices(fOut);
	}

	private static <T> Set<T> computeZeroEdgeVertices(MultiMap<T, T> map) {
		Set<T> candidates= map.keySet();
		Set<T> roots= new LinkedHashSet<>(candidates.size());
		for (Iterator<T> it= candidates.iterator(); it.hasNext();) {
			T candidate= it.next();
			if (map.get(candidate).isEmpty())
				roots.add(candidate);
		}
		return roots;
	}

	/**
	 * Returns the direct children of a vertex. The returned {@link Set} is unmodifiable.
	 *
	 * @param vertex the parent vertex
	 * @return the direct children of <code>vertex</code>
	 */
	public Set<E> getChildren(E vertex) {
		return Collections.unmodifiableSet(fOut.get(vertex));
	}

	private boolean hasPath(E start, E end) {
		// break condition
		if (start == end)
			return true;

		Set<E> children= fOut.get(start);
		for (Iterator<E> it= children.iterator(); it.hasNext();)
			// recursion
			if (hasPath(it.next(), end))
				return true;
		return false;
	}

	@Override
	public String toString() {
		return "Out: " + fOut.toString() + " In: " + fIn.toString(); //$NON-NLS-1$ //$NON-NLS-2$
	}
}

Back to the top