Given two name histories (grouped by block), merge them.

def name_history_merge(h1, h2):
    """
    Given two name histories (grouped by block), merge them.
    """
    ret = {}
    blocks_1 = [int(b) for b in h1.keys()]
    blocks_2 = [int(b) for b in h2.keys()]

    # find overlapping blocks
    overlap = list(set(blocks_1).intersection(set(blocks_2)))
    if len(overlap) > 0:
        for b in overlap:
            h = h1[str(b)] + h2[str(b)]
            h.sort(lambda v1, v2: -1 if v1['vtxindex'] < v2['vtxindex'] else 1)
            
            uniq = []
            last_vtxindex = None
            for i in range(0, len(h)):
                if h[i]['vtxindex'] != last_vtxindex:
                    uniq.append(h[i])
                    last_vtxindex = h[i]['vtxindex']
                
            ret[str(b)] = uniq
    
    all_blocks = list(set(blocks_1 + blocks_2))
    for b in all_blocks:
        if b in overlap:
            continue

        if b in blocks_1:
            ret[str(b)] = h1[str(b)]
        else:
            ret[str(b)] = h2[str(b)]

    return ret