Beispiel #1
0
 def __partially_compact(self):
     '''Try to make the NodeSelection a bit more compact.'''
     # todo: consider defining a canonic decomposition of a node selection to
     # node ranges. This will make a few things easier, like checking
     # equality.
     first, second = None, None
     for (r1, r2) in cute_iter_tools.orderless_combinations(self.ranges, 2):
         if r1.start in r2:
             second, first = r1, r2
             break
         elif r2.start in r1:
             first, second = r1, r2
             break
         else:
             pass
     if first is not None and second is not None:
         if second.end in first:
             pass
         else: # second.end not in first
             for current in second:
                 if current not in first:
                     break
             if current.parent is first.end:
                 self.ranges.remove(first)
                 new_range = NodeRange(start=first.start, end=second.end)
             else:
                 new_range = NodeRange(start=current, end=second.end)
             self.ranges.append(new_range)
           
         self.ranges.remove(second)
         return
     else:
         raise CompletelyCompact
Beispiel #2
0
def all_equal(iterable, exhaustive=False):
    '''
    Return whether all elements in the iterable are equal to each other.
    
    If `exhaustive` is set to False, it's assumed that the equality relation is
    transitive, therefore not every member is tested against every other member.
    So in a list of size n, n-1 equality checks will be made.
    
    If `exhaustive` is set to True, every member will be checked against every
    other member. So in a list of size n, (n*(n-1))/2 equality checks will be
    made.
    '''
    
    if exhaustive is True:
        pairs = cute_iter_tools.orderless_combinations(iterable, 2)        
    else: # exhaustive is False
        pairs = cute_iter_tools.consecutive_pairs(iterable)
        
    return all(a==b for (a, b) in pairs)