-- | Smart constructor that deals with caching the 'Graph' representation.
--
mkInstallPlan :: (HasUnitId ipkg,   PackageFixedDeps ipkg,
                  HasUnitId srcpkg, PackageFixedDeps srcpkg)
              => PlanIndex ipkg srcpkg iresult ifailure
              -> IndependentGoals
              -> GenericInstallPlan ipkg srcpkg iresult ifailure
mkInstallPlan index indepGoals =
    GenericInstallPlan {
      planIndex      = index,
      planIndepGoals = indepGoals,

      -- lazily cache the graph stuff:
      planGraph      = graph,
      planGraphRev   = Graph.transposeG graph,
      planPkgIdOf    = vertexToPkgId,
      planVertexOf   = fromMaybe noSuchPkgId . pkgIdToVertex
    }
  where
    (graph, vertexToPkgId, pkgIdToVertex) =
      PlanIndex.dependencyGraph index
    noSuchPkgId = internalError "package is not in the graph"
mkInstallPlan :: (HasUnitId ipkg,   PackageFixedDeps ipkg,
                  HasUnitId srcpkg, PackageFixedDeps srcpkg)
              => PlanIndex ipkg srcpkg iresult ifailure
              -> IndependentGoals
              -> GenericInstallPlan ipkg srcpkg iresult ifailure
mkInstallPlan index indepGoals =
    GenericInstallPlan {
      planIndex      = index,
      planIndepGoals = indepGoals,

      -- lazily cache the graph stuff:
      planGraph      = graph,
      planGraphRev   = Graph.transposeG graph,
      planPkgIdOf    = vertexToPkgId,
      planVertexOf   = fromMaybe noSuchPkgId . pkgIdToVertex
    }
  where
    (graph, vertexToPkgId, pkgIdToVertex) =
      PlanIndex.dependencyGraph index
    noSuchPkgId = internalError "package is not in the graph"
mkInstallPlan index indepGoals =
    GenericInstallPlan {
      planIndex      = index,
      planIndepGoals = indepGoals,

      -- lazily cache the graph stuff:
      planGraph      = graph,
      planGraphRev   = Graph.transposeG graph,
      planPkgIdOf    = vertexToPkgId,
      planVertexOf   = fromMaybe noSuchPkgId . pkgIdToVertex
    }
  where
    (graph, vertexToPkgId, pkgIdToVertex) =
      PlanIndex.dependencyGraph index
    noSuchPkgId = internalError "package is not in the graph"