diff options
author | Brian Harring <ferringb@gentoo.org> | 2005-12-27 06:09:24 +0000 |
---|---|---|
committer | Brian Harring <ferringb@gentoo.org> | 2005-12-27 06:09:24 +0000 |
commit | ba00a87bb0865ae9c21947822adaef74a7cbf723 (patch) | |
tree | 99c7a7664e89b62fdb6bd274846f57e5082caf32 | |
parent | 239ba964b23836c428ef8688b74c184eba810a4e (diff) | |
download | portage-ba00a87bb0865ae9c21947822adaef74a7cbf723.tar.gz portage-ba00a87bb0865ae9c21947822adaef74a7cbf723.tar.bz2 portage-ba00a87bb0865ae9c21947822adaef74a7cbf723.zip |
linear unique_array lifted from saviour, lifted from a python cookbook.
svn path=/main/trunk/; revision=2488
-rw-r--r-- | pym/portage_util.py | 43 |
1 files changed, 34 insertions, 9 deletions
diff --git a/pym/portage_util.py b/pym/portage_util.py index 2441509d9..f45f46b60 100644 --- a/pym/portage_util.py +++ b/pym/portage_util.py @@ -392,15 +392,6 @@ def pickle_read(filename,default=None,debug=0): data = default return data -def unique_array(array): - """Takes an array and makes sure each element is unique.""" - mya = [] - for x in array: - if x not in mya: - mya.append(x) - return mya - - def dump_traceback(msg, noiselevel=1): import sys, traceback info = sys.exc_info() @@ -417,3 +408,37 @@ def dump_traceback(msg, noiselevel=1): if error: writemsg(error+"\n", noiselevel=noiselevel) writemsg("====================================\n\n", noiselevel=noiselevel) + +def unique_array(s): + """lifted from python cookbook, credit: Tim Peters + Return a list of the elements in s in arbitrary order, sans duplicates""" + n = len(s) + # assume all elements are hashable, if so, it's linear + try: + return list(set(s)) + except TypeError: + pass + + # so much for linear. abuse sort. + try: + t = sorted(s) + except TypeError: + pass + else: + assert n > 0 + last = t[0] + lasti = i = 1 + while i < n: + if t[i] != last: + t[lasti] = last = t[i] + lasti += 1 + i += 1 + return t[:lasti] + + # blah. back to original portage.unique_array + u = [] + for x in s: + if x not in u: + u.append(x) + return u + |