Browse Source

Fixed TLink removal during iteration issue.

woollybah 9 years ago
parent
commit
3eb9a72fa9
1 changed files with 25 additions and 3 deletions
  1. 25 3
      linkedlist.mod/linkedlist.bmx

+ 25 - 3
linkedlist.mod/linkedlist.bmx

@@ -6,12 +6,14 @@ bbdoc: Data structures/Linked lists
 End Rem
 Module BRL.LinkedList
 
-ModuleInfo "Version: 1.09"
+ModuleInfo "Version: 1.10"
 ModuleInfo "Author: Mark Sibly"
 ModuleInfo "License: zlib/libpng"
 ModuleInfo "Copyright: Blitz Research Ltd"
 ModuleInfo "Modserver: BRL"
 
+ModuleInfo "History: 1.10"
+ModuleInfo "History: Fixed TLink removal during iteration issue."
 ModuleInfo "History: 1.09"
 ModuleInfo "History: Added internal count."
 ModuleInfo "History: (Debug) Assertion on modification during iteration."
@@ -40,6 +42,7 @@ Type TLink
 
 	Field _value:Object
 	Field _succ:TLink,_pred:TLink
+	Field _iterHold:Int, _remove:Int
 	
 	Rem
 	bbdoc: Returns the Object associated with this Link.
@@ -69,8 +72,11 @@ Type TLink
 		_value=Null
 		_succ._pred=_pred
 		_pred._succ=_succ
-		_pred=Null
-		_succ=Null
+		_remove = True
+		If Not _iterHold
+			_pred=Null
+			_succ=Null
+		End If
 	End Method
 
 End Type
@@ -116,7 +122,22 @@ Type TListEnum
 ?
 		Local value:Object=_link._value
 		Assert value<>_link
+		
+		Local tmp:TLink = _link
+		
 		_link=_link._succ
+		
+		If _link Then
+			_link._iterHold :+ 1
+		End If
+		
+		tmp._iterHold :- 1
+		
+		If tmp._remove And tmp._iterHold <= 0 Then
+			tmp._pred=Null
+			tmp._succ=Null
+		End If
+		
 		Return value
 	End Method
 
@@ -158,6 +179,7 @@ Type TList
 	End Rem
 	Method Clear()
 		While _head._succ<>_head
+			_head._iterHold = 0
 			_head._succ.Remove
 		Wend
 ?ngcmod