Este código se puede traducir fácilmente a ANSI C. su implementanción es facil y es una pequeña practica que me estoy dejando ( Crear varias clases para C/C++, fáciles de usar ).
La clase aun no esta terminada (Esta apenas al 30%, pero ya esta funcional).
Código: Seleccionar todo
#ifndef CLIST_H
#define CLIST_H
#include <stdio.h>
#include "../include/citerator.h"
#include "../include/mutiles.h"
template <class T>
class clist
{
protected:
size_t _szt_ln; // Cantidad de elementos en {**__pp_T}
T **__pp_T; // Datos.
clist *__p_link; // Puntero con relación a otra lista.
T *__p_rT; // Vector que contiene ordenada la lista {**__pp_T}.
private:
void deleteelement( size_t _szt_index )
{
if ( this->_szt_ln==0 ) return;
if ( this->check( _szt_index )==false ) return;
T **__pp_T = this->__pp_T; // Respaldamos para posteriormente eliminar el vector.
--this->_szt_ln;
if ( this->_szt_ln>0 ) {
this->__pp_T = new T*[this->_szt_ln];
if ( this->_szt_ln>1 ) {
memcicle( &__pp_T[_szt_index] , &__pp_T[this->_szt_ln+1] , -1 , sizeof(T*) ); // Giramos hacia la izquierda ( esto deja {_szt_index1} en la ultima posicion ).
}
memcpy( &this->__pp_T[0] , &__pp_T[0] , this->_szt_ln*sizeof(T*) ); // Copiamos la cantidad deseada de punteros.
} else {
this->__pp_T = NULL;
}
delete[] __pp_T; // Eliminamos el vector anterior a los punteros.
}
size_t addelement( T *_r_T ) {
T **__pp_T = this->__pp_T; // Respaldamos para posteriormente eliminar el vector.
size_t _szt_ret = this->_szt_ln++;
this->__pp_T = new T*[this->_szt_ln];
if ( _szt_ret>0 ) {
memcpy( &this->__pp_T[0] , &__pp_T[0] , _szt_ret*sizeof(T*) ); // Creamos una nueva cantidad de punteros y copiamos.
}
delete[] __pp_T; // Eliminamos el vector anterior a los punteros.
this->__pp_T[_szt_ret] = _r_T; // Apuntamos al elemento.
return _szt_ret;
}
inline bool check (size_t _szt_index) { if (_szt_index<this->_szt_ln)
return true; else return false; }
public:
typedef _T_iterator_normal<T , clist> iterator;
clist() { this->_szt_ln=0; this->__pp_T=NULL; this->__p_link=NULL; this->__p_rT=NULL; }
virtual ~clist() { if ( __pp_T != NULL ) { delete[] __pp_T; } }
clist &operator<<( T &__r_it ) { this->add(&__r_it); return *this; }
clist &operator>>( T &__r_it ) {
iterator _it_ret = this->find( &__r_it );
if ( _it_ret.reference() !=NULL ) { this->remove(_it_ret.index()); }
return *this;
}
iterator operator[](size_t _szt_index) {
if ( this->_szt_ln==0 ) {
iterator _it_ret;
return _it_ret;
}
if ( _szt_index >= this->_szt_ln ) { _szt_index = this->_szt_ln; }
iterator _it_ret( &this->__pp_T[0] , _szt_index , &this->_szt_ln , this );
return _it_ret;
}
size_t count () { return this->_szt_ln; }
iterator add ( T *_p_it ) {
iterator _it_ret( &this->__pp_T[0] , this->addelement(_p_it) , &this->_szt_ln , this );
return _it_ret;
}
iterator add ( T **_pp_it ) {}
size_t remove ( size_t _szt_index ) { deleteelement(_szt_index); return this->_szt_ln; }
bool replace ( size_t _szt_index, T &__r_vT ) { if (this->check(_szt_index)==false) { return false; } this->__pp_T[_szt_index] = &__r_vT; return true; };
iterator find( T *__p_vT ) {
size_t _szt_i = 0;
for(;_szt_i<this->_szt_ln;++_szt_i )
if ( __p_vT == this->__pp_T[_szt_i] ) {
iterator _it_ret( &this->__pp_T[0] , _szt_i , &this->_szt_ln , this );
return _it_ret;
}
iterator _it_ret( NULL , 0 , NULL , NULL );
return _it_ret;
}
T *getptr ( size_t _szt_index ) { return this->check( _szt_index )==true? this->__pp_T[_szt_index] : NULL; }
};
#endif // CLIST_H