Enumerators.txt 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. ==Enumerators==
  2. -------------------------------------------------------------------------------
  3. - Description
  4. Enumerators provide generic support to enumerating a data structure. They imp
  5. lement the same concept of stl iterators.
  6. - How they work
  7. Once one gets an enumerator from a structure implementing IEnumerable (for ex
  8. ample), the object obtained represents tipically a reference to an unexisting
  9. element placed before the first one.
  10. Ex:
  11. IEnumerator e = list.getBegin();
  12. // Visual representation of e:
  13. // | |a|b|c| |
  14. // ^
  15. By using the next() method the iterator moves forward, and returns true if it
  16. points to a valid element, and false if it has reached the end.
  17. Ex:
  18. while (e.next())
  19. {
  20. //...process e.current()...
  21. }
  22. Once e.next() returns false, the representation of e is the following:
  23. Ex:
  24. // Visual representation of e when at the end:
  25. // | |a|b|c| |
  26. // ^
  27. - Classes
  28. . Enumerator interfaces.
  29. - IEnumerator<T>: Describes the interface that must be implemented in an en
  30. umerator that exposes forward and/or backward capabilities. If one of bot
  31. h are not implemented, they must always act as if the iteration is comple
  32. ted (by returning false in the respective method)
  33. - EnumeratorHelper<T>: A wrapper around other enumerators that automaticall
  34. y deletes them when it goes out of scope. Must be instantiated as value,
  35. that is, in the stack and not in the heap. (It's actually more like an ad
  36. vice than a restrictive need, because it's useless if it must be deleted
  37. manually).
  38. Ex:
  39. /* Normal usage */
  40. IEnumerator<T>* e = list.getBegin();
  41. //...use e...
  42. delete e;
  43. /* EnumeratorHelper<T> usage */
  44. EnumeratorHelper<T> e = list.getBegin();
  45. //...use e...
  46. //no need to delete it.
  47. As Type complexity may vary, it's better to use the Enumerator type defin
  48. ition that each data structure should contain, to declare the enumerator
  49. helper.
  50. Ex:
  51. List<int>::Enumerator e = list.getBegin();