Skip to content

Commit 9930554

Browse files
authored
Create dvector.h
1 parent 0374379 commit 9930554

File tree

1 file changed

+174
-0
lines changed

1 file changed

+174
-0
lines changed

src/dvector.h

Lines changed: 174 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
1+
///////////////////////////////////////////////////////////////////
2+
// Copyright (c) 2019 Rohit Sharma. All rights reserved.
3+
// This program is free software; you can redistribute it and/or
4+
// modify it under the terms as GNU General Public License.
5+
///////////////////////////////////////////////////////////////////
6+
//
7+
// Generic dvector class for efficiency.
8+
9+
#include <assert.h>
10+
#include <stdlib.h> /* malloc, free, rand */
11+
12+
#pragma once
13+
14+
#define VECTOR_INIT_SIZE 12
15+
// Ref: https://stackoverflow.com/questions/5232198/about-vectors-growth
16+
#define VECTOR_GROWTH_FACTOR 1.5
17+
18+
template <class T>
19+
class dvector {
20+
typedef size_t Index;
21+
protected:
22+
T* _m_data;
23+
size_t _sz;
24+
size_t _capacity;
25+
static T NULL_DATA;
26+
27+
void stretch_capacity()
28+
{
29+
_capacity = static_cast<size_t>(VECTOR_GROWTH_FACTOR * _capacity);
30+
_m_data = static_cast<T*>(realloc(_m_data, _capacity));
31+
assert(_m_data);
32+
}
33+
// move data values from 'index' to end by n away from start.
34+
void move_right(Index index, size_t n)
35+
{
36+
if (index < 0 || index >= _sz) return; // bad index
37+
while (_sz + n >= _capacity)
38+
stretch_capacity();
39+
for (Index i = _sz + n - 1; i > index + n - 1; i--)
40+
_m_data[i] = _m_data[i - 1];
41+
_sz = _sz + n;
42+
}
43+
// move data values from 'index' to end by n towards start.
44+
void move_left(Index index, size_t n)
45+
{
46+
if (index < 0 || index >= _sz) return; // bad index
47+
if (index + n >= _sz) {
48+
_sz = index;
49+
return;
50+
}
51+
for (Index i = index; i < _sz; i++)
52+
_m_data[i] = _m_data[i + n];
53+
_sz = _sz - n;
54+
}
55+
public:
56+
// These selected vector methods are inspired from STL C++ vector template
57+
// default constructor
58+
dvector(size_t max_sz = VECTOR_INIT_SIZE)
59+
{
60+
_sz = 0;
61+
_capacity = max_sz;
62+
if (_capacity > 0) {
63+
_m_data = static_cast<T*>(malloc(_capacity * sizeof(T)));
64+
assert(_m_data);
65+
}
66+
}
67+
68+
// copy constructor
69+
dvector(const dvector& other)
70+
{
71+
_sz = other._sz;
72+
_capacity = other._capacity;
73+
_m_data = static_cast<T*>(malloc(_capacity * sizeof(T)));
74+
assert(_m_data);
75+
for (Index i = 0; i < _sz; i++)
76+
_m_data[i] = other._m_data[i];
77+
}
78+
79+
// assignment operator
80+
dvector& operator=(const dvector& other)
81+
{
82+
if (this != other) {
83+
if (_capacity < other._capacity) {
84+
_m_data = static_cast<T*>(realloc(_m_data, other._capacity));
85+
assert(_m_data);
86+
_capacity = other._capacity;
87+
}
88+
for (Index i = 0; i < other._sz; i++)
89+
_m_data[i] = other._m_data[i];
90+
_sz = other._sz;
91+
}
92+
return *this;
93+
}
94+
95+
// destructor
96+
~dvector()
97+
{
98+
if (_m_data)
99+
free(_m_data);
100+
}
101+
102+
short dimension()
103+
{
104+
return 1;
105+
}
106+
107+
// unsafe
108+
T& operator[](const Index& index) const
109+
{
110+
return _m_data[index];
111+
}
112+
113+
void clear()
114+
{
115+
_sz = 0;
116+
}
117+
118+
size_t size()
119+
{
120+
return _sz;
121+
}
122+
123+
bool empty()
124+
{
125+
return _sz == 0;
126+
}
127+
128+
void push_back(const T& data)
129+
{
130+
if (_sz == _capacity)
131+
stretch_capacity();
132+
_m_data[_sz++] = data;
133+
}
134+
135+
void pop_back()
136+
{
137+
_sz = (_sz > 0) ? --_sz : _sz;
138+
}
139+
140+
T& back()
141+
{
142+
if (_sz > 0)
143+
return _m_data[_sz - 1];
144+
assert(false);
145+
return NULL_DATA; // return reference on stack? unsafe TODOs: use smart_ptrs
146+
}
147+
148+
149+
void insert(size_t index, const T& data)
150+
{
151+
move_right(index, 1);
152+
_m_data[index] = data;
153+
}
154+
155+
// erase 'n' element starting from 'index'
156+
void erase(size_t index, size_t n)
157+
{
158+
move_left(index, n);
159+
}
160+
161+
// linear search
162+
int find(const T& data)
163+
{
164+
for (Index i = 0; i < _sz; i++)
165+
if (_m_data[i] == data)
166+
return i;
167+
return -1;
168+
}
169+
170+
bool exists(const T& data)
171+
{
172+
return (find(data) > 0);
173+
}
174+
};

0 commit comments

Comments
 (0)