osteslag / Changeset

Minimal edits from one collection to another
MIT License
802 stars 46 forks source link

Trees #8

Closed hfossli closed 7 years ago

hfossli commented 8 years ago

Hey, I just read the readme. I like this a lot. Do you think this could be applied to trees as well? Imagine I have a tree of lightweight structs. Can I compare two trees and find the minimum changes required going from one to the other?

osteslag commented 8 years ago

Do you think this could be applied to trees as well?

The short answer is yes. But you probably have a little bit of work to do first…

All you have to do is to make your struct a CollectionType and implement subscripting via Int. Read more on NSHipster’s Swift Collection Protocols. You could traverse your structure recursively, like you would a file/folder hierarchy for example, and implement functions to convert between an Int and a node in your tree.

I could probably get rid of the latter requirement and rely on CollectionType’s Indexable behaviour. That would make it possible to use NSIndexPath, for example, as the index type instead of only Int, I guess, which might be a convenient way of addressing specific nodes in your tree. It would certainly be useful when using Changeset on a table/collection view data source because it would support elements moving across section boundaries out of the box.

That would be a nice improvement to Changeset, I believe, but I’ll have investigate further. I’m still a newbie when it comes to Swift.

hfossli commented 8 years ago

Interesting. Thanks. I'll subscribe to changes on this repo and I will let you know if I got a working example.

hfossli commented 8 years ago

Close at will

osteslag commented 8 years ago

You’re welcome. Will keep issue open as a reminder for me to look at the Index type stuff.