Skip to content

Commit f283818

Browse files
committed
fix: consistently use 1-based indexing for paths and 0 for indexes
Fixes #103
1 parent 2907c86 commit f283818

File tree

6 files changed

+12
-11
lines changed

6 files changed

+12
-11
lines changed

src/core/ClusterMap.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -95,15 +95,14 @@ void ClusterMap::readTree(const std::string& filename, bool includeFlow)
9595

9696
pathStream.clear();
9797
pathStream.str(pathString);
98-
unsigned int childIndex;
98+
unsigned int childNumber;
9999
Path path;
100-
while (pathStream >> childIndex)
100+
while (pathStream >> childNumber)
101101
{
102102
pathStream.get(); // Extract the delimiting character also
103-
if (childIndex == 0)
103+
if (childNumber == 0)
104104
throw FileFormatError("There is a '0' in the tree path, lowest allowed integer is 1.");
105-
--childIndex;
106-
path.push_back(childIndex);
105+
path.push_back(childNumber); // Keep 1-based indexing in path
107106
}
108107
m_nodePaths.add(nodeId, path);
109108
}

src/core/ClusterMap.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
namespace infomap {
1717

18-
using Path = std::deque<unsigned int>;
18+
using Path = std::deque<unsigned int>; // 1-based indexing
1919
struct NodePath
2020
{
2121
NodePath(unsigned int nodeId, const Path& path)
@@ -33,6 +33,7 @@ struct NodePaths
3333
unsigned int size() { return nodePaths.size(); }
3434
void clear() { nodePaths.clear(); }
3535
void add(unsigned int nodeId, const Path& path) {
36+
// path should use 1-based indexing
3637
add(NodePath(nodeId, path));
3738
}
3839
void add(NodePath&& path) {

src/core/InfoNode.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ std::vector<unsigned int> InfoNode::calculatePath() const
169169
const InfoNode* current = this;
170170
std::vector<unsigned int> path;
171171
while (current->parent != nullptr) {
172-
path.push_back(current->childIndex());
172+
path.push_back(current->childIndex() + 1);
173173
current = current->parent;
174174
if (current->owner != nullptr) {
175175
current = current->owner;

src/core/InfoNode.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,7 @@ class InfoNode
356356

357357
unsigned int childIndex() const;
358358

359+
// Generate 1-based tree path
359360
std::vector<unsigned int> calculatePath() const;
360361

361362
unsigned int infomapChildDegree() const;

src/core/InfomapBase.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -534,10 +534,10 @@ InfomapBase& InfomapBase::initTree(const NodePaths& tree)
534534
}
535535
for (unsigned int i = 0; i < path.size(); ++i)
536536
{
537-
auto childIndex = path[i];
537+
auto childNumber = path[i]; // 1-based indexing
538538
// Create new node if path doesn't exist
539539
// TODO: Check correct tree indexing?
540-
if (node->childDegree() <= childIndex)
540+
if (node->childDegree() < childNumber)
541541
{
542542
InfoNode* child = leafNode;
543543
if (i + 1 < path.size()) {
@@ -2385,7 +2385,7 @@ void InfomapBase::writeTreeLinks(std::ostream& outStream, bool states)
23852385
if (sourceParentIt == targetParentIt) {
23862386
// Skip self-links
23872387
if (sourceChildIndex != targetChildIndex) {
2388-
auto parentId = io::stringify(sourceParentIt->calculatePath(), ":", 1);
2388+
auto parentId = io::stringify(sourceParentIt->calculatePath(), ":");
23892389
auto& linkMap = moduleLinks[parentId];
23902390
linkMap[std::make_pair(sourceChildIndex + 1, targetChildIndex + 1)] += flow;
23912391
}

src/core/InfomapIterator.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ struct InfomapIterator
133133

134134
unsigned int childIndex() const
135135
{
136-
return m_path.empty() ? 0 : m_path.back();
136+
return m_path.empty() ? 0 : m_path.back() - 1;
137137
}
138138

139139
unsigned int depth() const

0 commit comments

Comments
 (0)