disorderedmaterials / dissolve

Structure refinement software for total scattering data
GNU General Public License v3.0
9 stars 16 forks source link

fix: Graph rename bug fix #1942

Closed Danbr4d closed 37 minutes ago

Danbr4d commented 2 weeks ago

Closes #1594.

Danbr4d commented 2 weeks ago

Not sure how big an issue is but if you rename to something with a number in then it causes a crash too.

trisyoungs commented 1 week ago

@Danbr4d the issue with renaming I think boils down to the function which was renaming (badly) the relevant data. I have updated that particular function in the best way possible. @rprospero if you wouldn't mind taking a look over the horrible code in question, that'd be great (I'll ask for a review from you). I don't see any other way of achieving the same thing...

rprospero commented 1 week ago

@trisyoungs I'm not entirely sure that we need to copy into the other map. The cpp reference seems to indicate that it is a safe action. The only iterator that is invalidated is the one which is literally being moved. I think we can do

for (auto iter = m.begin(); iter < m.end(); ++m) {
    auto node = m.extract(iter);
    if (test(node.key())) {
        node.key() = new_key(node.key());
        m.insert(std::move(node));
    }
}

Even if the iteration invalidation causes an issue, we can still go O(n²) in access.

do {
    bool done = true
    for (auto iter = m.begin(); iter < m.end(); ++m) {
        auto node = m.extract(iter);
        if (test(node.key())) {
            node.key() = new_key(node.key());
            m.insert(std::move(node));
        }
        done = false;
        break;
    }
} while(!done)

It's still ugly, but at least it's constant memory.

trisyoungs commented 1 week ago

@rprospero You're aboslutely right - not sure why I got stuck in the copying methodology. I'm still convinced that there are issues trying to do this in a single loop, so I have reworked in line with your second suggestion, but in a slightly more std algorithm way.