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

Commit

Permalink
Add map method to List
Browse files Browse the repository at this point in the history
  • Loading branch information
Phil Pluckthun committed Dec 29, 2016
1 parent 6a2554f commit d8017ab
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 3 deletions.
20 changes: 19 additions & 1 deletion src/List.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Option } from './constants'
import { Transform, Option } from './constants'
import ArrayNode from './persistentVector/ArrayNode'
import push from './listHelpers/push'
import pop from './listHelpers/pop'
Expand Down Expand Up @@ -73,6 +73,24 @@ export default class List<T> {
return pop<T>(this)
}

map<G>(
transform: Transform<number, Option<T>, Option<G>>
): List<G> {
const tail = this.tail.map<G>(transform, this.owner) as LeafNode<G>
const root = !this.root ?
undefined :
this.root.map<G>(transform, this.owner) as ArrayNode<G>

if (this.owner) {
const res = (this as List<any>)
res.root = root
res.tail = tail
return (res as List<G>)
}

return makeList<G>(tail, root)
}

clear(): List<T> {
return emptyList<T>()
}
Expand Down
27 changes: 26 additions & 1 deletion src/persistentVector/ArrayNode.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Node from './Node'
import { Option, BUCKET_SIZE } from '../constants'
import { Transform, Option, BUCKET_SIZE } from '../constants'
import { replaceValue, copyArray, push, pop } from '../util/array'
import { maskHash } from '../util/bitmap'
import LeafNode from './LeafNode'
Expand Down Expand Up @@ -60,6 +60,31 @@ export default class ArrayNode<T> {
)
}

map<G>(
transform: Transform<number, Option<T>, Option<G>>,
owner?: Object
): Node<G> {
const length = this.content.length
const content: Node<G>[] = new Array(length)
for (let i = 0; i < length; i++) {
const node = this.content[i]
content[i] = node.map<G>(transform, owner)
}

if (owner && owner === this.owner) {
const res = (this as ArrayNode<any>)
res.content = content
return (res as ArrayNode<G>)
}

return new ArrayNode(
this.level,
content,
this.size,
owner
)
}

pushLeafNode(node: LeafNode<T>, owner?: Object): ArrayNode<T> {
let content: Node<T>[]
let size: number
Expand Down
23 changes: 22 additions & 1 deletion src/persistentVector/LeafNode.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Option } from '../constants'
import Node from './Node'
import { Transform, Option } from '../constants'
import { replaceValue, spliceIn, spliceOut } from '../util/array'
import { maskHash } from '../util/bitmap'

Expand Down Expand Up @@ -47,6 +48,26 @@ export default class LeafNode<T> {
return new LeafNode<T>(content, owner)
}

map<G>(
transform: Transform<number, Option<T>, Option<G>>,
owner?: Object
): Node<G> {
const length = this.content.length
const content: Option<G>[] = new Array(length)
for (let i = 0; i < length; i++) {
const value = this.content[i]
content[i] = transform(value, i)
}

if (owner && owner === this.owner) {
const res = (this as LeafNode<any>)
res.content = content
return (res as LeafNode<G>)
}

return new LeafNode(content, owner)
}

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

Expand Down

0 comments on commit d8017ab

Please sign in to comment.