Skip to content
This repository has been archived by the owner on Jan 11, 2019. It is now read-only.

Commit

Permalink
Add unfinished pop implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
Phil Pluckthun committed Dec 29, 2016
1 parent 1c90b7a commit b798b9e
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 3 deletions.
26 changes: 26 additions & 0 deletions src/listHelpers/pop.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import List, { makeList } from '../List'

function pop<T>(list: List<T>): List<T> {
let tail = list.tail
let root = list.root

if (tail.size === 1) {
return list // TODO: Implement tail shift
} else {
tail = tail.pop(list.owner)
}

if (list.owner) {
list.tail = tail
list.root = root
list.size = tail.size + (root ? root.size : 0)
return list
}

return makeList<T>(
tail,
root
)
}

export default pop
20 changes: 17 additions & 3 deletions src/persistentVector/LeafNode.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Option } from '../constants'
import { spliceIn } from '../util/array'
import { spliceIn, spliceOut } from '../util/array'
import { maskHash } from '../util/bitmap'

let EMPY_NODE: LeafNode<any>
Expand Down Expand Up @@ -36,11 +36,25 @@ export default class LeafNode<T> {

push(value: T, owner?: Object): LeafNode<T> {
const content = spliceIn<T>(this.content, this.size, value)
const size = this.size + 1

if (owner && owner === this.owner) {
this.content = content
this.size = size
this.size = this.size + 1
return this
}

return new LeafNode<T>(
content,
owner
)
}

pop(owner?: Object): LeafNode<T> {
const content = spliceOut<T>(this.content, this.size - 1)

if (owner && owner === this.owner) {
this.content = content
this.size = this.size - 1
return this
}

Expand Down

0 comments on commit b798b9e

Please sign in to comment.