#include "btree.h"
#include "clave_fija.h"
-
-void generar_random (std::list<int> &lst, uint n);
+#include "random.h"
+#include "keymanager.h"
int main (int argc, char *argv[])
{
- std::list<int> lst;
-
- BTree tree ("test.idx", 64);
-
- if (argc != 2) {
- printf ("Falta parametro cantidad de elementos a agregar\n");
+ if (argc != 4) {
+ std::cout << "Uso: " << argv[0] << " <block size> <cantidad de altas> <cantidad de bajas>\n";
+ std::cout << "\tEl tamaño de bloque usado es 512*2^<block size>.\n";
return 1;
}
- generar_random (lst, atoi(argv[1]));
+ int bloque = 128 << (atoi (argv[1]) + 2);
+ int altas = atoi (argv[2]);
+ int bajas = atoi (argv[3]);
- std::list<int>::iterator it = lst.begin ();
- while (it != lst.end ()) {
- ClaveFija c(*it);
+ KeyManager<int> km;
+ BTree tree ("test.idx", bloque);
+
+ std::list<int> lst;
+ std::list<int>::iterator it;
+ Random::Init ();
+ Random::Ints (lst, altas);
- std::cout << "Agregando " << (*it) << std::endl;
- tree.AddKey (c);
- it++;
- }
+ double paltas = bajas / (double)(altas + bajas);
it = lst.begin ();
while (it != lst.end ()) {
- ClaveFija c(*it);
+ ClaveFija c(*it, 0);
- if (tree.FindKey (c))
- std::cout << (*it) << " encontrada\n";
- else
- std::cout << (*it) << " NO encontrada\n";
- it++;
+ double l = Random::Double (0.0f, 1.0f);
+ std::cout << l << " >= " << paltas << std::endl;
+ if (!km.Size() // si no tenemos claves agregadas, mejor que hagamos un alta
+ || l >= paltas) {
+ try {
+ tree.AddKey (c);
+ km.AddValue (*it);
+ } catch (Exception *e) {
+ std::cout << e->Message () << std::endl;
+ }
+ it++;
+ } else {
+ /* Tengo que borrar una clave entre 0 e "i" de la lista
+ * porque son las que ya agregue. */
+ ClaveFija c(km.GetRandom (), 0);
+
+ try {
+ tree.DelKey (c);
+ std::string sss = c;
+ std::cout << "Clave Borrada " << sss << std::endl;
+ } catch (Exception *e) {
+ std::cout << e->Message () << std::endl;
+ }
+ }
}
- return 0;
-}
+ km.PrintInfo ();
-void generar_random (std::list<int> &lst, uint n)
-{
- /* Genero N numeros aleatorios entre -3*n y 3*n */
- bool *numeros = new bool [6*n+1];
- int random;
-
- memset (numeros, 0, (6*n+1)*sizeof (bool));
-
- srand (time (NULL));
- for (uint i=0; i < n; i++) {
- do {
- random = 1 + (int)(6.0f * n * rand () / (RAND_MAX + 1.0f) - 3.0f * n);
- std::cout << random << std::endl;
- } while (numeros[random + 3 * n] == true);
- numeros[random + 3 * n] = true;
- lst.push_back (random);
+ std::list <int> l = km.GetList ();
+ it = l.begin ();
+ int bien = 0;
+ int mal = 0;
+ while (it != l.end ()) {
+ ClaveFija c(*it, 0);
+ BTreeFindResult *r;
+
+ r = tree.FindKey (c);
+ if (r != NULL) {
+ std::cout << "** Clave " << (*it) << " encontrada.\n";
+ bien++;
+ delete r;
+ } else {
+ std::cout << "** Clave " << (*it) << " NO encontrada.\n";
+ mal++;
+ }
+ it++;
}
+
+ std::cout << "\n\n === Resultados ===\n";
+ std::cout << " Hist : " << bien << "\n";
+ std::cout << " Miss : " << mal << "\n";
+ std::cout << " Total : " << (bien+mal) << "\n";
+
+ return mal;
}