1/* Copyright 2020 The TensorFlow Authors. All Rights Reserved.
2
3Licensed under the Apache License, Version 2.0 (the "License");
4you may not use this file except in compliance with the License.
5You may obtain a copy of the License at
6
7 http://www.apache.org/licenses/LICENSE-2.0
8
9Unless required by applicable law or agreed to in writing, software
10distributed under the License is distributed on an "AS IS" BASIS,
11WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12See the License for the specific language governing permissions and
13limitations under the License.
14==============================================================================*/
15
16#ifndef TENSORFLOW_CORE_UTIL_RAGGED_TO_DENSE_UTIL_COMMON_H_
17#define TENSORFLOW_CORE_UTIL_RAGGED_TO_DENSE_UTIL_COMMON_H_
18
19#include <string>
20#include <unordered_map>
21#include <vector>
22
23namespace tensorflow {
24enum class RowPartitionType {
25 FIRST_DIM_SIZE,
26 VALUE_ROWIDS,
27 ROW_LENGTHS,
28 ROW_SPLITS,
29 ROW_LIMITS,
30 ROW_STARTS
31};
32
33inline std::string RowPartitionTypeToString(
34 RowPartitionType row_partition_type) {
35 switch (row_partition_type) {
36 case RowPartitionType::FIRST_DIM_SIZE:
37 return "FIRST_DIM_SIZE";
38 case RowPartitionType::VALUE_ROWIDS:
39 return "VALUE_ROWIDS";
40 case RowPartitionType::ROW_LENGTHS:
41 return "ROW_LENGTHS";
42 case RowPartitionType::ROW_SPLITS:
43 return "ROW_SPLITS";
44 case RowPartitionType::ROW_LIMITS:
45 return "ROW_LIMITS";
46 case RowPartitionType::ROW_STARTS:
47 return "ROW_STARTS";
48 default:
49 return "UNKNOWN ROW PARTITION TYPE";
50 }
51}
52
53inline std::vector<RowPartitionType> GetRowPartitionTypesHelper(
54 const std::vector<std::string>& row_partition_type_strings) {
55 static const auto kStringToType =
56 new std::unordered_map<std::string, RowPartitionType>(
57 {{"FIRST_DIM_SIZE", RowPartitionType::FIRST_DIM_SIZE},
58 {"VALUE_ROWIDS", RowPartitionType::VALUE_ROWIDS},
59 {"ROW_LENGTHS", RowPartitionType::ROW_LENGTHS},
60 {"ROW_SPLITS", RowPartitionType::ROW_SPLITS},
61 {"ROW_LIMITS", RowPartitionType::ROW_LIMITS},
62 {"ROW_STARTS", RowPartitionType::ROW_STARTS}});
63 std::vector<RowPartitionType> result;
64 for (const auto& type_str : row_partition_type_strings) {
65 const auto iter = kStringToType->find(type_str);
66 if (iter == kStringToType->end()) {
67 break;
68 }
69 result.push_back(iter->second);
70 }
71 return result;
72}
73
74inline int GetRaggedRank(
75 const std::vector<RowPartitionType>& row_partition_types) {
76 if (row_partition_types.empty()) {
77 return 0;
78 }
79 if (row_partition_types[0] == RowPartitionType::FIRST_DIM_SIZE) {
80 return row_partition_types.size() - 1;
81 }
82 return row_partition_types.size();
83}
84
85} // namespace tensorflow
86
87#endif // TENSORFLOW_CORE_UTIL_RAGGED_TO_DENSE_UTIL_COMMON_H_
88