xref: /aosp_15_r20/external/federated-compute/fcp/aggregation/core/vector_string_data.h (revision 14675a029014e728ec732f129a32e299b2da0601)
1 /*
2  * Copyright 2023 Google LLC
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 #ifndef FCP_AGGREGATION_CORE_VECTOR_STRING_DATA_H_
17 #define FCP_AGGREGATION_CORE_VECTOR_STRING_DATA_H_
18 
19 #include <cstddef>
20 #include <string>
21 #include <utility>
22 #include <vector>
23 
24 #include "fcp/aggregation/core/datatype.h"
25 #include "fcp/aggregation/core/tensor_data.h"
26 
27 namespace fcp {
28 namespace aggregation {
29 
30 class VectorStringData : public TensorData {
31  public:
VectorStringData(std::vector<std::string> && strings)32   explicit VectorStringData(std::vector<std::string>&& strings)
33       : strings_(std::move(strings)) {
34     string_views_.reserve(strings_.size());
35     for (const std::string& s : strings_) string_views_.emplace_back(s);
36   }
37   ~VectorStringData() override = default;
38 
39   // Implementation of TensorData methods.
byte_size()40   size_t byte_size() const override {
41     return string_views_.size() * sizeof(string_view);
42   }
data()43   const void* data() const override { return string_views_.data(); }
44 
45  private:
46   std::vector<std::string> strings_;
47   std::vector<string_view> string_views_;
48 };
49 
50 }  // namespace aggregation
51 }  // namespace fcp
52 
53 #endif  // FCP_AGGREGATION_CORE_VECTOR_STRING_DATA_H_
54