-
Notifications
You must be signed in to change notification settings - Fork 0
/
Bag.h
83 lines (69 loc) · 2.28 KB
/
Bag.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
#pragma once
#pragma once
#define DEFAULT_BAG 20
#include <stdlib.h>
//bags allow us to remove RANDOM objects from it (that were previously placed inside)
// NOTE: your main should initialize a random number generator (via srand) when using Bag<T> objects
template <class T>
class Bag {
public:
Bag(unsigned int max_capacity = DEFAULT_BAG); // construct an empty bag of specified "max_capacity"
// if no value is provided, DEFAULT_BAG is used
~Bag(); // destructor (recycles memory allocated by constructor)
void add(T value); // adds a new T into the bag,
// PRECONDITION: the bag must not be "full"
T getOne(); // extracts an T from the bag,
// PRECONDITION: the bag must not be "empty"
bool isEmpty() const;
bool isFull() const;
unsigned int currentSize() const; // returns # of Ts now in bag
unsigned int capacity() const; // returns bag’s upper capacity
private:
unsigned int m_size; // number of elements currently stored in the bag
unsigned int m_max_capacity; // the maximum capacity of the bag
T * m_container; // internal storage array of pointers to Ts
};
template <class T>
Bag<T> ::Bag(unsigned int max_capacity) {
m_max_capacity = max_capacity;
m_size = 0;
m_container = new T[m_max_capacity];
}
template <class T>
Bag<T> :: ~Bag() {
delete[] m_container;
}
template <class T>
void Bag<T> ::add(T value) {
// PRECONDITION: the bag must not be "full"
assert(!isFull() && "Bag is full (in add)");
m_container[m_size] = value; // store the pointer
m_size++;
}
template <class T>
T Bag<T> ::getOne() {
// PRECONDITION: the bag must not be empty when "getOne()" is called
assert(!isEmpty() && "Bag is empty (in getOne)");
//generate a random number between 0 and (m_size-1);
unsigned int index = (unsigned int)(rand() % m_size);
T value = m_container[index];
m_container[index] = m_container[m_size - 1];
m_size--;
return value;
}
template <class T>
bool Bag<T> ::isEmpty() const {
return (m_size == 0);
}
template <class T>
bool Bag<T> ::isFull() const {
return (m_size == m_max_capacity);
}
template <class T>
unsigned int Bag<T> ::currentSize() const {
return m_size;
}
template <class T>
unsigned int Bag<T> ::capacity() const {
return m_max_capacity;
}