J
jacob navia
In the last discussion we discussed how to do "GetNext" etc with containers.
I think it is important that the user code stays as abstract as
possible, without getting into the specifics of any container, as Mr
Becarisse said (if I remember correctly).
Well, this could be done as follows. Please tell me what do you think.
Thanks
--------
All containers should be able to support the "GetNext" and the
"GetPrevious" functions. A "Rewind" operation resets the
iterator object to be reused with the same container.
To iterate ANY container from your user code you do:
Iterator *it = Container->Vtable->StartIterator(Container);
then:
void *object;
for (object = it->GetNext();
object != NULL;
object = it->GetNext()) {
// use the object
}
The iterator object is as follows:
typedef struct tagIterator {
/* Public (Required) fields */
void *(*GetNext)(void);
void *(*GetPrevious)(void);
void (*Rewind)(void);
/* Private fields follow, specific to each container
Here implementations store a pointer to the container
being iterated, the current index, whatever they need
*/
} Iterator;
For a single linked list, GetPrevious can return always NULL
(performance is really bad if you have a long list) or it can
supply the previous by going through the list at all times.
Containers are good for some operations but bad for others.
For hash tables or trees, GetNext() returns the "next" object in
an unspecified sequence that is guaranteed however to visit all elements
and return NULL when all elements have been visited.
Beides, a new operation is added to all containers:
Container->Vtable->StoreCurrent(Iterator *it,void *element);
All this operations take always the container itself as the first
argument. In this case it is unnecessary since the iterator already
has this information.
I do not know if this incoherence is a good idea...
jacob
I think it is important that the user code stays as abstract as
possible, without getting into the specifics of any container, as Mr
Becarisse said (if I remember correctly).
Well, this could be done as follows. Please tell me what do you think.
Thanks
--------
All containers should be able to support the "GetNext" and the
"GetPrevious" functions. A "Rewind" operation resets the
iterator object to be reused with the same container.
To iterate ANY container from your user code you do:
Iterator *it = Container->Vtable->StartIterator(Container);
then:
void *object;
for (object = it->GetNext();
object != NULL;
object = it->GetNext()) {
// use the object
}
The iterator object is as follows:
typedef struct tagIterator {
/* Public (Required) fields */
void *(*GetNext)(void);
void *(*GetPrevious)(void);
void (*Rewind)(void);
/* Private fields follow, specific to each container
Here implementations store a pointer to the container
being iterated, the current index, whatever they need
*/
} Iterator;
For a single linked list, GetPrevious can return always NULL
(performance is really bad if you have a long list) or it can
supply the previous by going through the list at all times.
Containers are good for some operations but bad for others.
For hash tables or trees, GetNext() returns the "next" object in
an unspecified sequence that is guaranteed however to visit all elements
and return NULL when all elements have been visited.
Beides, a new operation is added to all containers:
Container->Vtable->StoreCurrent(Iterator *it,void *element);
All this operations take always the container itself as the first
argument. In this case it is unnecessary since the iterator already
has this information.
I do not know if this incoherence is a good idea...
jacob