import Node from './Node' import { Predicate, Transform, Option } from '../constants' import CollisionNode from './CollisionNode' import resolveConflict from './resolveConflict' export default class ValueNode { level: number // NOTE: Receives their level from the parent size: number hashCode: number key: K value: T owner?: Object constructor( level: number, hashCode: number, key: K, value: T, owner?: Object ) { this.level = level this.size = 1 this.hashCode = hashCode this.key = key this.value = value this.owner = owner } get(hashCode: number, key: K, notSetVal?: T): Option { if (key !== this.key) { return notSetVal } return this.value } set(hashCode: number, key: K, value: T, owner?: Object): Node { if (key === this.key) { if (owner && owner === this.owner) { this.value = value return this } return new ValueNode( this.level, this.hashCode, this.key, value, owner ) } if (hashCode === this.hashCode) { const keys: K[] = [ this.key, key ] const values: T[] = [ this.value, value ] return new CollisionNode( this.level, this.hashCode, keys, values, owner ) } return resolveConflict( this.level, this.hashCode, this.clone(owner), hashCode, new ValueNode(0, hashCode, key, value, owner), owner ) } delete(hashCode: number, key: K, owner?: Object): Option> { if (key === this.key) { return undefined } return this } map(transform: Transform, owner?: Object): Node { const value = transform(this.value, this.key) if (owner && owner === this.owner) { const res = (this as ValueNode) res.value = value return (res as ValueNode) } return new ValueNode( this.level, this.hashCode, this.key, value, owner ) } iterate(step: Predicate) { return step(this.value, this.key) } iterateReverse(step: Predicate) { return step(this.value, this.key) } clone(owner?: Object): ValueNode { return new ValueNode( this.level, this.hashCode, this.key, this.value, owner ) } }