Obtain a pointer to data inside a trend items array
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 22

Thread: Obtain a pointer to data inside a trend items array

  1. #1
    Join Date
    Oct 2010
    Posts
    18

    Unhappy 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?

  2. #2
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Posts
    12,047

    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?

  3. #3
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Fairfax, VA
    Posts
    10,888

    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.

  4. #4
    Join Date
    Oct 2010
    Posts
    18

    Smile 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!!

  5. #5
    Join Date
    Oct 2010
    Posts
    18

    Re: Obtain a pointer to data inside a trend items array

    @Linley,

    can you explain me how to use a map in my case.

  6. #6
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Fairfax, VA
    Posts
    10,888

    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.
    Last edited by Lindley; October 13th, 2010 at 10:10 AM.

  7. #7
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Posts
    12,047

    Re: Obtain a pointer to data inside a trend items array

    What kind of list? Can you post the exact definition please?

  8. #8
    Join Date
    Oct 2010
    Posts
    18

    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.

  9. #9
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Fairfax, VA
    Posts
    10,888

    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.....

  10. #10
    Join Date
    Oct 2010
    Posts
    18

    Re: Obtain a pointer to data inside a trend items array

    @Lindley,

    the array is sorted according to timestamps of the data points.

  11. #11
    Join Date
    Oct 2010
    Posts
    18

    Re: Obtain a pointer to data inside a trend items array

    do you have any suggestions GCDEF?

  12. #12
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,564

    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 ?

  13. #13
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Posts
    12,047

    Re: Obtain a pointer to data inside a trend items array

    Quote Originally Posted by amara.fortheworld@gmail.com View Post
    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.
    Last edited by GCDEF; October 13th, 2010 at 11:02 AM.

  14. #14
    Join Date
    Oct 2010
    Posts
    18

    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.

  15. #15
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,564

    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 ?

Page 1 of 2 12 LastLast

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center