You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
166 lines
3.9 KiB
166 lines
3.9 KiB
#ifndef __BASIC_TEMPLATES_HEADER__
|
|
#define __BASIC_TEMPLATES_HEADER__
|
|
|
|
template <typename T> struct _Primitive_Ring_Element{
|
|
T *data;
|
|
U32 id;
|
|
_Primitive_Ring_Element<T> *prev;
|
|
_Primitive_Ring_Element<T> *next;
|
|
};
|
|
|
|
template <class T> class _Primitive_Ring{
|
|
private:
|
|
_Primitive_Ring_Element<T> *cursor;
|
|
_Primitive_Ring_Element<T> *first;
|
|
_Primitive_Ring_Element<T> *last;
|
|
public:
|
|
U32 count;
|
|
_Primitive_Ring(); // create open ring
|
|
_Primitive_Ring( U32 size ); // create closed ring
|
|
~_Primitive_Ring();
|
|
void add( T *str );
|
|
int remove( void );
|
|
int removeNode( void );
|
|
void setAtBegin( void ){ cursor = first; };
|
|
void setAtEnd( void ){ cursor = last; };
|
|
T *fetch( U32 id );
|
|
T *getAtCursor( void );
|
|
void getSequentalUp( void );
|
|
void getSequentalDown( void );
|
|
};
|
|
|
|
extern U32 _Primitive_Ring_List_INTERATOR;
|
|
|
|
template <class T> _Primitive_Ring<T>::_Primitive_Ring(){
|
|
count = 0;
|
|
cursor = NULL;
|
|
first = NULL;
|
|
last = NULL;
|
|
};
|
|
|
|
template <class T> void _Primitive_Ring<T>::add( T *str ){
|
|
if ( last == NULL ){
|
|
last = (_Primitive_Ring_Element<T> *) malloc ( sizeof( _Primitive_Ring_Element<T> ) );
|
|
last->prev = NULL;
|
|
last->next = NULL;
|
|
last->data = str;
|
|
last->id = _Primitive_Ring_List_INTERATOR++;
|
|
cursor = last;
|
|
first = last;
|
|
} else {
|
|
last->next = (_Primitive_Ring_Element<T> *) malloc ( sizeof( _Primitive_Ring_Element<T> ) );
|
|
last->next->prev = last;
|
|
last->next->data = str;
|
|
last->next->next = NULL;
|
|
last = last->next;
|
|
};
|
|
count++;
|
|
};
|
|
|
|
template <class T> int _Primitive_Ring<T>::remove(void){
|
|
_Primitive_Ring_Element<T> *temp;
|
|
if( cursor != NULL ){
|
|
delete cursor->data;
|
|
if ( cursor->prev != NULL && cursor->next != NULL ){
|
|
cursor->next->prev = cursor->prev;
|
|
cursor->prev->next = cursor->next;
|
|
temp = cursor->next;
|
|
delete cursor;
|
|
cursor = temp;
|
|
} else {
|
|
if ( cursor->next == NULL ){
|
|
temp = cursor->prev;
|
|
delete cursor;
|
|
cursor = temp;
|
|
last = temp;
|
|
if( last != NULL ) last->next = NULL;
|
|
} else if ( cursor->prev == NULL ) {
|
|
temp = cursor->next;
|
|
delete cursor;
|
|
cursor = temp;
|
|
first = temp;
|
|
if( first != NULL ) first->prev = NULL;
|
|
} else {
|
|
delete cursor;
|
|
last = NULL;
|
|
first = NULL;
|
|
cursor = NULL;
|
|
};
|
|
};
|
|
count --;
|
|
} else {
|
|
count = 0;
|
|
}
|
|
return count;
|
|
};
|
|
|
|
template <class T> int _Primitive_Ring<T>::removeNode(void){
|
|
_Primitive_Ring_Element<T> *temp;
|
|
if( cursor != NULL ){
|
|
//delete cursor->data;
|
|
if ( cursor->prev != NULL && cursor->next != NULL ){
|
|
cursor->next->prev = cursor->prev;
|
|
cursor->prev->next = cursor->next;
|
|
temp = cursor->next;
|
|
delete cursor;
|
|
cursor = temp;
|
|
} else {
|
|
if ( cursor->next == NULL ){
|
|
temp = cursor->prev;
|
|
delete cursor;
|
|
cursor = temp;
|
|
last = temp;
|
|
if( last != NULL ) last->next = NULL;
|
|
} else if ( cursor->prev == NULL ) {
|
|
temp = cursor->next;
|
|
delete cursor;
|
|
cursor = temp;
|
|
first = temp;
|
|
if( first != NULL ) first->prev = NULL;
|
|
} else {
|
|
delete cursor;
|
|
last = NULL;
|
|
first = NULL;
|
|
cursor = NULL;
|
|
};
|
|
};
|
|
count --;
|
|
} else {
|
|
count = 0;
|
|
}
|
|
return count;
|
|
};
|
|
|
|
template <class T> T *_Primitive_Ring<T>::fetch( U32 id ){
|
|
setAtBegin();
|
|
while ( cursor != NULL ){
|
|
if( cursor->id == id ) return cursor->data;
|
|
getSequentalUp();
|
|
};
|
|
return NULL;
|
|
};
|
|
|
|
template <class T> T *_Primitive_Ring<T>::getAtCursor( void ){
|
|
if ( cursor != NULL ) return cursor->data;
|
|
return NULL;
|
|
};
|
|
|
|
template <class T> void _Primitive_Ring<T>::getSequentalUp( void ){
|
|
if ( cursor != NULL ){
|
|
cursor = cursor->next;
|
|
};
|
|
};
|
|
|
|
template <class T> void _Primitive_Ring<T>::getSequentalDown( void ){
|
|
if ( cursor != NULL ){
|
|
cursor = cursor->prev;
|
|
};
|
|
};
|
|
|
|
template <class T> _Primitive_Ring<T>::~_Primitive_Ring(){
|
|
setAtBegin();
|
|
while( remove() );
|
|
};
|
|
|
|
|
|
#endif
|