Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Project setup. No real functionality.
- Loading branch information
maxgarfinkel
committed
Aug 28, 2012
1 parent
7c760d4
commit 0819613
Showing
15 changed files
with
536 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<classpath> | ||
<classpathentry kind="src" output="target/classes" path="src/main/java"/> | ||
<classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources"/> | ||
<classpathentry kind="src" output="target/test-classes" path="src/test/java"/> | ||
<classpathentry excluding="**" kind="src" output="target/test-classes" path="src/test/resources"/> | ||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/J2SE-1.5"/> | ||
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER"/> | ||
<classpathentry kind="lib" path="/Users/maxgarfinkel/.m2/repository/junit/junit/4.8.2/junit-4.8.2.jar" sourcepath="/Users/maxgarfinkel/.m2/repository/junit/junit/4.8.2/junit-4.8.2-sources.jar"> | ||
<attributes> | ||
<attribute name="javadoc_location" value="jar:file:/Users/maxgarfinkel/.m2/repository/junit/junit/4.8.2/junit-4.8.2-javadoc.jar!/"/> | ||
<attribute name="maven.groupId" value="junit"/> | ||
<attribute name="maven.artifactId" value="junit"/> | ||
<attribute name="maven.version" value="4.8.2"/> | ||
<attribute name="maven.scope" value="test"/> | ||
</attributes> | ||
</classpathentry> | ||
<classpathentry kind="output" path="target/classes"/> | ||
</classpath> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<projectDescription> | ||
<name>suffixTree</name> | ||
<comment></comment> | ||
<projects> | ||
</projects> | ||
<buildSpec> | ||
<buildCommand> | ||
<name>org.eclipse.jdt.core.javabuilder</name> | ||
<arguments> | ||
</arguments> | ||
</buildCommand> | ||
<buildCommand> | ||
<name>org.eclipse.m2e.core.maven2Builder</name> | ||
<arguments> | ||
</arguments> | ||
</buildCommand> | ||
</buildSpec> | ||
<natures> | ||
<nature>org.eclipse.jdt.core.javanature</nature> | ||
<nature>org.eclipse.m2e.core.maven2Nature</nature> | ||
</natures> | ||
</projectDescription> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
#Tue Aug 28 20:37:12 BST 2012 | ||
eclipse.preferences.version=1 | ||
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.5 | ||
org.eclipse.jdt.core.compiler.compliance=1.5 | ||
org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning | ||
org.eclipse.jdt.core.compiler.source=1.5 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
#Tue Aug 28 20:37:12 BST 2012 | ||
activeProfiles= | ||
eclipse.preferences.version=1 | ||
resolveWorkspaceProjects=true | ||
version=1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
<groupId>com.maxgarfinkel</groupId> | ||
<artifactId>suffixTree</artifactId> | ||
<version>0.0.1-SNAPSHOT</version> | ||
<dependencies> | ||
<dependency> | ||
<groupId>org.hamcrest</groupId> | ||
<artifactId>hamcrest-all</artifactId> | ||
<version>1.3</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>log4j</groupId> | ||
<artifactId>log4j</artifactId> | ||
<version>1.2.17</version> | ||
</dependency> | ||
</dependencies> | ||
<build> | ||
<plugins> | ||
<plugin> | ||
|
||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-eclipse-plugin</artifactId> | ||
<configuration> | ||
<downloadSources>true</downloadSources> | ||
<downloadJavadocs>true</downloadJavadocs> | ||
</configuration> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
</project> |
36 changes: 36 additions & 0 deletions
36
src/main/java/com/maxgarfinkel/suffixTree/ActivePoint.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package com.maxgarfinkel.suffixTree; | ||
|
||
class ActivePoint<T> { | ||
private Node<T> activeNode; | ||
private Edge<T> activeEdge; | ||
private int activeLength; | ||
|
||
ActivePoint(Node<T> root){ | ||
activeNode = root; | ||
activeEdge = null; | ||
activeLength = 0; | ||
} | ||
|
||
/** | ||
* Inserts a suffix at the active point. | ||
* @param suffix the suffix to be inserted at the active point | ||
* @return true if suffix was explicitly inserted, false otherwise. | ||
*/ | ||
boolean insertSuffix(Suffix suffix){ | ||
boolean isExplicitInsert = false; | ||
if(activeEdge != null){ | ||
//insert on the edge | ||
isExplicitInsert = activeEdge.insert(suffix, this); | ||
}else{ | ||
//insert on the node | ||
isExplicitInsert = activeNode.insert(suffix, this); | ||
} | ||
return false; | ||
} | ||
|
||
void setPosition(Node<T> node, Edge<T> edge, int length){ | ||
activeNode = node; | ||
activeEdge = edge; | ||
activeLength = length; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package com.maxgarfinkel.suffixTree; | ||
|
||
public class Algorithms { | ||
|
||
public <T> boolean containsSuffix(Object[] suffix, SuffixTree<T> tree){ | ||
suffix = Utils.addLeafToSequence(suffix, Leaf.getInstance()); | ||
Node<T> node = tree.getRoot(); | ||
Edge<T> edge = null; | ||
int position = 0; | ||
for(Object item : suffix){ | ||
position++; | ||
if(edge == null){ | ||
Edge<T> e = node.getEdgeStarting(item); | ||
if(e != null){ | ||
edge = e; | ||
position = 0; | ||
}else{ | ||
return false; | ||
} | ||
}else{ | ||
int length = edge.getLength(); | ||
if(position == length && edge.isTerminating()){ | ||
node = edge.getTerminal(); | ||
Edge<T> e = node.getEdgeStarting(item); | ||
if(e != null){ | ||
edge = e; | ||
position = 0; | ||
} | ||
}else if(!edge.getItemAt(position).equals(item)) | ||
return false; | ||
} | ||
} | ||
|
||
if(edge == null || edge.getItemAt(position) == null ) | ||
return false; | ||
else if(edge.isTerminating()) | ||
return false; | ||
else | ||
return true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package com.maxgarfinkel.suffixTree; | ||
|
||
class Edge<T> { | ||
private final int start; | ||
private int end = -1; | ||
private final Node<T> parentNode; | ||
private final Object[] sequence; | ||
private Node<T> terminal = null; | ||
private SuffixTree<T> tree = null; | ||
|
||
/** | ||
* Create a new <code>Edge</code> object. | ||
* @param start The position in the master sequence of the first item in this suffix. | ||
* @param parent The parent {@link Node} | ||
* @param sequence The master sequence which the {@link SuffixTree} indexes. | ||
* @param tree The master {@link SuffixTree} containing the root element which this edge is a child of. | ||
*/ | ||
Edge(int start, Node<T> parent, Object[] sequence, SuffixTree<T> tree){ | ||
this.start = start; | ||
this.parentNode = parent; | ||
this.sequence = sequence; | ||
this.tree = tree; | ||
} | ||
|
||
boolean isStarting(Object item){ | ||
return sequence[start].equals(item); | ||
} | ||
|
||
/** | ||
* | ||
* @param suffix | ||
* @param activePoint | ||
* @return | ||
*/ | ||
boolean insert(Suffix<T> suffix, ActivePoint<T> activePoint) { | ||
// TODO Auto-generated method stub | ||
return false; | ||
} | ||
|
||
int getEnd(){ | ||
return end != -1 ? end : tree.getCurrentEnd(); | ||
} | ||
|
||
boolean isTerminating(){ | ||
return terminal != null; | ||
} | ||
|
||
int getLength() { | ||
int realEnd = getEnd(); | ||
return realEnd - start; | ||
} | ||
|
||
Node<T> getTerminal(){ | ||
return terminal; | ||
} | ||
|
||
T getItemAt(int position){ | ||
return (T) sequence[start+position]; | ||
} | ||
|
||
T getStartItem(){ | ||
return (T) sequence[start]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package com.maxgarfinkel.suffixTree; | ||
|
||
class Leaf { | ||
|
||
private static Leaf leafInstance = null; | ||
|
||
static Leaf getInstance(){ | ||
if(leafInstance == null) | ||
leafInstance = new Leaf(); | ||
return leafInstance; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o){ | ||
return Leaf.class.isInstance(o); | ||
} | ||
|
||
@Override | ||
public String toString(){ | ||
return "$"; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package com.maxgarfinkel.suffixTree; | ||
|
||
import java.util.HashMap; | ||
import java.util.Iterator; | ||
import java.util.Map; | ||
|
||
class Node<T> implements Iterable<Edge<T>>{ | ||
private final Map<T,Edge<T>> edges = new HashMap<T,Edge<T>>(); | ||
private final Edge<T> incomingEdge; | ||
private final Object[] sequence; | ||
private final SuffixTree<T> tree; | ||
private Node<T> link = null; | ||
|
||
Node(Edge<T> incomingEdge, Object[] sequence, SuffixTree<T> tree){ | ||
this.incomingEdge = incomingEdge; | ||
this.sequence = sequence; | ||
this.tree = tree; | ||
} | ||
|
||
boolean insert(Suffix<T> suffix, ActivePoint<T> activePoint){ | ||
if(edges.containsKey(suffix.getEndItem())){ | ||
Edge<T> edge = edges.get(suffix.getEndItem()); | ||
activePoint.setPosition(this, edge, 1); | ||
return false; | ||
}else{ | ||
Edge<T> edge = new Edge<T>(suffix.getEndPosition(), this, sequence, tree); | ||
edges.put(edge.getStartItem(),edge); | ||
return true; | ||
} | ||
} | ||
|
||
Edge<T> getEdgeStarting(Object item){ | ||
return edges.get(item); | ||
} | ||
|
||
boolean hasSuffixLink(){ | ||
return link != null; | ||
} | ||
|
||
int getEdgeCount(){ | ||
return edges.size(); | ||
|
||
} | ||
|
||
public Iterator<Edge<T>> iterator() { | ||
return edges.values().iterator(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package com.maxgarfinkel.suffixTree; | ||
|
||
class Suffix<T> { | ||
private int start; | ||
private int end; | ||
private Object[] sequence; | ||
|
||
public Suffix(int currentEnd, int remainder, Object[] sequence) { | ||
start = currentEnd-(remainder-1); | ||
end = currentEnd; | ||
this.sequence = sequence; | ||
} | ||
|
||
@Override | ||
public String toString(){ | ||
StringBuilder sb = new StringBuilder(); | ||
int end = getEndPosition(); | ||
for(int i = start; i < end; i++){ | ||
sb.append(sequence[i]).append(","); | ||
} | ||
return sb.toString(); | ||
} | ||
|
||
/** | ||
* | ||
* @return The position in the master sequence of the end item in this suffix. | ||
* This value is inclusive, thus and end of 0 implies the suffix contains only | ||
* the item at <code>sequence[0]</code> | ||
*/ | ||
int getEndPosition(){ | ||
return end; | ||
} | ||
|
||
Object getEndItem(){ | ||
return sequence[end]; | ||
} | ||
} |
Oops, something went wrong.