libStatGen Software  1
NonOverlapRegionPos Class Reference

This class contains a list of non-overlapping regions, just positions, not including chromosomes (see NonOverlapRegions for chromosomes and positions). More...

#include <NonOverlapRegions.h>

List of all members.

Public Member Functions

 NonOverlapRegionPos (const NonOverlapRegionPos &reg)
 Copy constructor, does not copy, but initializes with an empty region list.
void add (int32_t start, int32_t end)
 End position is not included in the region.
bool inRegion (int32_t pos)
 Return whether or not the position was found within a region.

Friends

class NonOverlapRegionsTest

Detailed Description

This class contains a list of non-overlapping regions, just positions, not including chromosomes (see NonOverlapRegions for chromosomes and positions).

When regions are added that overlap, it merges them. After adding regions, you can check to see if a position is found in one of the regions. It is designed to work fastest if you make calls in sequential order.

Definition at line 33 of file NonOverlapRegions.h.


Constructor & Destructor Documentation

NonOverlapRegionPos::NonOverlapRegionPos ( const NonOverlapRegionPos reg)

Copy constructor, does not copy, but initializes with an empty region list.

Definition at line 59 of file NonOverlapRegions.cpp.

    : myRegions()
{
    myRegionIter = myRegions.begin();
    myTmpIter = myRegions.begin();
}

Member Function Documentation

void NonOverlapRegionPos::add ( int32_t  start,
int32_t  end 
)

End position is not included in the region.

If this region overlaps another region(s), they will be merged into one region.

Definition at line 75 of file NonOverlapRegions.cpp.

References inRegion().

{
    // Check to see if the start/end are valid in relation.
    if(start >= end)
    {
        std::cerr << "NonOverlapRegionPos::add: Invalid Range, "
                  << "start must be < end, but " << start << " >= " << end 
                  << std::endl;
        return;
    }

    bool added = false;
    // Locate the correct position in the region list for this start/end.
    if(inRegion(start))
    {
        // Check if the region end needs to be updated.
        if(end > myRegionIter->second)
        {
            myRegionIter->second = end;
        }
        added = true;
    }
    else
    {
        // Check to see if we are at the end.
        if(myRegionIter != myRegions.end())
        {
            // Not at the end.
            // Check to see if the region overlaps the current region.
            if(end >= myRegionIter->first)
            {
                // Overlaps, so update the start.
                myRegionIter->first = start;
                // Check if the end needs to be updated.
                if(myRegionIter->second < end)
                {
                    myRegionIter->second = end;
                }
                added = true;
            }
        }
    }

    // If we already added the record, check to see if the end of the
    // new region overlaps any additional regions (know that myRegionIter is
    // not at the end.
    if(added)
    {
        // Check to see if any other regions were overlapped by this record.
        myTmpIter = myRegionIter;
        ++myTmpIter;
        while(myTmpIter != myRegions.end())
        {
            // If the region starts before the end of this one, consume it.
            if(myTmpIter->first <= end)
            {
                if(myTmpIter->second > myRegionIter->second)
                {
                    // Update this region with the new end.
                    myRegionIter->second = myTmpIter->second;
                }
                
                myTmpIter = myRegions.erase(myTmpIter);
            }
            else
            {
                // This region is not overlapped by the new region, so stop.
                break;
            }
        }
    }
    else
    {
        // Add the region.
        myRegionIter = myRegions.insert(myRegionIter, 
                                         std::make_pair(start, end));
    }
}
bool NonOverlapRegionPos::inRegion ( int32_t  pos)

Return whether or not the position was found within a region.

If it is found within the region, myRegionIter will point to the region otherwise myRegionIter will point to the region after the position or to the end if the position is after the last region.

Definition at line 155 of file NonOverlapRegions.cpp.

Referenced by add().

{
    // Return whether or not the position was found within a region.
    // If it is found within the region, myRegionIter will point to the region
    // otherwise myRegionIter will point to the region after the position 
    // or to the end if the position is after the last region.

    // Determine if it needs to search to the left
    //   a) it is at the end
    //   b) the region starts after the position.
    if(myRegionIter == myRegions.end())
    {
        // If the iterator is at the end, search to the left.
        return(findLeft(pos));
    }
    else if(pos < myRegionIter->first)
    {
        // Not at the end, so search left if the position is less
        // than this region's start.
        return(findLeft(pos));
    }
    else
    {
        return(findRight(pos));
    }
}

The documentation for this class was generated from the following files:
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends