3 ms·
You mentioned changing the order of two elements in a list, such that one comes after the other. With a vector of pointers to the elements (C++ std::vector<T*>
by volta83 5y ago
You mentioned changing the order of two elements in a list, such that one comes after the other.
With a vector of pointers to the elements (C++ std::vector<T*>) this can be easily done in O(1) as I explained above.
If now that I've proven you wrong, you want to change the problem to something else, feel free to state your new problem, and I'll proceed to prove you wrong again.
- Jensson 5y ago> You mentioned changing the order of two elements in a list, such that one comes after the other. Putting B after A is not swapping them, it is putting the element B so it is right after A. You could technically interpret it as you did, but only a contrarian would do that. If you want more people to support rust then you should stop being a contrarian. If you want me and others to assume that the Rust community is full of hard to work with people then please continue, but that wont make people more likely to pick up rust. If instead of behaving like you do here people would just say "Sure thing, in order to get that performance in rust you just do X and Y!" I bet people would be way more supportive of rust. But if working in the language means that people like you will come and argue like this then why not just write a C library, and then someone will write a Rust wrapper and people are happy? While if they'd write it in unsafe Rust people would come and complain like hell.
- volta83 5y ago> You could technically interpret it as you did, but only a contrarian would do that. Or you could have been more clearer. I didn't intend any animosity, but you clearly do. > If you want more people to support rust I don't care whether more people support Rust or not. I am just fighting the spectacular amount of disinformation in this thread by malicious actors that have never used Rust. > Putting B after A is not swapping them, it is putting the element B so it is right after A. Then you should have written that instead. Take the pointer in the vector right after A, and swap it with B. That way, A is now right before B, and B is now right after A.
- Jensson 5y ago> Take the pointer in the vector right after A, and swap it with B. That way, A is now right before B, and B is now right after A. This is a joke, right? I don't see how it can't be. But in case it isn't, in almost all problems like this it is assumed that you don't want to change the order of the other elements. For example, if you wrote a function MoveToDirectlyAfter(a, b), and it did move elements other than a or b people would say that your function contains bugs or that it doesn't do what it says.
- volta83 5y agoBefore I delve more, are you talking about `rotate` ? https://en.cppreference.com/w/cpp/algorithm/rotate https://en.cppreference.com/w/cpp/algorithm/rotate Is that what you want?
- pas 5y agoIn a real life system what is the usual use case for this? Let's say a user reorders documents with drag and drop. What's the low-level equivalent? Could you help me come up with an algorithm that depends on this? If write speed is important, I'd just use an in-memory LevelDB-like thing (so a log structured merge tree). If the data is not much, then I'd optimize for code simplicity.