1#
2# Copyright (C) 2020 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
17import itertools
18
19def DeduplicateKeepOrder(input):
20    '''Remove duplicate items from a sequence while keeping the item order.
21
22    Args:
23        input: a sequence that might have duplicated items.
24
25    Returns:
26        A deduplicated list where item order is kept.
27    '''
28    return MergeUniqueKeepOrder(input)
29
30def MergeUniqueKeepOrder(*lists):
31    '''Merge two list, remove duplicate items, and order.
32
33    Args:
34        lists: any number of lists
35
36    Returns:
37        A merged list where items are unique and original order is kept.
38    '''
39    seen = set()
40    return [
41        x for x in itertools.chain(*lists) if not (x in seen or seen.add(x))
42    ]