Skip to content

Commit 8a3e7f2

Browse files
committed
add exception to stack, and set const to other exceptions
1 parent 9d442b0 commit 8a3e7f2

File tree

3 files changed

+47
-14
lines changed

3 files changed

+47
-14
lines changed

include/binary_search_tree.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ namespace alg
5353

5454
private:
5555
treeNode * m_root;
56-
BSTException error;
56+
const BSTException error;
5757

5858
public:
5959
BST():m_root(NULL){};

include/perfect_hash.h

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,19 @@
2222

2323
namespace alg
2424
{
25-
class PerfHTException: public std::exception
25+
template<typename T=uintptr_t>
26+
class PerfHT
2627
{
27-
public:
28-
virtual const char * what() const throw()
28+
private:
29+
class PerfHTException: public std::exception
2930
{
30-
return "key does not exist";
31-
}
32-
};
31+
public:
32+
virtual const char * what() const throw()
33+
{
34+
return "key does not exist";
35+
}
36+
};
3337

34-
template<typename T=uintptr_t>
35-
class PerfHT {
36-
private:
3738
// Level-2 Slot definition
3839
class SlotL2 {
3940
public:
@@ -60,7 +61,7 @@ namespace alg
6061
struct SlotL1 * slots;// level 1 slots
6162
struct UHash params; // 1st level
6263
uint32_t num_slots;
63-
PerfHTException error;
64+
const PerfHTException error;
6465

6566
public:
6667
PerfHT(uint32_t keys[], uint32_t len)

include/stack.h

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
#include <stdint.h>
2121
#include <stdbool.h>
22+
#include <exception>
2223

2324
/**
2425
* Stack has three properties. capacity stands for the maximum number of
@@ -29,9 +30,29 @@ template<typename T=uintptr_t>
2930
class Stack
3031
{
3132
private:
33+
class StackEmptyException: public std::exception
34+
{
35+
public:
36+
virtual const char * what() const throw()
37+
{
38+
return "stack is empty";
39+
}
40+
};
41+
42+
class StackIndexOutOfBoundException: public std::exception
43+
{
44+
public:
45+
virtual const char * what() const throw()
46+
{
47+
return "Index out of bound.";
48+
}
49+
};
50+
3251
uint32_t m_capacity;// the total capacity
3352
uint32_t m_size;// current stack size
3453
T * m_elements;// the elements
54+
const StackEmptyException exp_empty;
55+
const StackIndexOutOfBoundException exp_ioob;
3556

3657
public:
3758
/**
@@ -47,10 +68,12 @@ class Stack
4768
delete [] m_elements;
4869
}
4970

71+
5072
private:
5173
Stack(const Stack&);
5274
Stack& operator=(const Stack&);
5375

76+
5477
public:
5578
/**
5679
* test whether the stack is empty
@@ -66,9 +89,12 @@ class Stack
6689
}
6790

6891
/**
69-
* get the top element, test is_empty() before top()
92+
* get the top element
7093
*/
71-
inline const T& top() const { return m_elements[m_size-1]; };
94+
inline const T& top() const {
95+
if (m_size==0) throw exp_empty;
96+
return m_elements[m_size-1];
97+
};
7298

7399
/**
74100
* push an element into the stack
@@ -88,7 +114,13 @@ class Stack
88114
*/
89115
inline uint32_t count() const { return m_size; }
90116

91-
inline const T& operator[] (int idx) const { return m_elements[m_size-1-idx]; }
117+
/**
118+
* return value by index
119+
*/
120+
inline const T& operator[] (int idx) const {
121+
if (idx<0 || idx >= m_capacity) throw exp_ioob;
122+
return m_elements[m_size-1-idx];
123+
}
92124
};
93125

94126
#endif //

0 commit comments

Comments
 (0)