From e46cf92d686d9edb4853b19dc66c8efa304fe3c0 Mon Sep 17 00:00:00 2001 From: jochen Date: Sun, 27 Jun 1999 21:05:07 +0000 Subject: [PATCH] Use java 2 collection classes git-svn-id: https://svn.code.sf.net/p/jode/code/trunk@937 379699f6-c40d-0410-875b-85095c16579e --- jode/jode/util/SimpleSet.java | 131 ++++++++-------------------------- 1 file changed, 30 insertions(+), 101 deletions(-) diff --git a/jode/jode/util/SimpleSet.java b/jode/jode/util/SimpleSet.java index f43d8a8..f168b04 100644 --- a/jode/jode/util/SimpleSet.java +++ b/jode/jode/util/SimpleSet.java @@ -18,24 +18,26 @@ */ package jode.util; -import java.util.Dictionary; -import java.util.Enumeration; ///#ifdef JDK12 ///import java.util.AbstractSet; ///import java.util.Iterator; ///#endif -public class SimpleSet -///#ifdef JDK12 -/// extends AbstractSet -///#endif - implements Cloneable +public class SimpleSet extends AbstractSet implements Cloneable { - Object[] elementObjects = new Object[2]; + Object[] elementObjects; int count = 0; + public SimpleSet() { + this(2); + } + + public SimpleSet(int initialSize) { + elementObjects = new Object[initialSize]; + } + public int size() { - return count; + return count; } public boolean add(Object element) { @@ -56,10 +58,6 @@ public class SimpleSet return true; } - public Enumeration elements() { - return new ArrayEnum(count, elementObjects); - } - public Object clone() { try { SimpleSet other = (SimpleSet) super.clone(); @@ -70,94 +68,25 @@ public class SimpleSet } } -///#ifdef JDK12 -/// public Iterator iterator() { -/// return new Iterator() { -/// int pos = 0; -/// -/// public boolean hasNext() { -/// return pos < count; -/// } -/// -/// public Object next() { -/// return elementObjects[pos++]; -/// } -/// -/// public void remove() { -/// if (pos < count) -/// System.arraycopy(elementObjects, pos, -/// elementObjects, pos-1, count - pos); -/// count--; -/// pos--; -/// } -/// }; -/// } -///#else - - public boolean isEmpty() { - return count == 0; - } - - public boolean addAll(SimpleSet other) { - boolean changed = false; - for (int i=0; i < other.count; i++) { - changed |= add(other.elementObjects[i]); - } - return changed; - } - - public boolean contains(Object element) { - for (int i=0; i < count; i++) { - if (elementObjects[i].equals(element)) - return true; - } - return false; - } - - public boolean remove(Object element) { - for (int i=0; i< count; i++) { - if (elementObjects[i].equals(element)) { - count--; - if (i < count) - elementObjects[i] = elementObjects[count]; - return true; - } - } - return false; - } - - public boolean retainAll(SimpleSet other) { - int low = 0; - for (int high=0; high < count; high++) { - if (other.contains(elementObjects[high])) - elementObjects[low++] = elementObjects[high]; - } - if (count == low) - return false; - count = low; - return true; - } - - public boolean removeAll(SimpleSet other) { - int low = 0; - for (int high=0; high < count; high++) { - if (!other.contains(elementObjects[high])) - elementObjects[low++] = elementObjects[high]; - } - if (count == low) - return false; - count = low; - return true; - } + public Iterator iterator() { + return new Iterator() { + int pos = 0; - public String toString() { - StringBuffer sb = new StringBuffer("{"); - String komma = ""; - for (int i=0; i< count; i++) { - sb.append(komma).append(elementObjects[i].toString()); - komma = ", "; - } - return sb.append("}").toString(); + public boolean hasNext() { + return pos < count; + } + + public Object next() { + return elementObjects[pos++]; + } + + public void remove() { + if (pos < count) + System.arraycopy(elementObjects, pos, + elementObjects, pos-1, count - pos); + count--; + pos--; + } + }; } -///#endif }