October 28, 2020, 11:12:08 PM

Author Topic: [bmx]Using Enumerations to iterate through a list backwards  (Read 1482 times)

Offline TomToad

  • Hero Member
  • *****
  • Posts: 517
[bmx]Using Enumerations to iterate through a list backwards
« on: April 03, 2018, 06:28:11 PM »
Title: TReverseableList
Author: TomToad
Posted: Apr 3, 2018

This extends the TList type so that you can iterate through it backwards with For/EachIn.  To use, create your list with Local List:TReverseableList = New TReverseableList.  The list acts like a normal TList.  To iterate through the list backwards using For/EachIn, just use For MyType = EachIn List.Backwards().  You can still iterate through it forwards in the normal way For MyType = Eachin List.
Code: BlitzMax
  1. SuperStrict
  2. 'TReverseableList type
  3. '  By TomToad
  4. '  Released into public domain
  5. '  use and modify as you please
  6. '
  7. '  Extends TList
  8. '  everything in a TList works exactly as before
  9. '  To use Eachin on the list backwards, just use
  10. '        For Instance = Eachin List.Backwards()
  11. '
  12. '  Example at end of source.
  13.  
  14.  
  15. Type TReverseableListEnum
  16.         Field _link:TLink
  17.        
  18.         Method HasNext:Int()
  19.                 Return _link._value<>_link
  20.         End Method
  21.  
  22.         Method NextObject:Object()
  23.                 Local value:Object=_link._value
  24.                 Assert value<>_link
  25.                 _link=_link._pred
  26.                 Return value
  27.         End Method
  28.  
  29. End Type
  30.  
  31. Type TReverseableListHold
  32.         Field List:TReverseableList
  33.        
  34.         Function Create:TReverseableListHold(List:TReverseableList)    
  35.                 Local ReverseableListHold:TReverseableListHold = New TReverseableListHold
  36.                 ReverseableListHold.List = List
  37.                 Return ReverseableListHold
  38.         End Function
  39.        
  40.         Method ObjectEnumerator:TReverseableListEnum()
  41.                 Local enum:TReverseableListEnum=New TReverseableListEnum
  42.                 enum._link=List._head._pred
  43.                 Return enum
  44.         End Method
  45. End Type
  46.  
  47. Type TReverseableList Extends TList
  48.  
  49.         Method Backwards:TReverseableListHold()
  50.                 Return TReverseableListHold.Create(Self)
  51.         End Method
  52.  
  53. End Type
  54.  
  55. Local List:TReverseableList = New TReverseableList
  56.  
  57. List.AddLast("Anthony")
  58. List.AddLast("Brian")
  59. List.AddLast("Charles")
  60. List.AddLast("Danny")
  61.  
  62. Print "~n--- populated list forwards"
  63. For Local s:String = EachIn List
  64.         Print s
  65. Next
  66.  
  67. Print "~n--- populated list backwards"
  68. For Local s:String = EachIn List.backwards()
  69.         Print s
  70. Next
------------------------------------------------
8 rabbits equals 1 rabbyte.

 

SimplePortal 2.3.6 © 2008-2014, SimplePortal