OLD | NEW |
---|---|
(Empty) | |
1 #!/usr/bin/python | |
2 | |
3 # Copyright 2014 Google Inc. | |
4 # | |
5 # Use of this source code is governed by a BSD-style license that can be | |
6 # found in the LICENSE file. | |
7 | |
8 import collections | |
9 | |
10 class OrderedSet(object): | |
11 """ | |
12 Ordered set of unique items that supports addition and removal. | |
13 """ | |
14 | |
15 def __init__(self): | |
16 self.__li = [] | |
17 | |
18 def add(self, item): | |
19 """ | |
20 Add item, if it is not already in the set. | |
21 @param item The item to add. | |
22 """ | |
23 if item not in self.__li: | |
24 self.__li.append(item) | |
25 | |
26 def __contains__(self, item): | |
27 """ | |
28 Whether the set contains item. | |
29 @param item The item to search for in the set. | |
30 @return bool Whether the item is in the set. | |
31 """ | |
32 return item in self.__li | |
33 | |
34 def __iter__(self): | |
35 """ | |
36 Iterator for the set. | |
37 """ | |
38 return self.__li.__iter__() | |
39 | |
40 def remove(self, item): | |
41 """ | |
42 Remove item from the set. | |
43 @param item Item to be removed. | |
44 """ | |
45 return self.__li.remove(item) | |
46 | |
47 def __len__(self): | |
48 """ | |
49 Number of items in the set. | |
50 """ | |
51 return len(self.__li) | |
52 | |
53 VAR_NAMES = ['LOCAL_CFLAGS', | |
54 'LOCAL_CPPFLAGS', | |
55 'LOCAL_SRC_FILES', | |
56 'LOCAL_SHARED_LIBRARIES', | |
57 'LOCAL_STATIC_LIBRARIES', | |
58 'LOCAL_C_INCLUDES', | |
59 'LOCAL_EXPORT_C_INCLUDE_DIRS', | |
60 'KNOWN_TARGETS'] | |
61 | |
62 class VarsDict(collections.namedtuple('VarsDict', VAR_NAMES)): | |
63 """ | |
64 Custom class for storing the arguments to Android.mk variables. | |
65 """ | |
66 | |
67 __slots__ = () | |
68 | |
69 def __new__(_cls): | |
70 lists = [] | |
71 for i in range(len(VAR_NAMES)): | |
72 lists.append(OrderedSet()) | |
73 return tuple.__new__(_cls, lists) | |
74 | |
75 | |
76 def intersect(var_dict_list): | |
77 """ | |
78 Find the intersection of a list of VarsDicts and trim each input to its | |
79 unique entries. | |
80 @param var_dict_list list of VarsDicts. | |
81 @return VarsDict containing list entries common to all VarsDicts in | |
82 var_dict_list | |
83 """ | |
84 intersection = VarsDict() | |
85 # First VarsDict | |
86 var_dictA = var_dict_list[0] | |
87 # The rest. | |
88 other_var_dicts = var_dict_list[1:] | |
89 | |
90 for i in range(len(var_dictA)): | |
scroggo
2014/01/31 22:16:24
As in write_local_vars, I essentially want a way t
scroggo
2014/02/01 16:24:01
Just figured this out. I'll override __getitem__ t
| |
91 # Copy A's list, so we can continue iterating after modifying the original. | |
92 aList = list(var_dictA[i]) | |
93 for item in aList: | |
94 # If item is in all lists, add to intersection, and remove from all. | |
95 in_all_lists = True | |
96 for var_dict in other_var_dicts: | |
97 if not item in var_dict[i]: | |
98 in_all_lists = False | |
99 break | |
100 if in_all_lists: | |
101 intersection[i].add(item) | |
102 for var_dict in var_dict_list: | |
103 var_dict[i].remove(item) | |
104 return intersection | |
105 | |
OLD | NEW |