// 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 <array>
#include <functional>
#include <map>
#include <vector>

#include "Iterator.hh"
#include "StringUtil.hh"

namespace sta {

class Transition;
class RiseFall;
class RiseFallBoth;

using TransitionMap = std::map<std::string, const Transition*, std::less<>>;

// Rise/fall transition.
class RiseFall
{
public:
  // Singleton accessors.
  static const RiseFall *rise() { return &rise_; }
  static const RiseFall *fall() { return &fall_; }
  static size_t riseIndex() { return rise_.sdf_triple_index_; }
  static size_t fallIndex() { return fall_.sdf_triple_index_; }
  const std::string &to_string(bool use_short = false) const;
  const std::string &name() const { return name_; }
  const std::string &shortName() const { return short_name_; }
  size_t index() const { return sdf_triple_index_; }
  const RiseFallBoth *asRiseFallBoth();
  const RiseFallBoth *asRiseFallBoth() const;
  const Transition *asTransition() const;
  // Find transition corresponding to rf_str.
  static const RiseFall *find(std::string_view rf_name);
  // Find transition from index.
  static const RiseFall *find(size_t index);
  const RiseFall *opposite() const;

  // for range support.
  // for (auto rf : RiseFall::range()) {}
  static const std::array<const RiseFall*, 2> &range() { return range_; }
  // for (auto rf_index : RiseFall::rangeIndex()) {}
  static const std::array<size_t, 2> &rangeIndex() { return range_index_; }
  static const size_t index_count = 2;
  static const size_t index_max = (index_count - 1);
  static const size_t index_bit_count = 1;

protected:
  RiseFall(std::string_view name,
           std::string_view  short_name,
           size_t sdf_triple_index);

  const std::string name_;
  const std::string short_name_;
  const size_t sdf_triple_index_;

  static const RiseFall rise_;
  static const RiseFall fall_;
  static const std::array<const RiseFall*, 2> range_;
  static const std::array<size_t, 2> range_index_;
};

// Rise/fall/risefall transition.
class RiseFallBoth
{
public:
  // Singleton accessors.
  static const RiseFallBoth *rise() { return &rise_; }
  static const RiseFallBoth *fall() { return &fall_; }
  static const RiseFallBoth *riseFall() { return &rise_fall_; }
  const std::string &to_string(bool use_short = false) const;
  const std::string &name() const { return name_; }
  const std::string &shortName() const { return short_name_; }
  size_t index() const { return sdf_triple_index_; }
  bool matches(const RiseFall *rf) const;
  bool matches(const Transition *tr) const;
  const RiseFall *asRiseFall() const { return as_rise_fall_; }
  // Find transition corresponding to string.
  static const RiseFallBoth *find(std::string_view rf_name);
  // for (const auto rf : rf->range()) {}
  const std::vector<const RiseFall*> &range() const { return range_; }
  // for (const auto rf_index : rf->rangeIndex()) {}
  const std::vector<size_t> &rangeIndex() const { return range_index_; }

  static const size_t index_count = 3;
  static const size_t index_max = (index_count - 1);
  static const size_t index_bit_count = 2;

protected:
  RiseFallBoth(std::string_view name,
               std::string_view short_name,
               size_t sdf_triple_index,
               const RiseFall *as_rise_fall,
               const std::vector<const RiseFall*> &range,
               const std::vector<size_t> &range_index);

  const std::string name_;
  const std::string short_name_;
  const size_t sdf_triple_index_;
  const RiseFall *as_rise_fall_;
  const std::vector<const RiseFall*> range_;
  const std::vector<size_t> range_index_;

  static const RiseFallBoth rise_;
  static const RiseFallBoth fall_;
  static const RiseFallBoth rise_fall_;
};

// General SDF transition.
class Transition
{
public:
  // Singleton accessors.
  static const Transition *rise() { return &rise_; }
  static const Transition *fall() { return &fall_; }
  static const Transition *tr0Z() { return &tr_0Z_; }
  static const Transition *trZ1() { return &tr_Z1_; }
  static const Transition *tr1Z() { return &tr_1Z_; }
  static const Transition *trZ0() { return &tr_Z0_; }
  static const Transition *tr0X() { return &tr_0X_; }
  static const Transition *trX1() { return &tr_X1_; }
  static const Transition *tr1X() { return &tr_1X_; }
  static const Transition *trX0() { return &tr_X0_; }
  static const Transition *trXZ() { return &tr_XZ_; }
  static const Transition *trZX() { return &tr_ZX_; }
  // Matches rise and fall.
  static const Transition *riseFall() { return &rise_fall_; }
  const std::string &to_string() const { return name_; }
  // As initial/final value pair.
  const std::string &asInitFinalString() const { return init_final_; }
  size_t sdfTripleIndex() const { return sdf_triple_index_; }
  size_t index() const { return sdf_triple_index_; }
  const RiseFall *asRiseFall() const { return as_rise_fall_; }
  const RiseFallBoth *asRiseFallBoth() const;
  bool matches(const Transition *tr) const;
  // Find transition corresponding to string.
  static const Transition *find(std::string_view tr_name);
  static size_t maxIndex() { return max_index_; }

private:
  Transition(std::string_view name,
             std::string_view init_final,
             const RiseFall *as_rise_fall,
             size_t sdf_triple_index);

  const std::string name_;
  const std::string init_final_;
  const RiseFall *as_rise_fall_;
  const size_t sdf_triple_index_;

  static const Transition rise_;
  static const Transition fall_;
  static const Transition tr_0Z_;
  static const Transition tr_Z1_;
  static const Transition tr_1Z_;
  static const Transition tr_Z0_;
  static const Transition tr_0X_;
  static const Transition tr_X1_;
  static const Transition tr_1X_;
  static const Transition tr_X0_;
  static const Transition tr_XZ_;
  static const Transition tr_ZX_;
  static const Transition rise_fall_;
  static const size_t index_count = 13;
  static const size_t index_max = (index_count - 1);
  static const size_t index_bit_count = 4;

  static TransitionMap transition_map_;
  static size_t max_index_;
};

} // namespace sta
