Package glue :: Module __segments :: Class segmentlist
[hide private]
[frames] | no frames]

Class segmentlist

object --+    
         |    
      list --+
             |
            segmentlist

The segmentlist class defines a list of segments, and is an extension of the built-in list class. This class provides addtional methods that assist in the manipulation of lists of segments. In particular, arithmetic operations such as union and intersection are provided. Unlike the segment class, the segmentlist class is closed under all supported arithmetic operations.

All standard Python sequence-like operations are supported, like slicing, iteration and so on, but the arithmetic and other methods in this class generally expect the segmentlist to be in what is refered to as a "coalesced" state --- consisting solely of disjoint segments listed in ascending order. Using the standard Python sequence-like operations, a segmentlist can be easily constructed that is not in this state; for example by simply appending a segment to the end of the list that overlaps some other segment already in the list. The class provides a coalesce() method that can be called to put it in the coalesced state. Following application of the coalesce method, all arithmetic operations will function reliably. All arithmetic methods themselves return coalesced results, so there is never a need to call the coalesce method when manipulating segmentlists exclusively via the arithmetic operators.

Example:

>>> x = segmentlist([segment(-10, 10)])
>>> x |= segmentlist([segment(20, 30)])
>>> x -= segmentlist([segment(-5, 5)])
>>> print(x)
[segment(-10, -5), segment(5, 10), segment(20, 30)]
>>> print(~x)
[segment(-infinity, -10), segment(-5, 5), segment(10, 20), segment(30, infinity)]
Instance Methods [hide private]
 
__abs__(x)
abs(x)
 
__add__(x, y)
x+y
 
__and__(x, y)
x&y
 
__contains__(x, y)
y in x
 
__iadd__(x, y)
x+=y
 
__iand__(x, y)
x&=y
 
__invert__(x)
~x
 
__ior__(x, y)
x|=y
 
__isub__(x, y)
x-=y
 
__or__(x, y)
x|y
 
__radd__(x, y)
y+x
 
__rand__(x, y)
y&x
 
__ror__(x, y)
y|x
 
__rsub__(x, y)
y-x
 
__rxor__(x, y)
y^x
 
__sub__(x, y)
x-y
 
__xor__(x, y)
x^y
 
coalesce(...)
Sort the elements of a list into ascending order, and merge continuous segments into single segments.
 
contract(...)
Execute the .contract() method on each segment in the list and coalesce the result.
 
extent(...)
Return the segment whose end-points denote the maximum and minimum extent of the segmentlist.
 
find(...)
Return the smallest i such that i is the index of an element that wholly contains item.
 
intersects(...)
Returns True if the intersection of self and the segmentlist other is not the null set, otherwise returns False.
 
intersects_segment(...)
Returns True if the intersection of self and the segment other is not the null set, otherwise returns False.
 
protract(...)
Execute the .protract() method on each segment in the list and coalesce the result.
 
shift(...)
Execute the .shift() method on each segment in the list.

Inherited from list: __delitem__, __delslice__, __eq__, __ge__, __getattribute__, __getitem__, __getslice__, __gt__, __imul__, __init__, __iter__, __le__, __len__, __lt__, __mul__, __ne__, __new__, __repr__, __reversed__, __rmul__, __setitem__, __setslice__, __sizeof__, append, count, extend, index, insert, pop, remove, reverse, sort

Inherited from object: __delattr__, __format__, __reduce__, __reduce_ex__, __setattr__, __str__, __subclasshook__

Class Variables [hide private]

Inherited from list: __hash__

Properties [hide private]

Inherited from object: __class__

Method Details [hide private]

__add__(x, y)
(Addition operator)

 

x+y

Overrides: list.__add__

__contains__(x, y)
(In operator)

 

y in x

Overrides: list.__contains__

__iadd__(x, y)

 

x+=y

Overrides: list.__iadd__

coalesce(...)

 

Sort the elements of a list into ascending order, and merge continuous segments into single segments. This operation is O(n log n).

contract(...)

 

Execute the .contract() method on each segment in the list and coalesce the result. Segmentlist is modified in place.

extent(...)

 

Return the segment whose end-points denote the maximum and minimum extent of the segmentlist. Does not require the segmentlist to be coalesced.

find(...)

 

Return the smallest i such that i is the index of an element that wholly contains item. Raises ValueError if no such element exists. Does not require the segmentlist to be coalesced.

intersects(...)

 

Returns True if the intersection of self and the segmentlist other is not the null set, otherwise returns False. The algorithm is O(n), but faster than explicit calculation of the intersection, i.e. by testing bool(self & other). Requires both lists to be coalesced.

intersects_segment(...)

 

Returns True if the intersection of self and the segment other is not the null set, otherwise returns False. The algorithm is O(log n). Requires the list to be coalesced.

protract(...)

 

Execute the .protract() method on each segment in the list and coalesce the result. Segmentlist is modified in place.

shift(...)

 

Execute the .shift() method on each segment in the list. The algorithm is O(n) and does not require the list to be coalesced nor does it coalesce the list. Segmentlist is modified in place.