File: autocomplete_grouper_groups.h

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (82 lines) | stat: -rw-r--r-- 3,161 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
// Copyright 2022 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef COMPONENTS_OMNIBOX_BROWSER_AUTOCOMPLETE_GROUPER_GROUPS_H_
#define COMPONENTS_OMNIBOX_BROWSER_AUTOCOMPLETE_GROUPER_GROUPS_H_

#include <map>
#include <memory>

#include "base/memory/raw_ptr.h"
#include "components/omnibox/browser/autocomplete_match.h"
#include "third_party/omnibox_proto/groups.pb.h"

using PMatches = std::vector<raw_ptr<AutocompleteMatch, VectorExperimental>>;

// `Group` class and subclasses used to compose `Section`s.

// Group containing matches with the given `GroupId`s, limited per `GroupId` and
// the overall `limit`.
// E.g., this can describe a group that can have up to 3 search matches, 4
// document matches, and 5 matches total.
class Group {
 public:
  struct LimitAndCount {
    size_t limit;
    size_t count;
  };
  using GroupIdLimitsAndCounts = std::map<omnibox::GroupId, LimitAndCount>;

  // Explicit to prevent uniform initialization (with curly braces {}) from
  // hiding the constructor call.
  explicit Group(size_t limit,
                 std::map<omnibox::GroupId, size_t> group_id_limits,
                 bool is_zps = true,
                 bool is_default = false);
  Group(const Group& group);
  Group& operator=(const Group& group);
  virtual ~Group();

  // Returns if `match` can be added to this `Group`. Checks if the `GroupId` of
  // the match is permitted in this `Group`, this `Group`'s total limit, and the
  // limit for the `GroupId` of the match.
  virtual bool CanAdd(const AutocompleteMatch& match) const;
  // Adds `match` to this `Group` and increments this `Group`'s total count and
  // the count for the `GroupId` of the match. `CanAdd()` should be verified by
  // the caller.
  void Add(const AutocompleteMatch& match);
  // Called before matches are read from this `Group`, this method performs
  // Additional grouping operation to ensure matches, which may not have been
  // grouped by a specific criterion due to being sorted by relevance, are
  // properly grouped together when read from this `Group`.
  void GroupMatches();

  size_t limit() const { return limit_; }
  void set_limit(size_t limit) { limit_ = limit; }
  const GroupIdLimitsAndCounts& group_id_limits_and_counts() const {
    return group_id_limits_and_counts_;
  }
  const PMatches& matches() const { return matches_; }

 private:
  // Performs semantic grouping by Search vs URL.
  void GroupMatchesBySearchVsUrl();
  // Performs semantic grouping by GroupId.
  void GroupMatchesByGroupId();

  // Max number of matches this `Group` can contain.
  size_t limit_{0};
  // The number of matches this `Group` contains.
  size_t count_{0};
  // The limit and count per `GroupId`.
  GroupIdLimitsAndCounts group_id_limits_and_counts_;
  // The matches this `Group` contains.
  PMatches matches_;
  // Whether this `Group` contains zero-prefix suggestions only.
  bool is_zps_{false};
  // Whether this `Group` contains `allowed_to_be_default_match` matches only.
  bool is_default_{false};
};

#endif  // COMPONENTS_OMNIBOX_BROWSER_AUTOCOMPLETE_GROUPER_GROUPS_H_