rope-sequence
Advanced tools
Comparing version 1.0.0 to 1.1.0
39
index.js
@@ -10,3 +10,3 @@ const GOOD_LEAF_SIZE = 50 | ||
// :: (union<[T], RopeSequence<T>) → RopeSequence<T> | ||
// :: (union<[T], RopeSequence<T>>) → RopeSequence<T> | ||
// Append an array or other rope to this one, returning a new rope. | ||
@@ -23,3 +23,3 @@ append(other) { | ||
// :: (union<[T], RopeSequence<T>) → RopeSequence<T> | ||
// :: (union<[T], RopeSequence<T>>) → RopeSequence<T> | ||
// Prepend an array or other rope to this one, returning a new rope. | ||
@@ -52,9 +52,12 @@ prepend(other) { | ||
// indices. This tends to be more efficient than looping over the | ||
// indices and calling `get`, because it doesn't have to desend the | ||
// indices and calling `get`, because it doesn't have to descend the | ||
// tree for every element. | ||
forEach(f, from = 0, to = this.length) { | ||
this.forEachInner(f, from, to, 0) | ||
if (from <= to) | ||
this.forEachInner(f, from, to, 0) | ||
else | ||
this.forEachInvertedInner(f, from, to, 0) | ||
} | ||
// :: (?union<[T], RopeSequence<T>) → RopeSequence<T> | ||
// :: (?union<[T], RopeSequence<T>>) → RopeSequence<T> | ||
// Create a rope representing the given array, or return the rope | ||
@@ -91,5 +94,11 @@ // itself if a rope was given. | ||
forEachInner(f, from, to, start) { | ||
for (let i = from; i < to; i++) f(this.values[i], start + i) | ||
for (let i = from; i < to; i++) | ||
if (f(this.values[i], start + i) === false) return false | ||
} | ||
forEachInvertedInner(f, from, to, start) { | ||
for (let i = from - 1; i >= to; i--) | ||
if (f(this.values[i], start + i) === false) return false | ||
} | ||
leafAppend(other) { | ||
@@ -133,6 +142,20 @@ if (this.length + other.length <= GOOD_LEAF_SIZE) | ||
let leftLen = this.left.length | ||
if (from < leftLen) this.left.forEachInner(f, from, Math.min(to, leftLen), start) | ||
if (to > leftLen) this.right.forEachInner(f, Math.max(from - leftLen, 0), Math.min(this.length, to) - leftLen, start + leftLen) | ||
if (from < leftLen && | ||
this.left.forEachInner(f, from, Math.min(to, leftLen), start) === false) | ||
return false | ||
if (to > leftLen && | ||
this.right.forEachInner(f, Math.max(from - leftLen, 0), Math.min(this.length, to) - leftLen, start + leftLen) === false) | ||
return false | ||
} | ||
forEachInvertedInner(f, from, to, start) { | ||
let leftLen = this.left.length | ||
if (from > leftLen && | ||
this.right.forEachInvertedInner(f, from - leftLen, Math.max(to, leftLen) - leftLen, start + leftLen) === false) | ||
return false | ||
if (to < leftLen && | ||
this.left.forEachInvertedInner(f, Math.min(from, leftLen), to, start) === false) | ||
return false | ||
} | ||
sliceInner(from, to) { | ||
@@ -139,0 +162,0 @@ if (from == 0 && to == this.length) return this |
{ | ||
"name": "rope-sequence", | ||
"version": "1.0.0", | ||
"version": "1.1.0", | ||
"description": "Rope-based persistent sequence type", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
@@ -15,3 +15,3 @@ # rope-sequence | ||
`static `**`from`**`(?union<[T], RopeSequence<T>) → RopeSequence<T>` | ||
`static `**`from`**`(?union<[T], RopeSequence<T>>) → RopeSequence<T>` | ||
@@ -29,7 +29,7 @@ Create a rope representing the given array, or return the rope itself | ||
**`append`**`(union<[T], RopeSequence<T>) → RopeSequence<T>` | ||
**`append`**`(union<[T], RopeSequence<T>>) → RopeSequence<T>` | ||
Append an array or other rope to this one, returning a new rope. | ||
**`prepend`**`(union<[T], RopeSequence<T>) → RopeSequence<T>` | ||
**`prepend`**`(union<[T], RopeSequence<T>>) → RopeSequence<T>` | ||
@@ -46,11 +46,16 @@ Prepend an array or other rope to this one, returning a new rope. | ||
**`forEach`**`(f: fn(element: T, index: number), from: ?number, to: ?number)` | ||
**`forEach`**`(f: fn(element: T, index: number) → ?bool, from: ?number, to: ?number)` | ||
Call the given function for each element between the given indices. | ||
This tends to be more efficient than looping over the indices and | ||
calling `get`, because it doesn't have to desend the tree for every | ||
calling `get`, because it doesn't have to descend the tree for every | ||
element. | ||
`to` may be less then `from`, in which case the iteration will happen | ||
in reverse (starting at index `from - 1`, down to index `to`. | ||
The iteration function may return `false` to abort iteration early. | ||
**`flatten`**`() → [T]` | ||
Return the content of this rope as an array. |
12
test.js
@@ -36,3 +36,3 @@ var RopeSequence = require("./index") | ||
rope.forEach(function(elt, i) { | ||
assert.equal(elt, cur + offset, "Proper element at " + cur + " in " + name + "::" + elt + " vs "+ (cur + offset)) | ||
assert.equal(elt, cur + offset, "Proper element at " + cur + " in " + name) | ||
assert.equal(cur, i, "Accurate index passed") | ||
@@ -42,2 +42,8 @@ cur++ | ||
assert.equal(cur, end, "Enough elements iterated in " + name) | ||
rope.forEach(function(elt, i) { | ||
cur-- | ||
assert.equal(elt, cur + offset, "Proper element during reverse iter at " + cur + " in " + name) | ||
assert.equal(cur, i, "Accurate index passed by reverse iter") | ||
}, end, start) | ||
assert.equal(cur, start, "Enough elements reverse-iterated in " + name + " -- " + cur + " " + start) | ||
} | ||
@@ -68,2 +74,6 @@ | ||
var sum = 0 | ||
small.forEach(function(v) { if (v == 2) return false; sum += v }) | ||
assert.equal(sum, 1, "abort iteration") | ||
console.log("All passed") |
11488
215
59