My favorites | Sign in
Project Home Wiki Issues Source
READ-ONLY: This project has been archived. For more information see this post.
Search
for
  Advanced search   Search tips   Subscriptions
Issue 69: problem with nested while/for loop
1 person starred this issue and may be notified of changes. Back to list
Status:  WontFix
Owner:  ----
Closed:  Dec 2010


 
Reported by michaelbaczynski@gmail.com, Oct 11, 2010
What steps will reproduce the problem?
1. compile source code. I tried to simplify it as much as possible.
2. an error is thrown in line24, because ArrayedStack#remove method is broken, probably due to the nested while/for construct.

What version of the product are you using? On what operating system?

haXe build from svn and hxcpp from svn

Please provide any additional information below.

class Bug
{
	static var _app:Bug;
	public static function main():Void
	{
		var s = new ArrayedStack<Int>();
		
		var values = [1, 2, 3, 4, 5];
		
		//push values onto stack
		for (i in values) s.push(i);
		
		//shuffle
		values = [3, 5, 1, 2, 4];
		
		//remove values from stack
		for (i in 0...5)
		{
			s.remove(values.pop());
			
			for (j in values)
			{
				if (!s.contains(j))
					throw 'error'; //should not happen
			}
		}
	}
}

class ArrayedStack<T>
{
	var _a:Array<T>;
	var _top:Int;
	
	public function new()
	{
		_a = new Array<T>();
		_top = 0;
	}
	
	inline public function push(x:T):Void
	{
		_a[_top++] = x;
	}
	
	public function contains(x:T):Bool
	{
		for (i in 0..._top)
		{
			if (_a[i] == x)
				return true;
		}
		return false;
	}
	
	public function remove(x:T):Bool
	{
		var NULL:Null<T> = null;
		var found = false;
		
		while (_top > 0)
		{
			found = false;
			for (i in 0..._top)
			{
				if (_a[i] == x)
				{
					var t = _top - 1;
					var p = i;
					while (p < t)
					{
						_a[p] = _a[++p];
					}
					_a[--_top] = NULL;
					found = true;
					break;
				}
			}
			
			if (!found) break;
		}
		
		return found;
	}
}


Oct 17, 2010
#1 michaelbaczynski@gmail.com
ok I found the bug, this is also a known quirk:

_a[p] = _a[++p];

should be:
_a[p++] = _a[p];

since evaluation order does matter here

Dec 21, 2010
Project Member #2 gameh...@gmail.com
Hi,
Yes, this is a bit unfortunate.
I can't think of a good way around this besides making the c++ code practically unreadable.
Status: WontFix

Powered by Google Project Hosting