141 lines
3.0 KiB
C++
141 lines
3.0 KiB
C++
#include "StdAfx.h"
|
|
#include "static_array_ord.h"
|
|
//***********************************************************************************
|
|
Cstatic_array_ord::Cstatic_array_ord(void)
|
|
{
|
|
comp_bus=NULL;
|
|
comp_add=NULL;
|
|
}
|
|
//***********************************************************************************
|
|
Cstatic_array_ord::~Cstatic_array_ord(void)
|
|
{
|
|
borra();
|
|
}
|
|
//***********************************************************************************
|
|
void Cstatic_array_ord::inicia( int size,int incremento/*=100*/ )
|
|
{
|
|
datos.z=size;
|
|
datos.incremento=incremento;
|
|
ind.z=sizeof(int);
|
|
ind.incremento=incremento;
|
|
}
|
|
//***********************************************************************************
|
|
void Cstatic_array_ord::borra()
|
|
{
|
|
datos.borra();
|
|
ind.borra();
|
|
}
|
|
//***********************************************************************************
|
|
BYTE* Cstatic_array_ord::get( int i )
|
|
{
|
|
return datos.get((*(int*)ind.get(i)));
|
|
}
|
|
//***********************************************************************************
|
|
BYTE* Cstatic_array_ord::get( BYTE* clave )
|
|
{
|
|
int i,f,c,r;
|
|
i=0;
|
|
f=ind.n;
|
|
c=ind.n/2;
|
|
BOOL enc=FALSE;
|
|
if (comp_bus==NULL)
|
|
return NULL;
|
|
//busqueda dicotomica------------------
|
|
while(c<f &&c>i)
|
|
{
|
|
r=comp_bus(datos.get(*(int*)ind.get(c)),clave);
|
|
if (r<0)
|
|
f=r;
|
|
else if(r>0)
|
|
i=r;
|
|
else
|
|
{
|
|
enc=TRUE;
|
|
break;
|
|
}
|
|
c=(f-i)/2;
|
|
}
|
|
if(!enc)
|
|
{
|
|
if (comp_bus(datos.get(*(int*)ind.get(i)),clave)==0)
|
|
{
|
|
c=i;
|
|
enc=TRUE;
|
|
}
|
|
else if (comp_bus(datos.get(*(int*)ind.get(f)),clave)==0)
|
|
{
|
|
c=f;
|
|
enc=TRUE;
|
|
}
|
|
}
|
|
//fin busqueda dico-----------------------------------
|
|
if (enc)
|
|
return get(c);
|
|
else
|
|
return NULL;
|
|
}
|
|
//***********************************************************************************
|
|
BYTE* Cstatic_array_ord::get_cp( int i )
|
|
{
|
|
return datos.get_cp((*(int*)ind.get(i)));
|
|
}
|
|
//***********************************************************************************
|
|
void Cstatic_array_ord::agueca( int i )
|
|
{
|
|
BYTE* buf= (BYTE*)malloc(ind.z*2);
|
|
BYTE* ba1= buf;
|
|
BYTE* ba2= &buf[ind.z];
|
|
BYTE* ba3;
|
|
if (i<0)
|
|
return;
|
|
memcpy(ba1,&ind.buf[ind.z*i],ind.z);
|
|
for (int j=i+1; j<ind.n; j++)
|
|
{
|
|
memcpy(ba2,&ind.buf[ind.z*j],ind.z);
|
|
memcpy(&ind.buf[ind.z*j],ba1,ind.z);
|
|
//intercambiamos punteros
|
|
ba3=ba1;
|
|
ba1=ba2;
|
|
ba2=ba3;
|
|
}
|
|
free(buf);
|
|
}
|
|
//***********************************************************************************
|
|
void Cstatic_array_ord::add( BYTE* e )
|
|
{
|
|
int i,f,c,r;
|
|
datos.add(e);
|
|
if (!comp_add)
|
|
ind.add((BYTE*)&datos.n-1);
|
|
//buscamos la posicion correspondiente-------------------
|
|
i=0;
|
|
f=ind.n;
|
|
c=ind.n/2;
|
|
BOOL enc=FALSE;
|
|
while(c<f &&c>i)
|
|
{
|
|
r=comp_add(datos.get(*(int*)ind.get(c)),e);
|
|
if (r<0)
|
|
f=r;
|
|
else if(r>0)
|
|
i=r;
|
|
else
|
|
{
|
|
enc=TRUE;
|
|
break;
|
|
}
|
|
c=(f-i)/2;
|
|
}
|
|
//quedan como mucho 2 elementos
|
|
if (!enc)
|
|
{
|
|
if (comp_add(datos.get(*(int*)ind.get(i)),e)<=0)
|
|
c=i;
|
|
else
|
|
c=f;
|
|
}
|
|
//fin busqueda------------------------------------------
|
|
agueca(c);
|
|
ind.add((BYTE*)&datos.n-1);
|
|
}
|
|
//***********************************************************************************
|