Skip to content

Commit

Permalink
Fixed the immutability leak when updating entries in Btree Nodes
Browse files Browse the repository at this point in the history
  • Loading branch information
applitopia committed Feb 4, 2019
1 parent d1680c2 commit d700f18
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 9 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ operations implemented using [Floyd-Rivest](https://en.wikipedia.org/wiki/Floyd%
Version
-------

The current version [immutable-sorted@0.2.10](https://github.com/applitopia/immutable-sorted/releases/tag/v0.2.10) is an extension of [immutable-js@4.0.0-rc.12](https://github.com/facebook/immutable-js/releases/tag/v4.0.0-rc.12).
The current version [immutable-sorted@0.2.11](https://github.com/applitopia/immutable-sorted/releases/tag/v0.2.11) is an extension of [immutable-js@4.0.0-rc.12](https://github.com/facebook/immutable-js/releases/tag/v4.0.0-rc.12).


Installation
Expand Down Expand Up @@ -105,7 +105,7 @@ Seq { "R", "Q", "P", "O", "N" }
Seq { "R", "Q", "P", "O", "N", "M", "L" }
```

We can also use the numeric index to efficiently iterate through the sorted collections numeric as if they were arrays. The method:
We can also use the numeric index to efficiently iterate through the sorted collections starting from numeric index as if they were arrays. The method:

```js
fromIndex(n, backwards)
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "immutable-sorted",
"version": "0.2.10",
"version": "0.2.11",
"description": "Immutable Sorted Data Collections",
"license": "MIT",
"homepage": "https://applitopia.github.io/immutable-sorted",
Expand Down
28 changes: 22 additions & 6 deletions src/SortedMapBtreeNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ class SortedMapBtreeNode extends SortedMapNode {
SetRef(didChangeSize);
}
newEntries = setIn(entries, idx, entry, canEdit);
newNodes = nodes;
newNodes = clone(nodes, canEdit);
}
} else {
// Inserting into entries or upserting nodes
Expand Down Expand Up @@ -271,7 +271,7 @@ class SortedMapBtreeNode extends SortedMapNode {
//
// No splitting, just setIn the updated subNode
//
newEntries = entries;
newEntries = clone(entries, canEdit);
newNodes = setIn(nodes, idx, updatedNode, canEdit);
}
} else {
Expand Down Expand Up @@ -337,7 +337,7 @@ class SortedMapBtreeNode extends SortedMapNode {
SetRef(didChangeSize);
const newEntry = [key, NOT_SET];
newEntries = setIn(entries, idx, newEntry, canEdit);
newNodes = nodes;
newNodes = clone(nodes, canEdit);
} else {
// Remove from node

Expand All @@ -355,7 +355,7 @@ class SortedMapBtreeNode extends SortedMapNode {
//
// No splitting, just setIn the updated subNode
//
newEntries = entries;
newEntries = clone(entries, canEdit);
newNodes = setIn(nodes, idx, updatedNode, canEdit);
} else {
// Nothing changed
Expand Down Expand Up @@ -427,7 +427,7 @@ class SortedMapBtreeNode extends SortedMapNode {
SetRef(didChangeSize);
const newEntry = [key, NOT_SET];
newEntries = setIn(entries, idx, newEntry, canEdit);
newNodes = nodes;
newNodes = clone(nodes, canEdit);
} else {
//
// OPERATION: REMOVE entry from the LEAF
Expand Down Expand Up @@ -1056,6 +1056,22 @@ function indent(level) {
return _indentStr.substring(0, indentCnt);
}

function clone(array, canEdit) {
if(array === undefined) {
return array;
}
if (canEdit) {
return array;
}

const newLen = array.length;
const newArray = allocArray(newLen);
for (let ii = 0; ii < newLen; ii++) {
newArray[ii] = array[ii];
}
return newArray;
}

function setIn(array, idx, val, canEdit) {
if (canEdit) {
array[idx] = val;
Expand Down Expand Up @@ -1704,7 +1720,7 @@ SortedMapBtreeNode.prototype.spliceNode = function(
} else if (updatedEntry) {
newEntries = setIn(entries, idx, updatedEntry, canEdit);
} else {
newEntries = entries;
newEntries = clone(entries, canEdit);
}
}

Expand Down

0 comments on commit d700f18

Please sign in to comment.