Ooops
October 28, 2021, 13:37:07

Author Topic: [bmx] TListF by dmaz [ 1+ years ago ]  (Read 643 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
[bmx] TListF by dmaz [ 1+ years ago ]
« on: June 29, 2017, 00:28:38 »
Title : TListF
Author : dmaz
Posted : 1+ years ago

Description : it allows
Code: [Select]
local list:TListF = new TListF
for blah:yourtype = eachin list.From(start:TLink,howMany:int=-1)
or
Code: [Select]
for blah:yourtype = eachin list.ReverseFrom(start:TLink,howMany:int=-1)or
Code: [Select]
for blah:TLink = eachin list.LinksFrom(start:TLink,howMany:int=-1)
if howMany is -1 then it will continue to the end or the begining as in the case of ReverseFrom. [/i]

Code :
Code: BlitzMax
  1. SuperStrict
  2.  
  3. Type TListF Extends TList
  4.         Method From:TEnumerator( start:TLink, howMany:Int=-1 )
  5.                 Local e:TFromEnum = New TFromEnum
  6.                 e._link = start
  7.                 e._howMany = howMany
  8.                 Local enum:TEnumerator = New TEnumerator
  9.                 enum.enumerator = e
  10.                 Return enum
  11.         End Method
  12.  
  13.         Method ReverseFrom:TEnumerator( start:TLink, howMany:Int=-1 )
  14.                 Local e:TReverseFromEnum = New TReverseFromEnum
  15.                 e._link = start
  16.                 e._howMany = howMany
  17.                 Local enum:TEnumerator = New TEnumerator
  18.                 enum.enumerator = e
  19.                 Return enum
  20.         End Method
  21.        
  22.         Method LinksFrom:TEnumerator( start:TLink, howMany:Int=-1 )
  23.                 Local e:TLinksEnum = New TLinksEnum
  24.                 e._link = start
  25.                 e._howMany = howMany
  26.                 Local enum:TEnumerator = New TEnumerator
  27.                 enum.enumerator = e
  28.                 Return enum
  29.         End Method
  30.  
  31. End Type
  32.  
  33. Type TEnumerator
  34.         Field enumerator:TFromEnum
  35.  
  36.         Method ObjectEnumerator:TFromEnum()
  37.                 Return enumerator
  38.         End Method
  39. End Type
  40.  
  41. Type TFromEnum
  42.         Field _link:TLink
  43.         Field _howMany:Int
  44.  
  45.         Method HasNext:Int()
  46.                 Return (_link._value<>_link) And _howMany
  47.         End Method
  48.  
  49.         Method NextObject:Object()
  50.                 Local value:Object=_link._value
  51.                 Assert value<>_link
  52.                 _link=_link._succ
  53.                 _howMany:-1
  54.                 Return value
  55.         End Method
  56. End Type
  57.  
  58. Type TReverseFromEnum Extends TFromEnum
  59.         Method HasNext:Int()
  60.                 Return (_link._value<>_link) And _howMany
  61.         End Method
  62.  
  63.         Method NextObject:Object()
  64.                 Local value:Object=_link._value
  65.                 Assert value<>_link
  66.                 _link=_link._pred
  67.                 _howMany:-1
  68.                 Return value
  69.         End Method
  70. End Type
  71.  
  72.  
  73. Type TLinksEnum Extends TFromEnum
  74.         Method NextObject:Object()
  75.                 Local rtn:Object=_link
  76.                 Assert _link._value<>_link
  77.                 _link=_link._succ
  78.                 _howMany:-1
  79.                 Return rtn
  80.         End Method
  81. End Type
  82.  
  83.  
  84.  
  85.  
  86. ' TEST PROGRAM
  87. Type t
  88.         Global gid:Int  = 0
  89.         Global list:TListF = New TListF
  90.        
  91.         Field id:Int
  92.         Field link:TLink
  93.        
  94.         Method New()
  95.                 id = gid
  96.                 gid :+ 1
  97.                 link = list.AddLast(Self)
  98.         End Method
  99.        
  100. End Type
  101.  
  102. Local start:TLink
  103.  
  104. 'create 16 of type t and record one in 'start'
  105. For Local i:Int=0 To 15
  106.         Local tmp:t = New t
  107.         If tmp.id = 5 Then start=tmp.link
  108. Next
  109.  
  110. Print "~nthe whole list the normal way"
  111. For Local i:t = EachIn t.list
  112.         Print i.id
  113. Next
  114.  
  115. Print "~n5 from the start TLink "
  116. For Local i:t = EachIn t.list.From(start,5)
  117.         Print ">"+i.id
  118. Next
  119.  
  120. Print "~nall from the start TLink "
  121. For Local i:t = EachIn t.list.From(start,-1)
  122.         Print ">"+i.id
  123. Next
  124.  
  125. Print "~nall link from the begining"
  126. For Local i:TLink = EachIn t.list.LinksFrom(t.list.FirstLink())
  127.         Print ">"+i.ToString()+": "+t(i._value).id
  128. Next
  129.  
  130. 't.list.Clear
  131. Print "~nreverse from last TLink"
  132. For Local i:t = EachIn t.list.ReverseFrom(t.list.LastLink())
  133.         Print ">"+i.id
  134. Next


Comments :


dmaz(Posted 1+ years ago)

 added:ReverseFromLinkFrom


 

SimplePortal 2.3.6 © 2008-2014, SimplePortal