Index: platform_tools/android/gyp_gen/vars_dict_lib.py |
diff --git a/platform_tools/android/gyp_gen/vars_dict_lib.py b/platform_tools/android/gyp_gen/vars_dict_lib.py |
new file mode 100644 |
index 0000000000000000000000000000000000000000..31731244498d5977eefadd5197f90c23a8fdecb8 |
--- /dev/null |
+++ b/platform_tools/android/gyp_gen/vars_dict_lib.py |
@@ -0,0 +1,105 @@ |
+#!/usr/bin/python |
+ |
+# Copyright 2014 Google Inc. |
+# |
+# Use of this source code is governed by a BSD-style license that can be |
+# found in the LICENSE file. |
+ |
+import collections |
+ |
+class OrderedSet(object): |
+ """ |
+ Ordered set of unique items that supports addition and removal. |
+ """ |
+ |
+ def __init__(self): |
+ self.__li = [] |
+ |
+ def add(self, item): |
+ """ |
+ Add item, if it is not already in the set. |
+ @param item The item to add. |
+ """ |
+ if item not in self.__li: |
+ self.__li.append(item) |
+ |
+ def __contains__(self, item): |
+ """ |
+ Whether the set contains item. |
+ @param item The item to search for in the set. |
+ @return bool Whether the item is in the set. |
+ """ |
+ return item in self.__li |
+ |
+ def __iter__(self): |
+ """ |
+ Iterator for the set. |
+ """ |
+ return self.__li.__iter__() |
+ |
+ def remove(self, item): |
+ """ |
+ Remove item from the set. |
+ @param item Item to be removed. |
+ """ |
+ return self.__li.remove(item) |
+ |
+ def __len__(self): |
+ """ |
+ Number of items in the set. |
+ """ |
+ return len(self.__li) |
+ |
+VAR_NAMES = ['LOCAL_CFLAGS', |
+ 'LOCAL_CPPFLAGS', |
+ 'LOCAL_SRC_FILES', |
+ 'LOCAL_SHARED_LIBRARIES', |
+ 'LOCAL_STATIC_LIBRARIES', |
+ 'LOCAL_C_INCLUDES', |
+ 'LOCAL_EXPORT_C_INCLUDE_DIRS', |
+ 'KNOWN_TARGETS'] |
+ |
+class VarsDict(collections.namedtuple('VarsDict', VAR_NAMES)): |
+ """ |
+ Custom class for storing the arguments to Android.mk variables. |
+ """ |
+ |
+ __slots__ = () |
+ |
+ def __new__(_cls): |
+ lists = [] |
+ for i in range(len(VAR_NAMES)): |
+ lists.append(OrderedSet()) |
+ return tuple.__new__(_cls, lists) |
+ |
+ |
+def intersect(var_dict_list): |
+ """ |
+ Find the intersection of a list of VarsDicts and trim each input to its |
+ unique entries. |
+ @param var_dict_list list of VarsDicts. |
+ @return VarsDict containing list entries common to all VarsDicts in |
+ var_dict_list |
+ """ |
+ intersection = VarsDict() |
+ # First VarsDict |
+ var_dictA = var_dict_list[0] |
+ # The rest. |
+ other_var_dicts = var_dict_list[1:] |
+ |
+ 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
|
+ # Copy A's list, so we can continue iterating after modifying the original. |
+ aList = list(var_dictA[i]) |
+ for item in aList: |
+ # If item is in all lists, add to intersection, and remove from all. |
+ in_all_lists = True |
+ for var_dict in other_var_dicts: |
+ if not item in var_dict[i]: |
+ in_all_lists = False |
+ break |
+ if in_all_lists: |
+ intersection[i].add(item) |
+ for var_dict in var_dict_list: |
+ var_dict[i].remove(item) |
+ return intersection |
+ |