From ce24e1d22d120e6613f3cd1e04d81135f3d048c9 Mon Sep 17 00:00:00 2001 From: Ricardo Markiewicz Date: Mon, 24 Oct 2005 20:48:50 +0000 Subject: [PATCH] Abreviado de Claves Variables. --- src/btree.cpp | 35 +++++++++++++++++++++++- src/btree.h | 4 +++ src/btree_data.cpp | 16 +++++++++++ src/btree_data.h | 3 +++ src/clave_variable.cpp | 61 ++++++++++++++++++++++++++++++++++++++++++ src/clave_variable.h | 6 ++++- 6 files changed, 123 insertions(+), 2 deletions(-) diff --git a/src/btree.cpp b/src/btree.cpp index c0e72a3..8747177 100644 --- a/src/btree.cpp +++ b/src/btree.cpp @@ -990,11 +990,44 @@ std::list BTree::ReadKeys (uchar *node, BTreeNodeHeader &node_heade keys.push_back (data); } + DeAbrevKey (keys); return keys; } +void BTree::AbrevKey (std::list &lst) +{ + /* Claves Fijas No se abrevian */ + if (key_type == KEY_FIXED) return; + + BTreeData *primera = NULL; + std::list::iterator it = lst.begin (); + + while (it != lst.end ()) { + if ((*it)->Abrev (primera) == false) + primera = (*it); + it++; + } +} + +void BTree::DeAbrevKey (std::list &lst) +{ + /* Claves Fijas No se abrevian */ + if (key_type == KEY_FIXED) return; + + BTreeData *primera = NULL; + std::list::iterator it = lst.begin (); + + while (it != lst.end ()) { + if ((*it)->DesAbrev (primera) == false) + primera = (*it); + it++; + } +} + void BTree::WriteKeys (uchar *node, BTreeNodeHeader &node_header, std::list &keys) { + AbrevKey (keys); + std::list::iterator it = keys.begin (); node += sizeof (BTreeNodeHeader); @@ -1016,7 +1049,7 @@ void BTree::WriteKeys (uchar *node, BTreeNodeHeader &node_header, std::list &keys); void DeleteKeys (std::list &keys); + /* Abreviacion de Claves */ + void AbrevKey (std::list &lst); + void DeAbrevKey (std::list &lst); + std::string filename; BTreeFileHeader header; int key_type; diff --git a/src/btree_data.cpp b/src/btree_data.cpp index bc0ce93..bf92909 100644 --- a/src/btree_data.cpp +++ b/src/btree_data.cpp @@ -63,6 +63,22 @@ bool BTreeData::operator == (const BTreeData &data) const return (*clave) == (*(data.clave)); } +bool BTreeData::Abrev (BTreeData *anterior) +{ + if (anterior == NULL) return false; + + ClaveVariable *c = (ClaveVariable *)clave; + return c->Abrev (dynamic_cast(anterior->getClave ())); +} + +bool BTreeData::DesAbrev (BTreeData *anterior) +{ + if (anterior == NULL) return false; + + ClaveVariable *c = (ClaveVariable *)clave; + return c->DesAbrev (dynamic_cast(anterior->getClave ())); +} + BTreeLeafData::BTreeLeafData (uchar *node, int key_type) { if (key_type == BTree::KEY_FIXED) diff --git a/src/btree_data.h b/src/btree_data.h index db40598..027c595 100644 --- a/src/btree_data.h +++ b/src/btree_data.h @@ -53,6 +53,9 @@ class BTreeData { /** Setea el hijo apuntado */ void setChild (uint c) { hijo = c; } + bool Abrev (BTreeData *anterior); + bool DesAbrev (BTreeData *anterior); + bool operator < (const BTreeData &data) const; bool operator == (const BTreeData &data) const; virtual operator std::string () const { diff --git a/src/clave_variable.cpp b/src/clave_variable.cpp index c4fd1f1..47d81ba 100644 --- a/src/clave_variable.cpp +++ b/src/clave_variable.cpp @@ -52,3 +52,64 @@ bool ClaveVariable::operator == (const Clave &c) const return data == ((ClaveVariable&)c).data; } +bool ClaveVariable::Abrev (ClaveVariable *c) +{ + if (c == NULL) return false; + int iguales = 0; + std::string resto; + + resto = abreviar (c->data, data, iguales); + + /* No hago nada si solo machea un caracter */ + if (iguales < 2) return false; + + std::stringstream ss; + ss << iguales; + ss << "|"; + ss << resto; + data = ss.str (); + + return true; +} + +bool ClaveVariable::DesAbrev (ClaveVariable *c) +{ + if (c == NULL) return false; + + int pos = data.find ("|"); + + /* No esta abreviada! */ + if (pos <= 0) return false; + + std::string siguales = data.substr (0, pos); + std::cout << siguales << std::endl; + int iguales = atoi (siguales.c_str ()); + + std::string primera_parte = c->data.substr (0, iguales); + std::string segunda_parte = data.substr (pos+1, data.size () - pos - 1); + + data = primera_parte + segunda_parte; + + return true; +} + +std::string ClaveVariable::abreviar(std::string &p, std::string &a, int &iguales) +{ + char *primera = (char *)p.c_str (); + char *actual = (char *)a.c_str (); + + iguales = 0; + while (((*primera) != '\0') && ((*actual) != '\0')) { + if ((*primera) == (*actual)) { + primera++; + actual++; + iguales++; + } else { + /* No coinciden mas! */ + break; + } + } + + return std::string (actual); +} + diff --git a/src/clave_variable.h b/src/clave_variable.h index 75cecfd..e568451 100644 --- a/src/clave_variable.h +++ b/src/clave_variable.h @@ -8,7 +8,7 @@ /** Representa una clave de longitud variable. * - * \TODO Usar abreviaciones + * \todo Usar abreviaciones */ class ClaveVariable : public Clave { public : @@ -20,12 +20,16 @@ class ClaveVariable : public Clave { uchar *ToArray () const; Clave *Clone () const; + bool Abrev (ClaveVariable *c); + bool DesAbrev (ClaveVariable *c); + virtual bool operator < (const Clave &c) const; virtual bool operator == (const Clave &c) const; virtual operator std::string () const { return data; } private: + std::string abreviar(std::string &primera, std::string &actual, int &iguales); std::string data; }; -- 2.43.0