This repository has been archived by the owner on Jan 11, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add initial RelaxedArrayNode implementation
- Loading branch information
Phil Pluckthun
committed
Jan 1, 2017
1 parent
7fecdca
commit 12d3dd5
Showing
3 changed files
with
99 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
import Node from './Node' | ||
import { Option } from '../constants' | ||
import { replaceValue } from '../util/array' | ||
import { maskHash } from '../util/bitmap' | ||
import createSizeTable from './util/createSizeTable' | ||
|
||
function findTableIndex( | ||
key: number, | ||
level: number, | ||
sizeTable: number[] | ||
): number { | ||
let index = maskHash(key, level) | ||
while (sizeTable[index] <= key) { | ||
index = index + 1 | ||
} | ||
|
||
return index | ||
} | ||
|
||
export default class RelaxedArrayNode<T> { | ||
level: number // NOTE: This can only be >= 1 | ||
content: Node<T>[] | ||
sizeTable: number[] | ||
size: number | ||
owner?: Object | ||
|
||
constructor( | ||
level: number, | ||
content: Node<T>[], | ||
size: number, | ||
owner?: Object | ||
) { | ||
this.level = level | ||
this.content = content | ||
this.sizeTable = createSizeTable(content) | ||
this.size = size | ||
this.owner = owner | ||
} | ||
|
||
get(key: number, notSetVal?: T): Option<T> { | ||
if (key >= this.size) { | ||
return notSetVal | ||
} | ||
|
||
const index = findTableIndex(key, this.level, this.sizeTable) | ||
const subNode = this.content[index] | ||
const newKey = index === 0 ? index : key - this.sizeTable[index - 1] | ||
|
||
return subNode.get(newKey, notSetVal) | ||
} | ||
|
||
set(key: number, value: T, owner?: Object): RelaxedArrayNode<T> { | ||
if (key >= this.size) { | ||
return this | ||
} | ||
|
||
const index = findTableIndex(key, this.level, this.sizeTable) | ||
const oldSubNode = this.content[index] | ||
const newKey = index === 0 ? index : key - this.sizeTable[index - 1] | ||
|
||
const subNode = oldSubNode.set(newKey, value, owner) | ||
if (oldSubNode === subNode) { | ||
return this | ||
} | ||
|
||
if (owner && owner === this.owner) { | ||
this.content[index] = subNode | ||
return this | ||
} | ||
|
||
const content = replaceValue(this.content, index, subNode) | ||
|
||
return new RelaxedArrayNode<T>( | ||
this.level, | ||
content, | ||
this.size, | ||
owner | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import Node from '../Node' | ||
|
||
function createSizeTable(content: Node<any>[]): number[] { | ||
const length = content.length | ||
const sizeTable = new Array(length) | ||
|
||
let sum = 0 | ||
for (let i = 0; i < length; i++) { | ||
const node = content[i] | ||
sum = sum + node.size | ||
sizeTable[i] = sum | ||
} | ||
|
||
return sizeTable | ||
} | ||
|
||
export default createSizeTable |