diff --git a/msu/classes/ordered_map.nut b/msu/classes/ordered_map.nut index 82eca5ed..952ea9c4 100644 --- a/msu/classes/ordered_map.nut +++ b/msu/classes/ordered_map.nut @@ -2,12 +2,13 @@ { Array = null; Table = null; - NextICache = null; + IIndexStack = null; constructor( _table = null ) { this.Array = []; this.Table = {}; + this.IIndexStack = []; if (_table != null) this.addTable(_table); } @@ -45,10 +46,14 @@ function _nexti( _prev ) { - if (_prev == null) this.NextICache = 0; - _prev = this.NextICache++; - - return _prev == this.Array.len() ? null : this.Array[_prev]; + if (_prev == null) this.IIndexStack.push(0); + _prev = this.IIndexStack[this.IIndexStack.len() - 1]++; + if (_prev == this.Array.len()) + { + this.IIndexStack.pop(); + return null; + } + return this.Array[_prev]; } function _cloned( _original ) diff --git a/msu/classes/weighted_container.nut b/msu/classes/weighted_container.nut index 8a7dc2e8..6232bb47 100644 --- a/msu/classes/weighted_container.nut +++ b/msu/classes/weighted_container.nut @@ -3,14 +3,15 @@ Total = null; Table = null; Forced = null; - NextIItems = null; - NextIIndex = null; + IItems = null; + IIndexStack = null; constructor( _array = null ) { this.Total = 0.0; this.Table = {}; this.Forced = []; + this.IIndexStack = []; if (_array != null) this.addArray(_array); } @@ -31,19 +32,21 @@ { if (_prev == null) { - this.NextIItems = ::MSU.Table.keys(this.Table); - this.NextIIndex = 0; + if (this.IItems == null) + this.IItems = ::MSU.Table.keys(this.Table); + this.IIndexStack.push(0) } - _prev = this.NextIIndex++; + _prev = this.IIndexStack[this.IIndexStack.len() - 1]++; if (_prev == this.Table.len()) { - this.NextIItems = null; - this.NextIIndex = null; + this.IIndexStack.pop(); + if (this.IIndexStack.len() == 0) + this.IItems = null; return null; } - return this.NextIItems[_prev]; + return this.IItems[_prev]; } function toArray( _itemsOnly = true )