summaryrefslogtreecommitdiff
path: root/tools/build/v2/util/sequence.py
blob: 1d32efd2e3712d499e1529eee22d744968383cee (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#  (C) Copyright David Abrahams 2002. Permission to copy, use, modify, sell and
#  distribute this software is granted provided this copyright notice appears in
#  all copies. This software is provided "as is" without express or implied
#  warranty, and with no claim as to its suitability for any purpose.

import operator

def unique (values, stable=False):
    if stable:
        s = set()
        r = []
        for v in values:
            if not v in s:
                r.append(v)
                s.add(v)
        return r
    else:
        return list(set(values))

def max_element (elements, ordered = None):
    """ Returns the maximum number in 'elements'. Uses 'ordered' for comparisons,
        or '<' is none is provided.
    """
    if not ordered: ordered = operator.lt

    max = elements [0]
    for e in elements [1:]:
        if ordered (max, e):
            max = e

    return max

def select_highest_ranked (elements, ranks):
    """ Returns all of 'elements' for which corresponding element in parallel
        list 'rank' is equal to the maximum value in 'rank'.
    """
    if not elements:
        return []

    max_rank = max_element (ranks)

    result = []
    while elements:
        if ranks [0] == max_rank:
            result.append (elements [0])

        elements = elements [1:]
        ranks = ranks [1:]

    return result