simple extend to TList

BlitzMax Forums/BlitzMax Module Tweaks/simple extend to TList

dmaz(Posted 2006) [#1]
allows
local list:TListF = new TListF
for blah = eachin list.From(start:TLink,howMany:int)

SuperStrict

Type TListF Extends TList
	Method From:TEnumerator( start:TLink, howMany:Int )
		Local e:TFromEnum = New TFromEnum
		e._link = start
		e._howMany = howMany
		Local enum:TEnumerator = New TEnumerator
		enum.enumerator = e
		Return enum
	End Method
End Type

Type TFromEnum
	Field _link:TLink
	Field _howMany:Int

	Method HasNext:Int()
		Return (_link._value<>_link) And _howMany
	End Method

	Method NextObject:Object()
		Local value:Object=_link._value
		Assert value<>_link
		_link=_link._succ
		_howMany:-1
		Return value
	End Method
End Type

Type TEnumerator
	Field enumerator:TFromEnum

	Method ObjectEnumerator:TFromEnum()
		Return enumerator
	End Method
End Type


' TEST PROGRAM
'Rem
Type t
	Global gid:Int	= 0
	Global list:TListF = New TListF
	
	Field id:Int
	Field link:TLink
	
	Method New()
		id = gid
		gid :+ 1
		link = list.AddLast(Self)
	End Method
	
End Type

Local start:TLink

'create 16 of type t and record one in 'start'
For Local i:Int=0 To 15
	Local tmp:t = New t
	If tmp.id = 5 Then start=tmp.link
Next

Print "~nthe whole list the normal way"
For Local i:t = EachIn t.list
	Print i.id
Next

Print "~n5 from the start TLink "
For Local i:t = EachIn t.list.From(start,5)
	Print ">"+i.id
Next

Print "~nall from the start TLink "
For Local i:t = EachIn t.list.From(start,-1)
	Print ">"+i.id
Next

'end rem



Dreamora(Posted 2006) [#2]
http://www.blitzbasic.com/Community/posts.php?topic=62354

This might be of interest for people looking for an even more featured enumeration range implementation without mixing with TList and no need to change code where they use regular TLists.


dmaz(Posted 2006) [#3]
hmmm... I don't understand what you mean by "without mixing with TList and no need to change code where they use regular TLists". How do those 2 points apply to the code above?


Koriolis(Posted 2006) [#4]
I think Dreamora missed the fact that you extend TList rather than tweaking it (which would clearly not be a good idea)


Dreamora(Posted 2006) [#5]
Yes, Koriolis is right, my error, sorry :(