libStatGen Software
1
|
00001 /* 00002 * Copyright (C) 2011 Regents of the University of Michigan 00003 * 00004 * This program is free software: you can redistribute it and/or modify 00005 * it under the terms of the GNU General Public License as published by 00006 * the Free Software Foundation, either version 3 of the License, or 00007 * (at your option) any later version. 00008 * 00009 * This program is distributed in the hope that it will be useful, 00010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00012 * GNU General Public License for more details. 00013 * 00014 * You should have received a copy of the GNU General Public License 00015 * along with this program. If not, see <http://www.gnu.org/licenses/>. 00016 */ 00017 00018 #include "PosList.h" 00019 #include <stdexcept> 00020 00021 PosList::PosList() 00022 : myNumRefs(24), 00023 myNumPos(100) 00024 { 00025 initVars(); 00026 } 00027 00028 00029 PosList::PosList(int numRefs, int numPositions) 00030 : myNumRefs(numRefs), 00031 myNumPos(numPositions) 00032 { 00033 initVars(); 00034 } 00035 00036 PosList::~PosList() 00037 { 00038 myPosList.clear(); 00039 } 00040 00041 00042 void PosList::addPosition(int refID, int refPosition) 00043 { 00044 // Check for negative numbers, if so, just return. 00045 if((refID < 0) || (refPosition < 0)) 00046 { 00047 return; 00048 } 00049 00050 // If the position list is smaller or equal to refID, it cannot handle an index, 00051 // so increase the size. 00052 if(myPosList.size() <= (unsigned int)refID) 00053 { 00054 // The position list does not currently have space for this reference id, 00055 // so add it. 00056 myPosList.resize(refID+1, std::vector<bool>(myNumPos, false)); 00057 myNumRefs = refID + 1; 00058 } 00059 00060 // The matrix is now sized for this reference id. 00061 // Check to see if this id holds this position. 00062 if((myPosList[refID]).size() <= (unsigned int)refPosition) 00063 { 00064 // The index for this position has not yet been created, 00065 // so increase the size for it. 00066 if(myNumPos <= refPosition) 00067 { 00068 // Our number of positions is smaller than 00069 // the current reference id, so reset 00070 // myNumPos for future use to be this position +1. 00071 myNumPos = refPosition + 1; 00072 } 00073 // Increase the size for this reference id to hold at least myNumPos. 00074 (myPosList[refID]).resize(myNumPos, false); 00075 } 00076 00077 // It now holds this position, so set it to true. 00078 myPosList[refID][refPosition] = true; 00079 } 00080 00081 bool PosList::hasPosition(int refID, int refPosition) 00082 { 00083 // Check for negative numbers, if so, just return false, not found. 00084 if((refID < 0) || (refPosition < 0)) 00085 { 00086 return(false); 00087 } 00088 bool found = false; 00089 try 00090 { 00091 if((myPosList.at(refID)).at(refPosition)) 00092 { 00093 found = true; 00094 } 00095 } 00096 catch (std::out_of_range& oor) 00097 { 00098 // Nothing to do here, if it was out of range, then 00099 // the position was not found (already set to false). 00100 } 00101 return(found); 00102 } 00103 00104 00105 void PosList::initVars() 00106 { 00107 myPosList.clear(); 00108 myPosList.resize(myNumRefs, std::vector<bool>(myNumPos, false)); 00109 }