-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMutableArray.h
156 lines (131 loc) · 2.99 KB
/
MutableArray.h
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
//
// MutableArray.cpp
// TankGame
//
// Created by Jacob Gonzalez on 4/05/2015.
// Copyright (c) 2015 Jacob Gonzalez. All rights reserved.
//
#ifndef _MUTABLE_ARRAY_H
#define _MUTABLE_ARRAY_H
#define DEFAULT_ARRAY_SIZE 128
#include <algorithm>
// :: template class for mutable array ::
// :: MutableArray<typename> array ::
template<typename T>
class MutableArray
{
public:
// : default constructor :
MutableArray()
{
init(DEFAULT_ARRAY_SIZE);
}
// : constructor for setting initial array capacaity :
MutableArray(int size)
{
init(size);
}
// : deletes pointer to array. does not remove actual items :
// : use clear first :
~MutableArray()
{
delete[] _items;
}
// : adds item to array :
// : if reached capacity resize array :
void add(T item)
{
if (_count >= _size)
{
// increasr the size of the array if we need
_resize();
}
_items[_count++] = item;
}
// : remove item from array and return pointer to deleted item :
T remove(size_t index)
{
if (index >= 0 && index < _count)
{
// get item being removed
T item = _items[index];
// shift everything right once
for (size_t i = index; i < _count; ++i)
{
_items[i] = _items[i+1];
}
--_count;
return item;
}
return T();
}
// : get item at index :
T operator[](size_t index)
{
return get_item(index);
}
// : return item at index :
T get_item(size_t index)
{
if (index >= 0 && index < _count)
{
return _items[index];
}
return T();
}
// : remove all the items :
// : deleting is left to the user :
void clear()
{
for (int i = 0; i < _count; ++i)
{
if (_items[i] != 0)
{
remove(i);
}
}
_count = 0;
}
// : returns how many items in array :
size_t get_count() const
{
return _count;
}
// : returns the current capacity of the array :
size_t get_size() const
{
return _size;
}
// : set item at index to item :
void set_item(size_t index, T item)
{
if (index >= 0 && index < _count)
{
_items[index] = item;
}
}
private:
T *_items;
size_t _count;
size_t _size;
size_t _increment_size;
// : init the array
void init(size_t size)
{
_count = 0;
_size = size;
_increment_size = _size;
_items = new T[_size];
}
// increase the size of the array
void _resize()
{
size_t new_size = _size + _increment_size;
T *new_items = new T[new_size];
std::copy(_items, _items+_size, new_items);
_size = new_size;
delete[] _items;
_items = new_items;
}
};
#endif // _MUTABLE_ARRAY_H