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>

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. More...
 
bool inRegion (int32_t pos)
 Return whether or not the position was found within a region. More...
 

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.

Member Function Documentation

◆ add()

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.

76 {
77  // Check to see if the start/end are valid in relation.
78  if(start >= end)
79  {
80  std::cerr << "NonOverlapRegionPos::add: Invalid Range, "
81  << "start must be < end, but " << start << " >= " << end
82  << std::endl;
83  return;
84  }
85 
86  bool added = false;
87  // Locate the correct position in the region list for this start/end.
88  if(inRegion(start))
89  {
90  // Check if the region end needs to be updated.
91  if(end > myRegionIter->second)
92  {
93  myRegionIter->second = end;
94  }
95  added = true;
96  }
97  else
98  {
99  // Check to see if we are at the end.
100  if(myRegionIter != myRegions.end())
101  {
102  // Not at the end.
103  // Check to see if the region overlaps the current region.
104  if(end >= myRegionIter->first)
105  {
106  // Overlaps, so update the start.
107  myRegionIter->first = start;
108  // Check if the end needs to be updated.
109  if(myRegionIter->second < end)
110  {
111  myRegionIter->second = end;
112  }
113  added = true;
114  }
115  }
116  }
117 
118  // If we already added the record, check to see if the end of the
119  // new region overlaps any additional regions (know that myRegionIter is
120  // not at the end.
121  if(added)
122  {
123  // Check to see if any other regions were overlapped by this record.
124  myTmpIter = myRegionIter;
125  ++myTmpIter;
126  while(myTmpIter != myRegions.end())
127  {
128  // If the region starts before the end of this one, consume it.
129  if(myTmpIter->first <= end)
130  {
131  if(myTmpIter->second > myRegionIter->second)
132  {
133  // Update this region with the new end.
134  myRegionIter->second = myTmpIter->second;
135  }
136 
137  myTmpIter = myRegions.erase(myTmpIter);
138  }
139  else
140  {
141  // This region is not overlapped by the new region, so stop.
142  break;
143  }
144  }
145  }
146  else
147  {
148  // Add the region.
149  myRegionIter = myRegions.insert(myRegionIter,
150  std::make_pair(start, end));
151  }
152 }

References inRegion().

◆ inRegion()

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.

156 {
157  // Return whether or not the position was found within a region.
158  // If it is found within the region, myRegionIter will point to the region
159  // otherwise myRegionIter will point to the region after the position
160  // or to the end if the position is after the last region.
161 
162  // Determine if it needs to search to the left
163  // a) it is at the end
164  // b) the region starts after the position.
165  if(myRegionIter == myRegions.end())
166  {
167  // If the iterator is at the end, search to the left.
168  return(findLeft(pos));
169  }
170  else if(pos < myRegionIter->first)
171  {
172  // Not at the end, so search left if the position is less
173  // than this region's start.
174  return(findLeft(pos));
175  }
176  else
177  {
178  return(findRight(pos));
179  }
180 }

Referenced by add().


The documentation for this class was generated from the following files:
NonOverlapRegionPos::inRegion
bool inRegion(int32_t pos)
Return whether or not the position was found within a region.
Definition: NonOverlapRegions.cpp:155