CFD Online Discussion Forums

CFD Online Discussion Forums (https://www.cfd-online.com/Forums/)
-   OpenFOAM Programming & Development (https://www.cfd-online.com/Forums/openfoam-programming-development/)
-   -   position of iterator? (https://www.cfd-online.com/Forums/openfoam-programming-development/129984-position-iterator.html)

maybee February 17, 2014 16:16

position of iterator?
 
hi,

when using something like

Code:

forAllIter(PtrDictionary<phaseModel>, phases_, iter) 
{
phaseModel& phase = iter();
.
.
.
}

is it possible to get the actual position/index of iterator "iter" (which pointer is the iterator refering to at the moment)?

Quote:

Additionally:

If I define a dictionary like

Code:

const dictionary& subDict_Droplets;
subDict_Droplets = properties.subDict("currentValue");

can I create an iterator for the dictionary, set this iterator to a specific position of the dictionary (the position I would get from the first iterator if possible :D -> see code snippet 1) and get the content of the dictionary at that position?
-> Ok, I guess I solved the second part more efficiently by using the datatype "list<dimensionedScalar>" instead of "dictionary". The elements of datatype "list<dimensionedScalar>" should be accessable like a vector (->array) right?


greetings
maybee

chrisb2244 February 25, 2014 02:17

In the case of the position/index of the iterator, what do you want your output to be? Like, 1,2,3 or phase1, phase2, phase3?

Because I think something like
Code:

Pout<< *iter << endl;
will give you the value in terms of the position, like 0, 1, 2, 3 etc.

maybee February 25, 2014 05:04

hi chris,

I want to get the position of the iterator as a number like 0,1,2,3....

But I guess

Quote:

*iter
will give me the content "iter is pointing to" , since an iterator is some kind of pointer and the operator * generally should dereference a pointer.

chrisb2244 February 25, 2014 05:28

So in the documentation, it turns out that iter() just returns the dereferencing of the iterator:

Code:

template<class T>
  220 inline T& Foam::PtrList<T>::iterator::operator*()
  221 {
  222    return **ptr_;
  223 }

template<class T>
  227 inline T& Foam::PtrList<T>::iterator::operator()()
  228 {
  229    return operator*();
  230 }

http://foam.sourceforge.net/docs/cpp/a01178.html might be vaguely helpful.
I'm afraid I don't have OF on this computer, and have left my workplace, so can't quickly check different permutations to get what you want.

I guess try different things and see what a) compiles, and then b) gives you the result you want?

chrisb2244 February 25, 2014 20:50

So I'm afraid I can't find anything that will give the integer position (although that is almost certainly due to my lack of imagination, rather than any inherent limitation in the OF framework).

You can however call iter.key() to get the phase. I know that was in fact the opposite of what you wanted, but maybe it will be helpful?

maybee February 28, 2014 09:36

@chris: Thx for your help, but unfortunately to get the phase does not help me at all.

Update:

I tried now something like

Code:

forAllIter(PtrDictionary<phaseModel>, fluid.phases(), iter)
{

PtrDictionary<phaseModel>& PtrDict = fluid.phases();
int IteratorPosition = std::distance(PtrDict.cbegin(), iter);

}

, but I get the error:

Code:

error: no matching function for call to ‘distance(Foam::DLListBase::const_iterator, Foam::LPtrList<Foam::DLListBase, Foam::phaseModel>::iterator&)
Perhaps someone knows to solve this problem?


All times are GMT -4. The time now is 12:48.