Skip to content

Update style and optimise #7

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 13 additions & 6 deletions JavaScript/1-graph.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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);
}
Expand All @@ -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;
Expand All @@ -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);
}
Expand Down
26 changes: 16 additions & 10 deletions JavaScript/2-insert-link-to.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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;
Expand All @@ -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);
}
Expand All @@ -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);
}
}
},
};
}

Expand Down
33 changes: 18 additions & 15 deletions JavaScript/3-index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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;
Expand Down Expand Up @@ -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);
}
},
};
}

Expand All @@ -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;
Expand Down