immer
immer copied to clipboard
Strategy for comparing vectors
When comparing two immer::vector
s, it should be possible (if I understand the underlying structure correctly) to quickly find the first element that is different (assuming it is unlikely to encounter large equal ranges that don't share structure). But I don't see any methods currently that would let me do this.
Is there a way to do this with immer or any intention to add a feature that enables it?
Yes, that is correct. Only operator ==
and !=
are only comparisons for now that leverage structural sharing, but I would like to add forms of diffing. What is the use-case you have in mind? Learning more about practical use-cases will help derive the best API for it.
@arximboldi I'm working on a programming language and using immer for the data structures when running via a virtual machine. In order to implement <
between arrays I need to find the first unequal element if it exists.
Cool project! For now I would suggest to just implement it doing a ==
check before doing the actual ordering comparison. I leave the issue open and will try to provide you with a more efficient way to compare these in the future.
Somewhat similar for a use case would be a method to generate a form of diffs to assist in merging disparate "branches" that share an ancestor. Preferably in the style of fully persistent data structures or something like git, but on a different scope. Quite possibly beyond the scope of this project but could be a useful feature if not too intrusive to provide.
@ruler501: to some degree that already exists for flex_vector
, in the form of the concatenation operations. For map
and set
a union operation in such style could be implemented, but I am still unsure about in which cases it can really be effective.