immer icon indicating copy to clipboard operation
immer copied to clipboard

Strategy for comparing vectors

Open voltrevo opened this issue 6 years ago • 5 comments

When comparing two immer::vectors, 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?

voltrevo avatar Dec 04 '18 06:12 voltrevo

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 avatar Dec 04 '18 09:12 arximboldi

@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.

voltrevo avatar Dec 04 '18 22:12 voltrevo

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.

arximboldi avatar Dec 05 '18 11:12 arximboldi

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 avatar Oct 01 '19 21:10 ruler501

@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.

arximboldi avatar Oct 02 '19 11:10 arximboldi