From 3aba38b8de21478489ca0e293ea6ab208c1736bf Mon Sep 17 00:00:00 2001 From: ray Date: Mon, 6 Feb 2006 18:49:31 +0000 Subject: [PATCH] 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 --- src/java/com/samskivert/util/ArrayIntSet.java | 45 ++++++++++--------- src/java/com/samskivert/util/IntSet.java | 2 +- src/java/com/samskivert/util/Interable.java | 30 +++++++++++++ 3 files changed, 56 insertions(+), 21 deletions(-) create mode 100644 src/java/com/samskivert/util/Interable.java diff --git a/src/java/com/samskivert/util/ArrayIntSet.java b/src/java/com/samskivert/util/ArrayIntSet.java index 60bf6f4f..e1509170 100644 --- a/src/java/com/samskivert/util/ArrayIntSet.java +++ b/src/java/com/samskivert/util/ArrayIntSet.java @@ -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) { diff --git a/src/java/com/samskivert/util/IntSet.java b/src/java/com/samskivert/util/IntSet.java index 677278db..d74beaa2 100644 --- a/src/java/com/samskivert/util/IntSet.java +++ b/src/java/com/samskivert/util/IntSet.java @@ -29,7 +29,7 @@ import java.util.Set; * provides all of the standard methods (in which Integer * objects will be converted to ints). */ -public interface IntSet extends Set +public interface IntSet extends Set, Interable { /** * Returns true if this set contains the specified element. diff --git a/src/java/com/samskivert/util/Interable.java b/src/java/com/samskivert/util/Interable.java new file mode 100644 index 00000000..5fe55e52 --- /dev/null +++ b/src/java/com/samskivert/util/Interable.java @@ -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 (); +}