The Python class has six requirements as listed below. Only bold terms are to be read as requirements.
What follows is left here for historical reasons (help the curious and prove that research was conducted).
After looking through Python's Standard Library (specifically the section on Data Types), I still have not found a class that fulfills the requirements requirements of a fragmentation table. collections.deque
is close to what is required, but it does not support keeping the data contained in it sorted. It provides:
Implementing an inefficient solution using lists would be trivial, but finding a class that performs well would be far more desirable. In a growing memory simulation with no upper limit, such a class could keep indexes of empty (deleted) cells and keep fragmentation levels down. The bisect
module may help:
array[-1]
to peek at last value in the array.The final candidate that failed to fully satisfy the requirements and appeared least promising was the heapq
module. While supporting what looked like efficient insertions and assuring that array[0]
was the smallest value, the array is not always in a fully sorted state. Nothing else was found to be as helpful.
Does anyone know of a class or data structure in Python that comes close to these six requirements?
Many thanks go out to katrielalex
for providing the inspiration that led to the following Python class:
import collections
import bisect
class FastTable:
def __init__(self):
self.__deque = collections.deque()
def __len__(self):
return len(self.__deque)
def head(self):
return self.__deque.popleft()
def tail(self):
return self.__deque.pop()
def peek(self):
return self.__deque[-1]
def insert(self, obj):
index = bisect.bisect_left(self.__deque, obj)
self.__deque.rotate(-index)
self.__deque.appendleft(obj)
self.__deque.rotate(index)