Debian Codegraph @

src/FDM/YASim/Vector.hpp

raw symbols SCIP
1 #ifndef _VECTOR_HPP
2 #define _VECTOR_HPP
3
4 #include <stdio.h>
5 #include <cassert>
6
7 namespace yasim {
8 //
9 // Excruciatingly simple vector-of-pointers class. Easy & useful.
10 // No support for addition of elements anywhere but at the end of the
11 // list, nor for removal of elements. Does not delete (or interpret
12 // in any way) its contents.
13 //
14 class Vector {
15 public:
16 Vector();
17 ~Vector();
18 int add(void* p);
19 void* get(int i) const;
20 void set(int i, void* p);
21 int size() const;
22 bool empty() const;
23 private:
24 void realloc();
25
26 int _nelem {0};
27 int _sz {0};
28 void** _array {nullptr};
29 };
30
31 inline Vector::Vector()
32 {
33 _nelem = 0;
34 _sz = 0;
35 _array = 0;
36 }
37
38 inline Vector::~Vector()
39 {
40 delete[] _array;
41 }
42
43 inline int Vector::add(void* p)
44 {
45 if(_nelem == _sz)
46 realloc();
47 _array[_sz] = p;
48 return _sz++;
49 }
50
51 inline void* Vector::get(int i) const
52 {
53 assert((i >= 0) && (i < _sz));
54 return _array[i];
55
56 }
57
58 inline void Vector::set(int i, void* p)
59 {
60 assert((i >= 0) && (i < _sz));
61 _array[i] = p;
62 }
63
64 inline int Vector::size() const
65 {
66 return _sz;
67 }
68
69 inline bool Vector::empty() const
70 {
71 return _sz == 0;
72 }
73
74 inline void Vector::realloc()
75 {
76 _nelem = 2*_nelem + 1;
77 void** array = new void*[_nelem];
78 for(int i=0; i<_sz; i++)
79 array[i] = _array[i];
80 delete[] _array;
81 _array = array;
82 }
83 }; //namespace yasim
84 #endif // _VECTOR_HPP