forked from thelinuxlich/artemis_CSharp
/
Bag.cs
235 lines (206 loc) · 4.91 KB
/
Bag.cs
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
using System;
namespace Artemis
{
public class Bag<E> where E : class
{
private E[] data;
private int size = 0;
/**
* Constructs an empty Bag with an initial capacity of 64.
*
*/
public Bag() {
data = new E[16];
}
/**
* Constructs an empty Bag with the specified initial capacity.
*
* @param capacity
* the initial capacity of Bag
*/
public Bag(int capacity)
{
data = new E[capacity];
}
/**
* Removes the element at the specified position in this Bag. does this by
* overwriting it was last element then removing last element
*
* @param index
* the index of element to be removed
* @return element that was removed from the Bag
*/
public E Remove(int index) {
E o = data[index]; // make copy of element to remove so it can be
// returned
data[index] = data[--size]; // overwrite item to remove with last
// element
data[size] = null; // null last element, so gc can do its work
return o;
}
/**
* Remove and return the last object in the bag.
*
* @return the last object in the bag, null if empty.
*/
public E RemoveLast() {
if(size > 0) {
E o = data[--size];
data[size] = null;
return o;
}
return default(E);
}
/**
* Removes the first occurrence of the specified element from this Bag, if
* it is present. If the Bag does not contain the element, it is unchanged.
* does this by overwriting it was last element then removing last element
*
* @param o
* element to be removed from this list, if present
* @return <tt>true</tt> if this list contained the specified element
*/
public bool Remove(E o) {
for (int i = 0; i < size; i++) {
Object o1 = data[i];
if (o.Equals(o1)) {
data[i] = data[--size]; // overwrite item to remove with last
// element
data[size] = null; // null last element, so gc can do its work
return true;
}
}
return false;
}
/**
* Check if bag contains this element.
*
* @param o
* @return
*/
public bool Contains(E o) {
for(int i = 0; size > i; i++) {
if(o.Equals(data[i])) {
return true;
}
}
return false;
}
/**
* Removes from this Bag all of its elements that are contained in the
* specified Bag.
*
* @param bag
* Bag containing elements to be removed from this Bag
* @return {@code true} if this Bag changed as a result of the call
*/
public bool RemoveAll(Bag<E> bag) {
bool modified = false;
for (int i = 0; i < bag.Size(); i++) {
Object o1 = bag.Get(i);
for (int j = 0; j < size; j++) {
Object o2 = data[j];
if (o1 == o2) {
Remove(j);
j--;
modified = true;
break;
}
}
}
return modified;
}
/**
* Returns the element at the specified position in Bag.
*
* @param index
* index of the element to return
* @return the element at the specified position in bag
*/
public E Get(int index) {
return data[index];
}
/**
* Returns the number of elements in this bag.
*
* @return the number of elements in this bag
*/
public int Size() {
return size;
}
/**
* Returns the number of elements the bag can hold without growing.
*
* @return the number of elements the bag can hold without growing.
*/
public int GetCapacity() {
return data.Length;
}
/**
* Returns true if this list contains no elements.
*
* @return true if this list contains no elements
*/
public bool IsEmpty() {
return size == 0;
}
/**
* Adds the specified element to the end of this bag. if needed also
* increases the capacity of the bag.
*
* @param o
* element to be added to this list
*/
public void Add(E o) {
// is size greater than capacity increase capacity
if (size == data.Length) {
Grow();
}
data[size++] = o;
}
/**
* Set element at specified index in the bag.
*
* @param index position of element
* @param o the element
*/
public void Set(int index, E o) {
if(index >= data.Length) {
Grow(index*2);
size = index+1;
} else if(index >= size) {
size = index+1;
}
data[index] = o;
}
private void Grow() {
int newCapacity = (data.Length * 3) / 2 + 1;
Grow(newCapacity);
}
private void Grow(int newCapacity) {
E[] oldData = data;
data = new E[newCapacity];
Array.Copy(oldData, 0, data, 0, oldData.Length);
}
/**
* Removes all of the elements from this bag. The bag will be empty after
* this call returns.
*/
public void Clear() {
// null all elements so gc can clean up
for (int i = 0; i < size; i++) {
data[i] = null;
}
size = 0;
}
/**
* Add all items into this bag.
* @param added
*/
public void AddAll(Bag<E> items) {
for(int i = 0; items.Size() > i; i++) {
Add(items.Get(i));
}
}
}
}