CFD Online Discussion Forums

CFD Online Discussion Forums (
-   OpenFOAM Programming & Development (
-   -   dynamic list (variable size) (

alquimista July 18, 2013 12:31

dynamic list (variable size)

I have a list of scalars with unknown size at declaration time. During the simulation I want to remove given elements from the list at different times.

I know that you can define a DynamicList (DynamicList<scalar> test;) and then add some values (test.append(value)), clear the list (test.clear()) or maybe delete the last introduced value. The thing is if I need to delete some elements from the list It occurs to me to make a temporal copy with the new size or something like that.

So my question is... what is the best strategy to declare a list of scalars which is expected to delete some given elements (and then varying the size of the list)? How can I do it effectively?

Thank you in advanced.


edit: the post is similar was treated before here and there's an interesting reply:


Originally Posted by deepsterblue (Post 423118)

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.

Someone can explain it in detail?

l_r_mcglashan July 18, 2013 17:37

Depends. What is the size of your list, how often will you be deleting elements and how many will you remove?

alquimista July 18, 2013 17:59

Hello Laurance.

The list has a size in a range 100-1000 and I need to remove 1 element each time I check a condition. Every timestep It can happen for around 10-70% of the size of the list. (Eg: size 300, 40 elements to remove in a timestep).

Thank you for your hints.

l_r_mcglashan July 19, 2013 04:53

That's quite small. I'd either use a HashTable if you have to remove elements individually, or if you can remove them as a block then do this:


PackedBoolList elemToRemove(l.size());

forAll(l, i)
    if (checkCondition)
        elemToRemove[i] = true;

inplaceSubset(elemToRemove, l);

alquimista July 19, 2013 07:25

I don't have experience using HashTable, I actually need to remove elements individually from Dynamic and Sortable lists. I'm going to review everything with this new feedback.


alquimista July 19, 2013 13:29

Here is how I did it to have a pairs of labels and scalars defined as a HashTable. With this new method I can efficiently remove an element. There's a strange issue, I'm using the findLower function but the problem comes when I erase a element of the HashTable the findLower still points to the removed index. I have checked that the element was erased correctly (apparently at least debugging) and the the size reduced.

I show an example:


    HashTable<scalar, label> testHash_;

    //- insert values with key 1, 2 and 3

    //- print HashTable
    Info << "testHash: " << testHash_ << nl << endl;

    //- find value lower than 0.6
    label j = findLower(testHash_,0.6);

    //- erase key j and his value

    //- print HashTable
    Info << "testHash: " << testHash_ << nl << endl;

    //- find another value lower than 0.6
    label k = findLower(testHash_,0.6);

    //- erase key k and his value

    Info << "testHash: " << testHash_ << nl;



3 0.3
1 0.5
2 0.7

3 0.3
2 0.7

1 not found in table.  Valid entries: 2(3 2)

still looking for key 1!!

The topic of the post has been resolved for me, thank you very much! Still I have that thing but maybe I made a mistake.

All times are GMT -4. The time now is 05:59.