GithubHelp home page GithubHelp logo

adkosm / lhash Goto Github PK

View Code? Open in Web Editor NEW
0.0 2.0 0.0 7 KB

Convenient identifiers for items of ordered collection with an emphasis on efficiency

License: MIT License

HTML 5.78% JavaScript 94.22%

lhash's Introduction

LHash

LHash is algorithm, which provides convenient identificators for items of ordered collection with an emphasis on efficiency while reordering items in collection.

With a naive approach with numerical ids, when you change position of one element in collection, you need to change ids of O(n) elements in collection (exactly of all elements in worst case).

Using linked list or real numbers can reduce the degree of tragedy, but is also a workarounds, because at some moment you reach the limit of the accuracy of real numbers and you still have to update many items.

LHash is a simple string. Main algorithm is a function 'allocate', which take two hashes and return new hash with a property, that this hash will be between original hashes in simple lexicographical order. It means, that if you want to change position of element, you need only to change id of current element to new hash from allocate function. Idea of algorithm bases on CRDT data structure LSeq. According to this document, length growth of hash is sublinear with respect to the total number of elements in the collection.

Using library

Include js file into project:

<script src="/path/to/LHash.js"></script>

Create hasher object:

var hasher = LHash({
    'boundary': 5, // optional - size of allocation boundary
    'id': 43 // optional - user id. if set, then activate anticollision mechanism based on id
});

You always have at least two elements with hashes - abstract element 'begin' with id = '0' and 'end' with id = 'z'.

You can use it in order to insert elements on the first and on the last position.

For example:

var firstElem = hasher.allocate('0', 'z'); // Insert element somewhere
var secondElem = hasher.allocate('0', firstElem); // Insert element before first element
var thirdElem = hasher.allocate(firstElem, 'z'); // Insert element after first element

You can insert element between any neighboring elements.

var elementA = hasher.allocate(firstElem, thirdElem); // Insert element between first ans third element

var elementB = hasher.allocate(secondElem, thirdElem); // WRONG! second and third elements are not neighbours because of first element between them

var elementC = haser.allocate(firstElem, secondElem); // WRONG! order in arguments is important! Second element must go before first in this example

Tests

To run tests, open tests/tests.html in browser.

According to tests, mean lenght of id for 100 elements is about 4, for 1000 - about 37 while pushing only to end. This is similar to the theoretical result of the sublinear growth rate.

Uniform pushing also shows very good results - mean is equal to about 6 with almost 10000 elements.

lhash's People

Contributors

adkosm 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.