KeyWrapper is pretty handy when you really just want a DSet stuffed
full of some pre-existing Streamable/Comparable type (e.g. String), so let's buff it up to let us do that cleanly, and publicize it. git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@5771 542714f4-19e9-0310-aa3c-eee0fc999fb1
This commit is contained in:
@@ -68,6 +68,24 @@ public class DSet<E extends DSet.Entry>
|
|||||||
Comparable<?> getKey ();
|
Comparable<?> getKey ();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A quick and easy DSet.Entry that holds some sort of Comparable.
|
||||||
|
*
|
||||||
|
* Remember: this type must also be {@link Streamable}.
|
||||||
|
*/
|
||||||
|
public static class KeyWrapper<T extends Comparable<?>> implements DSet.Entry
|
||||||
|
{
|
||||||
|
public KeyWrapper (T key) {
|
||||||
|
_key = key;
|
||||||
|
}
|
||||||
|
|
||||||
|
public T getKey () {
|
||||||
|
return _key;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected T _key;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a new DSet of the appropriate generic type.
|
* Creates a new DSet of the appropriate generic type.
|
||||||
*/
|
*/
|
||||||
@@ -138,9 +156,9 @@ public class DSet<E extends DSet.Entry>
|
|||||||
*/
|
*/
|
||||||
public DSet (E[] source)
|
public DSet (E[] source)
|
||||||
{
|
{
|
||||||
for (int ii = 0; ii < source.length; ii++) {
|
for (E element : source) {
|
||||||
if (source[ii] != null) {
|
if (element != null) {
|
||||||
add(source[ii]);
|
add(element);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -186,7 +204,8 @@ public class DSet<E extends DSet.Entry>
|
|||||||
public E get (Comparable<?> key)
|
public E get (Comparable<?> key)
|
||||||
{
|
{
|
||||||
// determine where we'll be adding the new element
|
// determine where we'll be adding the new element
|
||||||
int eidx = ArrayUtil.binarySearch(_entries, 0, _size, new KeyWrapper(key), ENTRY_COMP);
|
int eidx = ArrayUtil.binarySearch(
|
||||||
|
_entries, 0, _size, new KeyWrapper<Comparable<?>>(key), ENTRY_COMP);
|
||||||
return (eidx < 0) ? null : _entries[eidx];
|
return (eidx < 0) ? null : _entries[eidx];
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -371,7 +390,8 @@ public class DSet<E extends DSet.Entry>
|
|||||||
}
|
}
|
||||||
|
|
||||||
// look up this entry's position in our set
|
// look up this entry's position in our set
|
||||||
int eidx = ArrayUtil.binarySearch(_entries, 0, _size, new KeyWrapper(key), ENTRY_COMP);
|
int eidx = ArrayUtil.binarySearch(
|
||||||
|
_entries, 0, _size, new KeyWrapper<Comparable<?>>(key), ENTRY_COMP);
|
||||||
|
|
||||||
// if we found it, remove it
|
// if we found it, remove it
|
||||||
if (eidx >= 0) {
|
if (eidx >= 0) {
|
||||||
@@ -454,8 +474,7 @@ public class DSet<E extends DSet.Entry>
|
|||||||
{
|
{
|
||||||
StringBuilder buf = new StringBuilder("(");
|
StringBuilder buf = new StringBuilder("(");
|
||||||
String prefix = "";
|
String prefix = "";
|
||||||
for (int i = 0; i < _entries.length; i++) {
|
for (E elem : _entries) {
|
||||||
Entry elem = _entries[i];
|
|
||||||
if (elem != null) {
|
if (elem != null) {
|
||||||
buf.append(prefix);
|
buf.append(prefix);
|
||||||
prefix = ", ";
|
prefix = ", ";
|
||||||
@@ -494,18 +513,6 @@ public class DSet<E extends DSet.Entry>
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Used to search for keys. */
|
|
||||||
protected static class KeyWrapper implements DSet.Entry
|
|
||||||
{
|
|
||||||
public KeyWrapper (Comparable<?> key) {
|
|
||||||
_key = key;
|
|
||||||
}
|
|
||||||
public Comparable<?> getKey () {
|
|
||||||
return _key;
|
|
||||||
}
|
|
||||||
protected Comparable<?> _key;
|
|
||||||
}
|
|
||||||
|
|
||||||
/** The entries of the set (in a sparse array). */
|
/** The entries of the set (in a sparse array). */
|
||||||
@SuppressWarnings("unchecked") protected E[] _entries = (E[])new Entry[INITIAL_CAPACITY];
|
@SuppressWarnings("unchecked") protected E[] _entries = (E[])new Entry[INITIAL_CAPACITY];
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user