45 template <
class T>
class Bag
52 iterator(
unsigned int start = 0, T* b = NULL):
62 bool operator==(
const iterator& src)
const {
return i==src.i; }
63 bool operator!=(
const iterator& src)
const {
return i!=src.i; }
64 T& operator*() {
return b[i]; }
65 const T& operator*()
const {
return b[i]; }
66 iterator& operator++() { i++;
return *
this; }
67 iterator& operator--() { i--;
return *
this; }
68 iterator& operator++(
int) { ++i;
return *
this; }
69 iterator& operator--(
int) { --i;
return *
this; }
77 Bag(
unsigned int cap = 8):
78 cap_(cap),size_(0),b(new T[cap]){}
85 for (
unsigned int i = 0; i < size_; i++)
95 for (
unsigned int i = 0; i < size_; i++)
102 unsigned tmp_cap_, tmp_size_;
105 tmp_size_ = src.size_;
119 for (
unsigned i = 0; i < size_; i++)
120 if (b[i] == a) result++;
124 unsigned size()
const {
return size_; }
126 bool empty()
const {
return size_ == 0; }
128 iterator
begin()
const {
return iterator(0,b); }
130 iterator
end()
const {
return iterator(size_,b); }
134 iterator p =
find(k);
146 iterator
find(
const T& k)
const
148 for (
unsigned i = 0; i < size_; i++)
149 if (b[i] == k)
return iterator(i,b);
155 if (cap_ == size_) enlarge(2*cap_);
159 ~
Bag() {
delete [] b; }
161 unsigned cap_, size_;
164 void enlarge(
unsigned adjustment)
166 cap_ = cap_ + adjustment;
168 for (
unsigned i = 0; i < size_; i++)
bool empty() const
Same as size()==0.
Definition: adevs_bag.h:126
void erase(const T &k)
Erase the first instance of k.
Definition: adevs_bag.h:132
iterator find(const T &k) const
Find the first instance of k, or end() if no instance is found. Uses == for comparing T...
Definition: adevs_bag.h:146
void clear()
Remove all of the elements from the bag.
Definition: adevs_bag.h:144
iterator begin() const
Get an iterator pointing to the first element in the bag.
Definition: adevs_bag.h:128
unsigned count(const T &a) const
Count the instances of a stored in the bag.
Definition: adevs_bag.h:116
iterator end() const
Get an iterator to the end of the bag (i.e., just after the last element)
Definition: adevs_bag.h:130
void erase(iterator p)
Erase the element pointed to by p.
Definition: adevs_bag.h:138
Bag(unsigned int cap=8)
Create an empty bag with an initial capacity.
Definition: adevs_bag.h:77
Bag(const Bag< T > &src)
Copy constructor uses the = operator of T.
Definition: adevs_bag.h:80
Bag< T > & swap(Bag< T > &src)
Swaps contents of this bag with the contents of the supplied bag. Returns this bag.
Definition: adevs_bag.h:100
A bidirectional iterator for the Bag.
Definition: adevs_bag.h:49
unsigned size() const
Get the number of elements in the bag.
Definition: adevs_bag.h:124
void insert(const T &t)
Put t into the bag.
Definition: adevs_bag.h:153
const Bag< T > & operator=(const Bag< T > &src)
Assignment operator uses the = operator of T.
Definition: adevs_bag.h:89
Definition: adevs_bag.h:45