CFD Online Discussion Forums

CFD Online Discussion Forums (http://www.cfd-online.com/Forums/)
-   OpenFOAM Programming & Development (http://www.cfd-online.com/Forums/openfoam-programming-development/)
-   -   How to delete from a DyanmicList? (http://www.cfd-online.com/Forums/openfoam-programming-development/116786-how-delete-dyanmiclist.html)

GPesch April 25, 2013 05:41

How to delete from a DyanmicList?
 
Hi,

this is probably a very daft question, but I just can't figure it out.
I have already looked into doxygen, but my experience in C++ is too low.

Okay, thats what I have (its taken from dsmcParcel::collisions() from the dsmc library):

Code:

        DynamicList<ParcelType*>& cellParcels(cellOccupancy_[cellI]);

        forAll(cellParcels, i) {
            ParcelType& p = *cellParcels[i];
            const bool allowedToMove = p.allowedToMove();
            if (!allowedToMove)
            {
                cellParcels[i].remove();
                notAllowedToMove++;
            }
        }

As I understand it, I have the the DynamicList cellParcels which is filled with the DsmcParcels which are in the cell CelII.
I want to remove all Parcels from the List in which the parameter allowedToMove is false.
However, I can't figure out how to remove an item from the list. According to doxygen I can remove from a list with DynamicList::remove(), but this is a method without any arguments. So how to tell remove() which item to remove?

Or is remove only used to remove the last item of a list??
cellParcels[i].remove(); doesn't work either.

So in case it is impossible to remove an item from a list I tried a different way by just filling the list manually by iterating over "cellParcels[cellI]" and just adding the elements which are "allowedToMove":

Code:

        DynamicList<ParcelType*>& cellParcels();

        forAll (cellOccupancy_[cellI],i)
        {
            const ParcelType& p = *cellOccupancy_[cellI][i];
            const bool allowedToMove = p.allowedToMove();
            if (allowedToMove)
            {
                cellParcels.append(p);
            }
        }

But then I get an error:

Fehler: Abfrage des Elementes »append« in »Foam::cellParcels<Foam::DsmcParcel<Foam::particle > >«, das vom Nicht-Klassentyp »Foam::DynamicList<Foam::DsmcParcel<Foam::particle >*, 0u, 2u, 1u>&()« ist

That's german and basically translates to that they are not from the same type.

Can somebody help me to sort this out? Either by telling me what I am doing wrong or by giving me a smart way of how to do this?

Thank you so much in advance!

Georg

deepsterblue April 25, 2013 09:03

Georg,

DynamicList only allows appends and removal to/from the end of the list (think of a stack-like operation). If you're looking to remove from the middle, you'll have to resort to a copy to another list (followed by a List::transfer).

This might boil down to an appropriate choice of container - if you're looking to frequently remove arbitrary items from a list, a linked-list or hash table may be a better option.

GPesch April 25, 2013 18:40

Hi,

thanks for your reply.
How do I sort a DynamicList? Iterating over all items and using a self-written sort algorithm?
Guess I have to looked at linked-lists in this case, is IDLList the appropriate OpenFOAM class?

Any ideas why the second version I posted doesn't work? I have the feeling it might have something to do with the & and *'s, which are still a little confusing to me. However, I tried billion different combinations to put * and & somewhere but never received working results.
But maybe it doesn't have anything to do with that?

Sorry, I am a little bit lost :) I somehow hoped there is a simple answer, especially the second version I posted seemed quite reasonable to me and I hope there's just some stupid mistake in there..?!

deepsterblue April 26, 2013 09:08

I haven't tried it, but you can probably use an STL sort using the List::begin and List::end iterators, since they are largely STL compliant.

cellParcels is a list of pointers to ParcelType, while you are trying to append objects of ParcelType. They need to be consistent.

GPesch April 30, 2013 05:27

Pointers and References are really breaking my balls.
Sorry that I have to reply again, but I just can't figure it out :eek:

To give a little background.
The list cellOccupancy is defined like that:

Code:

List<DynamicList<ParcelType*> > cellOccupancy_;
So it is a list filled with pointers to ParcelType (hence the asterisk in <ParcelType*>, right?)

It is filled like that:
Code:

    forAllIter(typename DsmcCloud<ParcelType>, *this, iter)
    {
        cellOccupancy_[iter().cell()].append(&iter());
    }

Why do I put the (&) before iter()?

So if I fill that list like that (now p should be a pointer because of the asterisk, right?), it still doesn't work:

Code:

        DynamicList<ParcelType*>& cellParcels();

        forAll (cellOccupancy_[cellI],i)
        {
            ParcelType* p
            p=cellOccupancy_[cellI][i];

            const bool allowedToMove = p->allowedToMove();
            if (allowedToMove)
            {
                cellParcels.append(p);
            }
        }

Interestingly, if I initialise the list like that:
Code:

        DynamicList<ParcelType*>& cellParcels(cellOccupancy_[cellI]);
instead of
Code:

        DynamicList<ParcelType*>& cellParcels();
, i can easily append (at least I do not get an error message) objects like this (although I do not have any idea of what I am doing :) ):
Code:

            ParcelType& p = *cellOccupancy_[cellI][i];

            const bool allowedToMove = p.allowedToMove();
            if (allowedToMove)
            {
                cellParcels.append(&p);
            }

Sorry that's a lot of code - I hope this is not to complicated

Can anyone help to get some light in my dark?
What does this mean?

ParcelType& p = *cellOccupancy_[cellI][i];

I thought I could either initialise a variable without putting anything between the type and the name or a pointer by putting an asterisk between type and variable?
My book teaches me that & points to the memory address of a variable. Why do I use it when I declare the variable? Why is defined with an asterisk before cellOccupancy? Isn't it a list filled with pointers anyway? Isn't the astersik meaning I have a pointer to a pointer (inceptionlike)... ?

Aiaiai... I hope someone still has the nerves to help me!

alquimista July 19, 2013 13:44

Hello,

I have been also discussing how to remove elements from a dynamic list here . I wrote an simple example inserting and removing values in a HashTable. For now I have a little problem using HashTable in combination with findLower but someone can find it interesting if is looking for this kind of information.

Regards.


All times are GMT -4. The time now is 10:58.