Implemented retainAll().

git-svn-id: https://samskivert.googlecode.com/svn/trunk@742 6335cc39-0255-0410-8fd6-9bcaacd3b74c
This commit is contained in:
mdb
2002-05-16 20:50:31 +00:00
parent a88624fc81
commit a9cca659a4
2 changed files with 61 additions and 4 deletions
@@ -1,5 +1,5 @@
//
// $Id: ArrayIntSet.java,v 1.4 2002/04/18 00:09:17 mdb Exp $
// $Id: ArrayIntSet.java,v 1.5 2002/05/16 20:50:31 mdb Exp $
//
// samskivert library - useful routines for java programs
// Copyright (C) 2001 Michael Bayne
@@ -236,7 +236,32 @@ public class ArrayIntSet extends AbstractSet
// documentation inherited from interface
public boolean retainAll (Collection c)
{
throw new UnsupportedOperationException();
if (c instanceof ArrayIntSet) {
ArrayIntSet other = (ArrayIntSet)c;
// make a new array that will hold the union of our two arrays
// (this is preferable to the complicated process of repeated
// shrinking or swapping)
int[] combined = new int[Math.min(_values.length,
other._values.length)];
int nsize = 0;
for (int ii = 0; ii < _size; ii++) {
if (other.contains(_values[ii])) {
combined[nsize++] = _values[ii];
}
}
// keep track of whether or not things changed
boolean changed = (_size != nsize);
// stuff the new values into the right place
_values = combined;
_size = nsize;
return changed;
} else {
throw new UnsupportedOperationException();
}
}
// documentation inherited from interface
@@ -252,7 +277,12 @@ public class ArrayIntSet extends AbstractSet
if (o instanceof ArrayIntSet) {
ArrayIntSet other = (ArrayIntSet)o;
if (other._size == _size) {
return Arrays.equals(_values, other._values);
for (int ii = 0; ii < _size; ii++) {
if (_values[ii] != other._values[ii]) {
return false;
}
}
return true;
}
}
@@ -269,6 +299,14 @@ public class ArrayIntSet extends AbstractSet
return hashCode;
}
/**
* Returns a string representation of this instance.
*/
public String toString ()
{
return StringUtil.toString(iterator());
}
/**
* Performs a binary search on our values array, looking for the
* specified value. Swiped from <code>java.util.Arrays</code> because
@@ -1,5 +1,5 @@
//
// $Id: ArrayIntSetTest.java,v 1.2 2002/04/11 04:07:42 mdb Exp $
// $Id: ArrayIntSetTest.java,v 1.3 2002/05/16 20:50:31 mdb Exp $
//
// samskivert library - useful routines for java programs
// Copyright (C) 2001 Michael Bayne
@@ -49,6 +49,25 @@ public class ArrayIntSetTest extends TestCase
int[] setvals = set.toIntArray();
assertTrue("values equal", Arrays.equals(values, setvals));
ArrayIntSet set1 = new ArrayIntSet();
set1.add(new int[] { 1, 2, 3, 5, 7, 12, 19, 35 });
ArrayIntSet set2 = new ArrayIntSet();
set2.add(new int[] { 3, 4, 5, 11, 13, 17, 19, 25 });
ArrayIntSet set3 = new ArrayIntSet();
set3.add(new int[] { 3, 5, 19 });
// intersect sets 1 and 2; making sure that retainAll() returns
// true to indicate that set 1 was modified
assertTrue("retain modifies", set1.retainAll(set2));
// make sure the intersections were correct
assertTrue("intersection", set1.equals(set3));
// make sure that retainAll returns false if we do something that
// doesn't modify the set
assertTrue("retain didn't modify", !set1.retainAll(set3));
}
public static Test suite ()