Obtain a pointer to data inside a trend items array
position = m_TrendItems[pen].GetTrendPoints().GetHeadPosition();
while(trendpoint .m_time < requiredtime)
trendpoint = m_TrendItems[pen].GetTrendPoints().GetPrev(position);
m_TrendItems is the global array which has almost 10,000 points for each pen. My question is instead of getting the trendpoint, i want to get the pointer for the (position or point).
I want to store this pointer in a separate array and use it in the future so that i don't have to go from the head position always.
How can i do this?
Do i have to go through all the time i have to find a point can't i store a pointer?
Re: Obtain a pointer to data inside a trend items array
Head position implies a list, not an array. What is it really?
Why would you want to store a single pointer in an array?
What trouble are you having storing a pointer?
Re: Obtain a pointer to data inside a trend items array
It sounds like you want to do more efficient lookups in this data structure? In that case, you should try a std::map or std::unordered_map.
Re: Obtain a pointer to data inside a trend items array
I am sorry it is a list.
i meant not to store a single point, i just want to know how to get a pointer, so that i can store as many as i want.
I have 10 hours of data points in my list, if i have to display points between 3 and 4 hours on the trend, the code is
position = m_TrendItems[pen].GetTrendPoints().GetHeadPosition();
while(trendpoint .m_time < requiredtime)
trendpoint = m_TrendItems[pen].GetTrendPoints().GetPrev(position);
if i have to display points between 5 and 6 hours on the trend, the code is again,
position = m_TrendItems[pen].GetTrendPoints().GetHeadPosition();
while(trendpoint .m_time < requiredtime)
trendpoint = m_TrendItems[pen].GetTrendPoints().GetPrev(position);
if i have to display points between 8 and 9 hours on the trend, the code is again,
position = m_TrendItems[pen].GetTrendPoints().GetHeadPosition();
while(trendpoint .m_time < requiredtime)
trendpoint = m_TrendItems[pen].GetTrendPoints().GetPrev(position);
here i am wasting time by iterating through all the data points that were present. Instead of this what if i can break my 10 hours of data into 10 parts (each of one hour) and store the pointers for the beginning of each hour.
That way if i have to display points between 8 and 9 hours on the trend i will go to the pointer which has position of datapoint starting at 8th hour and then iterate from there. So instead of
position = m_TrendItems[pen].GetTrendPoints().GetHeadPosition();
i will have position = &pointer for 8th hour. How can i do this!!
Re: Obtain a pointer to data inside a trend items array
@Linley,
can you explain me how to use a map in my case.
Re: Obtain a pointer to data inside a trend items array
It definitely seems like you want a std::map or std::multimap. In particular, check out the lower_bound and upper_bound member functions.
http://www.cplusplus.com/reference/stl/map/
Now, *if* the linked list is in sorted order and *if* it conforms to the STL container interface (it doesn't look to right now), then you could use std::lower_bound() directly on it without needing to use a map as the underlying container....but it would still be more efficient if you had random access iterators rather than merely bidirectional iterators, eg, you had a vector or deque rather than a list.
Re: Obtain a pointer to data inside a trend items array
What kind of list? Can you post the exact definition please?
Re: Obtain a pointer to data inside a trend items array
@GCDEF, is this what you are asking for?
typedef CArray<CTrendItem, CTrendItem&> CTrendItems;
CTrendItems m_TrendItems;
@Lindley, sure will check now.
Re: Obtain a pointer to data inside a trend items array
Ah---you're using MFC containers rather than STL containers. Well, I don't know those very well, but it's possible MFC has a binary tree container equivalent to std::map.....
Re: Obtain a pointer to data inside a trend items array
@Lindley,
the array is sorted according to timestamps of the data points.
Re: Obtain a pointer to data inside a trend items array
do you have any suggestions GCDEF?
Re: Obtain a pointer to data inside a trend items array
I am having problems following exactly what is going on.
1) You are calling GetHeadPositiion() and then looping on GetPrev() ??
2) Could you show the data members for CTrendItem ?
Re: Obtain a pointer to data inside a trend items array
Quote:
do you have any suggestions GCDEF?
No. I'm still confused about what data types you're working with and I'm tired of trying to figure it out.
GetHeadPosition and GetPrev are members of MFC's CList class. They return a POSITION. You can store that and use it to position yourself anywhere in the list you want. If you need a position in an array, just store the index.
Re: Obtain a pointer to data inside a trend items array
@CDEFG and @ Philip Nicoletti
Thinks this should help,
CTrendItems m_TrendItems;
typedef CArray<CTrendItem, CTrendItem&> CTrendItems;
CCsTrendPoints& GetTrendPoints(){
return m_TrendPoints;
}
CCsTrendPoints m_TrendPoints;
typedef CList<CCsTrendPoint, CCsTrendPoint> CCsTrendPoints;
class CCsTrendPoint
{
public:
// functions
CCsTrendPoint() {m_Time = (time_t)0; m_Value =0;}
CCsTrendPoint(CTime time, float value) {m_Time = time; m_Value = value;}
CCsTrendPoint(CCsTime time, float value){m_Time = time; m_Value = value;}
CCsTrendPoint(CCsTrendPoint& point) { *this = point; }
CCsTrendPoint& operator =(CCsTrendPoint& point)
{m_Time = point.m_Time; m_Value = point.m_Value; return *this;}
// variables
CCsTime m_Time;
float m_Value;
};
@Philip
yes i am looping through prev
members of dataitems has
CCsTrendPoints m_TrendPoints; which is protected.
Let me know if you guys need mroe info, thanks for all the time.
Re: Obtain a pointer to data inside a trend items array
I haven't used the MFC containers for a while, so maybe I am missing
something simple. But I still do not understand the following:
Code:
position = m_TrendItems[pen].GetTrendPoints().GetHeadPosition();
while(trendpoint.m_time < requiredtime)
trendpoint = m_TrendItems[pen].GetTrendPoints().GetPrev(position);
After the first time thru the while loop, won't position equal NULL ?
Where did trendpoint come from initially ?
Re: Obtain a pointer to data inside a trend items array
i just gave that to shiw the looping logic i am going through:
pos = m_TrendItems[pen].GetTrendPoints().GetTailPosition();
if(pos)
trendpoint = m_TrendItems[pen].GetTrendPoints().GetPrev(pos);
while(pos && trendpoint.m_time < m_selection.time)
{
trendpoint = m_TrendItems[pen].GetTrendPoints().GetPrev(pos );
}
.GetTrendPoints() returns m_TrendPoints which is of type
typedef CList<CCsTrendPoint, CCsTrendPoint> CCsTrendPoints;
so technically i am working on CList m_TrendPoints;
m_TrendPoints.GetPrev(pos );
Gets the list element identified by pos, then sets pos to the POSITION value of the previous entry in the list.
now instead of getting the point, i want to get a pointer to the point something like:
trendpoint* = &m_TrendPoints.GetPrev(pos );
now, is the above code correct, will me pointer have a life as global, if so, how to make this pointer an global array, if so how to make the global array two dimensional, so that first dimension is pen and second dimension is index and value at it is the pointer
something like trendpoint[pen][i]*, how to do this?
Re: Obtain a pointer to data inside a trend items array
continued.........
trendpoint[pen][i]* = &m_TrendPoints.GetPrev(pos ); (where the point is start of 1st hour)
trendpoint[pen][i]* = &m_TrendPoints.GetPrev(pos ); (where the point is start of second hour)
trendpoint[pen][i]* = &m_TrendPoints.GetPrev(pos ); (where the point is start of third hour) etc
like so for each pen.
Re: Obtain a pointer to data inside a trend items array
Quote:
i just gave that to shiw the looping logic i am going through:
POSITIONS are not pointers -- POSITIONS are POSITIONS. It acts like a pointer, but it isn't one. It is similar to an STL "iterator". There is no interface to turn a POSITION into a pointer because the POSITION is your "pointer".
So if you have a POSITION of something in the CList, then you have a "pointer" to what you want (even though technically, POSITIONS are not pointers).
Regards,
Paul McKenzie
Re: Obtain a pointer to data inside a trend items array
Quote:
Originally Posted by
Paul McKenzie
POSITIONS are not pointers -- POSITIONS are POSITIONS. It acts like a pointer, but it isn't one. It is similar to an STL "iterator". There is no interface to turn a POSITION into a pointer because the POSITION is your "pointer".
So if you have a POSITION of something in the CList, then you have a "pointer" to what you want (even though technically, POSITIONS are not pointers).
Regards,
Paul McKenzie
Right. As I said earlier, you can use the POSITION to hop directly to any point in the list. Then GetNext or GetPrev to move backwards or forwards.
Re: Obtain a pointer to data inside a trend items array
Quote:
trendpoint* = &m_TrendPoints.GetPrev(pos );
now, is the above code correct, will me pointer have a life as global, if so, how to make this pointer an global array, if so how to make the global array two dimensional, so that first dimension is pen and second dimension is index and value at it is the pointer
something like trendpoint[pen][i]*, how to do this?
That makes no sense at all to me.
Re: Obtain a pointer to data inside a trend items array
Quote:
now, is the above code correct, will me pointer have a life as global, if so, how to make this pointer an global array, if so how to make the global array two dimensional,
Your terminology is very confusing.
1) A pointer is not an array. You cannot turn a pointer into an array.
2) You now want to turn this "array" into a two-dimensional array. You can't turn a one-dimensional array into 2-dimensions without defining what you mean by turning a 1-d array into a 2-d array.
Regards,
Paul McKenzie
Re: Obtain a pointer to data inside a trend items array
You know what might be more beneficial -- you tell us what your high-level problem is that you're trying to solve, instead of telling us how you are "solving" it and having us try to make your solution work.
That way, others can give you much better advice than trying to take what you think is a solution, and trying to make something work out of something that is unworkable.
Regards,
Paul McKenzie