Advertisement
Guest User

IntRingBuffer

a guest
Feb 11th, 2011
275
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.30 KB | None | 0 0
  1. package mt3.util;
  2. /*************************************************************************
  3.  *  Compilation:  javac RingBuffer.java
  4.  *  Execution:    java RingBuffer
  5.  *  
  6.  *  Ring buffer (fixed size queue) implementation using a circular array
  7.  *  (array with wrap-around).
  8.  *  
  9.  *  Copyright (c) 2007, Robert Sedgewick and Kevin Wayne.
  10.  *  Edited by Chris Dennett (C.R.Dennett@2006.ljmu.ac.uk)
  11.  *
  12.  *************************************************************************/
  13.  
  14.  
  15. // suppress unchecked warnings in Java 1.5.0_6 and later
  16. @SuppressWarnings("unchecked")
  17.  
  18. public class IntRingBuffer {
  19.     private int[] a;             // queue elements
  20.     private int N = 0;           // number of elements on queue
  21.     private int first = 0;       // index of first element of queue
  22.     private int last  = 0;       // index of next available slot
  23.    
  24.     private int iter_i = 0;
  25.    
  26.  
  27.     // cast needed since no generic array creation in Java
  28.     public IntRingBuffer(int capacity) {
  29.         a = new int[capacity];
  30.         for (int i = 0; i < capacity; i++) {
  31.             a[i] = Integer.MIN_VALUE;
  32.         }
  33.     }
  34.  
  35.     public boolean isEmpty() { return N == 0; }
  36.     public int size()        { return N;      }
  37.  
  38.     public void enqueue(int item) {
  39.         if (N == a.length) { throw new RuntimeException("Ring buffer overflow"); }
  40.         a[last] = item;
  41.         last = (last + 1) % a.length;     // wrap-around
  42.         N++;
  43.     }
  44.  
  45.     // remove the least recently added item - doesn't check for underflow
  46.     public int dequeue() {
  47.         if (isEmpty()) { throw new RuntimeException("Ring buffer underflow"); }
  48.         int item = a[first];
  49.         a[first] = Integer.MIN_VALUE;                  // to help with garbage collection
  50.         N--;
  51.         first = (first + 1) % a.length;   // wrap-around
  52.         return item;
  53.     }
  54.    
  55.     public void iter_reset() {
  56.         iter_i = 0;
  57.     }
  58.    
  59.     public boolean contains(final int n) {
  60.         iter_reset();
  61.         int i = 0;
  62.         while ((i = iter_next()) != Integer.MIN_VALUE) {
  63.             if (i == n) return true;
  64.         }
  65.         return false;
  66.     }
  67.    
  68.     public int iter_next() {
  69.         if (!iter_hasNext()) return Integer.MIN_VALUE;
  70.         return a[iter_i++];    
  71.     }  
  72.    
  73.     public boolean iter_hasNext() {
  74.         return iter_i < N;
  75.     }
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement