From 29babaf1e0cce9430ccf42d5f2ca56bdb6882a14 Mon Sep 17 00:00:00 2001 From: Ian Gulliver Date: Fri, 5 Jul 2019 16:18:22 +0000 Subject: [PATCH] Create StringMap --- Layout.js | 26 +++++++++++++------------- LayoutGroup.js | 2 +- LayoutNode.js | 8 ++++---- utils.js | 18 ++++++++++++++++++ 4 files changed, 36 insertions(+), 18 deletions(-) diff --git a/Layout.js b/Layout.js index 2a9f6ce..7385882 100644 --- a/Layout.js +++ b/Layout.js @@ -3,7 +3,7 @@ class Layout { this.graph_ = graph; this.nodes_ = []; - this.nodesByPos_ = new Map(); + this.nodesByPos_ = new StringMap(); this.nodesByGraphNode_ = new Map(); this.lineSteps_ = []; @@ -81,12 +81,12 @@ class Layout { let newOffset = null; let newTension = this.getTotalTension(objects); for (let obj of objects) { - let offsets = new Map(); + let offsets = new StringMap(); let addOffset = (x, y) => { if (x == 0 && y == 0) { return; } - offsets.set([x, y].toString(), [x, y]); + offsets.set([x, y], [x, y]); }; for (let dir of [-1, 0, 1]) { addOffset(Math.sign(obj.vec[0]), dir); @@ -177,15 +177,15 @@ class Layout { } // Mapping to lines.svg clock-style numbering - outPointLookup = new Map([ - ['0,-1', 0], - ['1,-1', 1], - ['1,0', 2], - ['1,1', 3], - ['0,1', 4], - ['-1,1', 5], - ['-1,0', 6], - ['-1,-1', 7], + outPointLookup = new StringMap([ + [[ 0,-1], 0], + [[ 1,-1], 1], + [[ 1, 0], 2], + [[ 1, 1], 3], + [[ 0, 1], 4], + [[-1, 1], 5], + [[-1, 0], 6], + [[-1,-1], 7], ]); drawLine(from, to) { @@ -196,7 +196,7 @@ class Layout { for (let i of [0, 1]) { offset[i] = Math.sign(to.pos[i] - pos[i]); } - let outPoint = this.outPointLookup.get(offset.toString()); + let outPoint = this.outPointLookup.get(offset); if (inPoint === null) { this.lineSteps_.push({ type: 'line', diff --git a/LayoutGroup.js b/LayoutGroup.js index 521c114..3d20535 100644 --- a/LayoutGroup.js +++ b/LayoutGroup.js @@ -45,7 +45,7 @@ class LayoutGroup { } // Fix up nodesByPos, as intra-group collisions may have corrupted it for (let node of this.nodes) { - this.nodesByPos_.set(node.pos.toString(), node); + this.nodesByPos_.set(node.pos, node); } } diff --git a/LayoutNode.js b/LayoutNode.js index 8340b92..596fa28 100644 --- a/LayoutNode.js +++ b/LayoutNode.js @@ -4,7 +4,7 @@ class LayoutNode { this.nodesByPos_ = nodesByPos; this.pos = pos; - this.nodesByPos_.set(this.pos.toString(), this); + this.nodesByPos_.set(this.pos, this); } resolveAffinity(nodesByGraphNode) { @@ -45,13 +45,13 @@ class LayoutNode { offsetCollides(offset) { let newPos = this.offsetToPos(offset); - return this.nodesByPos_.get(newPos.toString()); + return this.nodesByPos_.get(newPos); } moveTo(pos) { - this.nodesByPos_.delete(this.pos.toString()); + this.nodesByPos_.delete(this.pos); this.pos = pos; - this.nodesByPos_.set(this.pos.toString(), this); + this.nodesByPos_.set(this.pos, this); } moveBy(offset) { diff --git a/utils.js b/utils.js index f42ff23..5643919 100644 --- a/utils.js +++ b/utils.js @@ -24,3 +24,21 @@ function intersects(set1, set2) { } return false; } + +class StringMap extends Map { + has(key) { + return super.has(key.toString()); + } + + get(key) { + return super.get(key.toString()); + } + + set(key, value) { + return super.set(key.toString(), value); + } + + delete(key) { + return super.delete(key.toString()); + } +}