// OpenSTA, Static Timing Analyzer
// Copyright (c) 2026, Parallax Software, Inc.
// 
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// 
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
// 
// The origin of this software must not be misrepresented; you must not
// claim that you wrote the original software.
// 
// Altered source versions must be plainly marked as such, and must not be
// misrepresented as being the original software.
// 
// This notice may not be removed or altered from any source distribution.

#pragma once

#include <cstddef>
#include <queue>
#include <vector>

#include "Delay.hh"
#include "Iterator.hh"
#include "LibertyClass.hh"
#include "Path.hh"
#include "SearchClass.hh"
#include "StaState.hh"

namespace sta {

class Diversion;
class PathEnumFaninVisitor;
class DiversionGreater;

using DiversionSeq = std::vector<Diversion*>;
using DiversionQueue = std::priority_queue<Diversion*,DiversionSeq,
                                             DiversionGreater>;

class DiversionGreater
{
public:
  DiversionGreater();
  DiversionGreater(const StaState *sta);
  bool operator()(Diversion *div1,
                  Diversion *div2) const;

private:
  const StaState *sta_;
};

// Iterator to enumerate sucessively slower paths.
class PathEnum : public Iterator<PathEnd*>, StaState
{
public:
  PathEnum(size_t group_path_count,
           size_t endpoint_path_count,
           bool unique_pins,
           bool unique_edges,
           bool cmp_slack,
           const StaState *sta);
  // Insert path ends that are enumerated in slack/arrival order.
  void insert(PathEnd *path_end);
  ~PathEnum() override;
  bool hasNext() override;
  PathEnd *next() override;

private:
  void makeDiversions(PathEnd *path_end,
                      Path *before);
  void insert(Diversion *div);
  void makeDivertedPath(Path *path,
                        Path *before_div,
                        Path *after_div,
                        Edge *div_edge,
                        TimingArc *div_arc,
                        // Returned values.
                        Path *&div_path,
                        Path *&after_div_copy);
  void updatePathHeadDelays(PathSeq &path,
                            Path *after_div);
  Arrival divSlack(Path *before_div,
                   Path *after_div,
                   const Edge *div_edge,
                   const TimingArc *div_arc);
  void reportDiversionPath(Diversion *div);
  void pruneDiversionQueue();
  void findNext();

  bool cmp_slack_;
  size_t group_path_count_;
  size_t endpoint_path_count_;
  bool unique_pins_;
  bool unique_edges_;
  DiversionQueue div_queue_;
  int div_count_{0};
  // Number of paths returned for each endpoint (limit to endpoint_path_count).
  VertexPathCountMap path_counts_;
  bool inserts_pruned_{false};
  PathEnd *next_{nullptr};

  friend class PathEnumFaninVisitor;
};

} // namespace sta
