Merge branch 'master' of ssh://demsky.eecs.uci.edu/home/git/constraint_compiler into...
[satune.git] / src / Collections / cppvector.h
1 #ifndef CPPVECTOR_H
2 #define CPPVECTOR_H
3 #include <string.h>
4
5 #define VECTOR_DEFCAP 8
6
7 template<typename type>
8 class Vector {
9  public:
10  Vector(uint _capacity = VECTOR_DEFCAP) :
11         size(0),
12                 capacity(_capacity),
13                 array((type *) ourmalloc(sizeof(type) * _capacity)) {
14         }                                                                     
15
16  Vector(uint _capacity, type * _array)  :
17         size(_capacity),
18                 capacity(_capacity),
19                 array((type *) ourmalloc(sizeof(type) * _capacity)) {
20                 memcpy(array, _array, capacity * sizeof(type));                 
21         }
22         
23         void pop() {
24                 size--;
25         }                                                                     
26
27         type last() {
28                 return array[size - 1];                               
29         }
30         
31         void setSize(uint _size) {       
32                 if (_size <= size) {                                         
33                         size = _size;                                                
34                         return;                                                           
35                 } else if (_size > capacity) {                               
36                         array = (type *)ourrealloc(array, _size * sizeof(type)); 
37                         capacity = _size;                                            
38                 }                                                                   
39                 bzero(&array[size], (_size - size) * sizeof(type)); 
40                 size = _size;
41         }                                                                     
42
43         void push(type item) {          
44                 if (size >= capacity) {                             
45                         uint newcap = capacity << 1;                                
46                         array = (type *)ourrealloc(array, newcap * sizeof(type)); 
47                         capacity = newcap;                                          
48                 }                                                                   
49                 array[size++] = item;                               
50         }
51         
52         type get(uint index) {         
53                 return array[index];                                        
54         }
55         
56         void setExpand(uint index, type item) { 
57                 if (index >= size)                                            
58                         setSize(index + 1);                         
59                 set(index, item);                             
60         }
61         
62         void set(uint index, type item) { 
63                 array[index] = item;                                          
64         }
65         
66         uint getSize() {
67                 return size;                                                
68         }
69         
70         ~Vector() {
71                 ourfree(array);                                             
72         }
73         
74         void clear() {
75                 size = 0;                                                     
76         }
77         
78         type *expose() {
79                 return array;                                               
80         }                                                                     
81         
82  private:
83         uint size;
84         uint capacity;
85         type *array;
86 };
87 #endif