Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf(python): Adding multiple links should be handled by c++ #113

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 3 additions & 2 deletions interfaces/python/infomap.py
Original file line number Diff line number Diff line change
Expand Up @@ -277,8 +277,9 @@ def add_links(self, links):
links : iterable of tuples
Iterable of tuples of int of the form (source_id, target_id, [weight])
"""
for link in links:
self.add_link(*link)
#for link in links:
# self.add_link(*link)
return super().addLinks(links)

def remove_link(self, source_id, target_id):
"""Remove a link.
Expand Down
4 changes: 4 additions & 0 deletions src/Infomap.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ struct InfomapWrapper : public InfomapCore {

void addLink(unsigned int sourceId, unsigned int targetId, double weight = 1.0) { m_network.addLink(sourceId, targetId, weight); }
void addLink(unsigned int sourceId, unsigned int targetId, unsigned long weight) { m_network.addLink(sourceId, targetId, weight); }
void addLinks(const std::vector<std::tuple<unsigned int, unsigned int>>& links) { m_network.addLinks(links); };
void addLinks(const std::vector<std::tuple<unsigned int, unsigned int, double>>& links) { m_network.addLinks(links); };
void addLinks(const std::vector<std::tuple<unsigned int, unsigned int, unsigned long>>& links) { m_network.addLinks(links); };

void addMultilayerLink(unsigned int layer1, unsigned int n1, unsigned int layer2, unsigned int n2, double weight = 1.0) { m_network.addMultilayerLink(layer1, n1, layer2, n2, weight); }

void setBipartiteStartId(unsigned int startId) { m_network.setBipartiteStartId(startId); }
Expand Down
50 changes: 50 additions & 0 deletions src/core/StateNetwork.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,56 @@ bool StateNetwork::addLink(unsigned int sourceId, unsigned int targetId, unsigne
return addLink(sourceId, targetId, static_cast<double>(weight));
}

size_t StateNetwork::addLinks(const std::vector<std::pair<unsigned int, unsigned int>>& links)
{
size_t num_added = 0;

unsigned int source;
unsigned int target;
for (auto& link : links) {
std::tie(source, target) = link;
if (addLink(source, target)) {
++num_added;
}
}

return num_added;
}

size_t StateNetwork::addLinks(const std::vector<std::tuple<unsigned int, unsigned int, double>>& links)
{
size_t num_added = 0;

unsigned int source;
unsigned int target;
double weight;
for (auto& link : links) {
std::tie(source, target, weight) = link;
if (addLink(source, target, weight)) {
++num_added;
}
}

return num_added;
}

size_t StateNetwork::addLinks(const std::vector<std::tuple<unsigned int, unsigned int, unsigned long>>& links)
{
size_t num_added = 0;

unsigned int source;
unsigned int target;
unsigned long weight;
for (auto& link : links) {
std::tie(source, target, weight) = link;
if (addLink(source, target, static_cast<double>(weight))) {
++num_added;
}
}

return num_added;
}

bool StateNetwork::removeLink(unsigned int sourceId, unsigned int targetId)
{
auto itSource = m_nodeLinkMap.find(sourceId);
Expand Down
4 changes: 4 additions & 0 deletions src/core/StateNetwork.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
#include <map>
#include <vector>
#include <utility>
#include <tuple>
#include "../io/Config.h"
// #include "../utils/FlowCalculator.h"

Expand Down Expand Up @@ -220,6 +221,9 @@ class StateNetwork
std::pair<std::map<unsigned int, std::string>::iterator, bool> addName(unsigned int id, std::string);
bool addLink(unsigned int sourceId, unsigned int targetId, double weight = 1.0);
bool addLink(unsigned int sourceId, unsigned int targetId, unsigned long weight);
size_t addLinks(const std::vector<std::pair<unsigned int, unsigned int>>& links);
size_t addLinks(const std::vector<std::tuple<unsigned int, unsigned int, double>>& links);
size_t addLinks(const std::vector<std::tuple<unsigned int, unsigned int, unsigned long>>& links);
/**
* Remove link
* Note: It will not remove nodes if they become dangling
Expand Down