3-way "merge" of lists



  • I have an (ordered) list of things called Assignments (which have a (non-unique) position id and a nullable person id).

    Well, really I have three such lists.

    • The original ones defined when a schedule (template for creating shifts) was created.
    • The (changed) ones defined for a particular instance of a shift was edited.
    • The new ones defined when the template (schedule) was edited.

    The final state should be (in priority)

    1. Any edits made to the particular instance win.
    2. Anything changed in the new schedule (relative to the old schedule) should be changed in the instance unless it conflicts with a manual edit to that instance.
    3. Anything unchanged from old -> instance -> new schedule should be left alone.

    Changes could be

    1. order
    2. elements could be added or removed
    3. person ids can be changed (from null -> person a, person a -> person b, or person a -> null).

    No single instance or schedule will have more than ~20 such assignments (business rule), most have < 5.

    Right now my thinking is
    a. diff the instance against the original, producing an ordered list of [null | new element] the size of the greater of the two.
    b. diff the new schedule against the original, producing the same sort of list.
    For each index in a, if it's null, use the corresponding element of b. Otherwise use the element from a. if both a and b are null, use the instance element at that position.

    Am I missing something?



  • Without thinking too much about it. :kneeling_warthog:

    Figure out all of the permutations of changes and their desired result.

    It might be able to be simplified.

    Depending on how it works, I may put those permutations into database table and whatever row the situation matches gets the result specified.


Log in to reply