Skip to main content

Contains a variety of ordered structures, in particular a SkipList.

Project description

SkipList

This project contains a SkipList implementation in C++ with Python bindings.

A SkipList behaves as an always sorted list with, typically, O(log(n)) cost for insertion, look-up and removal. This makes it ideal for such operations as computing the rolling median of a large dataset.

See the full documentation on this project at ReadTheDocs.

A SkipList is implemented as a singly linked list of ordered nodes where each node participates in a subset of, sparser, linked lists. These additional 'sparse' linked lists provide rapid indexing and mutation of the underlying linked list. It is a probabilistic data structure using a random function to determine how many 'sparse' linked lists any particular node participates in. As such SkipList is an alternative to binary tree, Wikipedia has a introductory page on SkipLists.

An advantage claimed for SkipLists are that the insert and remove logic is simpler (however I do not subscribe to this). The drawbacks of a SkipList include its larger space requirements and its O(log(N)) lookup behaviour compared to other, more restricted and specialised, data structures that may have either faster runtime behaviour or lower space requirements or both.

This project contains a SkipList implementation in C++ with Python bindings with:

  • No capacity restrictions apart from available memory.
  • Works with any C++ type that has meaningful comparison operators.
  • The C++ SkipList can be compiled as thread safe.
  • The Python SkipList is thread safe.
  • The SkipList has exhaustive internal integrity checks.
  • Python SkipLists can be long/float/bytes/object types, the latter can have user defined comparison functions.
  • With Python 3.8+ SkipLists can be combined with the multiprocessing.shared_memory module for concurrent operation on large arrays. For example concurrent rolling medians which speed up near linearly with the number of cores.
  • The implementation is extensively performance tested in C++ and Python.

There are a some novel features to this implementation:

Credits

Originally written by Paul Ross with credits to: Wilfred Hughes (AHL), Luke Sewell (AHL) and Terry Tsantagoeds (AHL).

Installation

C++

This SkipList requires:

  • A C++11 compiler.
  • -I<skiplist>/src/cpp as an include path.
  • <skiplist>/src/cpp/SkipList.cpp to be compiled/linked.
  • The macro SKIPLIST_THREAD_SUPPORT set if you want a thread safe SkipList using C++ mutexes.

Python

This SkipList version supports Python 3.6, 3.7, 3.8, 3.9, 3.10, 3.11 (and, probably, some earlier Python 3 versions).

From PyPi

$ pip install orderedstructs

From source

$ git clone https://github.com/paulross/skiplist.git
$ cd <skiplist>
$ python setup.py install

Testing

This SkipList has extensive tests for correctness and performance.

C++

To run all the C++ functional and performance tests:

$ cd <skiplist>/src/cpp
$ make release
$ ./SkipList_R.exe

To run the C++ functional tests with agressive internal integrity checks:

$ cd <skiplist>/src/cpp
$ make debug
$ ./SkipList_D.exe

To run all the C++ functional and performance tests for a thread safe SkipList:

$ cd <skiplist>/src/cpp
$ make release CXXFLAGS=-DSKIPLIST_THREAD_SUPPORT
$ ./SkipList_R.exe

Python

Testing requires pytest and hypothesis:

To run all the C++ functional and performance tests:

$ cd <skiplist>
$ py.test -vs tests/

Examples

Here are some examples of using a SkipList in your code:

C++

#include "SkipList.h"
    
// Declare with any type that has sane comparison.
OrderedStructs::SkipList::HeadNode<double> sl;

sl.insert(42.0);
sl.insert(21.0);
sl.insert(84.0);

sl.has(42.0) // true
sl.size()    // 3
sl.at(1)     // 42.0, throws OrderedStructs::SkipList::IndexError if index out of range

sl.remove(21.0); // throws OrderedStructs::SkipList::ValueError if value not present

sl.size()    // 2
sl.at(1)     // 84.0

The C++ SkipList is thread safe when compiled with the macro SKIPLIST_THREAD_SUPPORT, then a SkipList can then be shared across threads:

#include <thread>
#include <vector>

#include "SkipList.h"

void do_something(OrderedStructs::SkipList::HeadNode<double> *pSkipList) {
    // Insert/remove items into *pSkipList
    // Read items inserted by other threads.
}

OrderedStructs::SkipList::HeadNode<double> sl;
std::vector<std::thread> threads;

for (size_t i = 0; i < thread_count; ++i) {
    threads.push_back(std::thread(do_something, &sl));
}
for (auto &t: threads) {
    t.join();
}
// The SkipList now contains the totality of the thread actions.

Python

An example of using a SkipList of always ordered floats:

import orderedstructs

# Declare with a type. Supported types are long/float/bytes/object.
sl = orderedstructs.SkipList(float)

sl.insert(42.0)
sl.insert(21.0)
sl.insert(84.0)

sl.has(42.0) # True
sl.size()    # 3
sl.at(1)     # 42.0

sl.has(42.0) # True
sl.size()    # 3
sl.at(1)     # 42.0, raises IndexError if index out of range

sl.remove(21.0); # raises ValueError if value not present

sl.size()    # 2
sl.at(1)     # 84.0

The Python SkipList can be used with user defined objects with a user defined sort order. In this example the last name of the person takes precedence over the first name:

import functools

@functools.total_ordering
class Person:
    """Simple example of ordering based on last name/first name."""
    def __init__(self, first_name, last_name):
        self.first_name = first_name
        self.last_name = last_name

    def __eq__(self, other):
        try:
            return self.last_name == other.last_name and self.first_name == other.first_name
        except AttributeError:
            return NotImplemented

    def __lt__(self, other):
        try:
            return self.last_name < other.last_name or self.first_name < other.first_name
        except AttributeError:
            return NotImplemented

    def __str__(self):
        return '{}, {}'.format(self.last_name, self.first_name)

import orderedstructs

sl = orderedstructs.SkipList(object)

sl.insert(Person('Peter', 'Pan'))
sl.insert(Person('Alan', 'Pan'))
assert sl.size() == 2
assert str(sl.at(0)) == 'Pan, Alan' 
assert str(sl.at(1)) == 'Pan, Peter' 

The Python SkipList is thread safe when using any acceptable Python type even if that type has user defined comparison methods. This uses Pythons mutex machinery which is independent of C++ mutexes.

History

0.3.8 (2023-08-28)

  • Add cmake build.
  • Support for Python 3.7, 3.7, 3.8, 3.9, 3.10, 3.11.
  • Remove mention of Python 2.

0.3.7 (2021-12-18)

  • Fix build on GCC (Issue #8).

0.3.6 (2021-12-18)

  • Add documentation on NaN in rolling median.
  • Add plots using shared_memory.
  • Add Python 3.10 support.

0.3.5 (2021-05-02)

  • Fix uncaught exception when trying to remove a NaN.

0.3.4 (2021-04-28)

  • Improve documentation mainly around multiprocessing.shared_memory and tests.

0.3.3 (2021-03-25)

  • Add Python benchmarks, fix some build issues.

0.3.2 (2021-03-18)

  • Fix lambda issues with Python 3.8, 3.9.

0.3.1 (2021-03-17)

  • Support Python 3.7, 3.8, 3.9.

0.3.0 (2017-08-18)

  • Public release.
  • Allows storing of PyObject* and rich comparison.

0.2.0

Python module now named orderedstructs.

0.1.0

Initial release.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

orderedstructs-0.3.8.tar.gz (33.5 kB view details)

Uploaded Source

Built Distributions

orderedstructs-0.3.8-cp311-cp311-macosx_10_9_universal2.whl (92.0 kB view details)

Uploaded CPython 3.11 macOS 10.9+ universal2 (ARM64, x86-64)

orderedstructs-0.3.8-cp310-cp310-macosx_10_9_universal2.whl (92.0 kB view details)

Uploaded CPython 3.10 macOS 10.9+ universal2 (ARM64, x86-64)

orderedstructs-0.3.8-cp39-cp39-macosx_10_9_universal2.whl (92.0 kB view details)

Uploaded CPython 3.9 macOS 10.9+ universal2 (ARM64, x86-64)

orderedstructs-0.3.8-cp38-cp38-macosx_10_9_x86_64.whl (49.5 kB view details)

Uploaded CPython 3.8 macOS 10.9+ x86-64

orderedstructs-0.3.8-cp37-cp37m-macosx_10_9_x86_64.whl (49.7 kB view details)

Uploaded CPython 3.7m macOS 10.9+ x86-64

File details

Details for the file orderedstructs-0.3.8.tar.gz.

File metadata

  • Download URL: orderedstructs-0.3.8.tar.gz
  • Upload date:
  • Size: 33.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.8

File hashes

Hashes for orderedstructs-0.3.8.tar.gz
Algorithm Hash digest
SHA256 45510a6d21e1734c112d0336bd92c05aeaa472f5e789fff5235413d681424744
MD5 4df0ddbc81d81119e60783b47e9e53b4
BLAKE2b-256 664a597fe102c2eafbf3a7074b80ad44efafe0314ec26f40a9736f7949719814

See more details on using hashes here.

File details

Details for the file orderedstructs-0.3.8-cp311-cp311-macosx_10_9_universal2.whl.

File metadata

File hashes

Hashes for orderedstructs-0.3.8-cp311-cp311-macosx_10_9_universal2.whl
Algorithm Hash digest
SHA256 64dbb02555bd125fa03390406c847bb9a32475a1d6f95260a565acff369bbb89
MD5 333062381ead8ae34476fc768984e24b
BLAKE2b-256 0fa0ad6da7e419154e8d106ea3dcdda3b19fe49be0d562d63b56bc98e786e580

See more details on using hashes here.

File details

Details for the file orderedstructs-0.3.8-cp310-cp310-macosx_10_9_universal2.whl.

File metadata

File hashes

Hashes for orderedstructs-0.3.8-cp310-cp310-macosx_10_9_universal2.whl
Algorithm Hash digest
SHA256 9b9687c81e3598d159133b13778b474c7f78aea1e9c45281c7f704eea0df633c
MD5 0cf9ddda4487d723017166c1fb1edcda
BLAKE2b-256 488ea662fff328a446786119174e8ed5d06d7a803f00475b753848fe929b7df6

See more details on using hashes here.

File details

Details for the file orderedstructs-0.3.8-cp39-cp39-macosx_10_9_universal2.whl.

File metadata

File hashes

Hashes for orderedstructs-0.3.8-cp39-cp39-macosx_10_9_universal2.whl
Algorithm Hash digest
SHA256 a354afd1b772960570a7f8f89b22d3f67fb2b6135b53760a5b1635b0f4244b80
MD5 5f1c3b2b63ea379db2312ad5aacfb21b
BLAKE2b-256 7bf81f8e2dc62f403735f7ede38c7c15216d9c47d937c8d49a52114498cfdb8a

See more details on using hashes here.

File details

Details for the file orderedstructs-0.3.8-cp38-cp38-macosx_10_9_x86_64.whl.

File metadata

File hashes

Hashes for orderedstructs-0.3.8-cp38-cp38-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 8ebcf960e10f69f6c1c23c1e325b0c8585d713331ed54dff1216929be43b8b5d
MD5 fe77607aace6abf122ff1c4e300249e8
BLAKE2b-256 08b8a31bebad5b005a1ce75d4e93531986c6891564ff844a8ccb3efedf270dfb

See more details on using hashes here.

File details

Details for the file orderedstructs-0.3.8-cp37-cp37m-macosx_10_9_x86_64.whl.

File metadata

File hashes

Hashes for orderedstructs-0.3.8-cp37-cp37m-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 0a79171ab35fc71428092412b0db181813b5248737cda19d627481f95b234a48
MD5 164cda98f15b295316cd84ac44871363
BLAKE2b-256 8b9b38a081245530e5735f3ffd6ec30644e0f9a4b2548c88b0bd9b53fe3973c8

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page