blob: c1298500ecf70818dc988c9afffdfea0da542a0f (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
|
/********************* */
/*! \file dynamic_array.h
** \verbatim
** Original author: taking
** Major contributors: none
** Minor contributors (to current version): none
** This file is part of the CVC4 prototype.
** Copyright (c) 2009, 2010 The Analysis of Computer Systems Group (ACSys)
** Courant Institute of Mathematical Sciences
** New York University
** See the file COPYING in the top-level source directory for licensing
** information.\endverbatim
**
** \brief [[ Add one-line brief description here ]]
**
** [[ Add lengthier description here ]]
** \todo document this file
**/
#include "cvc4_private.h"
#ifndef __CVC4__UTIL__DYNAMICARRAY_H
#define __CVC4__UTIL__DYNAMICARRAY_H
#include "util/Assert.h"
namespace CVC4 {
template <class T>
class DynamicArray {
private:
T* d_arr;
unsigned d_size;
unsigned d_allocated;
bool d_callDestructor;
void grow(){
bool empty = (d_arr == NULL);
d_allocated = empty ? d_allocated = 15 : d_allocated * 2 + 1;
unsigned allocSize = sizeof(T) * d_allocated;
T* tmpList = (T*) (empty ? malloc(allocSize) :realloc(d_arr, allocSize));
if(tmpList == NULL) {
throw std::bad_alloc();
}
d_arr = tmpList;
}
public:
DynamicArray(bool deallocate = false):
d_arr(NULL),
d_size(0),
d_allocated(0),
d_callDestructor(deallocate){
}
~DynamicArray(){
if(d_callDestructor) {
for(unsigned i = 0; i < d_size; ++i) {
d_arr[i].~T();
}
}
free(d_arr);
}
unsigned size() const{
return d_size;
}
bool empty() const{
return size() == 0;
}
void push_back(const T& data) {
if(d_size == d_allocated) {
grow();
}
Assert(d_size < d_allocated);
::new((void*)(d_arr + d_size)) T(data);
++d_size;
}
T& operator[](unsigned i) {
Assert(i < d_size, "index out of bounds in DynamicArray::operator[]");
return d_arr[i];
}
const T& back() const{
Assert(d_size > 0, "DynamicArray::back() called on empty list");
return d_arr[d_size - 1];
}
void pop_back() {
Assert(d_size > 0, "DynamicArray::back() called on empty list");
--d_size;
if(d_callDestructor){
d_arr[d_size].~T();;
}
}
};/* CVC4::DynamicArray */
}/* CVC4 namespace */
#endif /* __CVC4__UTIL__DYNAMICARRAY_H */
|