Dictionary trouble

Always adds a reference rather than a copy of the value

Page 1 of 1

3 Replies - 4981 Views - Last Post: 29 April 2008 - 04:02 PM Rate Topic: -----

#1 killnine  Icon User is offline

  • D.I.C Head

Reputation: 19
  • View blog
  • Posts: 161
  • Joined: 12-February 07

Dictionary trouble

Posted 29 April 2008 - 08:22 AM

I am creating a dictionary that is basically a log file. The key is a double value of the timestamp, and the value is a double array of signal values. However, I have found that when I output my log, all my values are the same. This is because I rewrite the same global variable (signalValues) and the dictionary only adds a reference to the value, and does not create a copy of the object itself.

				 if (!currentLoopTime.Equals(previousLoopTime))
				{
					double time = (currentLoopTime - timeOffset)/1000;
					timeAndSignals.Add(time, signalValues);									  
					previousLoopTime = currentLoopTime;
					signalTimeNotChanging = 0;
				}
.
.
.

			//Output each key and value to log
			foreach (KeyValuePair<double, double[]> kvp in timeAndSignals)
			{
				string valueString = string.Empty;
				foreach (double value in kvp.Value)
				{
					valueString += value + ", ";
				}

				log.Add(kvp.Key + "," + valueString);
			}



The code above causes problems, because as I iterate through the dictionary, I see that they all have the last-gathered signal information.

So I have to do this right now, but it seems pretty hack-ish. Thoughts?

				if (!currentLoopTime.Equals(previousLoopTime))
				{
					double time = (currentLoopTime - timeOffset)/1000;
					double[] signalValuesNonreference = new double[signalValues.Length];
					signalValues.CopyTo(signalValuesNonreference, 0);					
					timeAndSignals.Add(time, signalValuesNonreference);					
					previousLoopTime = currentLoopTime;
					signalTimeNotChanging = 0;
				}
.
.
.
			//Output each key and value to log
			foreach (KeyValuePair<double, double[]> kvp in timeAndSignals)
			{
				string valueString = string.Empty;
				foreach (double value in kvp.Value)
				{
					valueString += value + ", ";
				}

				log.Add(kvp.Key + "," + valueString);
			}


This post has been edited by killnine: 29 April 2008 - 08:25 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Dictionary trouble

#2 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5780
  • View blog
  • Posts: 12,595
  • Joined: 16-October 07

Re: Dictionary trouble

Posted 29 April 2008 - 09:41 AM

I'd use a custom class. Also, it doesn't really look like you need a dictionary, I'd use a list instead.

e.g.
class TimeAndSignals {
	public double time;
	public List<double> signals;

	public override string ToString() {
		string s = this.time.ToString();
		foreach (double value in this.signals) {
			s += "," + value;
		}
		return s;
	}
}

class TimeAndSignalsCollection : List<TimeAndSignals> {
	public void Add(double time, List<double> signals) {
		TimeAndSignals item = new TimeAndSignals();
		item.time = time;
		item.signals = new List<double>(signals);
		this.Add(item);
	}

	public void Add(double time, double[] signals) {
		TimeAndSignals item = new TimeAndSignals();
		item.time = time;
		item.signals = new List<double>(signals);
		this.Add(item);
	}
}


Was This Post Helpful? 0
  • +
  • -

#3 killnine  Icon User is offline

  • D.I.C Head

Reputation: 19
  • View blog
  • Posts: 161
  • Joined: 12-February 07

Re: Dictionary trouble

Posted 29 April 2008 - 11:39 AM

Neat. I have this nasty habit of just using standard data types to represent irregular data types. Thanks for the example.

Would this be faster, as well? I take signals at 100Hz (every 10ms), and each list of doubles is an array about 330 items long. I think that's a fair amount of data flowing, so I want to do what will save some time.


For anyone that might stumble across this thread, I thought about using a dictionary because the key offered a convenient way of using the timestamp as an identifier for a specific set of data. *shrug*. Also, the dictionary is based on a hash table so I figured that would be fairly quick to reference later, regardless that the dictionary is just iterated linearly...

This post has been edited by killnine: 29 April 2008 - 11:51 AM

Was This Post Helpful? 0
  • +
  • -

#4 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5780
  • View blog
  • Posts: 12,595
  • Joined: 16-October 07

Re: Dictionary trouble

Posted 29 April 2008 - 04:02 PM

View Postkillnine, on 29 Apr, 2008 - 02:39 PM, said:

Would this be faster, as well?


Building a List is probably about the same overhead as a Dictionary. The primary advantage a Dictionary would offer is the uniqueness of the key. However, if you should happen to generate a duplicate key, you're in trouble. Even if a Dictionary seems to make more sense, I'd still probably use a custom class for the payload; it's just easier to maintain.

You can actually build some logic into a custom class to speed up lookups, if that's an issue.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1