Skip to content

Commit

Permalink
changed type of Keys from shared pointers to keys
Browse files Browse the repository at this point in the history
  • Loading branch information
riasc committed Jul 31, 2024
1 parent 2b7166b commit 9452e52
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 16 deletions.
12 changes: 6 additions & 6 deletions include/Node.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ class Node {
// getter & setter
int getOrder();
void setOrder(int k);
std::vector<KeyPtr>& getKeys();
void setKeys(std::vector<KeyPtr> keys);
std::vector<Key*>& getKeys();
void setKeys(std::vector<Key*>& keys);
std::vector<Node*>& getChildren();
void setChildren(std::vector<Node*> children);
void setNext(Node* next);
Expand All @@ -36,12 +36,12 @@ class Node {
* @param key the key (Interval, Node) to add
* @param index the index at which to add the key
*/
void insertKey(std::shared_ptr<Key> key);
void insertKey(std::shared_ptr<Key> key, int index);
void insertKey(Key* key);
void insertKey(Key* key, int index);
/*
* @brief calculates the (parent) node interval based on the keys (of the child)
*/
dtp::Interval calcParentKey();
Interval calcParentKey();
//
// /*
// * @brief moves keys from one node to another
Expand All @@ -68,7 +68,7 @@ class Node {

private:
int order; // order of the Node
std::vector<KeyPtr> keys;
std::vector<Key*> keys;
std::vector<Node*> children; // children of the Node
Node* next;
bool isLeaf; // is the Node a leaf
Expand Down
42 changes: 32 additions & 10 deletions src/Node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,32 +6,54 @@ Node::Node(int k) : order(k), keys{}, children{}, next{}, isLeaf{false} {}
// getter & setter
int Node::getOrder() { return this->order; }
void Node::setOrder(int k) { this->order = k; }
std::vector<std::shared_ptr<Key>>& Node::getKeys() { return this->keys; }
void Node::setKeys(std::vector<std::shared_ptr<Key>> keys) { this->keys = keys; }
std::vector<Key*>& Node::getKeys() { return this->keys; }
void Node::setKeys(std::vector<Key*>& keys) { this->keys = keys; }
std::vector<Node*>& Node::getChildren() { return this->children; }
void Node::setChildren(std::vector<Node*> children) { this->children = children; }
void Node::setNext(Node* next) { this->next = next; }
Node* Node::getNext() { return this->next; }
void Node::setIsLeaf(bool leaf) { this->isLeaf = leaf; }
bool Node::getIsLeaf() { return this->isLeaf; }

void Node::insertKey(std::shared_ptr<Key> key) {
void Node::insertKey(Key* key) {
std::cout << "insert key: ";
std::cout << "[" << key->getInterval().getStart() << "," << key->getInterval().getEnd() << "]\n";
std::cout << "keys in node: ";
for(int i = 0; i < this->keys.size(); ++i) {
std::cout << "[" << this->keys[i]->getInterval().getStart() << ","
<< this->keys[i]->getInterval().getEnd() << "] ";
}
std::cout << "\n";

for(int i = 0; i < this->keys.size(); ++i) {
std::cout << key->getInterval().getStart() << "," << key->getInterval().getEnd();
std::cout << " > " << getKeys()[i]->getInterval().getStart() << "," << getKeys()[i]->getInterval().getEnd();
std::cout << ": " << (key->getInterval() > this->keys[i]->getInterval()) << "\n";

if(key->getInterval().getStart() > this->keys[i]->getInterval().getStart()) {
std::cout << "getter and setter check: ";
std::cout << key->getInterval().getStart() << "," << key->getInterval().getEnd();
std::cout << " > " << getKeys()[i]->getInterval().getStart() << "," << getKeys()[i]->getInterval().getEnd();
std::cout << ": " << (key->getInterval().getStart() > this->keys[i]->getInterval().getStart()) << "\n";
}
}

int i=0;
while(i < this->keys.size() && key > this->keys[i]) { i++; }
while(i < this->keys.size() && key->getInterval() > this->keys[i]->getInterval()) { i++; }
std::cout << "at index: " << i << "\n";
this->keys.insert(this->keys.begin() + i, key);
std::cout << "Inserted key: " << key->getInterval().first << "," << key->getInterval().second << "\n";
}
void Node::insertKey(std::shared_ptr<Key> key, int index) {
void Node::insertKey(Key* key, int index) {
this->keys.insert(this->keys.begin() + index, key);
}
/*
* Calculates the (parent) node interval based on the keys (of the child)
*/
dtp::Interval Node::calcParentKey() {
dtp::Interval intvl = {std::string::npos, 0};
Interval Node::calcParentKey() {
Interval intvl{std::string::npos, 0};
for(int i=0; i < keys.size(); i++) {
if(keys[i]->getInterval().first < intvl.first) { intvl.first = keys[i]->getInterval().first; }
if(keys[i]->getInterval().second > intvl.second) { intvl.second = keys[i]->getInterval().second; }
if(keys[i]->getInterval().getStart() < intvl.getStart()) { intvl.setStart(keys[i]->getInterval().getStart()); }
if(keys[i]->getInterval().getEnd() > intvl.getEnd()) { intvl.setEnd(keys[i]->getInterval().getEnd()); }
}
return intvl;
}
Expand Down

0 comments on commit 9452e52

Please sign in to comment.