1/* 2 * Copyright (C) 2014 The Android Open Source Project 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 17#include <utils/KeyedVector.h> 18#include <utils/SortedVector.h> 19#include <utils/Vector.h> 20 21#include "Grouper.h" 22#include "Rule.h" 23#include "RuleGenerator.h" 24#include "SplitSelector.h" 25 26namespace split { 27 28using namespace android; 29 30SplitSelector::SplitSelector() { 31} 32 33SplitSelector::SplitSelector(const Vector<SplitDescription>& splits) 34 : mGroups(groupByMutualExclusivity(splits)) { 35} 36 37static void selectBestFromGroup(const SortedVector<SplitDescription>& splits, 38 const SplitDescription& target, Vector<SplitDescription>& splitsOut) { 39 SplitDescription bestSplit; 40 bool isSet = false; 41 const size_t splitCount = splits.size(); 42 for (size_t j = 0; j < splitCount; j++) { 43 const SplitDescription& thisSplit = splits[j]; 44 if (!thisSplit.match(target)) { 45 continue; 46 } 47 48 if (!isSet || thisSplit.isBetterThan(bestSplit, target)) { 49 isSet = true; 50 bestSplit = thisSplit; 51 } 52 } 53 54 if (isSet) { 55 splitsOut.add(bestSplit); 56 } 57} 58 59Vector<SplitDescription> SplitSelector::getBestSplits(const SplitDescription& target) const { 60 Vector<SplitDescription> bestSplits; 61 const size_t groupCount = mGroups.size(); 62 for (size_t i = 0; i < groupCount; i++) { 63 selectBestFromGroup(mGroups[i], target, bestSplits); 64 } 65 return bestSplits; 66} 67 68KeyedVector<SplitDescription, sp<Rule> > SplitSelector::getRules() const { 69 KeyedVector<SplitDescription, sp<Rule> > rules; 70 71 const size_t groupCount = mGroups.size(); 72 for (size_t i = 0; i < groupCount; i++) { 73 const SortedVector<SplitDescription>& splits = mGroups[i]; 74 const size_t splitCount = splits.size(); 75 for (size_t j = 0; j < splitCount; j++) { 76 sp<Rule> rule = Rule::simplify(RuleGenerator::generate(splits, j)); 77 if (rule != NULL) { 78 rules.add(splits[j], rule); 79 } 80 } 81 } 82 return rules; 83} 84 85} // namespace split 86