Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Created by SpyMomiji on 2017/4/27.
- */
- public class RingList <T> {
- private class I {
- int count = 1;
- int fill = 0;
- V cv;
- V fv;
- void push(T obj){
- fv.value = obj;
- if(++fill >= count){
- V v = new V();
- v.next = fv.next;
- fv.next = v;
- fv = v;
- count++;
- } else {
- fv = fv.next;
- }
- }
- T shift(){
- if(fill <= 0) return null;
- T rv = cv.value;
- cv = cv.next;
- fill--;
- return rv;
- }
- void clear(){
- fill = 0;
- fv = cv;
- }
- void gc(){
- System.out.println("\t" + fill);
- System.out.println("\t" + count);
- int rp = count - fill -1;
- if( rp <= 0 ) return;
- V tv = fv;
- for (int i = 0; i < rp ; i++) {
- tv = tv.next;
- }
- fv.next = tv;
- count -= rp;
- }
- }
- private I index;
- public RingList() {
- index = new I();
- V v1 = new V();
- v1.next = v1;
- index.cv = v1;
- index.fv = v1;
- }
- private class V {
- T value = null;
- V next;
- }
- public void push(T obj){
- synchronized (index){
- index.push(obj);
- }
- }
- T shift(){
- synchronized (index){
- return index.shift();
- }
- }
- public void clear(){
- synchronized (index){
- index.clear();
- }
- }
- public int size(){
- synchronized (index){
- return index.fill;
- }
- }
- public void gc(){
- synchronized (index){
- index.gc();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement