or_iterator.h 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /*
  2. * Tencent is pleased to support the open source community by making wwsearch
  3. * available.
  4. *
  5. * Copyright (C) 2018-present Tencent. All Rights Reserved.
  6. *
  7. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  8. * use this file except in compliance with the License. You may obtain a copy of
  9. * the License at
  10. *
  11. * https://opensource.org/licenses/Apache-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  15. * WARRANTIES OF ANY KIND, either express or implied. See the License for the
  16. * specific language governing permissions and limitations under the License.
  17. */
  18. #pragma once
  19. #include "doc_iterator.h"
  20. namespace wwsearch {
  21. /* Notice : Get union set from vector<DocIdSetIterator*>
  22. */
  23. class OrIterator : public DocIdSetIterator {
  24. private:
  25. std::vector<DocIdSetIterator*> sub_iterator_;
  26. public:
  27. OrIterator() {}
  28. virtual ~OrIterator() {}
  29. virtual DocumentID DocID() override;
  30. virtual DocumentID NextDoc() override;
  31. virtual DocumentID Advance(DocumentID target) override;
  32. virtual CostType Cost() override;
  33. virtual int FieldId() override {
  34. if (this->sub_iterator_.empty()) return -1;
  35. return this->sub_iterator_.front()->FieldId();
  36. };
  37. void AddSubIterator(DocIdSetIterator* iterator) {
  38. this->sub_iterator_.push_back(iterator);
  39. }
  40. inline void FinishAddIterator() {
  41. if (!this->sub_iterator_.empty()) {
  42. std::make_heap(sub_iterator_.begin(), sub_iterator_.end(),
  43. OrIterator::IteratorGreater);
  44. }
  45. }
  46. // greater is first.
  47. static inline bool IteratorGreater(DocIdSetIterator* left,
  48. DocIdSetIterator* right) {
  49. return left->DocID() < right->DocID();
  50. }
  51. private:
  52. DocumentID InnderNextDoc(bool use_advance = false,
  53. DocumentID target = NO_MORE_DOCS);
  54. };
  55. } // namespace wwsearch