Coder Social home page Coder Social logo

alexdremov / treearray Goto Github PK

View Code? Open in Web Editor NEW
3.0 2.0 0.0 1.08 MB

Swift tree-based array implementation. Efficient for random insertions/deletions

Swift 99.54% Shell 0.46%
swift algorithms array efficiency efficient-algorithm ios ios-app ios-swift optimization optimization-algorithms

treearray's Introduction

TreeArray

Swift implementation of implicit treap. Data structure with efficient random insert / remove.

Usage

TreeArray behaves like a usual array but has a different implementation under the hood that allows some operations to work faster. You can just replace Array with TreeArray and it should work as expected.

import TreeArray

var foo: TreeArray = [1, 2, 3, 4, 5]
foo.insert(0, at: 0)
print(foo) // [0, 1, 2, 3, 4, 5]

Complexity

According to performance tests, the visible difference starts to appear around 16k elements. After that, TreeArray outperform Array and Deque on random insertions and deletions.

Operation Complexity Complexity Array
append O(log n) O(1)
subscript O(log n) O(1)
random insert O(log n) O(n)
random delete O(log n) O(n)
iteration (iterator) O(n) O(n)
iteration (subscript) O(n * log n) O(n)
build from array O(n) O(n)
build from unknown-sized seq O(n * log n) O(n)
reverse O(n) O(n)
contains O(n) O(n)
append array O(m + log n) O(m)
insert array O(m + log n) O(m + n)

Comparison

The data structure is built upon a self-balancing random search tree. It allows performing random insertions and deletions efficiently with the cost of worse performance on other operations. In the directory Benchmarks/results you can explore full comparison results. Here, I will note only the most important cases.

Random insertions

Random removals

Prepend

Remove first


However, on other tests, it works worse. For example, iteration or build.

treearray's People

Contributors

alexdremov avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.