Exemplo n.º 1
0
def union(a, b):
    """
    删
    This is much much slower than the builtin set.union!
    <s>
    Given two sorted list, return the union of them.

    *Each list shouldn't have duplicated data, according the attribute of set.*
    </s>
    """
    if len(a) == 0:
        return b
    if len(b) == 0:
        return a
    ha = 0
    hb = 0
    la = len(a) - 1  # limit of array a
    lb = len(b) - 1  # limit of array b
    tmp = CQueue()
    while True:
        pb = __dimidiate_search_ge(a[ha], hb, lb, b)
        if pb < lb and a[ha] == b[pb]:
            tmp.extend(b[hb:pb + 1])
            ha += 1
            hb = pb + 1
        else:
            tmp.extend(b[hb:pb])
            tmp.put(a[ha])
            ha += 1
            hb = pb
        if ha > la or hb > lb:
            break

        pa = __dimidiate_search_ge(b[hb], ha, la, a)
        if pa < la and a[pa] == b[hb]:
            tmp.extend(a[ha:pa + 1])
            hb += 1
            ha = pa + 1
        else:
            tmp.extend(a[ha:pa])
            tmp.put(b[hb])
            hb += 1
            ha = pa
        if ha > la or hb > lb:
            break

    tmp.extend(a[ha:la + 1])
    tmp.extend(b[hb:lb + 1])

    return tmp.get_queue_copy()
Exemplo n.º 2
0
def union(a, b):
    """
    删
    This is much much slower than the builtin set.union!
    <s>
    Given two sorted list, return the union of them.

    *Each list shouldn't have duplicated data, according the attribute of set.*
    </s>
    """
    if len(a) == 0:
        return b
    if len(b) == 0:
        return a
    ha = 0
    hb = 0
    la = len(a) - 1  # limit of array a
    lb = len(b) - 1  # limit of array b
    tmp = CQueue()
    while True:
        pb = __dimidiate_search_ge(a[ha], hb, lb, b)
        if pb < lb and a[ha] == b[pb]:
            tmp.extend(b[hb: pb + 1])
            ha += 1
            hb = pb + 1
        else:
            tmp.extend(b[hb: pb])
            tmp.put(a[ha])
            ha += 1
            hb = pb
        if ha > la or hb > lb:
            break

        pa = __dimidiate_search_ge(b[hb], ha, la, a)
        if pa < la and a[pa] == b[hb]:
            tmp.extend(a[ha: pa + 1])
            hb += 1
            ha = pa + 1
        else:
            tmp.extend(a[ha: pa])
            tmp.put(b[hb])
            hb += 1
            ha = pa
        if ha > la or hb > lb:
            break

    tmp.extend(a[ha: la + 1])
    tmp.extend(b[hb: lb + 1])

    return tmp.get_queue_copy()
Exemplo n.º 3
0
def intersect(a, b):
    """
    删
    This is much much slower than the builtin set.intersection!
    <s>
    Given two sorted list, return the intersection of them.

    *Each list shouldn't have duplicated data, according the attribute of set.*
    </s>
    """
    if len(a) == 0 or len(b) == 0:
        return []
    ha = 0
    hb = 0
    la = len(a) - 1  # limit of array a
    lb = len(b) - 1  # limit of array b
    tmp = CQueue()
    while True:
        p2 = __dimidiate_search_ge(a[ha], hb, lb, b)
        if p2 > lb:
            break
        if a[ha] == b[p2]:
            tmp.put(a[ha])
            ha += 1
            hb = p2 + 1
        else:
            ha += 1
            hb = p2

        if ha > la or hb > lb:
            break

        p1 = __dimidiate_search_ge(b[hb], ha, la, a)
        if p1 > la:
            break
        if a[p1] == b[hb]:
            tmp.put(b[hb])
            hb += 1
            ha = p1 + 1
        else:
            hb += 1
            ha = p1 + 1
        if ha > la or hb > lb:
            break
    return tmp.get_queue_copy()
Exemplo n.º 4
0
def intersect(a, b):
    """
    删
    This is much much slower than the builtin set.intersection!
    <s>
    Given two sorted list, return the intersection of them.

    *Each list shouldn't have duplicated data, according the attribute of set.*
    </s>
    """
    if len(a) == 0 or len(b) == 0:
        return []
    ha = 0
    hb = 0
    la = len(a) - 1  # limit of array a
    lb = len(b) - 1  # limit of array b
    tmp = CQueue()
    while True:
        p2 = __dimidiate_search_ge(a[ha], hb, lb, b)
        if p2 > lb:
            break
        if a[ha] == b[p2]:
            tmp.put(a[ha])
            ha += 1
            hb = p2 + 1
        else:
            ha += 1
            hb = p2

        if ha > la or hb > lb:
            break

        p1 = __dimidiate_search_ge(b[hb], ha, la, a)
        if p1 > la:
            break
        if a[p1] == b[hb]:
            tmp.put(b[hb])
            hb += 1
            ha = p1 + 1
        else:
            hb += 1
            ha = p1 + 1
        if ha > la or hb > lb:
            break
    return tmp.get_queue_copy()