From b08d09c0c5ba065ea663371f3ad577a81948860d Mon Sep 17 00:00:00 2001 From: Ricardo Markiewicz Date: Mon, 17 Oct 2005 00:47:16 +0000 Subject: [PATCH] Hago pivo con el padre cuando pido clave a un hermano. --- src/btree.cpp | 56 ++++++++++++++++++++++++++++++++++++++++++++++-- src/btree.h | 3 ++- src/btree_data.h | 1 + 3 files changed, 57 insertions(+), 3 deletions(-) diff --git a/src/btree.cpp b/src/btree.cpp index fc8c604..7c7a46b 100644 --- a/src/btree.cpp +++ b/src/btree.cpp @@ -483,17 +483,21 @@ void BTree::DelKeyFromLeaf (Clave *k, uint node_num, uint padre) uint hi, hd; Clave *pedida; - FindB (node_num, padre, hi, hd); + FindBrothers (node_num, padre, hi, hd); if ((pedida = GetKey (hi, 1)) != NULL) { std::string s = *pedida; std::cout << "Clave Pedida : " << s << std::endl; + pedida = ReplaceKeyInFather (node_num, padre, pedida); + node_keys.insert (node_keys.begin (), new BTreeLeafData (pedida)); } else if ((pedida = GetKey (hd, 0)) != NULL) { std::string s = *pedida; std::cout << "Clave Pedida : " << s << std::endl; + pedida = ReplaceKeyInFather (node_num, padre, pedida); + node_keys.push_back (new BTreeLeafData (pedida)); } else { std::cout << "NADIE ME PUEDE PRESTAR, FUNDIR NODOS\n"; @@ -545,7 +549,7 @@ Clave *BTree::GetKey (uint node_num, char maxmin) return k; } -void BTree::FindB (uint node_num, uint padre, uint &left, uint &right) +void BTree::FindBrothers (uint node_num, uint padre, uint &left, uint &right) { uchar *node; BTreeNodeHeader node_header; @@ -589,6 +593,54 @@ void BTree::FindB (uint node_num, uint padre, uint &left, uint &right) } } +Clave *BTree::ReplaceKeyInFather (uint node_num, uint padre, Clave *k) +{ + uchar *node; + BTreeNodeHeader node_header; + std::list node_keys; + + node = ReadBlock (padre); + ReadNodoHeader (node, &node_header); + node_keys = ReadKeys (node, node_header); + + std::list::iterator it = node_keys.begin (); + std::list::iterator anterior = node_keys.begin (); + std::list::iterator siguiente; + + BTreeData *lchild = (*it++); + + if (lchild->getChild () == node_num) { + Clave *ret = (*it)->getClave (); + (*it)->setClave (k); + + WriteKeys (node, node_header, node_keys); + WriteNodoHeader (node, &node_header); + WriteBlock (node, padre); + DeleteKeys (node_keys); + + delete [] node; + return ret; + } + + while (it != node_keys.end ()) { + if ((*it)->getChild () == node_num) + break; + anterior = it; + it++; + } + + Clave *ret = (*it)->getClave (); + (*it)->setClave (k); + + WriteKeys (node, node_header, node_keys); + WriteNodoHeader (node, &node_header); + WriteBlock (node, padre); + DeleteKeys (node_keys); + + delete [] node; + return ret; +} + void BTree::DelKeyFromOther (const Clave &k, BTreeFindResult *r) { } diff --git a/src/btree.h b/src/btree.h index 3f7aecb..861b535 100644 --- a/src/btree.h +++ b/src/btree.h @@ -150,7 +150,8 @@ class BTree { void DelKeyR (BTreeData *k, uint node, uint padre); void DelKeyFromLeaf (Clave *k, uint node_num, uint padre); void DelKeyFromOther (const Clave &k, BTreeFindResult *r); - void FindB (uint node_num, uint padre, uint &left, uint &right); + void FindBrothers (uint node_num, uint padre, uint &left, uint &right); + Clave *ReplaceKeyInFather (uint node_num, uint padre, Clave *k); Clave *GetKey (uint node_num, char maxmin); /* Funciones de Búsqueda */ diff --git a/src/btree_data.h b/src/btree_data.h index 573de74..db40598 100644 --- a/src/btree_data.h +++ b/src/btree_data.h @@ -47,6 +47,7 @@ class BTreeData { /** Retorna la clave manejada */ Clave* getClave () { return clave; } + void setClave (Clave *k) { clave = k; } /** Retorna el hijo apuntado */ uint getChild () { return hijo; } /** Setea el hijo apuntado */ -- 2.43.0