package sets.multysets; import java.util.*; public class MultySetClass<E> implements MultySet<E> { public class Couple { private int card; private E element; public Couple(int c, E e) { card = c; element = e; } /* public Object copy() { return new Couple(card, element); } */ public boolean equals(Couple c) { return this.element.equals(c.element); } public String toString() { return "( " + element.toString() + " - " + card + " )"; } } private class SetIterator implements Iterator<E> { int card = 0; E element = null; Iterator<Couple> it = body.iterator(); public boolean hasNext() { return card != 0 || it.hasNext(); } public E next() { if(card > 0) card--; else { Couple c = it.next(); card = c.card - 1; element = c.element; } return element; } public void remove() { throw new UnsupportedOperationException(); } } /* Vector body holds the elements of the multyset */ private Vector<Couple> body = new Vector<Couple>(); public MultySetClass() { } public MultySetClass(E []array) { for(E a: array) { this.add(a, 1); } } private MultySetClass(Vector<Couple> v) { body = v; } public boolean isEmpty() { return body.isEmpty(); } public void clear() { body.clear(); } public Iterator<E> iterator() { return new SetIterator(); } public Iterator<Couple> coupleIterator() { //return new CoupleIterator(); return body.iterator(); } public int size() { Iterator<Couple> it = coupleIterator(); int count = 0; while(it.hasNext()){ count += it.next().card; } return count; } public MultySetClass<E> clone() { MultySetClass<E> result = null; try { result = (MultySetClass<E>) super.clone(); } catch (CloneNotSupportedException e) { e.printStackTrace(); } result.body = (Vector<Couple>) body.clone(); return result; } public void add(E el, int card) { Couple cNew = new Couple(card, el); Iterator<Couple> it = body.iterator(); Couple c; int index = 0; while(it.hasNext()){ c = it.next(); if(c.equals(cNew)) { body.set(index, new Couple(cNew.card+c.card, c.element)); return; } index++; } body.add(cNew); } public int remove(E el) { int result = 0; Iterator<Couple> it = body.iterator(); Couple c = new Couple(1, el), tmp; while(it.hasNext()) { tmp = it.next(); if(tmp.equals(c)) { result = tmp.card; it.remove(); } break; } return result; } public int contains(E el){ Iterator<Couple> it = coupleIterator(); Couple c = new Couple(1, el), tmp; while(it.hasNext()){ tmp = it.next(); if(c.equals(tmp)) return tmp.card; } return 0; } public boolean equals(Object other) { if(this == other) return true; if(!(other instanceof MultySetClass)) return false; MultySetClass<E> mSet = (MultySetClass<E>)other; Iterator<Couple> it = coupleIterator(); Couple c; while(it.hasNext()) { if(mSet.contains((c = it.next()).element) != c.card) return false; } Iterator<Couple> it1 = mSet.coupleIterator(); while(it1.hasNext()) { if(this.contains((c = it1.next()).element) != c.card) return false; } return true; } public String toString() { if(isEmpty()) return "EMPTY"; StringBuilder buffer = new StringBuilder(); Iterator<Couple> it = coupleIterator(); Couple tmp; while(it.hasNext()) { tmp = it.next(); buffer.append(tmp.toString()); if(it.hasNext()) buffer.append(" , "); } return buffer.toString(); } public boolean SubSetOf(MultySet<E> other) { Iterator<Couple> it = coupleIterator(); Couple c; while(it.hasNext()) { c = it.next(); if(other.contains(c.element) == 0) return false; } return true; } public MultySet<E> union(MultySet<E> other) { MultySetClass<E> result = this.clone(); Iterator<Couple> it = ((MultySetClass)other).coupleIterator(); Couple c; while(it.hasNext()) { c = it.next(); result.add(c.element, c.card); } return result; } public MultySet<E> intersection(MultySet<E> other) { MultySetClass<E> result = new MultySetClass<E>(); Iterator<Couple> it = coupleIterator(); Couple c; int n; while(it.hasNext()) { c = it.next(); n = other.contains(c.element); if(n != 0) result.add(c.element, Math.min(c.card, n)); } return result; } public MultySet<E> difference(MultySet<E> other) { MultySetClass<E> result = new MultySetClass<E>(); Iterator<Couple> it = coupleIterator(); Couple c; while(it.hasNext()) { c = it.next(); if(other.contains(c.element) != 0) result.add(c.element, c.card); } return result; } }