Changed uses of std::vector in BBlockImpl and FunctionImpl to TmpVector.
[jitcs.git] / src / jitcs_int_adt_tmpvector.h
blob6105efc373cc285dba260bd509db50b682f829f9
1 //===-- evm/function.h - Class to represent a single basic block --*- C++ -*-===//
2 //
3 // A basicblock contains nodes, and must end in a terminal instruction node.
4 // Nodes can be instructions, constants. Each node may produce more than one
5 // result.
6 //
7 //===----------------------------------------------------------------------===//
9 #ifndef _JITCS_INT_ADT_TMPVECTOR_H_
10 #define _JITCS_INT_ADT_TMPVECTOR_H_
12 #include "jitcs_adt_slice.h"
13 #include "jitcs_base.h"
14 #include "jitcs_int_adt_array.h"
15 #include "jitcs_tmpalloc.h"
17 namespace jitcs {
19 template <typename T, unsigned N>
20 struct TmpVector : public ArrayRef<T> {
21 typedef T ItemType;
22 typedef ArrayRef<T> super;
23 ItemType _short_data[N];
24 size_t _capacity;
25 TempAllocator& _alloc;
27 TmpVector(TempAllocator& a) : super(_short_data, 0), _capacity(N), _alloc(a) {}
29 TmpVector() = delete;
30 TmpVector(const TmpVector&) = delete;
31 TmpVector& operator =(const TmpVector&) = delete;
33 void push_back(ItemType i) {
34 if (this->_size >= _capacity) _resize(1);
35 this->_data[this->_size++] = i;
37 void append(Slice<ItemType> items) {
38 if (this->_size + items.size() > _capacity) _resize(items.size());
39 std::copy(items.begin(), items.end(), this->_data + this->_size);
40 this->_size += items.size();
42 bool isLocalData() const { return this->_data == _short_data; }
44 void initSizeAndClear(size_t sz, const ItemType& filler) {
45 if (sz > _capacity) _resize(sz);
46 this->_size = sz;
47 std::fill(this->_data, this->_data + sz, filler);
50 private:
51 void _resize(size_t g) {
52 size_t newsize = (_capacity | g) * 2 + 2;
53 ItemType* newdata = _alloc.allocTypedArray<T>(newsize);
54 std::copy(this->_data, this->_data + this->_size, newdata);
55 this->_data = newdata;
56 this->_capacity = newsize;
60 } // end of namespace jitcs
62 #endif
63 // _JITCS_INT_ADT_TMPVECTOR_H_