INDEX_TEMPLATE_ARGUMENTS intB_PLUS_TREE_LEAF_PAGE_TYPE::KeyIndex(const KeyType &key, const KeyComparator &comparator)const{ int l = 0; int r = GetSize() - 1; while (l < r) { int mid = (l + r) >> 1; if (comparator(KeyAt(mid), key) >= 0) { r = mid; } else { l = mid + 1; } } if (comparator(KeyAt(l), key) < 0) { returnGetSize(); } return l; }
Set and Get
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
INDEX_TEMPLATE_ARGUMENTS KeyType B_PLUS_TREE_LEAF_PAGE_TYPE::KeyAt(int index)const{ // replace with your own code return array[index].first; }
/* * Helper method to find and return the key & value pair associated with input * "index"(a.k.a array offset) */ INDEX_TEMPLATE_ARGUMENTS const MappingType &B_PLUS_TREE_LEAF_PAGE_TYPE::GetItem(int index){ // replace with your own code return array[index]; }
/* * Remove half of key & value pairs from this page to "recipient" page */ INDEX_TEMPLATE_ARGUMENTS voidB_PLUS_TREE_LEAF_PAGE_TYPE::MoveHalfTo(BPlusTreeLeafPage *recipient){ int start_index = GetMinSize(); int move_num = GetSize() - start_index; recipient->CopyNFrom(array + start_index, move_num); IncreaseSize(-move_num); }
/* * Copy starting from items, and copy {size} number of elements into me. */ INDEX_TEMPLATE_ARGUMENTS voidB_PLUS_TREE_LEAF_PAGE_TYPE::CopyNFrom(MappingType *items, int size){ std::copy(items, items + size, array + GetSize()); IncreaseSize(size); }
/***************************************************************************** * MERGE *****************************************************************************/ /* * Remove all of key & value pairs from this page to "recipient" page. Don't forget * to update the next_page id in the sibling page */ INDEX_TEMPLATE_ARGUMENTS voidB_PLUS_TREE_LEAF_PAGE_TYPE::MoveAllTo(BPlusTreeLeafPage *recipient){ recipient->CopyNFrom(array, GetSize()); SetSize(0); }
/***************************************************************************** * REDISTRIBUTE *****************************************************************************/ /* * Remove the first key & value pair from this page to "recipient" page. */ INDEX_TEMPLATE_ARGUMENTS voidB_PLUS_TREE_LEAF_PAGE_TYPE::MoveFirstToEndOf(BPlusTreeLeafPage *recipient){ recipient->CopyLastFrom(array[0]); IncreaseSize(-1); for (int i = 0; i < GetSize(); i++) { array[i] = array[i + 1]; } }
/* * Copy the item into the end of my item list. (Append item to my array) */ INDEX_TEMPLATE_ARGUMENTS voidB_PLUS_TREE_LEAF_PAGE_TYPE::CopyLastFrom(const MappingType &item){ array[GetSize()] = item; IncreaseSize(1); }
/* * Remove the last key & value pair from this page to "recipient" page. */ INDEX_TEMPLATE_ARGUMENTS voidB_PLUS_TREE_LEAF_PAGE_TYPE::MoveLastToFrontOf(BPlusTreeLeafPage *recipient){ recipient->CopyFirstFrom(array[GetSize() - 1]); IncreaseSize(-1); }
/* * Insert item at the front of my items. Move items accordingly. */ INDEX_TEMPLATE_ARGUMENTS voidB_PLUS_TREE_LEAF_PAGE_TYPE::CopyFirstFrom(const MappingType &item){ for (int i = GetSize(); i >= 0; i--) { array[i + 1] = array[i]; } array[0] = item; IncreaseSize(1); }