-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArray_Base.h
More file actions
202 lines (172 loc) · 5.43 KB
/
Array_Base.h
File metadata and controls
202 lines (172 loc) · 5.43 KB
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
// -*- C++ -*-
//==============================================================================
/**
* @file Array_Base.h
*
* Honor Pledge:
*
* I pledge that I have neither given nor received any help
* on this assignment.
*/
//==============================================================================
#ifndef _ARRAY_BASE_H_
#define _ARRAY_BASE_H_
#include <cstring> // for size_t definition
#include <stdexcept> // for std::out_of_bounds exception
/**
* @class Array_Base
*
* Basic implementation of a standard array class for chars.
*/
template <typename T>
class Array_Base
{
public:
/// Type definition of the element type.
typedef T type;
/// Default constructor.
Array_Base (void);
/**
* Initializing constructor.
*
* @param[in] length Initial size
*/
Array_Base (size_t length);
/**
* Initializing constructor.
*
* @param[in] length Initial size
* @param[in] fill Initial value for each element
*/
Array_Base (size_t length, T fill);
/**
* Copy constructor
*
* @param[in] arr The source array.
*/
Array_Base (const Array_Base & arr);
/// Destructor.
~Array_Base (void);
/**
* Retrieve the current size of the array.
*
* @return The current size
*/
size_t size (void) const;
/**
* Retrieve the maximum size of the array.
*
* @return The maximum size
*/
size_t max_size (void) const;
/**
* Get the character at the specified index. If the index is not
* within the range of the array, then std::out_of_range exception
* is thrown.
*
* @param[in] index Zero-based location
* @exception std::out_of_range Invalid \a index value
*/
T & operator [] (size_t index);
/**
* @overload
*
* The returned character is not modifiable.
*/
const T & operator [] (size_t index) const;
/**
* Get the character at the specified index. If the \a index is not within
* the range of the array, then std::out_of_range exception is thrown.
*
* @param[in] index Zero-based location
* @return Character at \index
* @exception std::out_of_range Invalid index value
*/
T get (size_t index) const;
/**
* Set the character at the specified \a index. If the \a index is not
* within range of the array, then std::out_of_range exception is
* thrown.
*
* @param[in] index Zero-based location
* @param[in] value New value for character
* @exception std::out_of_range Invalid \a index value
*/
void set (size_t index, T value);
/**
* Locate the specified character in the array. The index of the first
* occurrence of the character is returned. If the character is not
* found in the array, then -1 is returned.
*
* @param[in] ch Character to search for
* @return Index value of the character
* @retval -1 Character not found
*/
int find (T element) const;
/**
* @overload
*
* This version allows you to specify the start index of the search. If
* the start index is not within the range of the array, then the
* std::out_of_range exception is thrown.
*
* @param[in] ch Character to search for
* @param[in] start Index to begin search
* @return Index value of first occurrence
* @retval -1 Character not found
* @exception std::out_of_range Invalid index
*/
int find (T element, size_t start) const;
/**
* Test the array for equality.
*
* @param[in] rhs Right hand side of equal to sign
* @retval true Left side is equal to right side
* @retval false Left side is not equal to right side
*/
bool operator == (const Array_Base <T> & rhs) const;
/**
* Test the array for inequality.
*
* @param[in] rhs Right-hand size of not equal to sign
* @retval true Left side is not equal to right side
* @retval false Left size is equal to right side
*/
bool operator != (const Array_Base <T> & rhs) const;
/**
* Fill the contents of the array.
*
* @param[in] ch Fill character
*/
void fill (T element);
/// Reverse the contents of the array such that the first element is now
/// the last element and the last element is the first element.
void reverse ( void );
/**
* The slice() method returns a shallow copy of a portion of an array into
* a new array object selected from begin to end (end not included). The original
* array will not be modified.
*
* @param[in] begin The starting index
* @return A new Array object
*/
Array_Base <T> slice (size_t begin) const;
/**
* @overload
*
* @param[in] begin The starting index
* @param[in] end The ending index
* @return A new Array object
*/
Array_Base <T> slice (size_t begin, size_t end) const;
protected:
/// Pointer to the actual data.
T * data_;
/// Current size of the array.
size_t cur_size_;
/// Maximum size of the array.
size_t max_size_;
};
#include "Array_Base.inl"
#include "Array_Base.cpp"
#endif // !defined _ARRAY_BASE_H_