tile_utils.h 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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_UTILS_H_
  5. #define COMPONENTS_QUERY_TILES_INTERNAL_TILE_UTILS_H_
  6. #include <map>
  7. #include <memory>
  8. #include <vector>
  9. #include "components/query_tiles/tile.h"
  10. namespace query_tiles {
  11. // Helper class to shuffle a vector of tiles beginning at the |start| position.
  12. class TileShuffler {
  13. public:
  14. TileShuffler() = default;
  15. TileShuffler(const TileShuffler& other) = delete;
  16. TileShuffler& operator=(const TileShuffler& other) = delete;
  17. virtual void Shuffle(std::vector<Tile>* tiles, int start) const;
  18. };
  19. // Function to sort a vector of tiles based on their score in |tile_stats|. If
  20. // a tile ID doesn't exists in |tile_stats|, a new entry will be created and
  21. // a score will be calculated. If a tile ID in |tile_stats| doesn't show up in
  22. // |tiles|, it will be removed if the tile isn't clicked recently.
  23. // To calculate scores for new tiles, ordering from the server response will
  24. // be taken into consideration. As the server has already ordered tiles
  25. // according to their importance.
  26. // For example, if the first tile returned by server never appeared before, we
  27. // should set its score to at least the 2nd tile. so that it can show up in
  28. // the first place if no other tiles in the back have a higher score. For
  29. // a new tile at position x, its score should be the minimum of its neighbors
  30. // at position x-1 and x+1. For new tiles showing up at the end, their score
  31. // will be set to 0.
  32. // For example, if the tile scores are (new_tile, 0.5, 0.7), then the adjusted
  33. // score will be (0.5, 0.5, 0.7). Simularly, (0.5, new_tile1, 0.7, new_tile2)
  34. // will result in (0.5, 0.5, 0.7, 0). And for new tiles at the front, they are
  35. // guaranteed a minimum score. So that if all the other tiles haven't been
  36. // clicked for a while, it will have a chance to be placed at the front.
  37. void SortTilesAndClearUnusedStats(std::vector<std::unique_ptr<Tile>>* tiles,
  38. std::map<std::string, TileStats>* tile_stats);
  39. // Calculates the current tile score based on |current_time|. Tile score will
  40. // decay over time.
  41. double CalculateTileScore(const TileStats& tile_stats, base::Time current_time);
  42. // Checks whether a tile ID is for trending tile.
  43. bool IsTrendingTile(const std::string& tile_id);
  44. // Shuffle tiles from position |TileConfig::GetTileShufflePosition()|
  45. // so that low score tiles has a chance to be seen.
  46. void ShuffleTiles(std::vector<Tile>* tiles, const TileShuffler& shuffer);
  47. } // namespace query_tiles
  48. #endif // COMPONENTS_QUERY_TILES_INTERNAL_TILE_UTILS_H_