From a356d91d88f7f3399d80e922f70e10a8f422a3ba Mon Sep 17 00:00:00 2001 From: Ricardo Markiewicz Date: Fri, 16 Sep 2005 04:16:29 +0000 Subject: [PATCH] Agregar recursivo (para testear) --- src/btree.cpp | 36 +++++++++++++++++++++++++++++------- src/btree_data.h | 1 + src/main.cpp | 4 ++-- 3 files changed, 32 insertions(+), 9 deletions(-) diff --git a/src/btree.cpp b/src/btree.cpp index 093377b..970d5c0 100644 --- a/src/btree.cpp +++ b/src/btree.cpp @@ -103,21 +103,41 @@ Clave* BTree::AddKeyR (const Clave *k, uint node_num, uint &left_child, uint &ri ReadNodoHeader (node, &node_header); if (node_header.level != 0) { + std::cout << "Entrando nodo level = " << node_header.level << std::endl; /* No estoy en una hoja, asi que tengo que buscar * para donde moverme para agregar la clave */ - /* TODO :) */ + std::cout << "Leo Claves" << std::endl; node_keys = ReadKeys (node, node_header); + std::list::iterator it = node_keys.begin (); + std::list::iterator posterior; + std::list::iterator ultima; /* Se supone que la primera es un hijo :) */ - //BTreeData *left_child = (*it); - PrintNode (node_num); - std::cout << "TODO!!" << std::endl; - return NULL; + BTreeData *lchild = (*it++); + posterior = it; + + while (it != node_keys.end ()) { + if ((*data) < (*(*it))) + break; + ultima = it; + it++; + } + + if (it == posterior) { + std::cout << "==== Me voy al nodo " << lchild->getChild () << std::endl; + k = AddKeyR (k, lchild->getChild (), left_child, right_child); + } else { + std::cout << "==== Me voy al nodo " << (*ultima)->getChild () << std::endl; + k = AddKeyR (k, (*ultima)->getChild (), left_child, right_child); + } } + /* Nada que hacer */ + if (!k) return NULL; + /* Estoy en una hoja, veo si lo puedo agregar */ if (node_header.free_space > data->Size ()) { @@ -131,6 +151,7 @@ Clave* BTree::AddKeyR (const Clave *k, uint node_num, uint &left_child, uint &ri if ((*data) < (*datait)) /* Me pase, lo agrego aca! */ break; + it++; } node_keys.insert (it, data); WriteKeys (node, node_header, node_keys); @@ -315,8 +336,9 @@ void BTree::PrintNode (uint num) std::cout << "Items : " << node_header.item_count << std::endl; std::cout << "Free : " << node_header.free_space << " (" << (header.block_size - sizeof (BTreeNodeHeader)) << ")" << std::endl; while (it != node_keys.end ()) { - std::string s = (*it); - std::cout << "(" << s << ") "; + std::string s = *(*it); + std::cout << s << " "; + it++; } std::cout << std::endl; diff --git a/src/btree_data.h b/src/btree_data.h index 7ea7d9c..d53b85f 100644 --- a/src/btree_data.h +++ b/src/btree_data.h @@ -19,6 +19,7 @@ class BTreeData { virtual uchar *ToArray () const; Clave* getClave () { return clave; } + uint getChild () { return hijo; } bool operator < (const BTreeData &data) const; virtual operator std::string () const { diff --git a/src/main.cpp b/src/main.cpp index c60cd14..3c30c4a 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -5,9 +5,9 @@ int main (int argc, char *argv[]) { - BTree tree ("test.idx", 1024); + BTree tree ("test.idx", 64); - for (int i=0; i<10000; i++) { + for (int i=0; i<=13; i++) { ClaveFija c(i); std::cout << "Agregando " << i << std::endl; -- 2.43.0