Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- public class MultiSet<E> extends AbstractCollection<E> {
- private int count;
- private int tried;
- private int top;
- private int end;
- private Object[] set;
- public MultiSet() {
- this.set = new Object[50];
- this.count = 0;
- this.tried = 0;
- this.top = 0;
- this.end = 0;
- }
- public MultiSet(MultiSet m) {
- this.set = m.set;
- this.count = m.count;
- this.tried = m.tried;
- this.top = m.top;
- this.end = m.end;
- }
- public String toString() {
- String result = null;
- for(Object o: set) {
- result += o + " ";
- }
- return result;
- }
- public Iterator<E> iterator() {
- return new Iterator<E>() {
- public boolean hasNext() {
- return count > tried;
- }
- public E next() {
- int index = (top + tried) % set.length;
- E e = (E) set[index];
- tried++;
- return e;
- }
- public void remove() {
- throw new UnsupportedOperationException();
- }
- };
- }
- public E remove() {
- E e = (E) set[top];
- top = (top + 1) % set.length;
- count--;
- return e;
- }
- public boolean add(E e) {
- set[end] = e;
- end = (end + 1) % set.length;
- count++;
- return true;
- }
- public int size() {
- return this.count;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement