Click here to Skip to main content
15,867,568 members
Please Sign up or sign in to vote.
5.00/5 (2 votes)
See more:
I have a DataType "OpcValue" -- its definition is

  OpcValue
      |
   ___|___
  |       |
_variant_t   CCsTime


1.) I am reading datavalues and timestamps from a file and inserting points of type OpcValue into a CLIST object -- (total 1000 points will be read from the file). A last inserted position is calculated when adding each point.

2.) For every 100th point added, i am storing the position of the 100th point(last inserted position of the 100th point) into a seperate array -- CARRAY.

3.) Now when ever i have to display the values from the CList (may be 388th item) depending on the timestamp, i go through the CARRAy to get

the position of 300th point(3rd item of the CArray) and iterate from there to get the 388th point, thereby avoiding iteration of first 300 points. this saves time and is working excellent.

Now i have 3 scenarios:

i.) The first 400 points are removed from the CLIST (the points near to the tail), and if i am trying to display the remaining points, if i use the CArray's reference (CLIST's position reference stored), the positions are still valid and i am getting the result.

ii.) If i add more points to the CLIST and use the CArray reference, the whole operation is working fine too.

iii.) But in the third scenario, if i add more points and remove some points from the list, the CArray positions are no more valid.


Why is this happening?

Does adding or removing points in a CLIST makes all the positions invalid? If so how is it wroking in the first two cases?
Posted
Comments
Nish Nishant 13-Apr-11 16:26pm    
Voted 5, overall an interesting thread/discussion.

Assuming you are using MFC's CList, there is no need to do all this extra work. Using a combination of GetAt and FindIndex you can get the item at a specific index.

See http://msdn.microsoft.com/en-us/library/et138h3x(v=VS.100).aspx[^]

[Edit]
------

After our discussion below I thought I should add this here. A CMap may be a better data structure for you in this specific scenario. That way you won't have to store lookup positions in the array.
 
Share this answer
 
v2
Comments
amarasat 13-Apr-11 14:45pm    
My CLIST is not based on index, i have to display the elements according to the timestamp, so u don't know at what index is an element with timestamp of may be 5 months back etc. Instead of iterating through whole LIST, i already have an array that has positions to the LIST, i check the timestamp of an element by using each position reference from the CARRAY.

Example: Out of all 1000 datapoints, i have to display a datapoint at April 4th 12:00 PM. Suppose this is the 999th point, i have to iterate through all 1000 points of my CLIST. But instead if i use my method, i will get the timestamp of the CLIST element at 100, 200, 300 etc using the reference positions stored in the CARRAY, check those timestamps and will find out which part i need to iterate.

** the timestamps are in increasing order, so to find 999th, i will find the 900th first and then iterate from there.
amarasat 13-Apr-11 15:14pm    
Were u able to understand my question Nishant?
Nish Nishant 13-Apr-11 15:24pm    
Yes I did. If you only add items or if you only remove items that are located before the starting position you should be okay. But say you store the position of the 300th item, and now you add and remove some items between 300 and 400 (relative to the original count). Now, if you want the 388th item, you cannot start at the stored position and move forward 88 times, because the pointers in between would all have significantly changed. And that makes sense too if you think of it. So the behavior you see is to be expected.
Nish Nishant 13-Apr-11 15:25pm    
A CMap may be a better data structure for you in this specific scenario. That way you won't have to store lookup positions in the array.
amarasat 13-Apr-11 16:09pm    
Thanks a lot Nishant for your information, what you are saying makes complete sense. Now i understand my algorithm has some limitations. The whole problem is, i have saved 100th, 200th, 300th and 400th position, now i have removed 225 items, but i haven't modified my CArray. I think as soon as i remove 225 items, if i remove the first 2 positions(100th and 200th point reference) from my CArray, my problem would be solved i think.

After removing the first two items from the CArray, Since the 300th and 400th point positions from the CArray are still valid according to what you said, i should start iterating through the 300th point reference and 400th point reference.

In other words, u said "If you only add items or if you only remove items that are located before the starting position you should be okay", my question is if i remove 225 points and madify my starting point by removing the first two elements of CArray(msdn says if we remove an element in the array, it shifts down all the elements above the removed element(s). It decrements the upper bound of the array but does not free memory.) i should be good right?
Just a guess, but it's possible that once you are removing and adding elements to the CList, some reallocation is taking place which will invalidate the positions you are storing. You will have to step through the CList code to better understand when these reallocations occur.
 
Share this answer
 
Comments
amarasat 13-Apr-11 16:13pm    
yes i think i should modify my position array whenever those positions are no more valid in the main array.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900