import random
class gapHandler:
def __init__(self):
self.greatest = 0
self.non_contiguous = set()
def handle_new_index(self, message_index):
"""
Called when a new numbered request is sent. Updates the current
index representing the greatest contiguous request.
"""
self.non_contiguous.add(message_index)
if message_index == self.greatest + 1:
self._update_greatest()
def _update_greatest(self):
done_updating = False
while done_updating is False:
next_index = self.greatest + 1
if next_index in self.non_contiguous:
self.greatest = next_index
self.non_contiguous.remove(next_index)
else:
done_updating = True
def demo_gap_handler():
""" Runs the gapHandler class through a mock trial. """
gh = gapHandler()
for block_id in range(20000):
start = block_id*500 + 1
end = (block_id + 1)*500 + 1
indices = [x for x in range(start, end)]
random.shuffle(indices)
while indices:
new_index = indices.pop()
gh.handle_new_index(new_index)
if new_index % 50 == 0:
print(gh.greatest)
if __name__ == "__main__":
demo_gap_handler()
我环顾了一下,没有马上找到一个很好的答案。你知道吗
这是我通过一个小的阻塞类来尝试的。从技术上讲,对于单个handle\u new\u索引,这可能是O(N),但是handle\u new\u index操作的平均时间仍然应该保证是恒定的。你知道吗
我不认为时间复杂度会变得更好,因为无论做什么,都必须在某种数据结构上进行插入。你知道吗
对于数十亿个请求和非常广泛的分布,非连续集可以有适度的内存占用。你知道吗
以下是一些基本测试:
相关问题 更多 >
编程相关推荐