jiter.ipp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /*##############################################################################
  2. HPCC SYSTEMS software Copyright (C) 2012 HPCC Systems®.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. ############################################################################## */
  13. #ifndef JITER_IPP
  14. #define JITER_IPP
  15. #include "jlib.hpp"
  16. #include "jiter.hpp"
  17. template<class ELEMENT, class ITER> class CArrayIteratorOf : public CInterfaceOf<ITER>
  18. {
  19. public:
  20. CArrayIteratorOf<ELEMENT,ITER>(const IArray & _values, aindex_t _start = 0, IInterface * _owner=NULL)
  21. : owner(LINK(_owner)), values(_values), start(_start)
  22. {
  23. current = start;
  24. }
  25. ~CArrayIteratorOf<ELEMENT,ITER>()
  26. {
  27. ::Release(owner);
  28. }
  29. virtual bool first()
  30. {
  31. current = start;
  32. return values.isItem(current);
  33. }
  34. virtual bool next()
  35. {
  36. current++;
  37. return values.isItem(current);
  38. }
  39. virtual bool isValid() { return values.isItem(current); }
  40. virtual ELEMENT & query() { return static_cast<ELEMENT &>(values.item(current)); }
  41. virtual ELEMENT & get() { return OLINK(static_cast<ELEMENT &>(values.item(current))); };
  42. protected:
  43. IInterface * owner;
  44. const IArray &values;
  45. aindex_t current;
  46. aindex_t start;
  47. };
  48. class jlib_decl CNullIterator : public CInterfaceOf<IIterator>
  49. {
  50. public:
  51. virtual bool first() { return false; }
  52. virtual bool next() { return false; }
  53. virtual bool isValid() { return false; }
  54. virtual IInterface & query() { IInterface * i = 0; return *i; }
  55. virtual IInterface & get() { IInterface * i = 0; return *i; }
  56. };
  57. template<class X, class Y> class jlib_decl CNullIteratorOf : public CInterfaceOf<Y>
  58. {
  59. public:
  60. virtual bool first() { return false; }
  61. virtual bool next() { return false; }
  62. virtual bool isValid() { return false; }
  63. virtual X & query() { X * i = 0; return *i; }
  64. virtual X & get() { X * i = 0; return *i; }
  65. };
  66. template<class X, class Y> class CCompoundIteratorOf : public CInterfaceOf<X>
  67. {
  68. public:
  69. CCompoundIteratorOf(X * _left, X * _right) : left(_left), right(_right)
  70. {
  71. doneLeft = true;
  72. }
  73. virtual bool first()
  74. {
  75. doneLeft = false;
  76. if (left->first())
  77. return true;
  78. doneLeft = true;
  79. return right->first();
  80. }
  81. virtual bool next()
  82. {
  83. if (!doneLeft)
  84. {
  85. if (left->next())
  86. return true;
  87. doneLeft = true;
  88. return right->first();
  89. }
  90. return right->next();
  91. }
  92. virtual bool isValid() { return doneLeft ? right->isValid() : left->isValid(); }
  93. virtual Y & query() { return doneLeft ? right->query() : left->query();}
  94. protected:
  95. Linked<X> left;
  96. Linked<X> right;
  97. bool doneLeft;
  98. };
  99. #endif