Giter Club home page Giter Club logo

sparse-arr-lib's Introduction

sparse-arr-lib License

Warning This library is unfinished & is a WIP. Use discretion, don't test in prod.

A library to assist with utilizing sparse storage arrays in Solidity.

Rationale

In Solidity, it is impossible to delete an element from the middle of a storage array without shifting all elements following the deleted element, disrupting order, or leaving a gap. This library is an experiment to enable the use of a sparse array-esq data structure to combat this shortcoming as efficiently as possible.

Intended Behavior

Sparse Demo

How does it work?

Before any elements are deleted, the library will treat the array as if it is normal- Elements will both be retrieved and stored at their canonical indicies (the canonical index is the true index of the element within the array, sans any offsets.)

When the first element is deleted with deleteAt, a sub array of deleted elements is created at slot keccak256(abi.encode(arrSlot, 0x535041525345)). This array contains the canonical indicies of the deleted elements.

If the subarray of deleted elements contains any values when a call to store, push, or get is made, a binary search will be performed over the array in order to find the nearest offset that is less than or equal to the supplied relative index. This offset will be added to the relative index in order to retrieve the canonical index.

Current limitations:

  • Elements must be deleted linearly and in continuously ascending order. (i.e., one cannot delete index 5 and then 3). This is due to the fact that the deleted elements subarray must be sorted for the binary search to work properly.
    • BUG: An element may not be deleted at the same relative index more than once.

Usage

To view docs, run forge doc --serve and navigate to http://localhost:3000/.

Contributions

Contributions are welcome!

sparse-arr-lib's People

Contributors

clabby avatar n0xmare avatar

Stargazers

 avatar Shun Kakinoki avatar Daoist space avatar  avatar Rappie avatar  avatar Juan Xavier Valverde avatar Harvey Specter avatar liquan.eth avatar Kames Geraghty avatar Ahmed Ali avatar Bakuchi avatar horsefacts avatar Michael Demarais avatar  avatar Lakshman Sankar avatar kaden avatar Maddiaa avatar Roman Krasiuk avatar chirag-bgh avatar 0xYYY avatar Michael Amadi avatar soju avatar ABIMS avatar sudo rm -rf --no-preserve-root / avatar Javed Khan avatar

Watchers

Michael Demarais avatar Rappie avatar Mark Tyneway avatar  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.