• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#  (C) Copyright David Abrahams 2001. Permission to copy, use, modify, sell and
2#  distribute this software is granted provided this copyright notice appears in
3#  all copies. This software is provided "as is" without express or implied
4#  warranty, and with no claim as to its suitability for any purpose.
5
6from b2.util import is_iterable
7from .utility import to_seq
8
9
10def difference (b, a):
11    """ Returns the elements of B that are not in A.
12    """
13    a = set(a)
14    result = []
15    for item in b:
16        if item not in a:
17            result.append(item)
18    return result
19
20def intersection (set1, set2):
21    """ Removes from set1 any items which don't appear in set2 and returns the result.
22    """
23    assert is_iterable(set1)
24    assert is_iterable(set2)
25    result = []
26    for v in set1:
27        if v in set2:
28            result.append (v)
29    return result
30
31def contains (small, large):
32    """ Returns true iff all elements of 'small' exist in 'large'.
33    """
34    small = to_seq (small)
35    large = to_seq (large)
36
37    for s in small:
38        if not s in large:
39            return False
40    return True
41
42def equal (a, b):
43    """ Returns True iff 'a' contains the same elements as 'b', irrespective of their order.
44        # TODO: Python 2.4 has a proper set class.
45    """
46    assert is_iterable(a)
47    assert is_iterable(b)
48    return contains (a, b) and contains (b, a)
49