-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathb_key_list.hpp
More file actions
179 lines (147 loc) · 3.46 KB
/
b_key_list.hpp
File metadata and controls
179 lines (147 loc) · 3.46 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
#ifndef __B_KEY_LIST__
#define __B_KEY_LIST__
#include <string>
#include <sstream>
#include <stdexcept>
#include "b_tree_errors.hpp"
#include "b_tree_constraints.hpp"
template<UsableInBTree T>
class BKeyList
{
public: // Public member variable
const size_t order;
private: // Private member variable
T* keys;
size_t currentSize;
public: // Public primary functions
BKeyList(size_t order) : order(ensureValidOrder(order)), currentSize(0), keys(new T[order]) {
}
~BKeyList() {
delete[] keys;
}
void insert(const T& key) {
size_t keyIndex = findIndex(key);
insertByIndex(key, keyIndex);
}
void remove(const T& key) {
size_t keyIndex = findIndex(key);
removeByIndex(key, keyIndex);
}
const bool search(const T& key) const {
size_t indexOfKey = findIndex(key);
try {
return (key == getKeyByIndex(indexOfKey));
}
catch (std::out_of_range&) {
return false;
}
}
const std::string traverse() const {
std::stringstream ss;
for (size_t i = 0; i < currentSize; i++) {
if (i != 0) ss << " ";
ss << keys[i];
}
return ss.str();
}
public: // Public helper functions
const size_t findIndex(const T& key) const {
size_t result = 0;
if (currentSize == 0) {
return 0;
}
else {
size_t begin = 0;
size_t end = currentSize - 1;
while (begin < end) {
size_t middle = (begin + end) / 2;
if (keys[middle] < key) {
begin = middle + 1;
}
else {
end = middle;
}
}
result = end;
if (keys[end] < key)
result++;
return result;
}
}
const bool splitRequired() const {
return currentSize >= order;
}
void splitBKeyList(BKeyList* tail) {
if (tail == nullptr) {
throw std::invalid_argument("The tail BKeyList is null");
}
else if (!tail->isEmpty()) {
throw std::invalid_argument("The tail BKeyList is not empty");
}
else if (currentSize < order) {
throw std::logic_error("There are not enough keys in the BKeyList");
}
else {
currentSize = order / 2;
for (size_t i = order / 2 + 1; i < order; i++) {
tail->insertByIndex(keys[i], i - (order / 2 + 1));
}
}
}
void mergeBKeyList(BKeyList* other) {
for (size_t i = 0; i < other->currentSize; i++) {
T currentKey = other->getKeyByIndex(i);
insert(currentKey);
}
}
const size_t getCurrentSize() const {
return currentSize;
}
const T& getKeyByIndex(size_t index) const {
if (index >= currentSize) {
throw std::out_of_range("The index is out of range");
}
return keys[index];
}
const T& getSmallestKey(void) const {
if (currentSize == 0) {
throw EmptyBKeyList("The BKeyList is empty");
}
return keys[0];
}
const T& getLargestKey(void) const {
if (currentSize == 0) {
throw EmptyBKeyList("The BKeyList is empty");
}
return keys[currentSize - 1];
}
const bool isEmpty(void) const {
return (currentSize == 0);
}
private: // Private helper functions
void insertByIndex(const T& key, size_t index) {
if (currentSize >= order) {
throw InsertionFailure("Failed to insert the key");
}
for (size_t i = currentSize; i > index; i--) {
keys[i] = keys[i - 1];
}
keys[index] = key;
currentSize++;
}
void removeByIndex(const T& key, size_t index) {
if (index >= currentSize) {
throw std::out_of_range("The index is out of range");
}
else if (keys[index] != key) {
throw DeletionFailure("Failed to delete the key");
}
else {
currentSize--;
for (size_t i = index; i < currentSize; i++) {
keys[i] = keys[i + 1];
}
}
}
};
#endif // !__B_KEY_LIST__