Skip to content
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

Generic Tree Addition #1

Open
wants to merge 8 commits 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
1 change: 0 additions & 1 deletion tree-edit-distance/.classpath
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,5 @@
<classpathentry kind="src" path="test"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6"/>
<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/4"/>
<classpathentry combineaccessrules="false" kind="src" path="/kyodai-parser"/>
<classpathentry kind="output" path="bin"/>
</classpath>
101 changes: 101 additions & 0 deletions tree-edit-distance/src/jrsmith/util/IdStore.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package jrsmith.util;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
* Collection that automatically assigns integer IDs to the items it contains.
* The first item stored in a IdStore is assigned the ID 0, and each ensuing
* items are stored sequentially. Items can be retrieved by their IDs.
*
* @author Joshua Smith
*
* @param <T>
* The type of the items stored in the IdStore.
*/
public class IdStore<T> {

/**
* The items in the IdStore. IDs of the items correspond to their indices.
*/
private List<T> items;

/**
* Creates an IdStore with no elements.
*/
public IdStore() {
this.items = new ArrayList<T>();
}

/**
* Creates an IdStore containing all elements in items.
*
* @param newItems
* the elements stored in the initial IdStore.
*/
public IdStore(final List<T> newItems) {
this.items = new ArrayList<T>(newItems);
}

/**
* Stores item in the IdStore, returning id assigned to that item. The item
* is assigned the lowest available ID greater than zero.
*
* @param item
* the element to store.
* @return the id assigned to item.
*/
public final int store(final T item) {
int id = items.size();
storing(item, id);
items.add(item);
return id;
}

/**
* Entry point will be called before item is added to the IdStore with ID
* id.
*
* @param id
* the id item will be assigned.
* @param item
* the item that will be added.
*/
protected void storing(final T item, final int id) {
}

/**
* Returns the item in the IdStore that was assigned id. An
* IndexOutOfBoundsException is thrown if id < 0 or id >= size().
*
* @param id
* The id assigned to the item being retrieved.
* @return the item which was assigned id.
*/
public final T getItem(final int id) {
if (id < 0 || id >= items.size()) {
throw new IndexOutOfBoundsException(
"IdStore does not contain an item at id " + id);
}
return items.get(id);
}

/**
* Returns the number of items in the IdStore.
*
* @return the number of items in the IdStore.
*/
public final int size() {
return items.size();
}

/**
* Returns all items in the IdStore in an unmodifiable list.
*
* @return all items in the IdStore in an unmodifiable list.
*/
public final List<T> items() {
return Collections.unmodifiableList(items);
}
}
105 changes: 105 additions & 0 deletions tree-edit-distance/src/jrsmith/util/LookupIdStore.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
package jrsmith.util;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.NoSuchElementException;

/**
* Collection that automatically assigns integer IDs to the items it contains.
* The first item stored in a LookupIdStore is assigned the ID 0, and each
* ensuing items are stored sequentially. Items can be retrieved by their IDs.
*
* In addition, IDs can be looked up by the items they are assigned to. This
* capability adds the restriction that equal items cannot be stored in a
* LookupIdStore.
*
* @author Joshua Smith
*
* @param <T>
* The type of the items stored in the LookupIdStore.
*/
public class LookupIdStore<T> extends IdStore<T> {

/**
* Lookup from items in IdStore to their IDs.
*/
private Map<T, Integer> ids;

/**
* Creates a LookupIdStore with no elements.
*/
public LookupIdStore() {
this(new ArrayList<T>(0));
}

/**
* Creates a LookupIdStore containing all elements in items. An
* IllegalArgumentException is thrown if items contains any duplicate
* elements
*
* @param items
* the elements stored in the initial IdStore.
*/
public LookupIdStore(final List<T> items) {
super(items);
this.ids = new HashMap<T, Integer>();
int index = 0;
for (T item : items) {
Integer oldValue = ids.put(item, index);
if (oldValue != null) {
throw new IllegalArgumentException(
"LookupIdStore can not store duplicate items.");
}
index++;
}
}

/**
* Adds the (item, id) pair to the IDs lookup map. An
* IllegalArgumentException is thrown if the LookupIdStore already contains
* an element equal to item.
*
* @param id
* the id item will be assigned.
* @param item
* the item that will be added.
*/
@Override
protected final void storing(final T item, final int id) {
if (ids.containsKey(item)) {
throw new IllegalArgumentException(
"LookupIdStore can not store duplicate items.");
}
ids.put(item, id);
}

/**
* Retrieves the ID assigned to item. If item was never added to the
* LookupIdStore, a NoSuchElementException is thrown.
*
* @param item
* the item whose ID is being retrieved.
* @return the ID assigned to item.
*/
public final int getId(final T item) {
if (!contains(item)) {
throw new NoSuchElementException(
"LookupIdStore does not contain item");
}
return ids.get(item);
}

/**
* Returns true if item is in the LookupIdStore, false otherwise.
*
* @param item
* the item to check the existence of.
* @return true if item is in the LookupIdStore, false otherwise.
*/
public final boolean contains(final T item) {
return ids.containsKey(item);
}

}
6 changes: 6 additions & 0 deletions tree-edit-distance/src/jrsmith/util/package-info.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/**
* A collection of useful reusable classes that are not related to
* any particular application.
*/
package jrsmith.util;

Loading