Switched to more efficient Ray-go-rithm for computing in-place

intersection.


git-svn-id: https://samskivert.googlecode.com/svn/trunk@747 6335cc39-0255-0410-8fd6-9bcaacd3b74c
This commit is contained in:
mdb
2002-05-16 22:15:08 +00:00
parent ca062718c6
commit 0f91263c38
@@ -1,5 +1,5 @@
// //
// $Id: ArrayIntSet.java,v 1.6 2002/05/16 20:53:54 mdb Exp $ // $Id: ArrayIntSet.java,v 1.7 2002/05/16 22:15:08 mdb Exp $
// //
// samskivert library - useful routines for java programs // samskivert library - useful routines for java programs
// Copyright (C) 2001 Michael Bayne // Copyright (C) 2001 Michael Bayne
@@ -238,26 +238,23 @@ public class ArrayIntSet extends AbstractSet
{ {
if (c instanceof ArrayIntSet) { if (c instanceof ArrayIntSet) {
ArrayIntSet other = (ArrayIntSet)c; ArrayIntSet other = (ArrayIntSet)c;
// make a new array that will hold the union of our two arrays int removals = 0;
// (this is preferable to the complicated process of repeated
// shrinking or swapping) // go through our array sliding all elements in the union
int[] combined = new int[Math.min(_values.length, // toward the front; overwriting any elements that were to be
other._values.length)]; // removed
int nsize = 0;
for (int ii = 0; ii < _size; ii++) { for (int ii = 0; ii < _size; ii++) {
if (other.contains(_values[ii])) { if (other.contains(_values[ii])) {
combined[nsize++] = _values[ii]; if (removals != 0) {
_values[ii - removals] = _values[ii];
}
} else {
removals++;
} }
} }
// keep track of whether or not things changed _size = _size - removals;
boolean changed = (_size != nsize); return (removals > 0);
// stuff the new values into the right place
_values = combined;
_size = nsize;
return changed;
} else { } else {
throw new UnsupportedOperationException(); throw new UnsupportedOperationException();