Advertisement
Guest User

Untitled

a guest
Dec 3rd, 2016
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.21 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. public class MultiSet<E> extends AbstractCollection<E> {
  4.    
  5.     private int count;
  6.     private int tried;
  7.     private int top;
  8.     private int end;
  9.     private Object[] set;
  10.    
  11.     public MultiSet() {
  12.         this.set = new Object[50];
  13.         this.count = 0;
  14.         this.tried = 0;
  15.         this.top = 0;  
  16.         this.end = 0;
  17.     }
  18.    
  19.     public MultiSet(MultiSet m) {
  20.         this.set = m.set;
  21.         this.count = m.count;
  22.         this.tried = m.tried;
  23.         this.top = m.top;
  24.         this.end = m.end;
  25.     }
  26.    
  27.     public String toString() {
  28.         String result = null;
  29.         for(Object o: set) {
  30.             result += o + " ";
  31.         }
  32.         return result;
  33.     }
  34.    
  35.     public Iterator<E> iterator() {
  36.        
  37.         return new Iterator<E>() {
  38.             public boolean hasNext() {
  39.                 return count > tried;
  40.             }
  41.            
  42.             public E next() {
  43.                 int index = (top + tried) % set.length;
  44.                 E e = (E) set[index];
  45.                 tried++;
  46.                 return e;
  47.             }
  48.            
  49.             public void remove() {
  50.                 throw new UnsupportedOperationException();
  51.             }
  52.         };
  53.        
  54.     }
  55.    
  56.     public E remove() {
  57.         E e = (E) set[top];
  58.         top = (top + 1) % set.length;
  59.         count--;
  60.         return e;
  61.     }
  62.    
  63.     public boolean add(E e) {
  64.         set[end] = e;
  65.         end = (end + 1) % set.length;
  66.         count++;
  67.         return true;
  68.     }
  69.     public int size() {
  70.         return this.count;
  71.     }
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement