×

Welcome to TagMyCode

Please login or create account to add a snippet.
0
0
 
0
Language: Java
Posted by: Shamir Yona
Added: Sep 12, 2015 5:30 AM
Modified: Sep 13, 2015 1:13 PM
Views: 8
Tags: no tags
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package cyclicbuffer;
  7.  
  8. import java.util.Iterator;
  9. import java.util.NoSuchElementException;
  10.  
  11. /**
  12.  *
  13.  * @author shay
  14.  */
  15. public class CyclicBuffer<T> implements Iterable<T> {
  16.    
  17.     T[] buffer;        // The Buffer array.
  18.     int bufferSize;    // Its size;
  19.     int firstPtr;      // offset to the first entry in the buffer.
  20.     int lastPtr;       // offset to the last entry in the buffer.
  21.    
  22.     int numElemStored; // # of elements stored - for the iterator.
  23.    
  24.    
  25.     public class CyclicBufferIterator<T> implements Iterator<T>
  26.     {
  27.         private int  iterNextPtr = -1; // Used by the iterator:
  28.         private int elemLeft = 0;
  29.  
  30.         public CyclicBufferIterator() {
  31.             iterNextPtr = firstPtr;
  32.             elemLeft = numElemStored;
  33.         }
  34.        
  35.         public boolean hasNext()
  36.         {
  37.             return(elemLeft > 0);
  38.         }
  39.  
  40.         public T next()
  41.         {
  42.             if (hasNext())
  43.             {
  44.                 T retVal = (T) buffer[iterNextPtr];
  45.                 iterNextPtr = nextPtrVal(iterNextPtr);
  46.                 elemLeft--;
  47.                 return(retVal);
  48.             }
  49.             throw new NoSuchElementException();
  50.         }
  51.     }
  52.    
  53.     public CyclicBuffer(int bufferSize)
  54.     {
  55.         this.bufferSize = bufferSize;
  56.         this.buffer = (T[]) new Object[bufferSize];
  57.         this.firstPtr = 0;
  58.         this.lastPtr = -1;
  59.         this.numElemStored = 0;
  60.     }
  61.  
  62.     /**
  63.      * Calculate the next value of a pointer to the buffer:
  64.      * @param currPtrVal - the current value.
  65.      * @return - the next value.
  66.      */
  67.     private int nextPtrVal(int currPtrVal)
  68.     {
  69.         return ((currPtrVal + 1) % bufferSize);
  70.     }
  71.    
  72.    
  73.     public void add(T val)
  74.     {
  75.         // Calculate the next location:
  76.         int nextPtr = nextPtrVal(lastPtr);
  77.         // If reached back to the first stored element in the buffer then
  78.         // all its entries have been filled; in such case, move the pointer to
  79.         // to the first element ahead, and don't count the # of entries in the
  80.         // buffer,
  81.         if (lastPtr != -1 && nextPtr == firstPtr)
  82.             firstPtr = nextPtrVal(firstPtr);
  83.         else
  84.             numElemStored++;
  85.         // Add the entry to the next free location.
  86.         buffer[nextPtr] = val;
  87.         lastPtr = nextPtr;
  88.     }
  89.    
  90.     public void printValues()
  91.     {
  92.         int next = firstPtr;
  93.         while (true)
  94.         {
  95.             System.out.println(buffer[next]);
  96.             if (next == lastPtr)
  97.                 break;
  98.             next = nextPtrVal(next);
  99.         }
  100.     }
  101.     @Override
  102.     public Iterator<T> iterator() {
  103.         return(new CyclicBufferIterator<>());
  104.     }
  105.            
  106.     /**
  107.      * @param args the command line arguments
  108.      */
  109.     public static void main(String[] args) {
  110.         String[] inpVals = {"111", "2222", "3333", "4444", "5555", "6666", "777"};
  111.         //String[] inpVals = {"111", "2222", "3333"};
  112.        
  113.         CyclicBuffer<String> buff = new CyclicBuffer<String>(5);
  114.        
  115. //        for (String str : inpVals)
  116. //            buff.add(str);
  117.        
  118.         for (int i = 0; i < 10; i++)
  119.         {
  120.                 String str = String.format("%1$d%1$d%1$d%1$d%1$d", i);
  121.             buff.add(str);
  122.         }
  123.        
  124. //        Iterator<String> bufIter = buff.iterator();
  125. //        while (bufIter.hasNext())
  126. //        {
  127. //            String str = bufIter.next();
  128. //            System.out.println(str);
  129. //        }
  130.        
  131.         for (String str : buff)
  132.             System.out.println(str);
  133.        
  134.   //      buff.printValues();
  135.     }
  136.  
  137.    
  138. }
  139.