container.monkey2 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. Namespace std.collections
  2. #rem monkeydoc The IContainer interface is a 'dummy' interface that container classes should implement for compatibility with Eachin loops.
  3. IContainer does not actually declare any members, but a class that implements IContainer should implement the follow methods:
  4. `Method All:IteratorType()` - Gets an iterator to all values in the container.
  5. ...where `IteratorType` is a class or struct type that implements the following properties and methods:
  6. `Property Current:ValueType()` - The current value pointed to by the iterator.
  7. `Property AtEnd:bool()` - true if iterator is at end of container.
  8. `Method Bump()` - Bumps the iterator so it points to the next value in the container.
  9. ...where 'ValueType' is the type of the values contained in the container.
  10. With these conditions met, a container can be used with eachin loops. Monkey2 will automatically convert code like this:
  11. ```
  12. For Local value:=Eachin container
  13. ...loop code here...
  14. Next
  15. ```
  16. ...to this...
  17. ```
  18. Local iterator:=container.All()
  19. While Not iterator.AtEnd
  20. Local value:=iterator.Current
  21. ...loop code here...
  22. iterator.Bump()
  23. Wend
  24. ```
  25. Iterators may also provide methods for inserting and removing items...
  26. Containers should not be modified while eachin is being used to loop through the values in the container as this can put the container into an inconsistent state. If you need to do this, you should use these optional iterator methods instead of modifying the container:
  27. `Method Erase:Void()` - Erase the iterator from the container.
  28. `Method Insert:Void( value:ValueType )` - Insert a value before the iterator.
  29. For example:
  30. ```
  31. Local iterator:=container.All()
  32. While Not iterator.AtEnd
  33. Local value:=iterator.Current
  34. Local eraseMe:=false
  35. ...loop code here - may set eraseMe to true to erase current value...
  36. If eraseMe
  37. iterator.Erase()
  38. Else
  39. iterator.Bump()
  40. Endif
  41. Wend
  42. ```
  43. Note that if you erase a value, you should NOT bump the iterator - erase implicitly does this for you.
  44. Finally, IContainer is not a 'real' interface because Monkey2 does not yet support generic interface methods. This feature is planned for a future version of monkey2.
  45. #end
  46. Interface IContainer<T>
  47. 'Property Empty:Bool()
  48. 'Method All:IIterator<T>()
  49. 'Sequence methods
  50. 'Method Add( value:T )
  51. 'Method AddAll( value:T[] )
  52. 'Method AddAll<C>( values:C )
  53. 'Method Contains:Bool( value:T )
  54. 'Method Remove:Bool( value:T )
  55. 'Method RemoveLast:Bool( value:T )
  56. 'Method RemoveEach:Int( value:T )
  57. 'Method RemoveIf:Int( condition:Bool( value:T ) )
  58. End
  59. #rem monkeydoc IIterator interface.
  60. #end
  61. Interface IIterator<T>
  62. 'Property AtEnd:Bool()
  63. 'Property Current:T()
  64. 'Method Bump()
  65. 'Sequence methods
  66. 'Method Erase()
  67. 'Method Insert( value:T )
  68. End