• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright 2020 The TensorFlow Authors. All Rights Reserved.
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 
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 
23 namespace tensorflow {
24 enum class RowPartitionType {
25   FIRST_DIM_SIZE,
26   VALUE_ROWIDS,
27   ROW_LENGTHS,
28   ROW_SPLITS,
29   ROW_LIMITS,
30   ROW_STARTS
31 };
32 
RowPartitionTypeToString(RowPartitionType row_partition_type)33 inline 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 
GetRowPartitionTypesHelper(const std::vector<std::string> & row_partition_type_strings)53 inline 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 
GetRaggedRank(const std::vector<RowPartitionType> & row_partition_types)74 inline 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