|
| 1 | +from functools import reduce |
| 2 | +from typing import Dict, List, Set |
| 3 | + |
| 4 | +from .pointer import Pointer |
| 5 | +from .types import Diffable |
| 6 | + |
| 7 | + |
| 8 | +def diff_lists(input: List, output: List, ptr: Pointer) -> List: |
| 9 | + memory = {(0, 0): {"ops": [], "cost": 0}} |
| 10 | + |
| 11 | + def dist(i, j): |
| 12 | + if (i, j) not in memory: |
| 13 | + if i > 0 and j > 0 and input[i - 1] == output[j - 1]: |
| 14 | + step = dist(i - 1, j - 1) |
| 15 | + else: |
| 16 | + paths = [] |
| 17 | + if i > 0: |
| 18 | + base = dist(i - 1, j) |
| 19 | + op = {"op": "remove", "idx": i - 1} |
| 20 | + paths.append( |
| 21 | + { |
| 22 | + "ops": base["ops"] + [op], |
| 23 | + "cost": base["cost"] + 1, |
| 24 | + } |
| 25 | + ) |
| 26 | + if j > 0: |
| 27 | + base = dist(i, j - 1) |
| 28 | + op = {"op": "add", "idx": j - 1, "value": output[j - 1]} |
| 29 | + paths.append( |
| 30 | + { |
| 31 | + "ops": base["ops"] + [op], |
| 32 | + "cost": base["cost"] + 1, |
| 33 | + } |
| 34 | + ) |
| 35 | + if i > 0 and j > 0: |
| 36 | + base = dist(i - 1, j - 1) |
| 37 | + op = { |
| 38 | + "op": "replace", |
| 39 | + "idx": i - 1, |
| 40 | + "original": input[i - 1], |
| 41 | + "value": output[j - 1], |
| 42 | + } |
| 43 | + paths.append( |
| 44 | + { |
| 45 | + "ops": base["ops"] + [op], |
| 46 | + "cost": base["cost"] + 1, |
| 47 | + } |
| 48 | + ) |
| 49 | + step = min(paths, key=lambda a: a["cost"]) |
| 50 | + memory[(i, j)] = step |
| 51 | + return memory[(i, j)] |
| 52 | + |
| 53 | + ops = dist(len(input), len(output))["ops"] |
| 54 | + |
| 55 | + def pad(state, op): |
| 56 | + ops, padding = state |
| 57 | + if op["op"] == "add": |
| 58 | + padded_idx = op["idx"] + 1 + padding |
| 59 | + idx_token = str(padded_idx) if padded_idx < len(input) + padding else "-" |
| 60 | + full_op = { |
| 61 | + "op": "add", |
| 62 | + "path": str(ptr.append(idx_token)), |
| 63 | + "value": op["value"], |
| 64 | + } |
| 65 | + return [ops + [full_op], padding + 1] |
| 66 | + elif op["op"] == "remove": |
| 67 | + full_op = { |
| 68 | + "op": "remove", |
| 69 | + "path": str(ptr.append(str(op["idx"] + padding))), |
| 70 | + } |
| 71 | + return [ops + [full_op], padding - 1] |
| 72 | + else: |
| 73 | + replace_ptr = ptr.append(str(op["idx"] + padding)) |
| 74 | + replace_ops = diff(op["original"], op["value"], replace_ptr) |
| 75 | + return [ops + replace_ops, padding] |
| 76 | + |
| 77 | + padded_ops, _ = reduce(pad, ops, [[], 0]) |
| 78 | + |
| 79 | + return padded_ops |
| 80 | + |
| 81 | + |
| 82 | +def diff_dicts(input: Dict, output: Dict, ptr: Pointer) -> List: |
| 83 | + ops = [] |
| 84 | + input_keys = set(input.keys()) |
| 85 | + output_keys = set(output.keys()) |
| 86 | + for key in input_keys - output_keys: |
| 87 | + ops.append({"op": "remove", "path": str(ptr.append(key)), "key": key}) |
| 88 | + for key in output_keys - input_keys: |
| 89 | + ops.append( |
| 90 | + { |
| 91 | + "op": "add", |
| 92 | + "path": str(ptr.append(key)), |
| 93 | + "key": key, |
| 94 | + "value": output[key], |
| 95 | + } |
| 96 | + ) |
| 97 | + for key in input_keys & output_keys: |
| 98 | + ops.extend(diff(input[key], output[key], ptr.append(key))) |
| 99 | + return ops |
| 100 | + |
| 101 | + |
| 102 | +def diff_sets(input: Set, output: Set, ptr: Pointer) -> List: |
| 103 | + # TODO: pointers? |
| 104 | + ops = [] |
| 105 | + for value in input - output: |
| 106 | + ops.append({"op": "remove", "value": value}) |
| 107 | + for value in output - input: |
| 108 | + ops.append({"op": "add", "value": value}) |
| 109 | + return ops |
| 110 | + |
| 111 | + |
| 112 | +def diff(input: Diffable, output: Diffable, ptr: Pointer = None) -> List: |
| 113 | + if input == output: |
| 114 | + return [] |
| 115 | + if ptr is None: |
| 116 | + ptr = Pointer() |
| 117 | + if isinstance(input, list) and isinstance(output, list): |
| 118 | + return diff_lists(input, output, ptr) |
| 119 | + if isinstance(input, dict) and isinstance(output, dict): |
| 120 | + return diff_dicts(input, output, ptr) |
| 121 | + if isinstance(input, set) and isinstance(output, set): |
| 122 | + return diff_sets(input, output, ptr) |
| 123 | + return [{"op": "replace", "path": str(ptr), "value": output}] |
0 commit comments