From fd68ec89fabf4a95e4ebba38fe54041a9ad6d667 Mon Sep 17 00:00:00 2001 From: Ricardo Markiewicz Date: Wed, 26 Oct 2005 18:15:00 +0000 Subject: [PATCH] Refactoring para unificar nomenclaturas. --- src/btree.cpp | 84 +++++++++++++++++++------------------- src/btree_data.cpp | 4 +- src/btree_data.h | 8 ++-- src/keymanager.h | 6 +++ src/main.cpp | 3 +- src/main_var.cpp | 14 +++---- viewer/view_btree.cpp | 2 +- viewer/view_node.cpp | 2 +- viewer/view_properties.cpp | 9 ++-- 9 files changed, 70 insertions(+), 62 deletions(-) diff --git a/src/btree.cpp b/src/btree.cpp index a88aae3..5b1c36d 100644 --- a/src/btree.cpp +++ b/src/btree.cpp @@ -199,7 +199,7 @@ Clave* BTree::AddKeyLeafR (const Clave *k, uint node_num, uint &left_child, uint used += d->Size (); it++; } - kout = (*it++)->getClave (); // Esta se retorna al "padre" para que se la agregue + kout = (*it++)->GetKey (); // Esta se retorna al "padre" para que se la agregue while (it != node_keys.end ()) { BTreeData *d = (*it); @@ -260,9 +260,9 @@ Clave* BTree::AddKeyOtherR (const Clave *k, uint node_num, uint &left_child, uin } if (it == posterior) { - k = AddKeyR (k, lchild->getChild (), left_child, right_child); + k = AddKeyR (k, lchild->GetChild (), left_child, right_child); } else { - k = AddKeyR (k, (*ultima)->getChild (), left_child, right_child); + k = AddKeyR (k, (*ultima)->GetChild (), left_child, right_child); } DeleteKeys (node_keys); @@ -356,9 +356,9 @@ Clave* BTree::AddKeyOtherR (const Clave *k, uint node_num, uint &left_child, uin used += d->Size (); it++; } - kout = (*it)->getClave (); // Esta se retorna al "padre" para que se la agregue + kout = (*it)->GetKey (); // Esta se retorna al "padre" para que se la agregue - new_node_keys.push_back ( new BTreeChildData ((*it)->getChild ())); + new_node_keys.push_back ( new BTreeChildData ((*it)->GetChild ())); it++; while (it != node_keys.end ()) { BTreeData *d = (*it); @@ -422,18 +422,18 @@ void BTree::DelKeyR (BTreeData *k, uint node_num, uint padre) if ((*k) == (*(*it))) { /* La encontre!, retorno */ if (node_header.level == 0) { - DelKeyFromLeaf (k->getClave (), node_num, padre); + DelKeyFromLeaf (k->GetKey (), node_num, padre); } else { uint left, right; if (it == posterior) { - left = lchild->getChild (); - right = (*it)->getChild (); + left = lchild->GetChild (); + right = (*it)->GetChild (); } else { - left = (*ultima)->getChild (); - right = (*it)->getChild (); + left = (*ultima)->GetChild (); + right = (*it)->GetChild (); } std::cout << "Eliminar de Nodo con hijos : " << left << " y " << right << std::endl; - DelKeyFromNode (k->getClave (), node_num, padre, left, right); + DelKeyFromNode (k->GetKey (), node_num, padre, left, right); } DeleteKeys (node_keys); delete [] node; @@ -456,9 +456,9 @@ void BTree::DelKeyR (BTreeData *k, uint node_num, uint padre) /* TODO: Aca faltaria liberar memoria */ if (it == posterior) { - DelKeyR (k, lchild->getChild (), node_num); + DelKeyR (k, lchild->GetChild (), node_num); } else { - DelKeyR (k, (*ultima)->getChild (), node_num); + DelKeyR (k, (*ultima)->GetChild (), node_num); } } @@ -602,22 +602,22 @@ void BTree::JoinNodes (uint node1, uint node2, uint padre, int tipohermano) Clave *cpadre; BTreeData *lchild = (*it++); - if (lchild->getChild () == node1) { - cpadre = (*it)->getClave (); + if (lchild->GetChild () == node1) { + cpadre = (*it)->GetKey (); borrar_padre = it; } else { while (it != nkpadre.end ()) { if (tipohermano == 0) { - if ((*it)->getChild () == node2) + if ((*it)->GetChild () == node2) break; } else { - if ((*it)->getChild () == node1) + if ((*it)->GetChild () == node1) break; } anterior = it; it++; } - cpadre = (*it)->getClave (); + cpadre = (*it)->GetKey (); borrar_padre = it; } if (it == nkpadre.end ()) { @@ -632,14 +632,14 @@ void BTree::JoinNodes (uint node1, uint node2, uint padre, int tipohermano) i = nk1.begin (); while (i != nk1.end ()) { - newkeys.push_back ( new BTreeLeafData ((*i)->getClave ()->Clone ())); + newkeys.push_back ( new BTreeLeafData ((*i)->GetKey ()->Clone ())); i++; } //if (tipohermano == 0) newkeys.push_back ( new BTreeLeafData (cpadre->Clone ())); i = nk2.begin (); while (i != nk2.end ()) { - newkeys.push_back ( new BTreeLeafData ((*i)->getClave ()->Clone ())); + newkeys.push_back ( new BTreeLeafData ((*i)->GetKey ()->Clone ())); i++; } @@ -670,7 +670,7 @@ void BTree::JoinNodes (uint node1, uint node2, uint padre, int tipohermano) /* TODO : Recuperar node2 */ /* Actualizo punero al padre */ - (*anterior)->setChild (node1); + (*anterior)->SetChild (node1); nkpadre.erase (borrar_padre); WriteKeys (npadre, nhp, nkpadre); @@ -729,12 +729,12 @@ Clave *BTree::GetKey (uint node_num, char maxmin) } if (maxmin == 0) { - k = (*it)->getClave ()->Clone (); + k = (*it)->GetKey ()->Clone (); node_keys.erase (it); } else { it = node_keys.end (); it--; - k = (*it)->getClave ()->Clone (); + k = (*it)->GetKey ()->Clone (); node_keys.erase (it); } @@ -764,28 +764,28 @@ void BTree::FindBrothers (uint node_num, uint padre, uint &left, uint &right) BTreeData *lchild = (*it++); - if (lchild->getChild () == node_num) { + if (lchild->GetChild () == node_num) { /* Solo tengo hermano derecho */ std::cout << "Hermano Izquierdo : NO TENGO" << std::endl; left = 0; - std::cout << "Hermano Derecho : " << (*it)->getChild () << std::endl; - right = (*it)->getChild (); + std::cout << "Hermano Derecho : " << (*it)->GetChild () << std::endl; + right = (*it)->GetChild (); return; } while (it != node_keys.end ()) { - if ((*it)->getChild () == node_num) + if ((*it)->GetChild () == node_num) break; anterior = it; it++; } siguiente = it++; - std::cout << "Hermano Izquierdo : " << (*anterior)->getChild () << std::endl; - left = (*anterior)->getChild (); + std::cout << "Hermano Izquierdo : " << (*anterior)->GetChild () << std::endl; + left = (*anterior)->GetChild (); if (siguiente != node_keys.end ()) { - right = (*siguiente)->getChild (); - std::cout << "Hermano Derecho : " << (*siguiente)->getChild () << std::endl; + right = (*siguiente)->GetChild (); + std::cout << "Hermano Derecho : " << (*siguiente)->GetChild () << std::endl; } else { right = 0; std::cout << "Hermano Derecho : NO TENGO" << std::endl; @@ -808,9 +808,9 @@ Clave *BTree::ReplaceKeyInFather (uint node_num, uint padre, Clave *k) BTreeData *lchild = (*it++); - if (lchild->getChild () == node_num) { - Clave *ret = (*it)->getClave (); - (*it)->setClave (k); + if (lchild->GetChild () == node_num) { + Clave *ret = (*it)->GetKey (); + (*it)->SetKey (k); WriteKeys (node, node_header, node_keys); WriteNodoHeader (node, &node_header); @@ -822,14 +822,14 @@ Clave *BTree::ReplaceKeyInFather (uint node_num, uint padre, Clave *k) } while (it != node_keys.end ()) { - if ((*it)->getChild () == node_num) + if ((*it)->GetChild () == node_num) break; anterior = it; it++; } - Clave *ret = (*it)->getClave (); - (*it)->setClave (k); + Clave *ret = (*it)->GetKey (); + (*it)->SetKey (k); WriteKeys (node, node_header, node_keys); WriteNodoHeader (node, &node_header); @@ -867,7 +867,7 @@ void BTree::DelKeyFromNode (Clave *k, uint node_num, uint padre, uint left, uint node_keyr = ReadKeys (node_r, node_hr); data_r = *(node_keyr.begin ()); padre_hijo = right; - right = data_r->getChild (); + right = data_r->GetChild (); DeleteKeys (node_keyr); delete [] node_r; @@ -903,8 +903,8 @@ void BTree::DelKeyFromNode (Clave *k, uint node_num, uint padre, uint left, uint PrintNode (node_num); exit (1); } - (*it)->setClave (reemplazar->getClave ()); - reemplazar->setClave (k->Clone ()); + (*it)->SetKey (reemplazar->GetKey ()); + reemplazar->SetKey (k->Clone ()); std::cout << "Tengo todo reemplazado ...\n"; @@ -1164,9 +1164,9 @@ BTreeFindResult *BTree::FindKeyR (const Clave *k, uint node_num) /* TODO: Aca faltaria liberar memoria */ BTreeFindResult *ret; if (it == posterior) - ret = FindKeyR (k, lchild->getChild ()); + ret = FindKeyR (k, lchild->GetChild ()); else - ret = FindKeyR (k, (*ultima)->getChild ()); + ret = FindKeyR (k, (*ultima)->GetChild ()); DeleteKeys (node_keys); delete [] node; diff --git a/src/btree_data.cpp b/src/btree_data.cpp index b74427b..70195fd 100644 --- a/src/btree_data.cpp +++ b/src/btree_data.cpp @@ -70,7 +70,7 @@ bool BTreeData::Abrev (BTreeData *anterior) if (anterior == NULL) return false; ClaveVariable *c = (ClaveVariable *)clave; - return c->Abrev (dynamic_cast(anterior->getClave ())); + return c->Abrev (dynamic_cast(anterior->GetKey ())); } bool BTreeData::DesAbrev (BTreeData *anterior) @@ -78,7 +78,7 @@ bool BTreeData::DesAbrev (BTreeData *anterior) if (anterior == NULL) return false; ClaveVariable *c = (ClaveVariable *)clave; - return c->DesAbrev (dynamic_cast(anterior->getClave ())); + return c->DesAbrev (dynamic_cast(anterior->GetKey ())); } BTreeLeafData::BTreeLeafData (uchar *node, int key_type) diff --git a/src/btree_data.h b/src/btree_data.h index 027c595..53af1fc 100644 --- a/src/btree_data.h +++ b/src/btree_data.h @@ -46,12 +46,12 @@ class BTreeData { virtual uchar *ToArray () const; /** Retorna la clave manejada */ - Clave* getClave () { return clave; } - void setClave (Clave *k) { clave = k; } + Clave* GetKey () { return clave; } + void SetKey (Clave *k) { clave = k; } /** Retorna el hijo apuntado */ - uint getChild () { return hijo; } + uint GetChild () { return hijo; } /** Setea el hijo apuntado */ - void setChild (uint c) { hijo = c; } + void SetChild (uint c) { hijo = c; } bool Abrev (BTreeData *anterior); bool DesAbrev (BTreeData *anterior); diff --git a/src/keymanager.h b/src/keymanager.h index d457fcb..3d78458 100644 --- a/src/keymanager.h +++ b/src/keymanager.h @@ -46,6 +46,12 @@ class KeyManager { } std::list < T >& GetList () { return keys; } + + /** Devuelve la cantidad de valores almacenados */ + typename std::list < T >::size_type Size() const + { + return keys.size(); + } private: std::list< T > keys; int altas; diff --git a/src/main.cpp b/src/main.cpp index 58a00ec..069b3ac 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -34,7 +34,8 @@ int main (int argc, char *argv[]) double l = Random::Double (0.0f, 1.0f); std::cout << l << " >= " << paltas << std::endl; - if (l >= paltas) { + if (!km.Size() // si no tenemos claves agregadas, mejor que hagamos un alta + || l >= paltas) { tree.AddKey (c); km.AddValue (*it); it++; diff --git a/src/main_var.cpp b/src/main_var.cpp index 592fec9..e6aed97 100644 --- a/src/main_var.cpp +++ b/src/main_var.cpp @@ -7,16 +7,15 @@ int main (int argc, char *argv[]) { - int bloque, altas, bajas; - if (argc != 4) { - printf ("Uso : %s \n", argv[0]); + std::cout << "Uso: " << argv[0] << " \n"; + std::cout << "\tEl tamaƱo de bloque usado es 512*2^.\n"; return 1; } - bloque = atoi (argv[1]); - altas = atoi (argv[2]); - bajas = atoi (argv[3]); + int bloque = 128 << (atoi (argv[1]) + 2); + int altas = atoi (argv[2]); + int bajas = atoi (argv[3]); KeyManager km; BTree tree ("test.idx", bloque, BTree::KEY_VARIABLE); @@ -34,7 +33,8 @@ int main (int argc, char *argv[]) double l = Random::Double (0.0f, 1.0f); std::cout << l << " >= " << paltas << std::endl; - if (l >= paltas) { + if (!km.Size() // si no tenemos claves agregadas, mejor que hagamos un alta + || l >= paltas) { std::string sss = c; std::cout << "Clave Agregada " << sss << std::endl; tree.AddKey (c); diff --git a/viewer/view_btree.cpp b/viewer/view_btree.cpp index 6bdcb67..3fba848 100644 --- a/viewer/view_btree.cpp +++ b/viewer/view_btree.cpp @@ -41,7 +41,7 @@ void ViewBTree::on_item_activated (BTreeData *data, uint num, ViewNode *vnode) { last_selected = NULL; - uint next = data->getChild (); + uint next = data->GetChild (); delete vnode; std::cout << next << std::endl; diff --git a/viewer/view_node.cpp b/viewer/view_node.cpp index 122ebc6..6dc6649 100644 --- a/viewer/view_node.cpp +++ b/viewer/view_node.cpp @@ -41,7 +41,7 @@ ViewNode::ViewNode (Canvas::Group *parent, uint num, uint padre, BTreeNodeHeader if (!dynamic_cast(data)) { /* Si no es un dato de una hoja, tiene hijos */ - hijos.push_back (data->getChild ()); + hijos.push_back (data->GetChild ()); } } } diff --git a/viewer/view_properties.cpp b/viewer/view_properties.cpp index c10bbae..65f782f 100644 --- a/viewer/view_properties.cpp +++ b/viewer/view_properties.cpp @@ -78,25 +78,26 @@ void ViewProperties::ShowItem (BTreeData *data, BTreeNodeHeader &header) std::string s4; std::stringstream ss4; - ss4 << data->getChild (); + ss4 << data->GetChild (); ss4 >> s4; data_child.set_label (s4); } else if (dynamic_cast(data)) { data_type.set_label ("BTreeLeafData"); data_child.set_label ("N/C"); - std::string s4 = *(data->getClave ()); + std::string s4 = *(data->GetKey ()); + std::cout << "----> " << s4 << std::endl; data_key.set_label (s4); } else { data_type.set_label ("BTreeData"); std::string s4; std::stringstream ss4; - ss4 << data->getChild (); + ss4 << data->GetChild (); ss4 >> s4; data_child.set_label (s4); - std::string s5 = *(data->getClave ()); + std::string s5 = *(data->GetKey ()); data_key.set_label (s5); } } -- 2.43.0