-
Notifications
You must be signed in to change notification settings - Fork 59
/
Copy pathImmutableBag.h
executable file
·234 lines (173 loc) · 4.22 KB
/
ImmutableBag.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
#ifndef _IMMUTBAG_H
#define _IMMUTBAG_H
#include <cstddef>
namespace artemis {
template<typename E>
class ImmutableBag {
public:
virtual E get(int index) = 0;
virtual int getCapacity() = 0;
virtual int getCount() = 0;
virtual bool isEmpty() = 0;
virtual ~ImmutableBag(){};
};
template<typename E>
class Bag : public ImmutableBag<E> {
public:
Bag() {
init(20);
};
Bag(int capacity) {
init(capacity);
};
/**
* If set is used, the bag might
* contain gaps between indexes. Use this to get a
* complete bag. No changes will be made to the original bag.
*/
Bag<E> * getGapless(){
Bag<E> * bag = new Bag<E>(this->count);
//int c = 0;
for(int i=0; i < this->size; i++)
{
if(this->data[i] != NULL){
bag->add(data[i]);
}
}
return bag;
}
/**
* Adds an object to the bag.
* Mixing add and set is not encouraged.
* Note: set can create gaps between indexes where
* the object count is irrelevant to the order.
*
* Autmatically grows the bag if necessary.
*
* @param o The object to be added.
**/
void add(E o) {
if(size == count)grow();
data[count++] = o;
};
void addAll(Bag<E> & bag) {
for(int i=0; i < bag.size ; i++) {
add(bag.data[i]);
}
};
/**
* Sets every pointer to NULLs. Does not delete data.
*/
void clear() {
for(int i=0; i<size; i++) {
data[i] = NULL;
}
count = 0;
};
bool contains(E o) {
for(int i=0; i<count; i++)
if(o == data[i])
return true;
return false;
};
virtual E get(int index) {
if (index >= size) return NULL;
return (E)data[index];
};
virtual int getCapacity() {return size;};
virtual bool isEmpty() {return count == 0;};
/**
* Returns the amount of objects in the bag.
*
**/
virtual int getCount() {return count;};
bool remove(E o) {
for(int i=0; i<count; i++) {
if(o == data[i]) {
remove(i);
return true;
}
}
return false;
};
E remove(int index) {
if(count == 0) return NULL;
E object = data[index];
data[index] = data[count-1];
data[count-1] = NULL;
count--;
return (E) object;
};
bool removeAll(Bag<E> bag) {
bool mod = false;
for(int i=0; i<bag.count; i++)
for(int j= 0; j< count ; j++)
if(bag.data[i] == data[j]) {
remove(j);
j--; // ?
mod = true;
break;
}
return mod;
};
E removeLast() {
if(!isEmpty()) {
E object = data[count-1];
data[count-1] = NULL;
--count;
return (E) object;
}
return NULL;
};
bool set(int index, E o) {
if(index >= size) grow(index*2);
if(o == NULL && data[index] != NULL){
count--;
}
else if(o != NULL && data[index] == NULL){
count++;
};
data[index] = o;
return true;
};
void operator += (Bag<E> &bag) {
addAll(bag);
};
void deleteData(){
for(int i=0; i<size; i++)
{
delete data[i];
data[i] = NULL;
}
count = 0;
}
virtual ~Bag<E>() {
delete[] data;
};
private:
int count;
int size;
E *data;
void grow() {
int newCapacity =(size * 3.0f) * 0.5f + 1.0f;
grow(newCapacity);
};
void grow(int newCapacity) {
E* newData = new E[newCapacity];
for (int i = 0; i < size; i++)
newData[i] = data[i];
for (int i = size; i < newCapacity; i++)
newData[i] = NULL;
delete[] data;
size = newCapacity;
data = newData;
};
void init(int capacity) {
size = capacity;
count = 0;
data = new E[capacity];
clear();
}
};
};
#endif // $(Guard token)