successor function for STL lists

  • Thread starter Christian Christmann
  • Start date
C

Christian Christmann

Hi,

I've some old code which I would like to replace by STL.
It's a self-written double linked list which consists of instances
of another class Element which contains the information as
void* (I'm going to replace the void* by a template but that's
not an issue here).

The class Element is defined as:
class Element
{
void* value;
Element* nextelem, *prevelem;
[snip]
};

The class List is defined as:
class List
{
private:
Element* first;
Element* last;
// used as forward iterator
Element* forward_iterator ;
// used as backward iterator
Element* backward_iterator;
[snip]
public:
void* NextElement();
[snip]
};

and the function NextElement() is defined as:

void* List::NextElement()
{
// isEmpty() checks wheather list empty
if (!forward_iterator || isEmpty())
return NULL;

// value is the void* content of Element
void *next = forward_iterator->value;
forward=forward->nextelem;
return next;
}


What would be the best and most efficient way to replace
this function by a function using STL lists?

If the function NextElement would require an Element
as parameter to find its successor like

void* List::NextElement(void* currentElement)

I would first use the algorithm find() and than
increment the iterator. Something like

return *(++(find(begin(), end(), currentElement)));

(hope the syntax is ok ;) )



Thank you for your help in advance

Regards,
Chris
 
V

Victor Bazarov

Christian said:
I've some old code which I would like to replace by STL.

Instead of attempting a piece-by-piece replacement, and mostly because
the functionality is not compatible, you'd be better off rewriting the
whole thing using iterators (and templatizing it while reimplementing).

V
 
S

Stephen Howe

I've some old code which I would like to replace by STL.
It's a self-written double linked list which consists of instances
of another class Element which contains the information as
void* (I'm going to replace the void* by a template but that's
not an issue here).

A few points:

1) Your class is intrusive in that it Element defines pointers to
previous/next elements.
std::list is non-intrusive. The redesigned class would be

class Element
{
// some data types
};

and therefore this newly designed Element could be placed in any of the
containers, not just linked lists. std::list supplies the pointers to
next/previous elements not class Element.

2) There is no such thing as "the current element" for std::list.

As Victor says, I would replace the equivalent functionality with std::list
and not seek to implement some kind of auxiliary functions to std::list that
basically replicate your old class.

Stephen Howe



The class Element is defined as:
class Element
{
void* value;
Element* nextelem, *prevelem;
[snip]
};

The class List is defined as:
class List
{
private:
Element* first;
Element* last;
// used as forward iterator
Element* forward_iterator ;
// used as backward iterator
Element* backward_iterator;
[snip]
public:
void* NextElement();
[snip]
};

and the function NextElement() is defined as:

void* List::NextElement()
{
// isEmpty() checks wheather list empty
if (!forward_iterator || isEmpty())
return NULL;

// value is the void* content of Element
void *next = forward_iterator->value;
forward=forward->nextelem;
return next;
}


What would be the best and most efficient way to replace
this function by a function using STL lists?

If the function NextElement would require an Element
as parameter to find its successor like

void* List::NextElement(void* currentElement)

I would first use the algorithm find() and than
increment the iterator. Something like

return *(++(find(begin(), end(), currentElement)));

(hope the syntax is ok ;) )



Thank you for your help in advance

Regards,
Chris
 
C

Christian Christmann

Instead of attempting a piece-by-piece replacement, and mostly because the
functionality is not compatible, you'd be better off rewriting the whole
thing using iterators (and templatizing it while reimplementing).

Generally I agree with you. But the problem is, that the functions of the
list class are used frequently in some other programs. Thus, when I
just replace the body of the function and keep the prototype definition
of that function I don't need to change all the programs using the
list class.
 
K

Kai-Uwe Bux

Christian said:
Generally I agree with you. But the problem is, that the functions of the
list class are used frequently in some other programs. Thus, when I
just replace the body of the function and keep the prototype definition
of that function I don't need to change all the programs using the
list class.

So, if you insist, what about something like this:


class bad_list {
private:

typedef void* void_p;
typedef std::list< void_p > void_p_list;

void_p_list the_list;
void_p_list::iterator the_cursor;

public:

void_p NextElement ( void ) {
if ( the_cursor == the_list.end() ) {
return ( 0 );
}
void_p result = *the_cursor;
++ the_cursor;
return( result );
}

};



Best

Kai-Uwe Bux
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,997
Messages
2,570,240
Members
46,829
Latest member
KimberAlli

Latest Threads

Top