| Issue 69: | problem with nested while/for loop | |
| 1 person starred this issue and may be notified of changes. | Back to list |
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
Dec 21, 2010
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
|