Advertisement
Guest User

Flow.Collections.GapBufferList

a guest
Apr 9th, 2013
387
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.21 KB | None | 0 0
  1.  
  2. package Flow.Collection;
  3.  
  4. import java.lang.reflect.Array;
  5. import java.util.ArrayList;
  6. import java.util.Collection;
  7. import java.util.Iterator;
  8. import java.util.List;
  9. import java.util.ListIterator;
  10. import Flow.InvalidOperationException;
  11. import Flow.NotSupportedException;
  12.  
  13. public class GapBufferList<T> implements List<T>
  14. {
  15.     public GapBufferList()
  16.     {
  17.         this(32);
  18.     }
  19.     @SuppressWarnings("unchecked")
  20.     public GapBufferList(int cap)
  21.     {
  22.         if (cap < 0) throw new InvalidOperationException("Initial capacity must be >= 0");
  23.         gapPos = 0;
  24.         gapSize = cap;
  25.         back = (T[])new Object[cap];
  26.     }
  27.    
  28.     private T[] back;
  29.     private int gapPos, gapSize;
  30.    
  31.     @Override
  32.     public int size()
  33.     {
  34.         return back.length - gapSize;
  35.     }
  36.     @Override
  37.     public boolean isEmpty()
  38.     {
  39.         return back.length == gapSize;
  40.     }
  41.    
  42.     @Override
  43.     public int indexOf(Object obj)
  44.     {
  45.         int sz = back.length - gapSize;
  46.         for (int q = 0; q < sz; q++)
  47.             if (obj == null ? get(q) == null : obj.equals(get(q))) return q;
  48.         return -1;
  49.     }
  50.     @Override
  51.     public int lastIndexOf(Object obj)
  52.     {
  53.         int sz = back.length - gapSize;
  54.         for (int q = sz - 1; q >= 0; q++)
  55.             if (obj == null ? get(q) == null : obj.equals(get(q))) return q;
  56.         return -1;
  57.     }
  58.    
  59.     @Override
  60.     public T get(int index)
  61.     {
  62.         if (index < 0 || index >= back.length - gapSize) throw new IndexOutOfBoundsException();
  63.         if (index < gapPos) return back[index];
  64.         else return back[index + gapSize];
  65.     }
  66.    
  67.     @Override
  68.     public boolean contains(Object obj)
  69.     {
  70.         return indexOf(obj) >= 0;
  71.     }
  72.     @Override
  73.     public boolean containsAll(Collection<?> c)
  74.     {
  75.         for (Object obj : c)
  76.             if (!contains(obj)) return false;
  77.         return true;
  78.     }
  79.    
  80.     @Override
  81.     public Iterator<T> iterator()
  82.     {
  83.         throw new NotSupportedException();
  84.     }
  85.     @Override
  86.     public ListIterator<T> listIterator()
  87.     {
  88.         throw new NotSupportedException();
  89.     }
  90.     @Override
  91.     public ListIterator<T> listIterator(int index)
  92.     {
  93.         throw new NotSupportedException();
  94.     }
  95.    
  96.    
  97.    
  98.     private void increaseSize()
  99.     {
  100.         int newcap = Math.max(back.length * 3, 4);
  101.         @SuppressWarnings("unchecked")
  102.         T[] temp = (T[])new Object[newcap];
  103.         for (int q = 0; q < gapPos; q++)
  104.             temp[q] = back[q];
  105.         gapSize = temp.length - back.length;
  106.         for (int q = back.length - 1; q >= gapPos; q--)
  107.             temp[q + gapSize] = temp[q];
  108.         back = temp;
  109.     }
  110.    
  111.     @Override
  112.     public boolean add(T t)
  113.     {
  114.         int pos = back.length - gapSize;
  115.         if (gapPos != pos)
  116.         {
  117.             if (gapSize != 0)
  118.             {
  119.                 if (gapSize != 0)
  120.                 {
  121.                     int diff = gapPos - pos;
  122.                     for (int q = 1; q <= diff; q++)
  123.                         back[gapPos - q + gapSize] = back[gapPos - q];
  124.                     diff = -diff; //pos - gapPos
  125.                     for (int q = 0; q < diff; q++)
  126.                         back[gapPos + q] = back[gapPos + gapSize + q];
  127.                 }
  128.             }
  129.             gapPos = pos;
  130.         }
  131.         //moveGap(back.length - gapSize);
  132.         if (gapSize == 0) increaseSize();
  133.         back[gapPos++] = t; gapSize--;
  134.         return true;
  135.     }
  136.     @Override
  137.     public void add(int index, T t)
  138.     {
  139.         if (index < 0 || index > back.length - gapSize) throw new IndexOutOfBoundsException();
  140.         if (gapPos > index)
  141.         {
  142.             int diff = gapPos - index;
  143.             for (int q = 1; q <= diff; q++)
  144.                 back[gapPos - q + gapSize] = back[gapPos - q];
  145.         }
  146.         else if (index > gapPos)
  147.         {
  148.             int diff = gapPos - index;
  149.             for (int q = 0; q < diff; q++)
  150.                 back[gapPos + q] = back[gapPos + gapSize + q];
  151.         }
  152.         gapPos = index;
  153.         if (gapSize == 0) increaseSize();
  154.         back[gapPos++] = t; gapSize--;
  155.     }
  156.     @Override
  157.     public boolean addAll(Collection<? extends T> c)
  158.     {
  159.         for (T t : c)
  160.             if (!add(t)) return false;
  161.         return true;
  162.     }
  163.     @Override
  164.     public boolean addAll(int index, Collection<? extends T> c)
  165.     {
  166.         for (T t : c)
  167.             add(index++, t);
  168.         return true;
  169.     }
  170.    
  171.     @Override
  172.     public T set(int index, T t)
  173.     {
  174.         int sz = back.length - gapSize;
  175.         if (index < 0 || index > sz) throw new IndexOutOfBoundsException();
  176.         if (index == sz)
  177.         {
  178.             add(t);
  179.             return null;
  180.         }
  181.         else if (index < gapPos)
  182.         {
  183.             T temp = back[index];
  184.             back[index] = t;
  185.             return temp;
  186.         }
  187.         else
  188.         {
  189.             T temp = back[index + gapSize];
  190.             back[index + gapSize] = t;
  191.             return temp;
  192.         }
  193.     }
  194.    
  195.     @Override
  196.     public boolean remove(Object obj)
  197.     {
  198.         int idx = indexOf(obj);
  199.         if (idx >= 0)
  200.         {
  201.             remove(idx);
  202.             return true;
  203.         }
  204.         return false;
  205.     }
  206.     @Override
  207.     public T remove(int index)
  208.     {
  209.         if (index < 0 || index >= back.length - gapSize) throw new IndexOutOfBoundsException();
  210.         if (gapPos > index + 1)
  211.         {
  212.             int diff = gapPos - (index + 1);
  213.             for (int q = 1; q <= diff; q++)
  214.                 back[gapPos - q + gapSize] = back[gapPos - q];
  215.         }
  216.         else
  217.         {
  218.             int diff = (index + 1) - gapPos;
  219.             for (int q = 0; q < diff; q++)
  220.                 back[gapPos + q] = back[gapPos + gapSize + q];
  221.         }
  222.         gapSize++;
  223.         return back[gapPos = index];
  224.     }
  225.     @Override
  226.     public boolean removeAll(Collection<?> c)
  227.     {
  228.         for (Object obj : c)
  229.             if (!remove(obj)) return false;
  230.         return true;
  231.     }
  232.     @Override
  233.     public void clear()
  234.     {
  235.         gapPos = 0;
  236.         gapSize = back.length;
  237.     }
  238.    
  239.     @Override
  240.     public boolean retainAll(Collection<?> c)
  241.     {
  242.         //Slightly inefficient method, could definitely be improved
  243.         int sz = back.length - gapSize;
  244.         for (int q = sz - 1; q >= 0; q--)
  245.             if (!c.contains(get(q)))
  246.                 remove(q);
  247.         return true;
  248.     }
  249.    
  250.     @Override
  251.     public List<T> subList(int fromIndex, int toIndex)
  252.     {
  253.         List<T> list = new ArrayList<T>();
  254.         for (int q = fromIndex; q < toIndex; q++)
  255.             list.add(get(q));
  256.         return list;
  257.     }
  258.     @Override
  259.     public Object[] toArray()
  260.     {
  261.         Object[] temp = new Object[back.length - gapSize];
  262.         for (int q = 0; q < temp.length; q++)
  263.             temp[q] = get(q);
  264.         return temp;
  265.     }
  266.     @SuppressWarnings({ "unchecked", "hiding" })
  267.     @Override
  268.     public <T> T[] toArray(T[] a)
  269.     {
  270.         T[] temp = (T[])Array.newInstance(a.getClass().getComponentType(), back.length - gapSize);
  271.         for (int q = 0; q < temp.length; q++)
  272.             temp[q] = (T)get(q);
  273.         return temp;
  274.     }
  275. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement