Ejemplo n.º 1
0
 def __init__(self):
     """
     Instantiate OrderBook object which uses RBTree as main data structure
     """
     self.bids = RBTree()
     self.asks = RBTree()
     # keep track of max and min for bid and ask orders respectively
     self.bid_max = -math.inf
     self.bid_min = math.inf
     self.ask_max = -math.inf
     self.ask_min = math.inf
Ejemplo n.º 2
0
def solve(par):
    n, m, X, Y, Z, A = par
    speed = []
    for i in range(n):
        speed.append(A[i % m])
        A[i % m] = (X * A[i % m] + Y * (i + 1)) % Z

    sumCount = RBTree()
    count = [1] * (n)

    for i in range(n):
        try:
            sumCount[speed[i]] += 0
        except KeyError:
            sumCount[speed[i]] = 0

        s = speed[i]
        while True:
            try:
                s, v = sumCount.prev_item(s)
                count[i] += v
            except KeyError:
                break
        sumCount[speed[i]] += count[i]

    return str(sum(count))
Ejemplo n.º 3
0
 def __init__(self, reservoir_size, alpha):
     self.values = RBTree()
     self.counter = Atomic(0)
     self.next_scale_time = Atomic(0)
     self.alpha = alpha
     self.reservoir_size = reservoir_size
     self.lock = RLock()
     self.clear()
Ejemplo n.º 4
0
 def __init__(self, size=0xffff):
     self.size = size # set consistent hash circul size
     self.rbt = RBTree()  # red black tree
Ejemplo n.º 5
0
            maybe_max = find_upper(root.right, elem)
            if _not_exists(maybe_max):
                return -1
            return maybe_max

    def find_host(self, id):
        id %= self.size
        idx = self._find_upper(self.rbt._root, id)
        if idx == -1:  # id larger than max id
            # assert tree is not empty
            return self.rbt.min_item()[1]
        return self.rbt.get_value(idx)


if __name__ == "__main__":
    rbt = RBTree()
    for i in range(0, 30, 2):
        rbt.insert(i, "{}".format(i))
    print(find_next(rbt._root, 3))
    print(find_next(rbt._root, 4))
    print(find_next(rbt._root, 5))
    print(find_next(rbt._root, 21))
    print(find_lower(rbt._root, 10)) # should be 10
    print(find_lower(rbt._root, 11)) # should be 10
    print(find_lower(rbt._root, 9)) # should be 8
    print(find_lower(rbt._root, 7)) # should be 6
    print(find_lower(rbt._root, 5)) # should be 4
    print(find_lower(rbt._root, 3)) # should be 2
    print(find_lower(rbt._root, 1)) # should be 0
    print(find_lower(rbt._root, 0)) # should be 0
    print(find_lower(rbt._root, -2)) # should be -1