changes to intruder
[IRC.git] / Robust / src / Benchmarks / SingleTM / Intruder / Vector_t.java
1 public class Vector_t {
2   int size;
3   int capacity;
4   Object[] elements;
5
6   /* =============================================================================
7    * Vector_alloc
8    * -- Returns null if failed
9    * =============================================================================
10    */
11   public Vector_t(int initCapacity) {
12     int capacity = Math.imax(initCapacity, 1);
13     this.size = 0;
14     this.capacity = capacity;
15     this.elements = new Object[capacity];
16   }
17
18   /* =============================================================================
19    * Vector_free
20    * =============================================================================
21    */
22   public void
23     vector_free ()
24     {
25       elements = null;
26     }
27
28   /* =============================================================================
29    * Vector_at
30    * -- Returns null if failed
31    * =============================================================================
32    */
33   public Object vector_at (int i) {
34     if ((i < 0) || (i >= size)) {
35       System.out.println("Illegal Vector.element\n");
36       return null;
37     }
38     return (elements[i]);
39   }
40
41
42   /* =============================================================================
43    * Vector_pushBack
44    * -- Returns false if fail, else true
45    * =============================================================================
46    */
47   public boolean vector_pushBack (Object dataPtr) {
48     if (size == capacity) {
49       int newCapacity = capacity * 2;
50       Object[] newElements = new Object[newCapacity];
51
52       //void** newElements = (void**)malloc(newCapacity * sizeof(void*));
53       if (newElements == null) {
54         return false;
55       }
56       capacity = newCapacity;
57       for (int i = 0; i < size; i++) {
58         newElements[i] = elements[i];
59       }
60       elements = null;
61       elements = newElements;
62     }
63
64     elements[size++] = dataPtr;
65
66     return true;
67   }
68
69   /* =============================================================================
70    * Vector_popBack
71    * -- Returns null if fail, else returns last element
72    * =============================================================================
73    */
74   public Object
75     vector_popBack ()
76     {
77       if (size < 1) {
78         return null;
79       }
80
81       return (elements[--(size)]);
82     }
83
84   /* =============================================================================
85    * Vector_getSize
86    * =============================================================================
87    */
88   public int
89     vector_getSize ()
90     {
91       return (size);
92     }
93
94   /* =============================================================================
95    * Vector_clear
96    * =============================================================================
97    */
98   public void
99     vector_clear ()
100     {
101       size = 0;
102     }
103   
104   /* =============================================================================
105    * Vector_sort
106    * =============================================================================
107    *
108   public void
109     vector_sort ()
110     {
111       //qsort.sort(elements, 0, (elements.length - 1));
112       qsort.sort(elements);
113       //qsort(elements, size, 4, compare);
114     }
115
116   * =============================================================================
117    * Vector_copy
118    * =============================================================================
119    */
120   public static boolean
121     vector_copy (Vector_t dstVectorPtr, Vector_t srcVectorPtr)
122     {
123       int dstCapacity = dstVectorPtr.capacity;
124       int srcSize = srcVectorPtr.size;
125       if (dstCapacity < srcSize) {
126         int srcCapacity = srcVectorPtr.capacity;
127         Object[] elements = new Object[srcCapacity];
128
129         if (elements == null) {
130           return false;
131         }
132         dstVectorPtr.elements = null;
133         dstVectorPtr.elements = elements;
134         dstVectorPtr.capacity = srcCapacity;
135       }
136
137       for(int i = 0; i< srcSize; i++) {
138         dstVectorPtr.elements[i] = srcVectorPtr.elements[i];
139       }
140
141       dstVectorPtr.size = srcSize;
142
143       return true;
144     }
145 }