|
[Sponsors] |
April 25, 2013, 05:41 |
How to delete from a DyanmicList?
|
#1 |
New Member
Join Date: Apr 2013
Posts: 24
Rep Power: 13 |
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++; } } 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); } } Fehler: Abfrage des Elementes »append« in »Foam::cellParcels<Foam:smcParcel<Foam:article > >«, das vom Nicht-Klassentyp »Foam:ynamicList<Foam:smcParcel<Foam:article >*, 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 |
|
April 25, 2013, 09:03 |
|
#2 |
Senior Member
Sandeep Menon
Join Date: Mar 2009
Location: Amherst, MA
Posts: 403
Rep Power: 25 |
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. |
|
April 25, 2013, 18:40 |
|
#3 |
New Member
Join Date: Apr 2013
Posts: 24
Rep Power: 13 |
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..?! |
|
April 26, 2013, 09:08 |
|
#4 |
Senior Member
Sandeep Menon
Join Date: Mar 2009
Location: Amherst, MA
Posts: 403
Rep Power: 25 |
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. |
|
April 30, 2013, 05:27 |
|
#5 |
New Member
Join Date: Apr 2013
Posts: 24
Rep Power: 13 |
Pointers and References are really breaking my balls.
Sorry that I have to reply again, but I just can't figure it out To give a little background. The list cellOccupancy is defined like that: Code:
List<DynamicList<ParcelType*> > cellOccupancy_; It is filled like that: Code:
forAllIter(typename DsmcCloud<ParcelType>, *this, iter) { cellOccupancy_[iter().cell()].append(&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); } } Code:
DynamicList<ParcelType*>& cellParcels(cellOccupancy_[cellI]); Code:
DynamicList<ParcelType*>& cellParcels(); Code:
ParcelType& p = *cellOccupancy_[cellI][i]; const bool allowedToMove = p.allowedToMove(); if (allowedToMove) { cellParcels.append(&p); } 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! |
|
July 19, 2013, 13:44 |
|
#6 |
Member
Join Date: Apr 2010
Posts: 61
Rep Power: 15 |
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. |
|
Thread Tools | Search this Thread |
Display Modes | |
|
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
how to set periodic boundary conditions | Ganesh | FLUENT | 15 | November 18, 2020 06:09 |
mesh delete | keeper | CFX | 7 | February 5, 2020 11:51 |
fluent add additional zones for the mesh file | SSL | FLUENT | 2 | January 26, 2008 11:55 |
to delete or not to delete? | Wooster | CFX | 15 | November 1, 2007 19:54 |
Change or delete the materials type: Al | Adi | FLUENT | 4 | April 19, 2001 08:08 |