1 // Copyright (C) 2019 Google LLC
2 //
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 //
7 //      http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include "icing/result/projection-tree.h"
16 
17 #include <algorithm>
18 
19 #include "icing/absl_ports/str_join.h"
20 #include "icing/schema/section-manager.h"
21 
22 namespace icing {
23 namespace lib {
24 
ProjectionTree(const TypePropertyMask & type_field_mask)25 ProjectionTree::ProjectionTree(const TypePropertyMask& type_field_mask) {
26   for (const std::string& field_mask : type_field_mask.paths()) {
27     Node* current_node = &root_;
28     for (std::string_view sub_field_mask :
29          absl_ports::StrSplit(field_mask, kPropertySeparator)) {
30       current_node = AddChildNode(sub_field_mask, &current_node->children);
31     }
32   }
33 }
34 
AddChildNode(std::string_view property_name,std::vector<Node> * current_children)35 ProjectionTree::Node* ProjectionTree::AddChildNode(
36     std::string_view property_name, std::vector<Node>* current_children) {
37   auto itr = std::find_if(current_children->begin(), current_children->end(),
38                           [&property_name](const Node& node) {
39                             return node.name == property_name;
40                           });
41   if (itr != current_children->end()) {
42     return &(*itr);
43   }
44   current_children->push_back(ProjectionTree::Node(property_name));
45   return &current_children->back();
46 }
47 
48 }  // namespace lib
49 }  // namespace icing
50