testing graph structure
This commit is contained in:
@@ -9,7 +9,7 @@
|
||||
</head>
|
||||
<body>
|
||||
<div id="app">
|
||||
<h1>Wew</h1>
|
||||
<h1>Workshopping DS/A for Procedural Music Generation</h1>
|
||||
<div id="where-thing-go">
|
||||
|
||||
</div>
|
||||
|
||||
@@ -3,10 +3,12 @@ import Edge from "./Edge";
|
||||
|
||||
export default class GraphNode<T> extends Node<T> {
|
||||
edges: Edge<T>[]
|
||||
id: number
|
||||
|
||||
constructor(data: T, edges?: Edge<T>[]) {
|
||||
constructor(data: T, id: number, edges?: Edge<T>[]) {
|
||||
super(data);
|
||||
this.edges = edges || new Array<Edge<T>>();
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
addEdge(destination: GraphNode<T>, weight?: number) {
|
||||
@@ -19,9 +21,11 @@ export default class GraphNode<T> extends Node<T> {
|
||||
|
||||
override print() {
|
||||
if (this.edges.length) {
|
||||
|
||||
for (let edge of this.edges) {
|
||||
console.log(`Event ${this.id} --> Event ${edge.end.id}`);
|
||||
}
|
||||
} else {
|
||||
console.log(this.data);
|
||||
console.log(`Event ${this.id} -->`);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,9 +1,10 @@
|
||||
import GraphNode from "./GraphNode";
|
||||
|
||||
export default class Graph<T> {
|
||||
isWeighted: boolean;
|
||||
isDirected: boolean;
|
||||
points: GraphNode<T>[];
|
||||
private isWeighted: boolean;
|
||||
private isDirected: boolean;
|
||||
private points: GraphNode<T>[];
|
||||
private count = 0;
|
||||
|
||||
constructor(isWeighted: boolean, isDirected: boolean, points?: GraphNode<T>[]) {
|
||||
this.isWeighted = isWeighted;
|
||||
@@ -12,17 +13,28 @@ export default class Graph<T> {
|
||||
}
|
||||
|
||||
createPoint(data: T) {
|
||||
const newPoint = new GraphNode<T>(data);
|
||||
const newPoint = new GraphNode<T>(data, this.count);
|
||||
this.points.push(newPoint);
|
||||
this.count++;
|
||||
return newPoint;
|
||||
}
|
||||
|
||||
addPoints(...points: GraphNode<T>[]) {
|
||||
addPoints(...points: T[]) {
|
||||
for (let point of points) {
|
||||
this.points.push(point);
|
||||
this.createPoint(point);
|
||||
}
|
||||
}
|
||||
|
||||
getPointByData(data: T): GraphNode<T> | null {
|
||||
for (let point of this.points) {
|
||||
if (point.data === data) {
|
||||
return point;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
removePoint(target: GraphNode<T>) {
|
||||
this.points.filter((point: GraphNode<T>) => point !== target);
|
||||
}
|
||||
@@ -32,7 +44,10 @@ export default class Graph<T> {
|
||||
}
|
||||
|
||||
addEdge(start: GraphNode<T>, end: GraphNode<T>, weight?: number) {
|
||||
if (this.isWeighted && weight) {
|
||||
if (this.isDirected) {
|
||||
this.isWeighted ? start.addEdge(end, weight) : start.addEdge(end);
|
||||
} else {
|
||||
if (this.isWeighted) {
|
||||
start.addEdge(end, weight);
|
||||
end.addEdge(start, weight);
|
||||
} else {
|
||||
@@ -40,6 +55,7 @@ export default class Graph<T> {
|
||||
end.addEdge(start);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
removeEdge(start: GraphNode<T>, end: GraphNode<T>) {
|
||||
try {
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
import { ChordQuality, EventData } from "./index";
|
||||
import LinkedList from "../LinkedList/index";
|
||||
import ListNode from "../LinkedList/ListNode";
|
||||
import Graph from "../Graph/index";
|
||||
import GraphNode from "../Graph/GraphNode";
|
||||
|
||||
const firstEvent: EventData = {
|
||||
root: "C",
|
||||
@@ -43,16 +45,23 @@ const fourthEvent: EventData = {
|
||||
|
||||
const eventlist = new LinkedList<EventData>();
|
||||
eventlist.addManyToTail(firstEvent, secondEvent, thirdEvent, fourthEvent);
|
||||
const thirdnode = eventlist.findByData(thirdEvent) as ListNode<EventData>;
|
||||
|
||||
// console.log(eventlist.findByNode(thirdnode));
|
||||
// eventlist.removeHead();
|
||||
// eventlist.removeTail();
|
||||
|
||||
// eventlist.removeAtPosition(3);
|
||||
// eventlist.removeByData(fourthEvent);
|
||||
|
||||
// const thirdnode = eventlist.findByData(thirdEvent) as ListNode<EventData>;
|
||||
// console.log(eventlist.findByNode(thirdnode));
|
||||
const eventgraph = new Graph<EventData>(false, true);
|
||||
|
||||
console.log(eventlist);
|
||||
eventgraph.addPoints(firstEvent, secondEvent, thirdEvent, fourthEvent);
|
||||
|
||||
export default { eventlist }
|
||||
const secondstop = eventgraph.getPointByData(secondEvent) as GraphNode<EventData>;
|
||||
const thirdstop = eventgraph.getPointByData(thirdEvent) as GraphNode<EventData>;
|
||||
const fourthstop = eventgraph.getPointByData(fourthEvent) as GraphNode<EventData>;
|
||||
eventgraph.addEdge(secondstop, thirdstop);
|
||||
eventgraph.addEdge(thirdstop, secondstop);
|
||||
eventgraph.addEdge(secondstop, fourthstop);
|
||||
|
||||
export default { eventlist, eventgraph }
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
import data from "./lib/helpers/sample";
|
||||
const { eventlist } = data;
|
||||
const { eventlist, eventgraph } = data;
|
||||
|
||||
eventlist.print();
|
||||
// eventlist.print();
|
||||
eventgraph.print();
|
||||
|
||||
// export default content;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user