Mercurial > Algorithms__Sedgewick
view algs4-c++/src/RandomBag.hpp @ 15:63df3e6590e2
Bag and RandomBag (1.3.34) classes.
There are not really robust enough to use as they will leak memory on
destruction if you use them to store dynamically allocated objects.
author | Eris Caffee <discordia@eldalin.com> |
---|---|
date | Thu, 11 Jun 2015 16:30:14 -0500 |
parents | |
children | a149b424b4e2 |
line source
1 #ifndef RANDOMBAG_HPP
2 #define RANDOMBAG_HPP
4 #include <cstddef>
5 #include <vector>
7 template <typename T>
8 class RandomBag {
10 public:
12 ////////////////////////////////////////
13 class iterator {
15 public:
17 iterator( T *b, T *f, T *e );
19 iterator& operator++();
20 iterator operator++(int);
22 T operator*();
23 bool operator!=( typename RandomBag<T>::iterator other );
25 private:
27 T *begin;
28 T *first;
29 T *end;
30 size_t curr;
31 std::vector<size_t> order;
32 };
34 ////////////////////////////////////////
35 RandomBag( void );
36 ~RandomBag( void );
38 void add( T &item );
40 bool is_empty( void );
41 size_t size( void );
43 iterator begin( void );
44 iterator end( void );
46 private:
48 size_t N;
49 size_t max;
50 T *data;
52 void resize( size_t new_max );
53 };
57 #endif