From 7664a903cc47969fb5b72dccfb1f4c0808f04ec4 Mon Sep 17 00:00:00 2001 From: Vincent Batts Date: Wed, 3 Oct 2012 21:40:17 -0400 Subject: [PATCH] pulling out more of the tree objects --- .../com/hashbangbash/trie/NodeContext.java | 8 +- .../java/com/hashbangbash/trie/NodePair.java | 28 ++++++ .../java/com/hashbangbash/trie/PathNode.java | 86 +++++++++++++++++++ 3 files changed, 118 insertions(+), 4 deletions(-) create mode 100644 src/main/java/com/hashbangbash/trie/NodePair.java create mode 100644 src/main/java/com/hashbangbash/trie/PathNode.java diff --git a/src/main/java/com/hashbangbash/trie/NodeContext.java b/src/main/java/com/hashbangbash/trie/NodeContext.java index 3ed5c00..417ffd1 100644 --- a/src/main/java/com/hashbangbash/trie/NodeContext.java +++ b/src/main/java/com/hashbangbash/trie/NodeContext.java @@ -5,21 +5,21 @@ package com.hashbangbash.trie; * an external incrementor to the node id */ public class NodeContext { - private long huffNodeId = 0; + private long nodeId = 0; public NodeContext() { } public NodeContext(long startId) { - this.huffNodeId = startId; + this.nodeId = startId; } public long getId() { - return this.huffNodeId; + return this.nodeId; } public long nextId() { - return this.huffNodeId++; + return this.nodeId++; } } diff --git a/src/main/java/com/hashbangbash/trie/NodePair.java b/src/main/java/com/hashbangbash/trie/NodePair.java new file mode 100644 index 0000000..1676ff7 --- /dev/null +++ b/src/main/java/com/hashbangbash/trie/NodePair.java @@ -0,0 +1,28 @@ +package com.hashbangbash.trie; + +public class NodePair { + private String name; + private PathNode connection; + + public NodePair(String name, PathNode connection) { + this.name = name; + this.connection = connection; + } + + public String getName() { + return this.name; + } + + public PathNode getConnection() { + return this.connection; + } + + public void setConnection(PathNode connection) { + this.connection = connection; + } + + public String toString() { + return "Name: " + name + ", Connection: " + connection.getId(); + } +} + diff --git a/src/main/java/com/hashbangbash/trie/PathNode.java b/src/main/java/com/hashbangbash/trie/PathNode.java new file mode 100644 index 0000000..c4fa258 --- /dev/null +++ b/src/main/java/com/hashbangbash/trie/PathNode.java @@ -0,0 +1,86 @@ +package com.hashbangbash.trie; + +import java.util.ArrayList; +import java.util.List; + +public class PathNode { + private long id = 0; + private List children = new ArrayList(); + private List parents = new ArrayList(); + private NodeContext ctx = null; + + public PathNode() { + this(new NodeContext()); + } + + public PathNode(NodeContext ctx) { + this.ctx = ctx; + this.id = this.ctx.nextId(); + } + + public long getId() { + return this.id; + } + + void addChild(NodePair cp) { + this.children.add(cp); + } + + void addParent(PathNode cp) { + if (!parents.contains(cp)) { + this.parents.add(cp); + } + } + + public List getChildren() { + return this.children; + } + + List getParents() { + return this.parents; + } + + void setParents(List parents) { + this.parents = parents; + } + + void addParents(List parents) { + for (PathNode pn : parents) { + addParent(pn); + } + } + + /* + * same number of children with the same names for child nodes + */ + boolean isEquivalentTo(PathNode that) { + if (this.getChildren().size() != that.getChildren().size()) { + return false; + } + + for (NodePair thisnp : this.getChildren()) { + boolean found = false; + for (NodePair thatnp : that.getChildren()) { + if (thisnp.getName().equals(thatnp.getName())) { + found = true; + break; + } + } + + if (!found) { + return false; + } + } + return true; + } + + public String toString() { + String parentList = ""; + for (PathNode parent : parents) { + parentList += ": " + parent.getId(); + } + parentList += ""; + return "ID: " + id + ", Parents" + parentList + ", Children: " + children; + } +} +