Giter VIP home page Giter VIP logo

fingertree.js's Introduction

FingerTree.js

Build Status

Implementation of Finger Tree, an immutable general-purpose data structure which can further be used to implement random-access sequences, priority-queues, ordered sequences, interval trees, etc.

Based on: Ralf Hinze and Ross Paterson, "Finger trees: a simple general-purpose data structure".

Note that Finger Tree is not meant to be used on its own. Instead, it should serve as an infrastructure for building other data structures. As an example, immutable-sequence.js is an immutable random-access sequence implementation built on top of this library.

Installation (Browser)

Download the js file and include it in your web page.

<script type="text/javascript" src="./fingertree.js"></script>

Installation (Node.js)

If you want to use it in Node.js, you may install it via npm.

npm install fingertree

Then, in your program:

var FingerTree = require('fingertree');

Quick Examples

// Create a finger-tree from an array
// By default, the tree is annotated with a size measurer.
var tree = FingerTree.fromArray([1, 2, 3, 4]);
tree.measure(); // 4

tree.peekFirst(); // 1
tree.peekLast();  // 4

// Create a new tree with an element added to the front
var tree2 = tree.addFirst(0);
tree2.peekFirst(); // 0

// Create a new tree with an element added to the end
var tree3 = tree.addLast(5);
tree3.peekLast(); // 5

// Create a new tree with the first element removed
var tree4 = tree.removeFirst();
tree4.peekFirst(); // 2

// Create a new tree with the last element removed
var tree5 = tree.removeLast();
tree5.peekLast(); // 3

// Split into two trees with a predicate on measure.
var split = tree.split(function (measure) {
  return measure > 3;
});
split[0].measure(); // 3
split[1].measure(); // 1


// Create a finger-tree with a custom measurer.
var measurer = {
  identity: function () {
    return -Infinity;
  },
  measure: function (x) {
    return x;
  },
  sum: function (a, b) {
    return Math.max(a, b);
  }
};
var maxTree = FingerTree.fromArray([1, 3, 2, 9, 7], measurer);
maxTree.measure(); // 9

License

MIT License

© 2014 Xueqiao Xu <[email protected]>

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

fingertree.js's People

Contributors

qiao avatar zot avatar

Stargazers

Utku Demir avatar Saad Shahd avatar  avatar Patrick Smith avatar Auto Fang avatar haigeno1 avatar  avatar Fangzhou Li avatar DawnING avatar qqq avatar nyz avatar MathxH Chen avatar catch on me  avatar zeromake avatar Bigger.Ding avatar Meng Sio avatar 原子 avatar xiaomao Feng avatar 司徒正美 avatar Charlie avatar Dee Cheung avatar Janry avatar 胡正伟 avatar  avatar Qiao Wang avatar 工业聚 avatar Zack Young avatar Dmitry avatar Yannick Versley avatar Amar Sood avatar Matt Keas avatar Dmitriy Tsvettsikh avatar Jaeho Lee (Jay) avatar Notas Hellout avatar  avatar Alexander Kyte avatar Michael Anthony avatar timelyportfolio avatar Diego Martelli avatar  avatar danielsdesk avatar aaa avatar  avatar  avatar Lu Qu avatar

Watchers

 avatar  avatar James Cloos avatar Michael Anthony avatar  avatar

Forkers

zot dieface epixode

fingertree.js's Issues

incorrect behavior when implementing simple priority queue

@qiao,

Here is my max-priority queue test.

It should sort [1,3,2,9,7] into [9,7,3,2,1] and print it out.

var FingerTree = require('fingertree');

// Create a finger-tree with a custom measurer.
var measurer = {
    identity: function () {
        return -Infinity;
    },
    measure: function (x) {
        return x;
    },
    sum: function (a, b) {
        return Math.max(a, b);
    }
};
var maxTree = FingerTree.fromArray([1, 3, 2,  9, 7], measurer);

var i = 1;
while(!maxTree.isEmpty()) {
    //console.log("tree: ",JSON.stringify(maxTree.toJSON(),null,2));
    var split = maxTree.splitTree(function(measure) {
        return measure>=maxTree.measure()
    },-Infinity);
    console.log(i+") "+split.mid+"; max:"+maxTree.measure()+" left:"+split.left.measure()+" right:"+split.right.measure());
    //console.log("left: ",JSON.stringify(split.left.toJSON(),null,2));
    //console.log("right: ",JSON.stringify(split.right.toJSON(),null,2));
    maxTree = split.left.concat(split.right)
    i++;
}

But it prints

1) 9; max:9 left:3 right:7
2) 7; max:7 left:3 right:2
3) 3; max:3 left:1 right:2
4) 2; max:2 left:1 right:2
5) 2; max:2 left:1 right:-Infinity
6) 1; max:1 left:-Infinity right:-Infinity

So I consider there is some bug in splitTree when item actually does not get removed from left/right subtree. You can uncomment console.log and you will see that after removing 7 the element 2 is both in right subtree and left subtree. Therefore it is printed twice, while source array contained only single element "2".

Could you please comment what I'm doing wrong??

Regards, mikhail.

Errors handling laziness in Deep.concat() and app3()

Deep.concat and app3() both do instanceof checks on their arguments and then access properties in them. You need to force the arguments if they're lazy, otherwise they fail the instanceof checks and default case in app3() fails on a property access.

For my CoffeeScript version, I just added a force method to FingerTree that returns this. Then, at the top of Deep.concat() and app3(), I just reassigned the arguments to be their forced versions.

bug in `nodes`

It there a reason why it is assumed that xs.length <= 8 here? There is no such assumption in Hinze and Paterson.

const FingerTree = require( 'fingertree' ) ;
let X = FingerTree.fromArray( [ ] ) ;
for ( let i = 0 ; i < ( 4 * 4 + 4 ) ; ++i ) X = X.addLast( i ) ;
let Y = FingerTree.fromArray( [ ] ) ;
for ( let i = 0 ; i < ( 4 * 4 + 4 ) ; ++i ) Y = Y.addFirst( i ) ;
X.concat( Y ).measure( )
Error: invalid number of nodes
    at nodes (/home/aureooms/node_modules/fingertree/src/fingertree.js:994:22)
    at DelayedFingerTree.thunk (/home/aureooms/node_modules/fingertree/src/fingertree.js:963:35)
    at DelayedFingerTree.force (/home/aureooms/node_modules/fingertree/src/fingertree.js:794:24)
    at DelayedFingerTree.measure (/home/aureooms/node_modules/fingertree/src/fingertree.js:810:17)
    at FingerTree.Deep.measure (/home/aureooms/node_modules/fingertree/src/fingertree.js:576:52)
    at DelayedFingerTree.measure (/home/aureooms/node_modules/fingertree/src/fingertree.js:810:25)
    at FingerTree.Deep.measure (/home/aureooms/node_modules/fingertree/src/fingertree.js:576:52)
    at repl:1:15
    at REPLServer.defaultEval (repl.js:164:27)
    at bound (domain.js:250:14)

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.