This project is read-only.

[Problem Identified]Deque problem

May 24, 2011 at 10:38 PM
Edited May 24, 2011 at 10:38 PM

First of all, thank you for the great code library. I am using Nuclex to create an engine used to teach students intro to game design.

 

I've run in to a snag with your Deque<> class implementation. While iterating through the contents, the null object at the end of the collection seems to be a valid entry within the collection

 

 

foreach (GUILayout gui in m_stackGUI)
    m_guiMgr.Screen.Desktop.Children.Add(gui.m_desktop);

With only a single element in the stack, the foreach loop runs twice. Once for the valid element, and an additional time where "gui" will hold the value "null". 
I have also tried this with specifically declaring an iterator with the same results.

var itr = m_stackGui.GetEnumerator();
while(itr.MoveNext())
     m_guiMgr.Screen.Desktop.Children.Add(gui.m_desktop);

for now, my fix is to simply test for the value of null within each iteration of the loop, which seems less than ideal. I admit that I am not well versed in 
C#, however, this seems like a needless check whenever using your Deque collection.


               
May 24, 2011 at 10:45 PM
Edited May 24, 2011 at 11:34 PM

I've found the problem.

First of all, a foreach loop on an newly created deque will toss an exception due to an error trying to "get" the First property. This does not happen with "System.Collections.Generic" collections.

Additionally, if the deque is empty, and you call AddFirst(), only the "First" property references the single item within the deque. the "Last" property does not get updated to point to the single object within the Deque.  However, AddLast() updates both the "First" and "Last" properties on an empty Deqeue.

This seems like an oversight in the design, as if you are trying to maintain a stack which pushes objects on the front, you cannot iterate safely without having to manually check your iterator for null. 

(Note: I understand that, by design, you shouldn't iterate a stack or a queue, but there are times when it is highly useful to use an stack which can be iterated)

 

My quick solution to this is simple:

 if (m_stackGUI.Count == 0)
     m_stackGUI.AddLast(gui);
 else
     m_stackGUI.AddFirst(gui);

May 28, 2011 at 1:33 AM

Cygon, 

Is there any chance for an update to resolve the issues with an empty deque throwing exceptions when iterated with a foreach loop, as well as the "AddFirst" method not updating the "Last" property when called on an empty Deque?

Overall I prefer the deque class over the list for its performance, but the extra check required are cumbersome.