-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathResizingArrayBag.h
102 lines (87 loc) · 2.64 KB
/
ResizingArrayBag.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
#ifndef CH1_RESIZINGARRAYBAG_H
#define CH1_RESIZINGARRAYBAG_H
// TODO: use only start and finish
#include <stdexcept>
using std::runtime_error;
/**
* The {@code ResizingArrayBag} class represents a bag (or multiset) of
* generic items. It supports insertion and iterating over the
* items in arbitrary order.
* <p>
* This implementation uses a resizing array.
* See {@link LinkedBag} for a version that uses a singly linked list.
* The <em>add</em> operation takes constant amortized time; the
* <em>isEmpty</em>, and <em>size</em> operations
* take constant time. Iteration takes time proportional to the number of items.
* <p>
* For additional documentation, see <a href="https://algs4.cs.princeton.edu/13stacks">Section 1.3</a> of
* <i>Algorithms, 4th Edition</i> by Robert Sedgewick and Kevin Wayne.
*
* @author Robert Sedgewick
* @author Kevin Wayne
*/
template<typename T>
class ResizingArrayBag {
private:
T *a; // array of items
int length; // capacity of array
int n; // number of elements on stack
// for iterator
T *start;
T *finish;
public:
/**
* Initializes an empty bag.
*/
ResizingArrayBag() : n(0), a(new T[2]), length(2), start(a), finish(a) {}
~ResizingArrayBag() {
delete[](a);
start->~T();
finish->~T();
}
/**
* Is this bag empty?
* @return true if this bag is empty; false otherwise
*/
bool isEmpty() const {
return n == 0;
}
/**
* Returns the number of items in this bag.
* @return the number of items in this bag
*/
int size() const {
return n;
}
/**
* Adds the item to this bag.
* @param item the item to add to this bag
*/
void add(T item) {
if (n == length) resize(2 * length); // double size of array if necessary
a[n++] = item; // add item
finish += 1;
}
// iterator
T *begin() noexcept { return start; }
T *end() noexcept { return finish; }
const T *begin() const noexcept { return start; }
const T *end() const noexcept { return finish; }
private:
// resize the underlying array holding the elements
void resize(const int capacity) {
if (capacity < n) throw runtime_error("illegal capacity");
// textbook implementation
auto temp = new T[capacity];
for (int i = 0; i < n; i++) {
temp[i] = a[i];
}
auto tmp = a;
a = temp;
delete[](tmp);
length = capacity;
start = a;
finish = a + n;
}
};
#endif //CH1_RESIZINGARRAYBAG_H