1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
|
// Copyright 2015 - 2025, GIBIS-UNIFESP and the wiRedPanda contributors
// SPDX-License-Identifier: GPL-3.0-or-later
#include "logicelement.h"
#include <QHash>
#include <QStack>
LogicElement::LogicElement(const int inputSize, const int outputSize)
: m_inputValues(inputSize, false)
, m_inputPairs(inputSize, {})
, m_outputValues(outputSize, false)
{
}
bool LogicElement::isValid() const
{
return m_isValid;
}
void LogicElement::clearSucessors()
{
for (const auto &logic : std::as_const(m_successors)) {
for (auto &inputPair : logic->m_inputPairs) {
if (inputPair.logic == this) {
inputPair.logic = nullptr;
inputPair.port = 0;
}
}
}
m_successors.clear();
}
bool LogicElement::updateInputs()
{
if (!m_isValid) {
return false;
}
for (int index = 0; index < m_inputPairs.size(); ++index) {
m_inputValues[index] = inputValue(index);
}
return true;
}
int LogicElement::priority() const
{
return m_priority;
}
bool LogicElement::inFeedbackLoop() const
{
return m_inFeedbackLoop;
}
int LogicElement::outputSize() const
{
return m_outputValues.size();
}
void LogicElement::connectPredecessor(const int index, LogicElement *logic, const int port)
{
m_inputPairs[index] = {logic, port};
if (!logic->m_successors.contains(this)) {
logic->m_successors.push_back(this);
}
}
void LogicElement::setOutputValue(const int index, const bool value)
{
m_outputValues[index] = value;
}
void LogicElement::setOutputValue(const bool value)
{
setOutputValue(0, value);
}
void LogicElement::validate()
{
m_isValid = std::all_of(m_inputPairs.cbegin(), m_inputPairs.cend(),
[](auto pair) { return pair.logic != nullptr; });
if (!m_isValid) {
for (auto *logic : std::as_const(m_successors)) {
logic->m_isValid = false;
}
}
}
int LogicElement::calculatePriority()
{
QStack<LogicElement *> stack;
QHash<LogicElement *, bool> inStack;
QHash<LogicElement *, int> maxPriority;
QHash<LogicElement *, bool> inFeedbackLoop;
stack.push(this);
inStack[this] = true;
while (!stack.isEmpty()) {
auto *current = stack.top();
if (current->m_priority != -1) {
stack.pop();
inStack[current] = false;
continue;
}
bool allProcessed = true;
int maxSuccessorPriority = 0;
bool hasFeedbackLoop = false;
for (auto *logic : std::as_const(current->m_successors)) {
if (logic->m_priority == -1) {
if (!inStack[logic]) {
stack.push(logic);
inStack[logic] = true;
allProcessed = false;
} else {
// FEEDBACK LOOP DETECTED: successor is already in processing stack
hasFeedbackLoop = true;
inFeedbackLoop[current] = true;
inFeedbackLoop[logic] = true;
// For feedback loops, don't wait for successor priority
// This breaks the circular dependency
}
}
// Only use successor priority if it's not a feedback loop
if (logic->m_priority != -1) {
maxSuccessorPriority = qMax(maxSuccessorPriority, logic->m_priority);
}
}
if (allProcessed || hasFeedbackLoop) {
if (hasFeedbackLoop) {
// Elements in feedback loops get special priority handling
// Set to a base priority that allows for iterative settling
current->m_priority = maxSuccessorPriority + 1;
current->m_inFeedbackLoop = true;
} else {
current->m_priority = maxSuccessorPriority + 1;
current->m_inFeedbackLoop = false;
}
stack.pop();
inStack[current] = false;
}
}
return m_priority;
}
bool LogicElement::outputValue(const int index) const
{
return m_outputValues.at(index);
}
bool LogicElement::inputValue(const int index) const
{
auto *pred = m_inputPairs.at(index).logic;
int port = m_inputPairs.at(index).port;
return pred->outputValue(port);
}
|