tile_iterator.h 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. // Copyright 2020 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef COMPONENTS_QUERY_TILES_INTERNAL_TILE_ITERATOR_H_
  5. #define COMPONENTS_QUERY_TILES_INTERNAL_TILE_ITERATOR_H_
  6. #include <queue>
  7. #include <utility>
  8. #include <vector>
  9. namespace query_tiles {
  10. struct Tile;
  11. struct TileGroup;
  12. // Breadth first search iterator that can iterate through first few levels or
  13. // everything in the tile tree structure. During iteration, tiles can't be
  14. // changed or deleted within the lifecycle of the iterator.
  15. // Example usage:
  16. // void IterateGroup(const TileGroup& group) {
  17. // TileIterator it(group, TileIterator::kAllTiles);
  18. // while (it->HasNext()) {
  19. // const auto* tile = it->Next();
  20. // // Use tile data.
  21. // }
  22. // }
  23. //
  24. class TileIterator {
  25. public:
  26. // Pass to |levels_| to iterates through all tiles.
  27. static constexpr int kAllTiles = -1;
  28. // Constructs an iterator that iterates first few |levels| of the |tiles|.
  29. // If |levels| is 0, only root tiles in |tiles| will be iterated.
  30. TileIterator(std::vector<const Tile*> tiles, int levels);
  31. // Constructs an iterator for a tile group.
  32. TileIterator(const TileGroup& tile_group, int levels);
  33. // Returns whether there are any remaining elements.
  34. bool HasNext() const;
  35. // Returns the next tile and moves to the next tile. When the iterator is
  36. // empty, return nullptr.
  37. const Tile* Next();
  38. ~TileIterator();
  39. TileIterator(const TileIterator&) = delete;
  40. TileIterator operator=(const TileIterator&) = delete;
  41. private:
  42. using TileLevelPair = std::pair<int, const Tile*>;
  43. using TilesQueue = std::queue<TileLevelPair>;
  44. // Adds an element to |tiles_queue_| if |tile| is valid.
  45. void MaybeAddToQueue(int level, const Tile* tile);
  46. TilesQueue tiles_queue_;
  47. // The number of levels of tiles to iterate on.
  48. const int levels_;
  49. };
  50. } // namespace query_tiles
  51. #endif // COMPONENTS_QUERY_TILES_INTERNAL_TILE_ITERATOR_H_