Commit 8e481f96 authored by Snigdha Saha's avatar Snigdha Saha
Browse files

p6

parents
No related merge requests found
Pipeline #77977 failed with stage
in 0 seconds
Showing with 187 additions and 0 deletions
+187 -0
package edu.caltech.cs2.coloring;
public enum InstructionType {
Write, Read
}
package edu.caltech.cs2.coloring;
import edu.caltech.cs2.datastructures.ChainingHashDictionary;
import edu.caltech.cs2.datastructures.ChainingHashSet;
import edu.caltech.cs2.datastructures.MoveToFrontDictionary;
import edu.caltech.cs2.interfaces.IDictionary;
import edu.caltech.cs2.interfaces.ISet;
public class NodeGraph {
private static class Node {
public int name;
public int color;
public ISet<Node> neighbors;
public ISet<Integer> neighborColors;
public ISet<Integer> neighborsColored;
public Node(int name, int color) {
this.name = name;
this.color = color;
this.neighbors = new ChainingHashSet<>();
this.neighborColors = new ChainingHashSet<>();
this.neighborsColored = new ChainingHashSet<>();
}
@Override
public String toString() {
return this.name + "[" + this.color + "]";
}
@Override
public boolean equals(Object o) {
if (!(o instanceof Node)) {
return false;
}
return ((Node)o).name == this.name;
}
@Override
public int hashCode() {
return this.name;
}
}
private final Node[] nodes;
public NodeGraph(int n) {
this.nodes = new Node[n];
for (int i = 0; i < n; i++) {
this.nodes[i] = new Node(i, 0);
}
}
public int numberOfVertices() {
return this.nodes.length;
}
public boolean isColored(int n) {
return this.nodes[n].color != 0;
}
public int getColor(int n) {
return this.nodes[n].color;
}
public void setColor(int n, int color) {
if (this.isColored(n)) {
throw new IllegalArgumentException("" + n + " already has a color.");
}
if (color <= 0) {
throw new IllegalArgumentException("only numbers >= 1 are valid colors");
}
checkColoringIsValid(n, color);
this.nodes[n].color = color;
for (Node m : this.nodes[n].neighbors) {
m.neighborColors.add(color);
m.neighborsColored.add(n);
}
}
private void checkColoringIsValid(int n, int color) {
ISet<Integer> adjs = this.neighbors(n);
for (int v : adjs) {
if (this.nodes[v].color == color) {
throw new IllegalColoringException();
}
}
}
public void addEdge(int n, int m) {
this.nodes[n].neighbors.add(this.nodes[m]);
this.nodes[m].neighbors.add(this.nodes[n]);
}
public int degreeOfSaturation(int n) {
return this.nodes[n].neighborColors.size();
}
public int numNeighbors(int n) {
return this.nodes[n].neighbors.size() - this.nodes[n].neighborsColored.size();
}
public int numVertices() {
return this.nodes.length;
}
public ISet<Integer> neighbors(int n) {
ISet<Integer> adj = new ChainingHashSet<>();
ISet<Node> neighbors = this.nodes[n].neighbors;
for (Node neigh : neighbors) {
adj.add(neigh.name);
}
return adj;
}
public IDictionary<Integer, Integer> getColoring() {
IDictionary<Integer, Integer> coloring = new ChainingHashDictionary<>(MoveToFrontDictionary::new);
for (Node n : this.nodes) {
coloring.put(n.name, n.color);
}
return coloring;
}
@Override
public String toString() {
StringBuilder result = new StringBuilder("{");
for (Node n : this.nodes) {
result.append(n.toString()).append("->").append(n.neighbors).append(", ");
}
return result + "}";
}
}
\ No newline at end of file
package edu.caltech.cs2.coloring;
import edu.caltech.cs2.datastructures.ArrayDeque;
import edu.caltech.cs2.datastructures.ChainingHashSet;
import edu.caltech.cs2.interfaces.IDeque;
import edu.caltech.cs2.interfaces.IDictionary;
import edu.caltech.cs2.interfaces.ISet;
import edu.caltech.cs2.interfaces.IStack;
import java.util.Iterator;
public class Program implements Iterable<Instruction> {
private IDeque<Instruction> program;
public Program(IDeque<Instruction> program) {
this.program = program;
}
public Program color(IDictionary<Integer, Integer> coloring) {
IDeque<Instruction> result = new ArrayDeque<>();
for (Instruction instr : program) {
result.add(new Instruction(instr.type(), coloring.get(instr.variable()) - 1));
}
return new Program(result);
}
public ISet<Integer> variables() {
ISet<Integer> vars = new ChainingHashSet<>();
for (Instruction instr : program) {
vars.add(instr.variable());
}
return vars;
}
public NodeGraph constructInterferenceGraph() {
return null;
}
@Override
public String toString() {
return this.program.toString();
}
@Override
public Iterator<Instruction> iterator() {
return this.program.iterator();
}
}
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment