Edge class

Пакет: com.hypixel.hytale.component.dependency

Файл: com/hypixel/hytale/component/dependency/DependencyGraph.java

implements: Comparable<DependencyGraph.Edge<ECS_TYPE>>

Поля (5)

МодификаторыТипИмя
private final ISystem<ECS_TYPE> afterSystem
private final ISystem<ECS_TYPE> beforeSystem
private boolean fulfilled
private final int priority
private boolean resolved

Методы (3)

МодификаторыВозвратСигнатура
public int compareTopublic int compareTo(@Nonnull DependencyGraph.Edge<ECS_TYPE> var1)
public static <ECS_TYPE> DependencyGraph.Edge<ECS_TYPE>[] emptyArraypublic static <ECS_TYPE> DependencyGraph.Edge<ECS_TYPE>[] emptyArray()
public String toStringpublic String toString()

Исходный код

Показать/скрыть
class="kw">package com.hypixel.hytale.component.dependency;

class="kw">import com.hypixel.hytale.component.ComponentRegistry;
class="kw">import com.hypixel.hytale.component.system.ISystem;
class="kw">import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap;
class="kw">import it.unimi.dsi.fastutil.objects.ObjectArrayList;
class="kw">import java.util.Arrays;
class="kw">import java.util.HashSet;
class="kw">import java.util.List;
class="kw">import java.util.Map;
class="kw">import java.util.Set;
class="kw">import javax.annotation.Nonnull;
class="kw">import javax.annotation.Nullable;

class="kw">public class DependencyGraph<ECS_TYPE> {
   @Nonnull
   class="kw">private class="kw">final ISystem<ECS_TYPE>[] systems;
   @Nonnull
   class="kw">private class="kw">final Map<ISystem<ECS_TYPE>, List<DependencyGraph.Edge<ECS_TYPE>>> beforeSystemEdges = new Object2ObjectOpenHashMap();
   @Nonnull
   class="kw">private class="kw">final Map<ISystem<ECS_TYPE>, List<DependencyGraph.Edge<ECS_TYPE>>> afterSystemEdges = new Object2ObjectOpenHashMap();
   @Nonnull
   class="kw">private class="kw">final Map<ISystem<ECS_TYPE>, Set<DependencyGraph.Edge<ECS_TYPE>>> afterSystemUnfulfilledEdges = new Object2ObjectOpenHashMap();
   class="kw">private DependencyGraph.Edge<ECS_TYPE>[] edges = DependencyGraph.Edge.emptyArray();

   class="kw">public DependencyGraph(@Nonnull ISystem<ECS_TYPE>[] var1) {
      this.systems = var1;

      for (int var2 = 0; var2 < var1.length; var2++) {
         ISystem var3 = var1[var2];
         this.beforeSystemEdges.put(var3, new ObjectArrayList());
         this.afterSystemEdges.put(var3, new ObjectArrayList());
         this.afterSystemUnfulfilledEdges.put(var3, new HashSet<>());
      }
   }

   @Nonnull
   class="kw">public ISystem<ECS_TYPE>[] getSystems() {
      class="kw">return this.systems;
   }

   class="kw">public void resolveEdges(@Nonnull ComponentRegistry<ECS_TYPE> var1) {
      for (ISystem var5 : this.systems) {
         for (Dependency var7 : var5.getDependencies()) {
            var7.resolveGraphEdge(var1, var5, this);
         }

         if (var5.getGroup() != null) {
            for (Dependency var16 : var5.getGroup().getDependencies()) {
               var16.resolveGraphEdge(var1, var5, this);
            }
         }
      }

      for (ISystem var13 : this.systems) {
         if (this.afterSystemEdges.get(var13).isEmpty()) {
            int var15 = 0;
            List var17 = this.beforeSystemEdges.get(var13);

            for (DependencyGraph.Edge var9 : var17) {
               var15 += var9.priority / var17.size();
            }

            this.addEdgeFromRoot(var13, var15);
         }
      }
   }

   class="kw">public void addEdgeFromRoot(@Nonnull ISystem<ECS_TYPE> var1, int var2) {
      this.addEdge(new DependencyGraph.Edge<>(null, var1, var2));
   }

   class="kw">public void addEdge(@Nonnull ISystem<ECS_TYPE> var1, @Nonnull ISystem<ECS_TYPE> var2, int var3) {
      this.addEdge(new DependencyGraph.Edge<>(var1, var2, var3));
   }

   class="kw">public void addEdge(@Nonnull DependencyGraph.Edge<ECS_TYPE> var1) {
      int var2 = Arrays.binarySearch(this.edges, var1);
      int var3;
      if (var2 >= 0) {
         var3 = var2;

         while (var3 < this.edges.length && this.edges[var3].priority == var1.priority) {
            var3++;
         }
      } else {
         var3 = -(var2 + 1);
      }

      int var4 = this.edges.length;
      int var5 = var4 + 1;
      if (var4 < var5) {
         this.edges = Arrays.copyOf(this.edges, var5);
      }

      System.arraycopy(this.edges, var3, this.edges, var3 + 1, var4 - var3);
      this.edges[var3] = var1;
      if (var1.beforeSystem != null) {
         this.beforeSystemEdges.get(var1.beforeSystem).add(var1);
      }

      this.afterSystemEdges.get(var1.afterSystem).add(var1);
      if (!var1.fulfilled) {
         this.afterSystemUnfulfilledEdges.get(var1.afterSystem).add(var1);
      }
   }

   class="kw">public void sort(@Nonnull ISystem<ECS_TYPE>[] var1) {
      int var2 = 0;

      label52:
      while (var2 < this.systems.length) {
         for (DependencyGraph.Edge var6 : this.edges) {
            if (!var6.resolved && var6.fulfilled) {
               ISystem var7 = var6.afterSystem;
               if (this.afterSystemUnfulfilledEdges.get(var7).isEmpty() && !this.hasEdgeOfLaterPriority(var7, var6.priority)) {
                  var1[var2++] = var7;
                  this.resolveEdgesFor(var7);
                  this.fulfillEdgesFor(var7);
                  class="kw">continue label52;
               }
            }
         }

         for (DependencyGraph.Edge var11 : this.edges) {
            if (!var11.resolved && var11.fulfilled) {
               ISystem var12 = var11.afterSystem;
               if (this.afterSystemUnfulfilledEdges.get(var12).isEmpty()) {
                  var1[var2++] = var12;
                  this.resolveEdgesFor(var12);
                  this.fulfillEdgesFor(var12);
                  class="kw">continue label52;
               }
            }
         }

         throw new IllegalArgumentException("Found a cyclic dependency!" + this);
      }
   }

   class="kw">private boolean hasEdgeOfLaterPriority(@Nonnull ISystem<ECS_TYPE> var1, int var2) {
      for (DependencyGraph.Edge var4 : this.afterSystemEdges.get(var1)) {
         if (!var4.resolved && var4.priority > var2) {
            class="kw">return true;
         }
      }

      class="kw">return false;
   }

   class="kw">private void resolveEdgesFor(@Nonnull ISystem<ECS_TYPE> var1) {
      for (DependencyGraph.Edge var3 : this.afterSystemEdges.get(var1)) {
         var3.resolved = true;
      }
   }

   class="kw">private void fulfillEdgesFor(@Nonnull ISystem<ECS_TYPE> var1) {
      for (DependencyGraph.Edge var3 : this.beforeSystemEdges.get(var1)) {
         var3.fulfilled = true;
         this.afterSystemUnfulfilledEdges.get(var3.afterSystem).remove(var3);
      }
   }

   @Nonnull
   @Override
   class="kw">public String toString() {
      class="kw">return "DependencyGraph{systems=" + Arrays.toString(this.systems) + ", edges=" + Arrays.toString(this.edges) + "}";
   }

   class="kw">private class="kw">static class Edge<ECS_TYPE> class="kw">implements Comparable<DependencyGraph.Edge<ECS_TYPE>> {
      @Nonnull
      class="kw">private class="kw">static class="kw">final DependencyGraph.Edge<?>[] EMPTY_ARRAY = new DependencyGraph.Edge[0];
      @Nullable
      class="kw">private class="kw">final ISystem<ECS_TYPE> beforeSystem;
      class="kw">private class="kw">final ISystem<ECS_TYPE> afterSystem;
      class="kw">private class="kw">final int priority;
      class="kw">private boolean fulfilled;
      class="kw">private boolean resolved;

      class="kw">public class="kw">static <ECS_TYPE> DependencyGraph.Edge<ECS_TYPE>[] emptyArray() {
         class="kw">return (DependencyGraph.Edge<ECS_TYPE>[])EMPTY_ARRAY;
      }

      class="kw">public Edge(@Nullable ISystem<ECS_TYPE> var1, @Nonnull ISystem<ECS_TYPE> var2, int var3) {
         this.beforeSystem = var1;
         this.afterSystem = var2;
         this.priority = var3;
         this.fulfilled = var1 == null;
      }

      class="kw">public int compareTo(@Nonnull DependencyGraph.Edge<ECS_TYPE> var1) {
         class="kw">return Integer.compare(this.priority, var1.priority);
      }

      @Nonnull
      @Override
      class="kw">public String toString() {
         class="kw">return "Edge{beforeSystem="
            + this.beforeSystem
            + ", afterSystem="
            + this.afterSystem
            + ", priority="
            + this.priority
            + ", fulfilled="
            + this.fulfilled
            + ", resolved="
            + this.resolved
            + "}";
      }
   }
}