Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class QueueArrayBuffer<T> {
- public abstract class Iterator {
- private int i = 0;
- public abstract void next(final T element);
- public final void delete_current() {
- delete(i);
- }
- }
- private final T[] buffer;
- private final int size;
- private int first = 0;
- private int last = 0;
- @SuppressWarnings("unchecked")
- public QueueArrayBuffer(final int size) {
- this.size = size;
- buffer = (T[]) new Object[size];
- }
- public void put(final T element) {
- if ((last + 1) % size == first)
- delete(first);
- last = (last + 1) % size;
- buffer[last] = element;
- }
- private void delete(final int index) {
- buffer[index] = null;
- if (first == index)
- while (buffer[first] == null && first != last)
- first = (first + 1) % size;
- }
- public void iterate(final Iterator iterator) {
- iterator.i = first;
- while (iterator.i != last) {
- if (buffer[iterator.i] != null)
- iterator.next(buffer[iterator.i]);
- iterator.i = (iterator.i + 1) % size;
- }
- }
- }
Add Comment
Please, Sign In to add comment