Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Anyone know this Python data structure?

The Python class has six requirements as listed below. Only bold terms are to be read as requirements.


  1. Close to O(1) performance for as many of the following four operations.
  2. Maintaining sorted order while inserting an object into the container.
  3. Ability to peek at last value (the largest value) contained in the object.
  4. Allowing for pops on both sides (getting the smallest or largest values).
  5. Capability of getting the total size or number of objects being stored.
  6. Being a ready made solution like the code in Python's standard library.

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:

  1. Efficient append and pops on either side of a deque with O(1) performance.
  2. Pops on both sides for the data contained within the object.
  3. Getting the total size or count of objects contained within.

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:

  1. Helps keep an array in sorted order while inserting new objects in array.
  2. Ready made solution for keeping lists sorted as objects are added.
  3. Would allow executing 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?

like image 960
Noctis Skytower Avatar asked Nov 04 '10 15:11

Noctis Skytower


People also ask

How do I know what data structure Python?

To get the type of a variable in Python, you can use the built-in type() function. In Python, everything is an object. So, when you use the type() function to print the type of the value stored in a variable to the console, it returns the class type of the object.

Is Python good for data structures?

Data Structures are fundamentals of any programming language around which a program is built. Python helps to learn the fundamental of these data structures in a simpler way as compared to other programming languages.

How many data structures does Python have?

Python has 4 built-in data structures, lists, dictionaries, tuples, and sets. These built-in data structures come with default methods and behind the scenes optimizations that make them easy to use.


3 Answers

Your requirements seem to be:

  1. O(1) pop from each end
  2. Efficient len
  3. Sorted order
  4. Peek at last value

for which you can use a deque with a custom insert method which rotates the deque, appends to one end, and unrotates.

>>> from collections import deque
>>> import bisect
>>> class FunkyDeque(deque):
...     def _insert(self, index, value):
...             self.rotate(-index)
...             self.appendleft(value)
...             self.rotate(index)
...
...     def insert(self, value):
...             self._insert(bisect.bisect_left(self, value), value)
...
...     def __init__(self, iterable):
...             super(FunkyDeque, self).__init__(sorted(iterable))
...
>>> foo = FunkyDeque([3,2,1])
>>> foo
deque([1, 2, 3])
>>> foo.insert(2.5)
>>> foo
deque([1, 2, 2.5, 3])

Notice that requirements 1, 2, and 4 all follow directly from the fact that the underlying data structure is a deque, and requirement 3 holds because of the way data is inserted. (Note of course that you could bypass the sorting requirement by calling e.g. _insert, but that's beside the point.)

like image 108
Katriel Avatar answered Sep 21 '22 08:09

Katriel


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)
like image 35
Noctis Skytower Avatar answered Sep 19 '22 08:09

Noctis Skytower


blist.sortedlist

  1. Close to O(1) performance for as many of the following four operations.
  2. Maintaining sorted order while inserting an object into the container.
  3. Ability to peek at last value (the largest value) contained in the object.
  4. Allowing for pops on both sides (getting the smallest or largest values).
  5. Capability of getting the total size or number of objects being stored.
  6. Being a ready made solution like the code in Python's standard library.

It's a B+ Tree.

like image 44
tzot Avatar answered Sep 19 '22 08:09

tzot