keys.push_back (data);
}
+ DeAbrevKey (keys);
return keys;
}
+void BTree::AbrevKey (std::list<BTreeData *> &lst)
+{
+ /* Claves Fijas No se abrevian */
+ if (key_type == KEY_FIXED) return;
+
+ BTreeData *primera = NULL;
+ std::list<BTreeData *>::iterator it = lst.begin ();
+
+ while (it != lst.end ()) {
+ if ((*it)->Abrev (primera) == false)
+ primera = (*it);
+ it++;
+ }
+}
+
+void BTree::DeAbrevKey (std::list<BTreeData *> &lst)
+{
+ /* Claves Fijas No se abrevian */
+ if (key_type == KEY_FIXED) return;
+
+ BTreeData *primera = NULL;
+ std::list<BTreeData *>::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<BTreeData *> &keys)
{
+ AbrevKey (keys);
+
std::list<BTreeData *>::iterator it = keys.begin ();
node += sizeof (BTreeNodeHeader);
it++;
}
- /* TODO : incrementar node_header.item_count aca o fuera de este metodo? */
+ DeAbrevKey (keys);
}
void BTree::PrintNode (uint num)
void WriteKeys (uchar *node, BTreeNodeHeader &node_header, std::list<BTreeData *> &keys);
void DeleteKeys (std::list<BTreeData *> &keys);
+ /* Abreviacion de Claves */
+ void AbrevKey (std::list<BTreeData *> &lst);
+ void DeAbrevKey (std::list<BTreeData *> &lst);
+
std::string filename;
BTreeFileHeader header;
int key_type;
return (*clave) == (*(data.clave));
}
+bool BTreeData::Abrev (BTreeData *anterior)
+{
+ if (anterior == NULL) return false;
+
+ ClaveVariable *c = (ClaveVariable *)clave;
+ return c->Abrev (dynamic_cast<ClaveVariable *>(anterior->getClave ()));
+}
+
+bool BTreeData::DesAbrev (BTreeData *anterior)
+{
+ if (anterior == NULL) return false;
+
+ ClaveVariable *c = (ClaveVariable *)clave;
+ return c->DesAbrev (dynamic_cast<ClaveVariable *>(anterior->getClave ()));
+}
+
BTreeLeafData::BTreeLeafData (uchar *node, int key_type)
{
if (key_type == BTree::KEY_FIXED)
/** 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 {
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);
+}
+
/** Representa una clave de longitud variable.
*
- * \TODO Usar abreviaciones
+ * \todo Usar abreviaciones
*/
class ClaveVariable : public Clave {
public :
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;
};