2013-10-26 13:25:51 -04:00
|
|
|
package mekanism.common;
|
|
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
import java.util.Iterator;
|
|
|
|
|
|
|
|
|
|
public class HashList<T> implements Iterable<T>
|
|
|
|
|
{
|
2013-11-02 12:00:29 -04:00
|
|
|
private ArrayList<T> list = new ArrayList<T>(256);
|
2014-03-07 19:00:25 -06:00
|
|
|
|
2013-10-26 13:25:51 -04:00
|
|
|
public boolean contains(T obj)
|
|
|
|
|
{
|
|
|
|
|
return list.contains(obj);
|
|
|
|
|
}
|
2014-03-07 19:00:25 -06:00
|
|
|
|
2013-10-26 13:25:51 -04:00
|
|
|
public void clear()
|
|
|
|
|
{
|
|
|
|
|
list.clear();
|
|
|
|
|
}
|
2014-03-07 19:00:25 -06:00
|
|
|
|
2013-10-26 13:25:51 -04:00
|
|
|
public T get(int index)
|
|
|
|
|
{
|
2013-10-27 11:44:19 -04:00
|
|
|
if(index > size()-1)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2014-03-07 19:00:25 -06:00
|
|
|
|
2013-10-26 13:25:51 -04:00
|
|
|
return list.get(index);
|
|
|
|
|
}
|
2014-03-07 19:00:25 -06:00
|
|
|
|
2013-10-26 13:25:51 -04:00
|
|
|
public void add(T obj)
|
|
|
|
|
{
|
|
|
|
|
if(!list.contains(obj))
|
|
|
|
|
{
|
|
|
|
|
list.add(obj);
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-03-07 19:00:25 -06:00
|
|
|
|
2013-10-26 13:25:51 -04:00
|
|
|
public void add(int index, T obj)
|
|
|
|
|
{
|
|
|
|
|
if(!list.contains(obj))
|
|
|
|
|
{
|
2013-11-24 12:12:42 -06:00
|
|
|
if(index > size())
|
|
|
|
|
{
|
|
|
|
|
for(int i = size(); i <= index-1; i++)
|
|
|
|
|
{
|
|
|
|
|
list.add(i, null);
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-03-07 19:00:25 -06:00
|
|
|
|
2013-10-26 13:25:51 -04:00
|
|
|
list.add(index, obj);
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-03-07 19:00:25 -06:00
|
|
|
|
2013-10-29 18:54:36 -04:00
|
|
|
public boolean isEmpty()
|
2013-10-27 17:47:17 -04:00
|
|
|
{
|
2013-10-29 18:54:36 -04:00
|
|
|
return list.isEmpty();
|
2013-10-27 17:47:17 -04:00
|
|
|
}
|
2014-03-07 19:00:25 -06:00
|
|
|
|
2013-10-29 18:54:36 -04:00
|
|
|
public void remove(int index)
|
2013-10-28 14:42:28 -04:00
|
|
|
{
|
2013-10-29 18:54:36 -04:00
|
|
|
if(isEmpty() || index > size()-1)
|
2013-10-28 14:42:28 -04:00
|
|
|
{
|
2013-10-29 18:54:36 -04:00
|
|
|
return;
|
2013-10-28 14:42:28 -04:00
|
|
|
}
|
2014-03-07 19:00:25 -06:00
|
|
|
|
2013-10-29 18:54:36 -04:00
|
|
|
list.remove(index);
|
|
|
|
|
}
|
2014-03-07 19:00:25 -06:00
|
|
|
|
2013-10-29 18:54:36 -04:00
|
|
|
public void replace(int index, T obj)
|
|
|
|
|
{
|
|
|
|
|
if(get(index) != null)
|
2013-10-28 14:42:28 -04:00
|
|
|
{
|
|
|
|
|
remove(index);
|
|
|
|
|
}
|
2014-03-07 19:00:25 -06:00
|
|
|
|
2013-10-28 14:42:28 -04:00
|
|
|
add(index, obj);
|
|
|
|
|
}
|
2014-03-07 19:00:25 -06:00
|
|
|
|
2013-10-27 17:47:17 -04:00
|
|
|
public void remove(T obj)
|
|
|
|
|
{
|
|
|
|
|
list.remove(obj);
|
|
|
|
|
}
|
2014-03-07 19:00:25 -06:00
|
|
|
|
2013-10-26 13:25:51 -04:00
|
|
|
public int indexOf(T obj)
|
|
|
|
|
{
|
|
|
|
|
return list.indexOf(obj);
|
|
|
|
|
}
|
2014-03-07 19:00:25 -06:00
|
|
|
|
2013-10-26 13:25:51 -04:00
|
|
|
public int size()
|
|
|
|
|
{
|
|
|
|
|
return list.size();
|
|
|
|
|
}
|
2015-04-05 23:03:56 +10:00
|
|
|
|
2016-03-19 22:03:17 -04:00
|
|
|
public void swap(int source, int target)
|
2015-04-05 23:03:56 +10:00
|
|
|
{
|
2016-03-19 22:03:17 -04:00
|
|
|
// Make sure both source and target are legal values
|
|
|
|
|
if(source == target) return;
|
|
|
|
|
if(source < 0 || target < 0) return;
|
|
|
|
|
if(source >= list.size() || target >= list.size()) return;
|
2015-04-05 23:03:56 +10:00
|
|
|
|
|
|
|
|
// Perform swap
|
2016-03-19 22:03:17 -04:00
|
|
|
T temp = list.get(source);
|
|
|
|
|
list.set(source, list.get( target));
|
|
|
|
|
list.set(target, temp);
|
2015-04-05 23:03:56 +10:00
|
|
|
}
|
2014-03-07 19:00:25 -06:00
|
|
|
|
2013-10-28 14:42:28 -04:00
|
|
|
@Override
|
|
|
|
|
public int hashCode()
|
|
|
|
|
{
|
|
|
|
|
return list.hashCode();
|
|
|
|
|
}
|
2014-03-07 19:00:25 -06:00
|
|
|
|
2013-10-28 14:42:28 -04:00
|
|
|
@Override
|
|
|
|
|
public boolean equals(Object obj)
|
|
|
|
|
{
|
|
|
|
|
return list.equals(obj);
|
|
|
|
|
}
|
2014-03-07 19:00:25 -06:00
|
|
|
|
2013-10-26 13:25:51 -04:00
|
|
|
@Override
|
2014-03-07 19:00:25 -06:00
|
|
|
public Iterator iterator()
|
2013-10-26 13:25:51 -04:00
|
|
|
{
|
|
|
|
|
return list.iterator();
|
|
|
|
|
}
|
|
|
|
|
}
|