We must assign the _size when setting the array like this.
Also: added duplicate-removing code to preserve Set-ness. Suddenly this is all looking like a "code-weirding" optimization. git-svn-id: https://samskivert.googlecode.com/svn/trunk@2557 6335cc39-0255-0410-8fd6-9bcaacd3b74c
This commit is contained in:
@@ -41,8 +41,24 @@ public class ArrayIntSet extends AbstractSet<Integer>
|
|||||||
public ArrayIntSet (int[] values)
|
public ArrayIntSet (int[] values)
|
||||||
{
|
{
|
||||||
this(values.length);
|
this(values.length);
|
||||||
System.arraycopy(values, 0, _values, 0, values.length);
|
_size = values.length;
|
||||||
|
System.arraycopy(values, 0, _values, 0, _size);
|
||||||
Arrays.sort(_values);
|
Arrays.sort(_values);
|
||||||
|
|
||||||
|
// remove duplicates
|
||||||
|
if (_size > 1) {
|
||||||
|
int last = _values[0];
|
||||||
|
for (int ii = 1; ii < _size; ) {
|
||||||
|
if (values[ii] == last) {
|
||||||
|
// shift everything down 1
|
||||||
|
_size--;
|
||||||
|
System.arraycopy(_values, ii + 1, _values, ii, _size - ii);
|
||||||
|
|
||||||
|
} else {
|
||||||
|
last = values[ii++];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in New Issue
Block a user