container.monkey2 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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 Erase()` - Erases the value pointed to by the iterator.
  9. `Method Bump()` - Bumps the iterator so it points to the next value in the container.
  10. ...where 'ValueType' is the type of the values contained in the container.
  11. With these conditions met, a container can be used with eachin loops. Monkey2 will automatically convert code like this:
  12. ```
  13. For Local value:=Eachin container
  14. ...loop code here...
  15. Next
  16. ```
  17. ...to this...
  18. ```
  19. Local iterator:=container.All()
  20. While Not iterator.AtEnd
  21. Local value:=iterator.Current
  22. ...loop code here...
  23. iterator.Bump()
  24. Wend
  25. ```
  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
  27. an inconsistent state. If you need to do this, you should manually iterate through the container and use the iterator 'Erase' method to erase
  28. values in a controlled way. For example:
  29. ```
  30. Local iterator:=container.All()
  31. While Not iterator.AtEnd
  32. Local value:=iterator.Current
  33. Local eraseMe:=false
  34. ...loop code here - may set eraseMe to true to erase current value...
  35. If eraseMe
  36. iterator.Erase()
  37. Else
  38. iterator.Bump()
  39. Endif
  40. Wend
  41. ```
  42. Note that if you erase a value, you should NOT bump the iterator - erase implicitly does this for you.
  43. Finally, IContainer is not a 'real' interface because Monkey2 does not yet support generic interface methods. This feature is planned for a
  44. future version of monkey2.
  45. #end
  46. Interface IContainer<T>
  47. 'Property Empty:Bool()
  48. 'Method All:IIterator<T>()
  49. 'Method Find:IIterator<T>( value:T ) Default...
  50. 'For sequences...
  51. 'Method Add( value:T )
  52. 'Method AddAll( value:T[] ) Default...
  53. 'Method AddAll<C>( values:C ) Where C Implements IContainer<T> Default...
  54. End
  55. #rem monkeydoc IIterator interface.
  56. #end
  57. Interface IIterator<T>
  58. 'Property AtEnd:Bool()
  59. 'Property Current:T()
  60. 'Method Bump()
  61. 'For sequences...
  62. 'Method Erase()
  63. 'Method Insert( value:T )
  64. End