mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
[vcs-log-graph] convert BfsUtil to kotlin
This commit is contained in:
@@ -13,59 +13,55 @@
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package com.intellij.vcs.log.graph.utils;
|
||||
package com.intellij.vcs.log.graph.utils
|
||||
|
||||
import com.intellij.util.containers.ContainerUtil;
|
||||
import com.intellij.vcs.log.graph.api.LiteLinearGraph;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import com.intellij.util.containers.ContainerUtil
|
||||
import com.intellij.vcs.log.graph.api.LiteLinearGraph
|
||||
import java.util.*
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Queue;
|
||||
object BfsUtil {
|
||||
fun getCorrespondingParent(graph: LiteLinearGraph, startNode: Int, endNode: Int, visited: Flags): Int {
|
||||
val candidates = graph.getNodes(startNode, LiteLinearGraph.NodeFilter.DOWN)
|
||||
if (candidates.size == 1) return candidates[0]
|
||||
if (candidates.contains(endNode)) return endNode
|
||||
|
||||
public class BfsUtil {
|
||||
public static int getCorrespondingParent(@NotNull LiteLinearGraph graph, int startNode, int endNode, @NotNull Flags visited) {
|
||||
List<Integer> candidates = graph.getNodes(startNode, LiteLinearGraph.NodeFilter.DOWN);
|
||||
if (candidates.size() == 1) return candidates.get(0);
|
||||
if (candidates.contains(endNode)) return endNode;
|
||||
|
||||
List<Queue<Integer>> queues = new ArrayList<>(candidates.size());
|
||||
for (int candidate : candidates) {
|
||||
queues.add(ContainerUtil.newLinkedList(candidate));
|
||||
val queues = ArrayList<Queue<Int>>(candidates.size)
|
||||
for (candidate in candidates) {
|
||||
queues.add(ContainerUtil.newLinkedList(candidate))
|
||||
}
|
||||
|
||||
int emptyCount;
|
||||
visited.setAll(false);
|
||||
var emptyCount: Int
|
||||
visited.setAll(false)
|
||||
do {
|
||||
emptyCount = 0;
|
||||
for (Queue<Integer> queue : queues) {
|
||||
emptyCount = 0
|
||||
for (queue in queues) {
|
||||
if (queue.isEmpty()) {
|
||||
emptyCount++;
|
||||
emptyCount++
|
||||
}
|
||||
else {
|
||||
boolean found = runNextBfsStep(graph, queue, visited, endNode);
|
||||
val found = runNextBfsStep(graph, queue, visited, endNode)
|
||||
if (found) {
|
||||
return candidates.get(queues.indexOf(queue));
|
||||
return candidates[queues.indexOf(queue)]
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
while (emptyCount < queues.size());
|
||||
while (emptyCount < queues.size)
|
||||
|
||||
return candidates.get(0);
|
||||
return candidates[0]
|
||||
}
|
||||
|
||||
private static boolean runNextBfsStep(@NotNull LiteLinearGraph graph, @NotNull Queue<Integer> queue, @NotNull Flags visited, int target) {
|
||||
private fun runNextBfsStep(graph: LiteLinearGraph, queue: Queue<Int>, visited: Flags, target: Int): Boolean {
|
||||
while (!queue.isEmpty()) {
|
||||
Integer node = queue.poll();
|
||||
if (!visited.get(node)) {
|
||||
visited.set(node, true);
|
||||
List<Integer> next = graph.getNodes(node, LiteLinearGraph.NodeFilter.DOWN);
|
||||
if (next.contains(target)) return true;
|
||||
queue.addAll(next);
|
||||
return false;
|
||||
val node = queue.poll()
|
||||
if (!visited.get(node!!)) {
|
||||
visited.set(node, true)
|
||||
val next = graph.getNodes(node, LiteLinearGraph.NodeFilter.DOWN)
|
||||
if (next.contains(target)) return true
|
||||
queue.addAll(next)
|
||||
return false
|
||||
}
|
||||
}
|
||||
return false;
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user