*** empty log message ***
[IRC.git] / Robust / Transactions / jcarderbenchmarks / mysrc / dstm2 / util / ArrayList.java
1 /*
2  * To change this template, choose Tools | Templates
3  * and open the template in the editor.
4  */
5
6 package dstm2.util;
7
8 import dstm2.AtomicArray;
9 import dstm2.factory.Factory;
10 import dstm2.Thread;
11 import dstm2.atomic;
12 import java.util.Arrays;
13
14
15 /**
16  *
17  * @author navid
18  */
19
20 public class ArrayList<V extends dstm2.AtomicSuperClass>{
21    transient volatile int modCount;
22     private volatile int size;
23     AtomicArray<entry<V>> elementData;
24     private Factory<entry> factory;
25
26     public ArrayList() {
27         this(10);
28     }
29     
30     public ArrayList(int initialCapacity) {
31         super ();
32         if (initialCapacity < 0)
33             throw new IllegalArgumentException("Illegal Capacity: "  + initialCapacity);
34        this .elementData = new AtomicArray<entry<V>>(entry.class,initialCapacity);
35     }
36     
37     
38       public void ensureCapacity(int minCapacity) {
39           modCount++;
40           int oldCapacity = elementData.size();
41           if (minCapacity > oldCapacity) {
42                 //Object oldData[] = elementData;
43                 int newCapacity = (oldCapacity * 3) / 2 + 1;
44                 if (newCapacity < minCapacity)
45                     newCapacity = minCapacity;
46                 // minCapacity is usually close to size, so this is a win:
47                 AtomicArray<entry<V>> newar =  new AtomicArray<entry<V>>(entry.class, newCapacity);
48                 for (int i=0; i<newCapacity; i++)
49                     newar.set(i, elementData.get(i));
50                 elementData = newar;
51           }
52       }
53
54             /**
55             * Returns the number of elements in this list.
56              *
57              * @return the number of elements in this list
58              */
59      public int size() {
60         return size;
61      }
62      
63      public boolean add(V value) {
64         ensureCapacity(size + 1); // Increments modCount!
65         entry<V> e = factory.create();
66         e.setValue(value);
67         elementData.set(size, e);
68         size++;
69         return true;
70      }
71      
72      public V get(int index){
73          return elementData.get(index).getValue();
74      }
75      
76      
77       public V remove(int index) {
78         rangeCheck(index);
79         modCount++;
80         entry<V> oldValue = elementData.get(index);
81
82         int numMoved = size - index - 1;
83         if (numMoved > 0)
84             System.arraycopy(elementData, index + 1, elementData,
85         index, numMoved);
86         size--;
87         elementData.set(size, null); // Let gc do its work
88         return oldValue.getValue();
89     }
90       
91       private void rangeCheck(int index) {
92          if (index < 0 || index >= this .size)
93              throw new IndexOutOfBoundsException();
94      }
95       
96       public void clear() {
97            modCount++;
98             // Let gc do its work
99            for (int i = 0; i < size; i++)
100               elementData.set(i, null);
101            size = 0;
102      } 
103     
104      @atomic interface entry<V>{
105         V getValue();
106         void setValue(V val);
107     }
108     
109
110
111 }