Created an Interable interface.
ArrayIntSet was redefining some methods in the superclass (and doing the exact same thing). Fixed up, but left in slightly more efficient implementations if the other collection is Interable or an IntSet. git-svn-id: https://samskivert.googlecode.com/svn/trunk@1780 6335cc39-0255-0410-8fd6-9bcaacd3b74c
This commit is contained in:
@@ -285,39 +285,43 @@ public class ArrayIntSet extends AbstractSet
|
||||
// documentation inherited from interface
|
||||
public boolean containsAll (Collection c)
|
||||
{
|
||||
Iterator iter = c.iterator();
|
||||
while (iter.hasNext()) {
|
||||
if (!contains(iter.next())) {
|
||||
return false;
|
||||
if (c instanceof Interable) {
|
||||
Interator inter = ((Interable) c).interator();
|
||||
while (inter.hasNext()) {
|
||||
if (!contains(inter.nextInt())) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
|
||||
} else {
|
||||
return super.containsAll(c);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
// documentation inherited from interface
|
||||
public boolean addAll (Collection c)
|
||||
{
|
||||
boolean modified = false;
|
||||
if (c instanceof ArrayIntSet) {
|
||||
ArrayIntSet other = (ArrayIntSet)c;
|
||||
for (int ii = 0; ii < other._size; ii++) {
|
||||
modified = (add(other._values[ii]) || modified);
|
||||
if (c instanceof Interable) {
|
||||
Interator inter = ((Interable) c).interator();
|
||||
boolean modified = false;
|
||||
while (inter.hasNext()) {
|
||||
if (add(other._values[ii])) {
|
||||
modified = true;
|
||||
}
|
||||
}
|
||||
return modified;
|
||||
|
||||
} else {
|
||||
Iterator iter = c.iterator();
|
||||
while (iter.hasNext()) {
|
||||
modified = (add(iter.next()) || modified);
|
||||
}
|
||||
return super.addAll(c);
|
||||
}
|
||||
return modified;
|
||||
}
|
||||
|
||||
// documentation inherited from interface
|
||||
public boolean retainAll (Collection c)
|
||||
{
|
||||
if (c instanceof ArrayIntSet) {
|
||||
ArrayIntSet other = (ArrayIntSet)c;
|
||||
if (c instanceof IntSet) {
|
||||
IntSet other = (IntSet)c;
|
||||
int removals = 0;
|
||||
|
||||
// go through our array sliding all elements in the union
|
||||
@@ -337,7 +341,7 @@ public class ArrayIntSet extends AbstractSet
|
||||
return (removals > 0);
|
||||
|
||||
} else {
|
||||
throw new UnsupportedOperationException();
|
||||
return super.retainAll(c);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -355,6 +359,8 @@ public class ArrayIntSet extends AbstractSet
|
||||
ArrayIntSet other = (ArrayIntSet)o;
|
||||
if (other._size == _size) {
|
||||
for (int ii = 0; ii < _size; ii++) {
|
||||
// we can't use Arrays.equals() because we only want to
|
||||
// compare the first _size values
|
||||
if (_values[ii] != other._values[ii]) {
|
||||
return false;
|
||||
}
|
||||
@@ -381,8 +387,7 @@ public class ArrayIntSet extends AbstractSet
|
||||
{
|
||||
try {
|
||||
ArrayIntSet nset = (ArrayIntSet)super.clone();
|
||||
nset._values = new int[_values.length];
|
||||
System.arraycopy(_values, 0, nset._values, 0, _size);
|
||||
nset._values = _values.clone();
|
||||
return nset;
|
||||
|
||||
} catch (CloneNotSupportedException cnse) {
|
||||
|
||||
@@ -29,7 +29,7 @@ import java.util.Set;
|
||||
* provides all of the standard methods (in which <code>Integer</code>
|
||||
* objects will be converted to ints).
|
||||
*/
|
||||
public interface IntSet extends Set
|
||||
public interface IntSet extends Set, Interable
|
||||
{
|
||||
/**
|
||||
* Returns <tt>true</tt> if this set contains the specified element.
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
//
|
||||
// samskivert library - useful routines for java programs
|
||||
// Copyright (C) 2001-2006 Michael Bayne
|
||||
//
|
||||
// This library is free software; you can redistribute it and/or modify it
|
||||
// under the terms of the GNU Lesser General Public License as published
|
||||
// by the Free Software Foundation; either version 2.1 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// This library is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
// Lesser General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Lesser General Public
|
||||
// License along with this library; if not, write to the Free Software
|
||||
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
|
||||
package com.samskivert.util;
|
||||
|
||||
/**
|
||||
* An interface similar to java.util.Iterable.
|
||||
*/
|
||||
public interface Interable
|
||||
{
|
||||
/**
|
||||
* Return an Interator over the ints in this object.
|
||||
*/
|
||||
public Interator interator ();
|
||||
}
|
||||
Reference in New Issue
Block a user