From 6b55a7a860d71041d88ce2b455113e2d7a5d0f91 Mon Sep 17 00:00:00 2001 From: Timur Sevimli Date: Fri, 26 Jan 2024 18:37:17 +0300 Subject: [PATCH] Update style and optimise --- JavaScript/1-graph.js | 19 +++++++++++++------ JavaScript/2-insert-link-to.js | 26 ++++++++++++++++---------- JavaScript/3-index.js | 33 ++++++++++++++++++--------------- 3 files changed, 47 insertions(+), 31 deletions(-) diff --git a/JavaScript/1-graph.js b/JavaScript/1-graph.js index 7e179c2..10b0a51 100644 --- a/JavaScript/1-graph.js +++ b/JavaScript/1-graph.js @@ -12,8 +12,8 @@ class Vertex { const { links } = this; const { keyField } = this.graph; for (const item of distinct) { - const value = item.data[keyField]; - links.set(value, item); + const key = item.data[keyField]; + links.set(key, item); } return this; } @@ -30,7 +30,10 @@ class Cursor { for (const vertex of vertices) { let condition = true; for (const name of names) { - condition = condition && vertex.links.has(name); + if (!vertex.links.has(name)) { + condition = false; + break; + } } if (condition) result.add(vertex); } @@ -45,9 +48,10 @@ class Graph { } add(data) { - const vertex = new Vertex(this, data); const key = data[this.keyField]; - if (this.vertices.get(key) === undefined) { + let vertex = this.vertices.get(key); + if (!vertex) { + vertex = new Vertex(this, data); this.vertices.set(key, vertex); } return vertex; @@ -60,7 +64,10 @@ class Graph { const { data } = vertex; if (data) { for (const field in query) { - condition = condition && data[field] === query[field]; + if (data[field] !== query[field]) { + condition = false; + break; + } } if (condition) vertices.add(vertex); } diff --git a/JavaScript/2-insert-link-to.js b/JavaScript/2-insert-link-to.js index a472fba..9abca5b 100644 --- a/JavaScript/2-insert-link-to.js +++ b/JavaScript/2-insert-link-to.js @@ -30,7 +30,10 @@ class Cursor { for (const vertex of vertices) { let condition = true; for (const name of names) { - condition = condition && vertex.links.has(name); + if (!vertex.links.has(name)) { + condition = false; + break; + } } if (condition) result.add(vertex); } @@ -45,9 +48,10 @@ class Graph { } add(data) { - const vertex = new Vertex(this, data); const key = data[this.keyField]; - if (this.vertices.get(key) === undefined) { + let vertex = this.vertices.get(key); + if (!vertex) { + vertex = new Vertex(this, data); this.vertices.set(key, vertex); } return vertex; @@ -60,7 +64,10 @@ class Graph { const { data } = vertex; if (data) { for (const field in query) { - condition = condition && data[field] === query[field]; + if (data[field] !== query[field]) { + condition = false; + break; + } } if (condition) vertices.add(vertex); } @@ -73,13 +80,12 @@ class Graph { const from = vertices.get(source); return { to(...destinations) { - if (from) { - destinations.forEach((destination) => { - const target = vertices.get(destination); - if (target) from.link(target); - }); + if (!from) return; + for (const destination of destinations) { + const target = vertices.get(destination); + if (target) from.link(target); } - } + }, }; } diff --git a/JavaScript/3-index.js b/JavaScript/3-index.js index 1d32582..d7191c7 100644 --- a/JavaScript/3-index.js +++ b/JavaScript/3-index.js @@ -32,7 +32,10 @@ class Cursor { for (const vertex of vertices.values()) { let condition = true; for (const name of names) { - condition = condition && vertex.links.has(name); + if (!vertex.links.has(name)) { + condition = false; + break; + } } if (condition) result.add(vertex); } @@ -48,9 +51,10 @@ class Graph { } add(data) { - const vertex = new Vertex(this, data); const key = data[this.keyField]; - if (this.vertices.get(key) === undefined) { + let vertex = this.vertices.get(key); + if (!vertex) { + vertex = new Vertex(this, data); this.vertices.set(key, vertex); } return vertex; @@ -81,10 +85,10 @@ class Graph { link(from) { return { to(...destinations) { - destinations.forEach((target) => { - if (target) from.link(target); - }); - } + for (const destination of destinations) { + from.link(destination); + } + }, }; } @@ -95,15 +99,14 @@ class Graph { vertices.push(vertex); const keys = Object.keys(record); for (const [key, idx] of this.indices) { - if (keys.includes(key)) { - const value = record[key]; - let records = idx.get(value); - if (!records) { - records = new Set(); - idx.set(value, records); - } - records.add(vertex); + if (!keys.includes(key)) continue; + const value = record[key]; + let records = idx.get(value); + if (!records) { + records = new Set(); + idx.set(value, records); } + records.add(vertex); } } return vertices; pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy