Advertisement
SpyMomiji

RingList

Apr 30th, 2017
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.88 KB | None | 0 0
  1.  
  2. /**
  3.  * Created by SpyMomiji on 2017/4/27.
  4.  */
  5.  
  6. public class RingList <T> {
  7.  
  8.     private class I {
  9.  
  10.         int count = 1;
  11.         int fill = 0;
  12.         V cv;
  13.         V fv;
  14.  
  15.         void push(T obj){
  16.             fv.value = obj;
  17.             if(++fill >= count){
  18.                 V v = new V();
  19.                 v.next = fv.next;
  20.                 fv.next = v;
  21.                 fv = v;
  22.                 count++;
  23.             } else {
  24.                 fv = fv.next;
  25.             }
  26.  
  27.         }
  28.  
  29.         T shift(){
  30.             if(fill <= 0) return null;
  31.             T rv = cv.value;
  32.             cv = cv.next;
  33.             fill--;
  34.             return rv;
  35.         }
  36.  
  37.         void clear(){
  38.             fill = 0;
  39.             fv = cv;
  40.         }
  41.  
  42.         void gc(){
  43.             System.out.println("\t" + fill);
  44.             System.out.println("\t" + count);
  45.             int rp = count - fill -1;
  46.             if( rp <= 0 ) return;
  47.  
  48.             V tv = fv;
  49.             for (int i = 0; i < rp ; i++) {
  50.                 tv = tv.next;
  51.             }
  52.             fv.next = tv;
  53.             count -= rp;
  54.  
  55.         }
  56.  
  57.     }
  58.  
  59.     private I index;
  60.  
  61.     public RingList() {
  62.         index = new I();
  63.         V v1 = new V();
  64.         v1.next = v1;
  65.         index.cv = v1;
  66.         index.fv = v1;
  67.     }
  68.  
  69.     private class V {
  70.         T value = null;
  71.         V next;
  72.     }
  73.  
  74.     public void push(T obj){
  75.         synchronized (index){
  76.             index.push(obj);
  77.         }
  78.     }
  79.  
  80.     T shift(){
  81.         synchronized (index){
  82.             return index.shift();
  83.         }
  84.     }
  85.  
  86.     public void clear(){
  87.         synchronized (index){
  88.             index.clear();
  89.         }
  90.     }
  91.  
  92.     public int size(){
  93.         synchronized (index){
  94.             return index.fill;
  95.         }
  96.     }
  97.  
  98.     public void gc(){
  99.         synchronized (index){
  100.             index.gc();
  101.         }
  102.     }
  103.  
  104.  
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement