Commit 33ed5c4a authored by Philipp Götze's avatar Philipp Götze

🐞 Renamed find functions to be more clear and fixed a corresponding bug

parent 964ef9da
......@@ -541,30 +541,30 @@ class BitHPBPTree {
if (pos > 0 && (prevNumKeys = leafRef.prevLeaf->bits.get_ro().count()) > middle) {
/// we have a sibling at the left for rebalancing the keys
balanceLeafNodes(leafRef.prevLeaf, leaf);
const auto newMin = leafRef.keys.get_ro()[findMinKey(leafRef.keys.get_ro(),
leafRef.bits.get_ro())];
const auto prevPos = findMinKeyGreaterThan(leafRef.keys.get_ro(), leafRef.bits.get_ro(),
newMin);
const auto newMin = leafRef.keys.get_ro()[findMinKeyPos(leafRef.keys.get_ro(),
leafRef.bits.get_ro())];
const auto prevPos = findMinKeyPosGreaterThan(leafRef.keys.get_ro(), leafRef.bits.get_ro(),
newMin);
nodeRef.keys[prevPos] = newMin;
} else if (pos < N && leafRef.nextLeaf->bits.get_ro().count() > middle) {
/// we have a sibling at the right for rebalancing the keys
balanceLeafNodes(leafRef.nextLeaf, leaf);
const auto &nextLeaf = *leafRef.nextLeaf;
nodeRef.keys[pos] =
nextLeaf.keys.get_ro()[findMinKey(nextLeaf.keys.get_ro(), nextLeaf.bits.get_ro())];
nextLeaf.keys.get_ro()[findMinKeyPos(nextLeaf.keys.get_ro(), nextLeaf.bits.get_ro())];
} else {
/// 2. if this fails we have to merge two leaf nodes but only if both nodes have the same
/// direct parent
pptr<LeafNode> survivor = nullptr;
if (findMinKey(nodeRef.keys, nodeRef.bits) != pos && prevNumKeys <= middle) {
if (findMinKeyPos(nodeRef.keys, nodeRef.bits) != pos && prevNumKeys <= middle) {
/// merge left
survivor = mergeLeafNodes(leafRef.prevLeaf, leaf);
deleteLeafNode(leaf);
/// move to next left slot
const auto prevPos = (pos == N) ?
findMaxKey(nodeRef.keys, nodeRef.bits) : ///< we need a new rightmost node
findMaxKeySmallerThan(nodeRef.keys, nodeRef.bits, nodeRef.keys[pos]);
findMaxKeyPos(nodeRef.keys, nodeRef.bits) : ///< we need a new rightmost node
findMaxKeyPosSmallerThan(nodeRef.keys, nodeRef.bits, nodeRef.keys[pos]);
nodeRef.children[pos] = nodeRef.children[prevPos];
nodeRef.bits.reset(prevPos);
} else if (pos < N && leafRef.nextLeaf->bits.get_ro().count() <= middle) {
......@@ -572,7 +572,7 @@ class BitHPBPTree {
survivor = mergeLeafNodes(leaf, leafRef.nextLeaf);
deleteLeafNode(leafRef.nextLeaf);
/// move to next right slot
const auto nextPos = findMinKeyGreaterThan(nodeRef.keys, nodeRef.bits, nodeRef.keys[pos]);
const auto nextPos = findMinKeyPosGreaterThan(nodeRef.keys, nodeRef.bits, nodeRef.keys[pos]);
nodeRef.children[nextPos] = nodeRef.children[pos];
nodeRef.bits.reset(pos);
} else assert(false); ///< this shouldn't happen?!
......@@ -649,7 +649,7 @@ class BitHPBPTree {
if (donorKeys[0] < receiverKeys[0]) {
/// move from one node to a node with larger keys
for (auto i = 0u; i < toMove; ++i) {
const auto max = findMaxKey(donorKeys, donorBits);
const auto max = findMaxKeyPos(donorKeys, donorBits);
const auto u = BitOperations::getFreeZero(receiverBits);
/// move the donor's maximum key to the receiver
receiverKeys[u] = donorKeys[max];
......@@ -660,7 +660,7 @@ class BitHPBPTree {
} else {
/// move from one node to a node with smaller keys
for (auto i = 0u; i < toMove; ++i) {
const auto min = findMinKey(donorKeys, donorBits);
const auto min = findMinKeyPos(donorKeys, donorBits);
const auto u = BitOperations::getFreeZero(receiverBits);
/// move the donor's minimum key to the receiver
receiverKeys[u] = donorKeys[min];
......@@ -733,10 +733,10 @@ class BitHPBPTree {
assert(node != nullptr);
assert(child != nullptr);
auto &nodeRef = *node;
const auto nMinKeyPos = findMinKey(nodeRef.keys, nodeRef.bits);
const auto prevPos = findMaxKeySmallerThan(nodeRef.keys, nodeRef.bits, nodeRef.keys[pos]); //could be N
const auto nMinKeyPos = findMinKeyPos(nodeRef.keys, nodeRef.bits);
const auto prevPos = findMaxKeyPosSmallerThan(nodeRef.keys, nodeRef.bits, nodeRef.keys[pos]); //could be N
const auto prevNumKeys = nodeRef.children[prevPos].branch->bits.count();
const auto nextPos = findMinKeyGreaterThan(nodeRef.keys, nodeRef.bits, nodeRef.keys[pos]);
const auto nextPos = findMinKeyPosGreaterThan(nodeRef.keys, nodeRef.bits, nodeRef.keys[pos]);
const auto nextNumKeys = nodeRef.children[nextPos].branch->bits.count();
constexpr auto middle = (N + 1) / 2;
......@@ -769,7 +769,7 @@ class BitHPBPTree {
mergeBranchNodes(child, nodeRef.keys[pos], rSibling);
deleteBranchNode(rSibling);
nodeRef.bits.reset(pos);
if (pos == findMaxKey(nodeRef.keys, nodeRef.bits))
if (pos == findMaxKeyPos(nodeRef.keys, nodeRef.bits))
nodeRef.children[N] = child; ///< new rightmost child
return child;
} else {
......@@ -793,8 +793,8 @@ class BitHPBPTree {
const auto &nodeRef = *node;
auto &sibRef = *sibling;
assert(key <= nodeRef.keys[findMinKey(nodeRef.keys, nodeRef.bits)]);
assert(sibRef.keys[findMaxKey(sibRef.keys, sibRef.bits)] < key);
assert(key <= nodeRef.keys[findMinKeyPos(nodeRef.keys, nodeRef.bits)]);
assert(sibRef.keys[findMaxKeyPos(sibRef.keys, sibRef.bits)] < key);
auto u = BitOperations::getFreeZero(sibRef.bits);
sibRef.keys[u] = key;
......@@ -844,7 +844,7 @@ class BitHPBPTree {
receiverRef.bits.set(u);
/// 1.2. move toMove-1 keys/children from donor to receiver
for (auto i = 1u; i < toMove; ++i) {
const auto max = findMaxKey(donorRef.keys, donorRef.bits);
const auto max = findMaxKeyPos(donorRef.keys, donorRef.bits);
u = BitOperations::getFreeZero(receiverRef.bits);
receiverRef.keys[u] = donorRef.keys[max];
receiverRef.children[u] = donorRef.children[max];
......@@ -852,7 +852,7 @@ class BitHPBPTree {
donorRef.bits.reset(max);
}
/// 1.3 set donors new rightmost child and new parent key
const auto dPos = findMaxKey(donorRef.keys, donorRef.bits);
const auto dPos = findMaxKeyPos(donorRef.keys, donorRef.bits);
donorRef.children[N] = donorRef.children[dPos];
parentRef.keys[pos] = donorRef.keys[dPos];
donorRef.bits.reset(dPos);
......@@ -867,14 +867,14 @@ class BitHPBPTree {
/// 2.2. move toMove-1 keys/children from donor to receiver
for (auto i = 1u; i < toMove; ++i) {
u = BitOperations::getFreeZero(receiverRef.bits);
const auto min = findMinKey(donorRef.keys, donorRef.bits);
const auto min = findMinKeyPos(donorRef.keys, donorRef.bits);
receiverRef.keys[u] = donorRef.keys[min];
receiverRef.children[u] = donorRef.children[min];
receiverRef.bits.set(u);
donorRef.bits.reset(min);
}
/// 2.3. set receivers new rightmost child and new parent key
const auto dPos = findMinKey(donorRef.keys, donorRef.bits);
const auto dPos = findMinKeyPos(donorRef.keys, donorRef.bits);
receiverRef.children[N] = donorRef.children[dPos];
parentRef.keys[pos] = donorRef.keys[dPos];
donorRef.bits.reset(dPos);
......@@ -917,7 +917,7 @@ class BitHPBPTree {
if (key > splitRef.key) {
insertInLeafNodeAtPosition(sibling, BitOperations::getFreeZero(sibRef.bits.get_ro()), key, val);
} else {
if (key > findMaxKey(nodeRef.keys.get_ro(), nodeRef.bits.get_ro())) {
if (key > nodeRef.keys.get_ro()[findMaxKeyPos(nodeRef.keys.get_ro(), nodeRef.bits.get_ro())]) {
/// Special case: new key would be the middle, thus must be right
insertInLeafNodeAtPosition(sibling, BitOperations::getFreeZero(sibRef.bits.get_ro()), key, val);
splitRef.key = key;
......@@ -1085,7 +1085,7 @@ class BitHPBPTree {
/// Insert new key and children
auto &hostRef = *host;
const auto u = BitOperations::getFreeZero(hostRef.bits);
const auto nextPos = findMinKeyGreaterThan(hostRef.keys, hostRef.bits, childSplitInfo.key);
const auto nextPos = findMinKeyPosGreaterThan(hostRef.keys, hostRef.bits, childSplitInfo.key);
hostRef.keys[u] = childSplitInfo.key;
hostRef.children[u] = childSplitInfo.leftChild;
......@@ -1130,7 +1130,7 @@ class BitHPBPTree {
* @return the position of the key + 1 (or 0 or @c N)
*/
auto lookupPositionInBranchNode(const BranchNode *node, const KeyType &key) const {
return findMinKeyGreaterThan(node->keys, node->bits, key);
return findMinKeyPosGreaterThan(node->keys, node->bits, key);
}
/**
......@@ -1206,14 +1206,14 @@ class BitHPBPTree {
BranchNode *newNode = newBranchNode();
newNode->children[0] = leaf;
auto &splitRef = *splitInfo;
splitRef.key = leafRef.keys.get_ro()[findMinKey(leafRef.keys.get_ro(),
splitRef.key = leafRef.keys.get_ro()[findMinKeyPos(leafRef.keys.get_ro(),
leafRef.bits.get_ro())];
splitRef.leftChild = node;
splitRef.rightChild = newNode;
return true;
} else {
nodeRef.bits.set(nNumKeys);
nodeRef.keys[nNumKeys] = leafRef.keys.get_ro()[findMinKey(leafRef.keys.get_ro(),
nodeRef.keys[nNumKeys] = leafRef.keys.get_ro()[findMinKeyPos(leafRef.keys.get_ro(),
leafRef.bits.get_ro())];
if (nNumKeys > 0)
nodeRef.children[nNumKeys] = nodeRef.children[N];
......
......@@ -477,16 +477,16 @@ class BitPBPTree {
/// we have a sibling at the left for rebalancing the keys
balanceLeafNodes(leafRef.prevLeaf, leaf);
const auto newMin =
leafRef.keys.get_ro()[findMinKey(leafRef.keys.get_ro(), leafRef.bits.get_ro())];
leafRef.keys.get_ro()[findMinKeyPos(leafRef.keys.get_ro(), leafRef.bits.get_ro())];
const auto prevPos =
findMinKeyGreaterThan(leafRef.keys.get_ro(), leafRef.bits.get_ro(), newMin);
findMinKeyPosGreaterThan(leafRef.keys.get_ro(), leafRef.bits.get_ro(), newMin);
nodeKeys[prevPos] = newMin;
} else if (pos < N && leafRef.nextLeaf->bits.get_ro().count() > middle) {
/// we have a sibling at the right for rebalancing the keys
balanceLeafNodes(leafRef.nextLeaf, leaf);
const auto &nextLeaf = *leafRef.nextLeaf;
nodeKeys[pos] =
nextLeaf.keys.get_ro()[findMinKey(nextLeaf.keys.get_ro(), nextLeaf.bits.get_ro())];
nextLeaf.keys.get_ro()[findMinKeyPos(nextLeaf.keys.get_ro(), nextLeaf.bits.get_ro())];
} else {
/// 2. if this fails we have to merge two leaf nodes but only if both nodes have the same
/// direct parent
......@@ -494,14 +494,14 @@ class BitPBPTree {
auto &nodeChilds = nodeRef.children.get_rw();
auto &nodeBits = nodeRef.bits.get_rw();
if (findMinKey(nodeKeys, nodeBits) != pos && prevNumKeys <= middle) {
if (findMinKeyPos(nodeKeys, nodeBits) != pos && prevNumKeys <= middle) {
/// merge left
survivor = mergeLeafNodes(leafRef.prevLeaf, leaf);
deleteLeafNode(leaf);
/// move to next left slot
const auto prevPos = (pos == N) ? findMaxKey(nodeKeys, nodeBits)
const auto prevPos = (pos == N) ? findMaxKeyPos(nodeKeys, nodeBits)
: ///< we need a new rightmost node
findMaxKeySmallerThan(nodeKeys, nodeBits, nodeKeys[pos]);
findMaxKeyPosSmallerThan(nodeKeys, nodeBits, nodeKeys[pos]);
nodeChilds[pos] = nodeChilds[prevPos];
nodeBits.reset(prevPos);
} else if (pos < N && leafRef.nextLeaf->bits.get_ro().count() <= middle) {
......@@ -509,7 +509,7 @@ class BitPBPTree {
survivor = mergeLeafNodes(leaf, leafRef.nextLeaf);
deleteLeafNode(leafRef.nextLeaf);
/// move to next right slot
const auto nextPos = findMinKeyGreaterThan(nodeKeys, nodeBits, nodeKeys[pos]);
const auto nextPos = findMinKeyPosGreaterThan(nodeKeys, nodeBits, nodeKeys[pos]);
nodeChilds[nextPos] = nodeChilds[pos];
nodeBits.reset(pos);
} else {
......@@ -596,7 +596,7 @@ class BitPBPTree {
if (donorKeys[0] < receiverKeys[0]) {
/// move from one node to a node with larger keys
for (auto i = 0u; i < toMove; ++i) {
const auto max = findMaxKey(donorKeys, donorBits);
const auto max = findMaxKeyPos(donorKeys, donorBits);
const auto u = BitOperations::getFreeZero(receiverBits);
/// move the donor's maximum key to the receiver
receiverKeys[u] = donorKeys[max];
......@@ -607,7 +607,7 @@ class BitPBPTree {
} else {
/// move from one node to a node with smaller keys
for (auto i = 0u; i < toMove; ++i) {
const auto min = findMinKey(donorKeys, donorBits);
const auto min = findMinKeyPos(donorKeys, donorBits);
const auto u = BitOperations::getFreeZero(receiverBits);
/// move the donor's minimum key to the receiver
receiverKeys[u] = donorKeys[min];
......@@ -688,10 +688,10 @@ class BitPBPTree {
const auto &nodeKeys = nodeRef.keys.get_ro();
const auto &nodeChilds = nodeRef.children.get_ro();
auto &nodeBits = nodeRef.bits.get_rw();
const auto nMinKeyPos = findMinKey(nodeKeys, nodeBits);
const auto prevPos = findMaxKeySmallerThan(nodeKeys, nodeBits, nodeKeys[pos]); //could be N
const auto nMinKeyPos = findMinKeyPos(nodeKeys, nodeBits);
const auto prevPos = findMaxKeyPosSmallerThan(nodeKeys, nodeBits, nodeKeys[pos]); //could be N
const auto prevNumKeys = nodeChilds[prevPos].branch->bits.get_ro().count();
const auto nextPos = findMinKeyGreaterThan(nodeKeys, nodeBits, nodeKeys[pos]);
const auto nextPos = findMinKeyPosGreaterThan(nodeKeys, nodeBits, nodeKeys[pos]);
const auto nextNumKeys = nodeChilds[nextPos].branch->bits.get_ro().count();
constexpr auto middle = (N + 1) / 2;
......@@ -724,7 +724,7 @@ class BitPBPTree {
mergeBranchNodes(child, nodeKeys[pos], rSibling);
deleteBranchNode(rSibling);
nodeBits.reset(pos);
if (pos == findMaxKey(nodeKeys, nodeBits))
if (pos == findMaxKeyPos(nodeKeys, nodeBits))
nodeRef.children.get_rw()[N] = child; ///< new rightmost child
return child;
} else {
......@@ -755,8 +755,8 @@ class BitPBPTree {
auto &sibChilds = sibRef.children.get_rw();
auto &sibBits = sibRef.bits.get_rw();
assert(key <= nodeKeys[findMinKey(nodeKeys, nodeBits)]);
assert(sibKeys[findMaxKey(sibKeys, sibBits)] < key);
assert(key <= nodeKeys[findMinKeyPos(nodeKeys, nodeBits)]);
assert(sibKeys[findMaxKeyPos(sibKeys, sibBits)] < key);
auto u = BitOperations::getFreeZero(sibBits);
sibKeys[u] = key;
......@@ -813,7 +813,7 @@ class BitPBPTree {
receiverBits.set(u);
/// 1.2. move toMove-1 keys/children from donor to receiver
for (auto i = 1u; i < toMove; ++i) {
const auto max = findMaxKey(donorKeys, donorBits);
const auto max = findMaxKeyPos(donorKeys, donorBits);
u = BitOperations::getFreeZero(receiverBits);
receiverKeys[u] = donorKeys[max];
receiverChilds[u] = donorChilds[max];
......@@ -821,7 +821,7 @@ class BitPBPTree {
donorBits.reset(max);
}
/// 1.3 set donors new rightmost child and new parent key
const auto dPos = findMaxKey(donorKeys, donorBits);
const auto dPos = findMaxKeyPos(donorKeys, donorBits);
donorChilds[N] = donorChilds[dPos];
parentKeys[pos] = donorKeys[dPos];
donorBits.reset(dPos);
......@@ -836,14 +836,14 @@ class BitPBPTree {
/// 2.2. move toMove-1 keys/children from donor to receiver
for (auto i = 1u; i < toMove; ++i) {
u = BitOperations::getFreeZero(receiverBits);
const auto min = findMinKey(donorKeys, donorBits);
const auto min = findMinKeyPos(donorKeys, donorBits);
receiverKeys[u] = donorKeys[min];
receiverChilds[u] = donorChilds[min];
receiverBits.set(u);
donorBits.reset(min);
}
/// 2.3. set receivers new rightmost child and new parent key
const auto dPos = findMinKey(donorKeys, donorBits);
const auto dPos = findMinKeyPos(donorKeys, donorBits);
receiverChilds[N] = donorChilds[dPos];
parentKeys[pos] = donorKeys[dPos];
donorBits.reset(dPos);
......@@ -886,7 +886,7 @@ class BitPBPTree {
if (key > splitRef.key) {
insertInLeafNodeAtPosition(sibling, BitOperations::getFreeZero(sibRef.bits.get_ro()), key, val);
} else {
if (key > findMaxKey(nodeRef.keys.get_ro(), nodeRef.bits.get_ro())) {
if (key > nodeRef.keys.get_ro()[findMaxKeyPos(nodeRef.keys.get_ro(), nodeRef.bits.get_ro())]) {
/// Special case: new key would be the middle, thus must be right
insertInLeafNodeAtPosition(sibling, BitOperations::getFreeZero(sibRef.bits.get_ro()), key, val);
splitRef.key = key;
......@@ -1066,7 +1066,7 @@ class BitPBPTree {
auto &hostChilds = hostRef.children.get_rw();
auto &hostBits = hostRef.bits.get_rw();
const auto u = BitOperations::getFreeZero(hostBits);
const auto nextPos = findMinKeyGreaterThan(hostKeys, hostBits, childSplitInfo.key);
const auto nextPos = findMinKeyPosGreaterThan(hostKeys, hostBits, childSplitInfo.key);
hostKeys[u] = childSplitInfo.key;
hostChilds[u] = childSplitInfo.leftChild;
......@@ -1112,7 +1112,7 @@ class BitPBPTree {
*/
auto lookupPositionInBranchNode(const pptr<BranchNode> &node, const KeyType &key) const {
const auto &nodeRef = *node;
return findMinKeyGreaterThan(nodeRef.keys.get_ro(), nodeRef.bits.get_ro(), key);
return findMinKeyPosGreaterThan(nodeRef.keys.get_ro(), nodeRef.bits.get_ro(), key);
}
/**
......
......@@ -513,7 +513,7 @@ class FPTree {
if (key > splitRef.key) {
insertInLeafNodeAtPosition(sibling, BitOperations::getFreeZero(sibRef.bits.get_ro()), key, val);
} else {
if (key > findMaxKey(nodeRef.keys.get_ro(), nodeRef.bits.get_ro())) {
if (key > nodeRef.keys.get_ro()[findMaxKeyPos(nodeRef.keys.get_ro(), nodeRef.bits.get_ro())]) {
/// Special case: new key would be the middle, thus must be right
insertInLeafNodeAtPosition(sibling, BitOperations::getFreeZero(sibRef.bits.get_ro()), key, val);
splitRef.key = key;
......@@ -869,13 +869,13 @@ class FPTree {
balanceLeafNodes(leafRef.prevLeaf, leaf);
nodeRef.keys[pos - 1] =
leafRef.keys.get_ro()[findMinKey(leafRef.keys.get_ro(), leafRef.bits.get_ro())];
leafRef.keys.get_ro()[findMinKeyPos(leafRef.keys.get_ro(), leafRef.bits.get_ro())];
} else if (pos < nodeRef.numKeys && leafRef.nextLeaf->bits.get_ro().count() > middle) {
/// we have a sibling at the right for rebalancing the keys
balanceLeafNodes(leafRef.nextLeaf, leaf);
auto &nextLeaf = *leafRef.nextLeaf;
nodeRef.keys[pos] =
nextLeaf.keys.get_ro()[findMinKey(nextLeaf.keys.get_ro(), nextLeaf.bits.get_ro())];
nextLeaf.keys.get_ro()[findMinKeyPos(nextLeaf.keys.get_ro(), nextLeaf.bits.get_ro())];
} else {
/// 2. if this fails we have to merge two leaf nodes but only if both nodes have the same
/// direct parent
......@@ -1011,7 +1011,7 @@ class FPTree {
if (donorKeys[0] < receiverKeys[0]) {
/// move to a node with larger keys
for (auto i = 0u; i < toMove; ++i) {
const auto max = findMaxKey(donorKeys, donorBits);
const auto max = findMaxKeyPos(donorKeys, donorBits);
const auto pos = BitOperations::getFreeZero(receiverBits);
receiverBits.set(pos);
receiverHashs[pos] = fpHash(donorKeys[max]);
......@@ -1022,7 +1022,7 @@ class FPTree {
} else {
/// move to a node with smaller keys
for (auto i = 0u; i < toMove; ++i) {
const auto min = findMinKey(donorKeys, donorBits);
const auto min = findMinKeyPos(donorKeys, donorBits);
const auto pos = BitOperations::getFreeZero(receiverBits);
receiverBits.set(pos);
receiverHashs[pos] = fpHash(donorKeys[min]);
......@@ -1227,13 +1227,13 @@ class FPTree {
auto newNode = newBranchNode();
newNode->children[0] = leaf;
splitRef.key =
leafRef.keys.get_ro()[findMinKey(leafRef.keys.get_ro(), leafRef.bits.get_ro())];
leafRef.keys.get_ro()[findMinKeyPos(leafRef.keys.get_ro(), leafRef.bits.get_ro())];
splitRef.leftChild = node;
splitRef.rightChild = newNode;
return true;
} else {
nodeRef.keys[nodeRef.numKeys] =
leafRef.keys.get_ro()[findMinKey(leafRef.keys.get_ro(), leafRef.bits.get_ro())];
leafRef.keys.get_ro()[findMinKeyPos(leafRef.keys.get_ro(), leafRef.bits.get_ro())];
++nodeRef.numKeys;
nodeRef.children[nodeRef.numKeys] = leaf;
return false;
......
......@@ -430,11 +430,11 @@ class UnsortedPBPTree {
if (pos > 0 && (prevNumKeys = leafRef.prevLeaf->numKeys.get_ro()) > middle) {
/// we have a sibling at the left for rebalancing the keys
balanceLeafNodes(leafRef.prevLeaf, leaf);
nodeKeys[pos-1] = leafRef.keys.get_ro()[findMinKey(leaf)];
nodeKeys[pos-1] = leafRef.keys.get_ro()[findMinKeyPos(leaf)];
} else if (pos < nNumKeys && (nextNumKeys = leafRef.nextLeaf->numKeys.get_ro() > middle)) {
/// we have a sibling at the right for rebalancing the keys
balanceLeafNodes(leafRef.nextLeaf, leaf);
nodeKeys[pos] = leafRef.nextLeaf->keys.get_ro()[findMinKey(leafRef.nextLeaf)];
nodeKeys[pos] = leafRef.nextLeaf->keys.get_ro()[findMinKeyPos(leafRef.nextLeaf)];
} else {
/// 2. if this fails we have to merge two leaf nodes but only if both nodes have the same
// direct parent
......@@ -533,7 +533,7 @@ class UnsortedPBPTree {
/// move from one node to a node with larger keys
/// move toMove keys/values from donor to receiver
for (auto i = 0u; i < toMove; ++i) {
const auto max = findMaxKey(donor);
const auto max = findMaxKeyPos(donor);
/// move the donor's maximum key to the receiver
receiverKeys[rNumKeys] = donorKeys[max];
receiverVals[rNumKeys] = donorVals[max];
......@@ -548,7 +548,7 @@ class UnsortedPBPTree {
/// mode from one node to a node with smaller keys
/// move toMove keys/values from donor to receiver
for (auto i = 0u; i < toMove; ++i) {
const auto min = findMinKey(donor);
const auto min = findMinKeyPos(donor);
/// move the donor's minimum key to the receiver
receiverKeys[rNumKeys] = donorKeys[min];
receiverVals[rNumKeys] = donorVals[min];
......@@ -915,7 +915,7 @@ class UnsortedPBPTree {
if (key > splitRef.key) {
insertInLeafNodeAtLastPosition(sibling, key, val);
} else {
if (key > findMaxKey(node)) {
if (key > nodeRef.keys.get_ro()[findMaxKeyPos(node)]) {
/// Special case: new key would be the middle, thus must be right
insertInLeafNodeAtLastPosition(sibling, key, val);
splitRef.key = key;
......@@ -925,7 +925,7 @@ class UnsortedPBPTree {
}
/* inform the caller about the split */
splitRef.key = sibling->keys.get_ro()[findMinKey(sibling)];
splitRef.key = sibling->keys.get_ro()[findMinKeyPos(sibling)];
split = true;
} else {
/* otherwise, we can simply insert the new entry at the last position */
......
......@@ -24,6 +24,7 @@
#include <array>
#include <bitset>
#include <cstdlib> //< rand()
#include <utility> //< pair
namespace dbis {
......@@ -36,10 +37,10 @@ using pptr = pmem::obj::persistent_ptr<Object>;
class ElementOfRankK {
public:
template <typename KeyType>
static size_t elementOfRankK(const size_t k, KeyType data[], const size_t start,
const size_t length) {
const size_t end = start + length - 1;
size_t i;
static std::size_t elementOfRankK(const std::size_t k, KeyType data[], const std::size_t start,
const std::size_t length) {
const std::size_t end = start + length - 1;
std::size_t i;
const auto parts = (length + 4) / 5;
KeyType median[parts];
for (i = 0; i < length / 5; ++i) median[i] = findMedian(data + start + i * 5, 5);
......@@ -48,7 +49,7 @@ class ElementOfRankK {
++i;
}
const KeyType medOfMed = (i == 1)? median[i - 1]: elementOfRankK(i / 2, median, 0, i);
const size_t pos = partition(data, start, end, medOfMed);
const std::size_t pos = partition(data, start, end, medOfMed);
if (pos - start == k - 1) return data[pos];
if (pos - start > k - 1) return elementOfRankK(k, data, start, pos - start);
return elementOfRankK(k - pos + start - 1, data, pos + 1, end - pos);
......@@ -57,14 +58,14 @@ class ElementOfRankK {
private:
template <typename KeyType>
static inline KeyType findMedian(KeyType data[], const size_t length) {
static inline KeyType findMedian(KeyType data[], const std::size_t length) {
std::sort(data, data + length);
return data[length / 2];
}
template <typename KeyType>
static auto partition(KeyType data[], const size_t start, const size_t end, const KeyType &x) {
size_t i;
static auto partition(KeyType data[], const std::size_t start, const std::size_t end, const KeyType &x) {
std::size_t i;
for (i = start; i < end; ++i) if (data[i] == x) break;
std::swap(data[i], data[end]);
......@@ -78,15 +79,15 @@ class ElementOfRankK {
}; /* end class */
template<typename KeyType, size_t M>
std::pair<std::bitset<M>, size_t>
template<typename KeyType, std::size_t M>
std::pair<std::bitset<M>, std::size_t>
findSplitKey(const KeyType * const data) {
///TODO: can this be accelerated?
std::array<KeyType, M> repData;
memcpy(repData.begin(), data, M * sizeof(KeyType));
std::bitset<M> b{};
const auto splitKey = ElementOfRankK::elementOfRankK((M+1)/2 + 1, repData.data(), 0, M);
size_t splitPos;
std::size_t splitPos;
for(auto i = 0u; i < M; ++i) {
if(data[i] < splitKey) {
b.set(i);
......@@ -102,7 +103,7 @@ findSplitKey(const KeyType * const data) {
* @return position of the minimum key
*/
template<typename Node>
static inline auto findMinKey(const pptr<Node> &node) {
static inline auto findMinKeyPos(const pptr<Node> &node) {
const auto &nodeRef = *node;
const auto &keysRef = nodeRef.keys.get_ro();
auto pos = 0u;
......@@ -120,8 +121,8 @@ static inline auto findMinKey(const pptr<Node> &node) {
* @param node the node to find the minimum key in
* @return position of the minimum key
*/
template<typename KeyType, size_t N>
static inline auto findMinKey(const std::array<KeyType, N> &keysRef,
template<typename KeyType, std::size_t N>
static inline auto findMinKeyPos(const std::array<KeyType, N> &keysRef,
const std::bitset<N> &bitsRef) {
auto pos = 0u;
auto currMinKey = std::numeric_limits<KeyType>::max();
......@@ -140,7 +141,7 @@ static inline auto findMinKey(const std::array<KeyType, N> &keysRef,
* @return position of the maximum key
*/
template<typename Node>
static inline auto findMaxKey(const pptr<Node> &node) {
static inline auto findMaxKeyPos(const pptr<Node> &node) {
const auto &nodeRef = *node;
const auto &keysRef = nodeRef.keys.get_ro();
auto pos = 0u;
......@@ -159,8 +160,8 @@ static inline auto findMaxKey(const pptr<Node> &node) {
* @param bitsRef a reference to the bitset of the node
* @return position of the maximum key
*/
template<typename KeyType, size_t N>
static inline auto findMaxKey(const std::array<KeyType, N> &keysRef,
template<typename KeyType, std::size_t N>
static inline auto findMaxKeyPos(const std::array<KeyType, N> &keysRef,
const std::bitset<N> &bitsRef) {
auto pos = 0u;
auto currMaxKey = std::numeric_limits<KeyType>::min();
......@@ -180,8 +181,8 @@ static inline auto findMaxKey(const std::array<KeyType, N> &keysRef,
* @param key the current minimum key
* @return position of the next minimum key
*/
template<size_t N, typename KeyType>
static inline auto findMinKeyGreaterThan(const std::array<KeyType, N> &keysRef,
template<std::size_t N, typename KeyType>
static inline auto findMinKeyPosGreaterThan(const std::array<KeyType, N> &keysRef,
const std::bitset<N> &bitsRef,
const KeyType &key) {
auto pos = 0ul;
......@@ -204,8 +205,8 @@ static inline auto findMinKeyGreaterThan(const std::array<KeyType, N> &keysRef,
* @param key the current maximum key
* @return position of the next maximum key
*/
template<size_t N, typename KeyType>
static inline auto findMaxKeySmallerThan(const std::array<KeyType, N> &keysRef,
template<std::size_t N, typename KeyType>
static inline auto findMaxKeyPosSmallerThan(const std::array<KeyType, N> &keysRef,
const std::bitset<N> &bitsRef,
const KeyType &key) {
auto pos = 0ul;
......@@ -229,7 +230,7 @@ static inline auto findMaxKeySmallerThan(const std::array<KeyType, N> &keysRef,
* @param key the searched key
* @return position of the key
*/
template<bool isBranch, typename KeyType, size_t N>
template<bool isBranch, typename KeyType, std::size_t N>
static inline unsigned int binarySearch(const std::array<KeyType, N> &keys, int l, int r,
const KeyType &key) {
auto pos = 0u;
......@@ -255,7 +256,7 @@ static inline unsigned int binarySearch(const std::array<KeyType, N> &keys, int
* @param key the searched key
* @return position of the key
*/
template<bool isBranch, typename KeyType, size_t N>
template<bool isBranch, typename KeyType, std::size_t N>
static inline unsigned int binarySearch(const std::array<KeyType, N> &keys,
const std::array<uint8_t, N+1> &slots, int l, int r,
const KeyType &key) {
......
......@@ -348,7 +348,7 @@ TEST_CASE("Finding the leaf node containing a key", "[BitHPBPTree]") {
btree.underflowAtBranchLevel(innerNodes[0], 0, innerNodes[1]);
REQUIRE(node1Ref.bits.count() == 1);
REQUIRE((node1Ref.keys)[dbis::findMinKey(node1Ref.keys, node1Ref.bits)] == 9);
REQUIRE((node1Ref.keys)[dbis::findMinKeyPos(node1Ref.keys, node1Ref.bits)] == 9);
REQUIRE(node2Ref.bits.count() == 2);
REQUIRE(node3Ref.bits.count() == 2);
......
......@@ -348,7 +348,7 @@ TEST_CASE("Finding the leaf node containing a key", "[BitPBPTree]") {
btree.underflowAtBranchLevel(innerNodes[0], 0, innerNodes[1]);
REQUIRE(node1Ref.bits.get_ro().count() == 1);
REQUIRE((node1Ref.keys.get_ro())[dbis::findMinKey(node1Ref.keys.get_ro(), node1Ref.bits.get_ro())] == 9);
REQUIRE((node1Ref.keys.get_ro())[dbis::findMinKeyPos(node1Ref.keys.get_ro(), node1Ref.bits.get_ro())] == 9);
REQUIRE(node2Ref.bits.get_ro().count() == 2);
REQUIRE(node3Ref.bits.get_ro().count() == 2);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment