OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 the V8 project authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "src/wasm/switch-logic.h" |
| 6 |
| 7 namespace v8 { |
| 8 namespace internal { |
| 9 namespace wasm { |
| 10 |
| 11 CaseNode* CreateBst(ZoneVector<CaseNode*>* nodes, size_t begin, size_t end) { |
| 12 if (end < begin) { |
| 13 return nullptr; |
| 14 } else if (end == begin) { |
| 15 return nodes->at(begin); |
| 16 } else { |
| 17 size_t root_index = (begin + end) / 2; |
| 18 CaseNode* root = nodes->at(root_index); |
| 19 if (root_index != 0) { |
| 20 root->left_ = CreateBst(nodes, begin, root_index - 1); |
| 21 } |
| 22 root->right_ = CreateBst(nodes, root_index + 1, end); |
| 23 return root; |
| 24 } |
| 25 } |
| 26 |
| 27 CaseNode* OrderCases(ZoneVector<int>* cases, Zone* zone) { |
| 28 const int max_distance = 2; |
| 29 const int min_size = 4; |
| 30 std::sort(cases->begin(), cases->end()); |
| 31 ZoneVector<size_t> table_breaks(zone); |
| 32 for (size_t i = 1; i < cases->size(); i++) { |
| 33 if (cases->at(i) - cases->at(i - 1) > max_distance) { |
| 34 table_breaks.push_back(i); |
| 35 } |
| 36 } |
| 37 table_breaks.push_back(cases->size()); |
| 38 ZoneVector<CaseNode*> nodes(zone); |
| 39 size_t curr_pos = 0; |
| 40 for (size_t i = 0; i < table_breaks.size(); i++) { |
| 41 size_t break_pos = table_breaks[i]; |
| 42 if (break_pos - curr_pos >= min_size) { |
| 43 int begin = cases->at(curr_pos); |
| 44 int end = cases->at(break_pos - 1); |
| 45 nodes.push_back(new (zone) CaseNode(begin, end)); |
| 46 curr_pos = break_pos; |
| 47 } else { |
| 48 for (; curr_pos < break_pos; curr_pos++) { |
| 49 nodes.push_back(new (zone) |
| 50 CaseNode(cases->at(curr_pos), cases->at(curr_pos))); |
| 51 } |
| 52 } |
| 53 } |
| 54 return CreateBst(&nodes, 0, nodes.size() - 1); |
| 55 } |
| 56 } // namespace wasm |
| 57 } // namespace internal |
| 58 } // namespace v8 |
OLD | NEW |