Files
viseq/src/connection-manager.h
T

56 lines
1.3 KiB
C++
Raw Normal View History

#pragma once
#include "connection.h"
#include <map>
2024-10-16 02:26:02 +02:00
#include <vector>
class ConnectionManager{
public:
ConnectionManager() {}
std::vector<Connection> connections;
std::map<Node*, int> counter;
2024-10-16 02:26:02 +02:00
Node* next(Node* current, Node* last){
std::vector<Connection*> paths;
2024-10-16 02:26:02 +02:00
bool last_ok = false;
for(auto& p : connections){
2024-10-16 02:26:02 +02:00
if(((p.v1 == current && p.v1_v2) || (p.v2 == current && p.v2_v1)) && (p.v1 != last || p.v2 != last)) {
p.active = false;
paths.push_back(&p);
}
2024-10-16 02:26:02 +02:00
if(p.v1 == last || p.v2 == last) last_ok = true;
}
2024-10-16 02:26:02 +02:00
if(paths.empty() && last_ok) return last;
if(paths.empty()) return nullptr;
int return_path = 0;
2024-10-16 02:26:02 +02:00
if(current->mode == Distribution::ROUNDROBIN){
if(counter.find(current) == counter.end()) {
counter[current] = 0;
} else {
counter[current] = (counter[current] + 1) % paths.size();
return_path = counter[current];
}
}
if(current->mode == Distribution::RANDOM){
return_path = rand() % paths.size();
}
2024-10-16 02:26:02 +02:00
paths.at(return_path)->active = true;
return paths.at(return_path)->v1 == current ? paths.at(return_path)->v2 : paths.at(return_path)->v1;
}
void draw(){
2024-10-16 02:26:02 +02:00
std::erase_if(connections, [](Connection c){ return c.remove == true;});
for(auto& c : connections){
c.draw();
}
}
};