Factored some bits into AbstractIntSet, added keySet() to IntIntMap.
git-svn-id: https://samskivert.googlecode.com/svn/trunk@2583 6335cc39-0255-0410-8fd6-9bcaacd3b74c
This commit is contained in:
@@ -0,0 +1,61 @@
|
|||||||
|
//
|
||||||
|
// $Id$
|
||||||
|
//
|
||||||
|
// samskivert library - useful routines for java programs
|
||||||
|
// Copyright (C) 2001-2008 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;
|
||||||
|
|
||||||
|
import java.util.AbstractSet;
|
||||||
|
import java.util.Iterator;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A base class for {@link IntSet} implementations.
|
||||||
|
*/
|
||||||
|
public abstract class AbstractIntSet extends AbstractSet<Integer>
|
||||||
|
implements IntSet
|
||||||
|
{
|
||||||
|
@Override // from AbstractSet
|
||||||
|
public Iterator<Integer> iterator ()
|
||||||
|
{
|
||||||
|
return interator();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override // from AbstractSet
|
||||||
|
public boolean add (int t)
|
||||||
|
{
|
||||||
|
throw new UnsupportedOperationException();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates an iterator that provides access to our int values without unboxing.
|
||||||
|
*/
|
||||||
|
public abstract Interator interator ();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Converts the contents of this set to an int array.
|
||||||
|
*/
|
||||||
|
public int[] toIntArray ()
|
||||||
|
{
|
||||||
|
int[] vals = new int[size()];
|
||||||
|
int ii=0;
|
||||||
|
for (Interator intr = interator(); intr.hasNext(); ) {
|
||||||
|
vals[ii++] = intr.nextInt();
|
||||||
|
}
|
||||||
|
return vals;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -391,72 +391,50 @@ public class HashIntMap<V> extends AbstractMap<Integer,V>
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
protected class IntKeySet extends AbstractSet<Integer>
|
|
||||||
implements IntSet
|
|
||||||
{
|
|
||||||
@Override public Iterator<Integer> iterator () {
|
|
||||||
return interator();
|
|
||||||
}
|
|
||||||
|
|
||||||
public Interator interator () {
|
|
||||||
return new Interator () {
|
|
||||||
public boolean hasNext () {
|
|
||||||
return i.hasNext();
|
|
||||||
}
|
|
||||||
public Integer next () {
|
|
||||||
return i.next().getKey();
|
|
||||||
}
|
|
||||||
public int nextInt () {
|
|
||||||
return i.next().getIntKey();
|
|
||||||
}
|
|
||||||
public void remove () {
|
|
||||||
i.remove();
|
|
||||||
}
|
|
||||||
private Iterator<IntEntry<V>> i = intEntrySet().iterator();
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override public int size () {
|
|
||||||
return HashIntMap.this.size();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override public boolean contains (Object t) {
|
|
||||||
return HashIntMap.this.containsKey(t);
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean contains (int t) {
|
|
||||||
return HashIntMap.this.containsKey(t);
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean add (int t) {
|
|
||||||
throw new UnsupportedOperationException();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override public boolean remove (Object o) {
|
|
||||||
return (null != HashIntMap.this.remove(o));
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean remove (int value) {
|
|
||||||
return (null != HashIntMap.this.remove(value));
|
|
||||||
}
|
|
||||||
|
|
||||||
public int[] toIntArray () {
|
|
||||||
int[] vals = new int[size()];
|
|
||||||
int ii=0;
|
|
||||||
for (Interator intr = interator(); intr.hasNext(); ) {
|
|
||||||
vals[ii++] = intr.nextInt();
|
|
||||||
}
|
|
||||||
return vals;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// documentation inherited from interface IntMap
|
// documentation inherited from interface IntMap
|
||||||
public IntSet intKeySet ()
|
public IntSet intKeySet ()
|
||||||
{
|
{
|
||||||
// damn Sun bastards made the 'keySet' variable with default access,
|
// damn Sun bastards made the 'keySet' variable with default access, so we can't share it
|
||||||
// so we can't share it
|
|
||||||
if (_keySet == null) {
|
if (_keySet == null) {
|
||||||
_keySet = new IntKeySet();
|
_keySet = new AbstractIntSet() {
|
||||||
|
public Interator interator () {
|
||||||
|
return new Interator () {
|
||||||
|
public boolean hasNext () {
|
||||||
|
return i.hasNext();
|
||||||
|
}
|
||||||
|
public Integer next () {
|
||||||
|
return i.next().getKey();
|
||||||
|
}
|
||||||
|
public int nextInt () {
|
||||||
|
return i.next().getIntKey();
|
||||||
|
}
|
||||||
|
public void remove () {
|
||||||
|
i.remove();
|
||||||
|
}
|
||||||
|
private Iterator<IntEntry<V>> i = intEntrySet().iterator();
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override public int size () {
|
||||||
|
return HashIntMap.this.size();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override public boolean contains (Object t) {
|
||||||
|
return HashIntMap.this.containsKey(t);
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean contains (int t) {
|
||||||
|
return HashIntMap.this.containsKey(t);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override public boolean remove (Object o) {
|
||||||
|
return (null != HashIntMap.this.remove(o));
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean remove (int value) {
|
||||||
|
return (null != HashIntMap.this.remove(value));
|
||||||
|
}
|
||||||
|
};
|
||||||
}
|
}
|
||||||
return _keySet;
|
return _keySet;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -309,6 +309,44 @@ public class IntIntMap
|
|||||||
return new KeyValueInterator(true, new IntEntryIterator());
|
return new KeyValueInterator(true, new IntEntryIterator());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public IntSet keySet ()
|
||||||
|
{
|
||||||
|
return new AbstractIntSet() {
|
||||||
|
public Interator interator () {
|
||||||
|
return IntIntMap.this.keys();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override public int size () {
|
||||||
|
return IntIntMap.this.size();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override public boolean contains (Object t) {
|
||||||
|
return (t instanceof Integer) ? IntIntMap.this.containsKey((Integer)t) : false;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean contains (int t) {
|
||||||
|
return IntIntMap.this.containsKey(t);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override public boolean remove (Object o) {
|
||||||
|
if (!(o instanceof Integer)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return remove((Integer)o);
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean remove (int value) {
|
||||||
|
// we have to check for presence in the map separately because we have no "not in
|
||||||
|
// the set" return value
|
||||||
|
if (!IntIntMap.this.containsKey(value)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
IntIntMap.this.remove(value);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
public Interator values ()
|
public Interator values ()
|
||||||
{
|
{
|
||||||
return new KeyValueInterator(false, new IntEntryIterator());
|
return new KeyValueInterator(false, new IntEntryIterator());
|
||||||
|
|||||||
@@ -23,8 +23,8 @@ package com.samskivert.util;
|
|||||||
import java.util.Iterator;
|
import java.util.Iterator;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Can be used as an Iterator, and all Objects returned should be Integer
|
* Can be used as an Iterator, and all Objects returned should be Integer objects, but can also can
|
||||||
* objects, but can also can avoid object creation by calling nextInt().
|
* avoid boxing by calling {@link #nextInt}.
|
||||||
*/
|
*/
|
||||||
public interface Interator extends Iterator<Integer>
|
public interface Interator extends Iterator<Integer>
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user