NBBST<T, Threads>::NBBST(){ root = newInternal(std::numeric_limits<int>::max()); root->update = Mark(nullptr, CLEAN); root->left = newLeaf(std::numeric_limits<int>::min()); root->right = newLeaf(std::numeric_limits<int>::max()); }
/** * @brief Creates a new branch with the specified boundaries * * This function creates a new branch on the heap with the specified boundaries * * @param l The lower bound x-value * @param r The upper bound x-value * @param b The lower bound y-value * @param t The upper bound y-value * @return A pointer to the new branch object */ branch* Quadtree::newBranch(float l, float r, float b, float t) { // Create branch in heap and put pointer into branch list branch *currBranch = new branch; branchList.push_back(currBranch); // Set the boundaries of the branch currBranch->bounds[0] = l; currBranch->bounds[1] = r; currBranch->bounds[2] = b; currBranch->bounds[3] = t; // Set all branch pointers in the branch to 0 for (int i=0; i<4; i++) currBranch->branches[i] = 0; // Create all four leaves in the branch float x = l+(r-l)/2.0; float y = b+(t-b)/2.0; currBranch->leaves[0] = newLeaf(x, r, y, t); currBranch->leaves[1] = newLeaf(l, x, y, t); currBranch->leaves[2] = newLeaf(l, x, b, y); currBranch->leaves[3] = newLeaf(x, r, b, y); return currBranch; }
void FiberSelector::updatePresentRois() { qDebug() << "updatePresentRois"; int numBranches = Models::r()->rowCount( QModelIndex() ); for ( int i = 0; i < numBranches; ++i ) { QList<QVector<bool> >newBranch; QVector<bool>newLeaf( m_numLines ); newBranch.push_back( newLeaf ); m_branchfields.push_back( newLeaf ); m_bitfields.push_back( newBranch ); updateBox( m_bitfields.size() - 1, 0 ); int leafCount = Models::r()->rowCount( createIndex( i, 0, 0 ) ); for ( int k = 0; k < leafCount; ++k ) { // inserted child roi QVector<bool>newLeaf( m_numLines ); m_bitfields[i].push_back( newLeaf ); updateBox( i, k + 1 ); } } }
void FiberSelector::roiInserted( const QModelIndex &parent, int start, int end ) { //qDebug() << "roi inserted" << parent.row() << start << end; if ( parent.row() == -1 ) { // inserted top level roi QList<QVector<bool> >newBranch; QVector<bool>newLeaf( m_numLines ); newBranch.push_back( newLeaf ); m_branchfields.push_back( newLeaf ); m_bitfields.push_back( newBranch ); updateBox( m_bitfields.size() - 1, 0 ); } else { // inserted child roi QVector<bool>newLeaf( m_numLines ); m_bitfields[parent.row()].push_back( newLeaf ); updateBox( parent.row(), m_bitfields[parent.row()].size() - 1 ); } }
inline std::pair<BufferFrame *, Leaf<KeyType, KeyComparator> *> BTree<KeyType, KeyComparator>::splitLeaf (Leaf<KeyType, KeyComparator> *leaf, BufferFrame *leafFrame, uint64_t leafPageId, InnerNode<KeyType, KeyComparator> *parent, KeyType key) { if (parent == nullptr) { throw std::invalid_argument("Parent is null"); } // invariant: parent node has space for one more entry Leaf<KeyType, KeyComparator> newLeaf(leafPageId, LeafHeader::INVALID_PAGE_ID); size_t arraySplitIndex = leaf->header.keyCount / 2; size_t splitLength = leaf->header.keyCount - arraySplitIndex; KeyType splitKey = leaf->entries[arraySplitIndex].key; size_t entrySize = sizeof(leaf->entries[0]); uint64_t newPageId = nextPageId(); leaf->header.nextLeafPageId = newPageId; memcpy(newLeaf.entries, leaf->entries + arraySplitIndex, splitLength * entrySize); leaf->header.keyCount = arraySplitIndex; newLeaf.header.keyCount = splitLength; parent->insertDefiniteFit(splitKey, leafPageId, newPageId, smallerComparator); BufferFrame &newFrame = bufferManager.fixPage(this->segmentId, newPageId, true); void *newFrameData = newFrame.getData(); memcpy(newFrameData, &newLeaf, sizeof(newLeaf)); if (smallerComparator(key, splitKey)) { bufferManager.unfixPage(newFrame, true); return std::make_pair( leafFrame, leaf); } else { bufferManager.unfixPage(*leafFrame, true); return std::make_pair( &newFrame, reinterpret_cast<Leaf<KeyType, KeyComparator> *>(newFrameData)); } }
bool NBBST<T, Threads>::add(T value){ int key = hash(value); Node* newNode = newLeaf(key); SearchResult search; while(true){ Search(key, &search); nodes.publish(search.l, 0); infos.publish(search.p->update, 0); infos.publish(search.pupdate, 1); if(search.l->key == key){ nodes.releaseNode(newNode); nodes.releaseAll(); infos.releaseAll(); return false; //Key already in the set } if(getState(search.pupdate) != CLEAN){ Help(search.pupdate); } else { Node* newSibling = newLeaf(search.l->key); Node* newInt = newInternal(std::max(key, search.l->key)); newInt->update = Mark(nullptr, CLEAN); //Put the smaller child on the left if(newNode->key <= newSibling->key){ newInt->left = newNode; newInt->right = newSibling; } else { newInt->left = newSibling; newInt->right = newNode; } Info* op = newIInfo(search.p, newInt, search.l); infos.publish(op, 2); Update result = search.p->update; if(CASPTR(&search.p->update, search.pupdate, Mark(op, IFLAG))){ HelpInsert(op); if(search.pupdate){ infos.releaseNode(Unmark(search.pupdate)); } nodes.releaseAll(); infos.releaseAll(); return true; } else { nodes.releaseNode(newInt); nodes.releaseNode(newSibling); nodes.releaseAll(); infos.releaseNode(op); infos.releaseAll(); Help(result); } } } }