blob: 15edf89bd60f63d2e05d5f8eb10476f714222b72 [file] [log] [blame]
Adam Lesinski40e8eef2014-09-16 14:43:29 -07001/*
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 "Grouper.h"
18
19#include "SplitDescription.h"
20
21#include <utils/KeyedVector.h>
22#include <utils/Vector.h>
23
24using namespace android;
25
26namespace split {
27
28template <typename Key, typename Value>
29static void addToVector(KeyedVector<Key, SortedVector<Value> >& group,
30 const Key& key, const Value& value) {
31 ssize_t idx = group.indexOfKey(key);
32 if (idx < 0) {
33 idx = group.add(key, SortedVector<Value>());
34 }
35 group.editValueAt(idx).add(value);
36}
37
38Vector<SortedVector<SplitDescription> >
39groupByMutualExclusivity(const Vector<SplitDescription>& splits) {
40 Vector<SortedVector<SplitDescription> > groups;
41
42 // Find mutually exclusive splits and group them.
43 KeyedVector<SplitDescription, SortedVector<SplitDescription> > densityGroups;
44 KeyedVector<SplitDescription, SortedVector<SplitDescription> > abiGroups;
45 KeyedVector<SplitDescription, SortedVector<SplitDescription> > localeGroups;
46 for (const SplitDescription& split : splits) {
47 if (split.config.density != 0) {
48 SplitDescription key(split);
49 key.config.density = 0;
50 key.config.sdkVersion = 0; // Ignore density so we can support anydpi.
51 addToVector(densityGroups, key, split);
52 } else if (split.abi != abi::Variant::none) {
53 SplitDescription key(split);
54 key.abi = abi::Variant::none;
55 addToVector(abiGroups, key, split);
56 } else if (split.config.locale != 0) {
57 SplitDescription key(split);
58 key.config.clearLocale();
59 addToVector(localeGroups, key, split);
60 } else {
61 groups.add();
62 groups.editTop().add(split);
63 }
64 }
65
66 const size_t densityCount = densityGroups.size();
67 for (size_t i = 0; i < densityCount; i++) {
68 groups.add(densityGroups[i]);
69 }
70
71 const size_t abiCount = abiGroups.size();
72 for (size_t i = 0; i < abiCount; i++) {
73 groups.add(abiGroups[i]);
74 }
75
76 const size_t localeCount = localeGroups.size();
77 for (size_t i = 0; i < localeCount; i++) {
78 groups.add(localeGroups[i]);
79 }
80 return groups;
81}
82
83} // namespace split