Giter Club home page Giter Club logo

rope's Introduction

rope

rope is an implementation of the rope data structure in JavaScript.

A rope is an efficient data structure for storing and manipulating very large mutable strings. It reduces memory reallocation and data copy overhead for applications that are constantly operating on very large strings by splitting them into multiple smaller strings transparently. Efficient random access is achieved via a binary tree.

The following table outlines the computational complexity of various operations over strings and ropes of length n.

Operation Regular JavaScript String Rope
Initialization O(n) O(n)
Removal of m characters O(n) O(m)
Insertion of m characters O(n) O(m)
Random access O(1) O(log n)
Concatenation of a string with length m Best Case O(1) / Worst Case O(n+m) * O(1)
Extraction of substring with length m O(m) O(m)

* Most JavaScript engines have certain optimizations in place for concatenating strings.

The Rope data structure really shines in outperforming regular JS strings when m <<< n. It's therefore best suited for applications that perform small, but very frequent operations in very large strings. (e.g. text editors)

Installation

Install with component(1):

$ component install component/rope

API

var r = rope(str);

Creates a Rope data structure, and initializes it with the string str.

Rope#toString()

Converts the entire rope to a JavaScript String.

Rope#remove(start, end)

Removes characters from start to end. This operation modifies the rope data structure.

The character at start is removed, but the character at end is not removed.

Rope#insert(position, text)

Inserts text at position.

Rope#length

Total length of the rope in characters.

Rope#rebuild()

Rebuilds the entire rope structure, producing a perfectly balanced binary tree. (Resouce intensive operation)

Rope#rebalance()

Walks on rope structure, finding unbalanced nodes and rebuilding them. (This is usually a less resouce intensive operation than Rope#rebuild(), but is still resource intensive.)

Rope#substring(start, end)

Returns text from the rope between the start and end positions. The character at start gets returned, but the character at end is not returned.

Rope#substr(start, length)

Returns a string of length characters from the rope, starting at the start position.

Rope#charAt(position)

Returns the character at position.

Rope#charCodeAt(position)

Returns the code of the character at position.

Rope.SPLIT_LENGTH

The threshold used to split a leaf node into two child nodes.

Rope.JOIN_LENGTH

The threshold used to join two child nodes into one leaf node.

Rope.REBALANCE_RATIO

The threshold used to trigger a tree node rebuild when rebalancing the rope.

Run Tests

npm install component-test
make test

License

The MIT License (MIT)

Copyright (c) 2014 Automattic, Inc.

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.

rope's People

Contributors

coreh avatar korczis avatar matthewmueller 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.