libStatGen Software 1
PosList.cpp
1/*
2 * Copyright (C) 2011 Regents of the University of Michigan
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18#include "PosList.h"
19#include <stdexcept>
20
22 : myNumRefs(24),
23 myNumPos(100)
24{
25 initVars();
26}
27
28
29PosList::PosList(int numRefs, int numPositions)
30 : myNumRefs(numRefs),
31 myNumPos(numPositions)
32{
33 initVars();
34}
35
37{
38 myPosList.clear();
39}
40
41
42void PosList::addPosition(int refID, int refPosition)
43{
44 // Check for negative numbers, if so, just return.
45 if((refID < 0) || (refPosition < 0))
46 {
47 return;
48 }
49
50 // If the position list is smaller or equal to refID, it cannot handle an index,
51 // so increase the size.
52 if(myPosList.size() <= (unsigned int)refID)
53 {
54 // The position list does not currently have space for this reference id,
55 // so add it.
56 myPosList.resize(refID+1, std::vector<bool>(myNumPos, false));
57 myNumRefs = refID + 1;
58 }
59
60 // The matrix is now sized for this reference id.
61 // Check to see if this id holds this position.
62 if((myPosList[refID]).size() <= (unsigned int)refPosition)
63 {
64 // The index for this position has not yet been created,
65 // so increase the size for it.
66 if(myNumPos <= refPosition)
67 {
68 // Our number of positions is smaller than
69 // the current reference id, so reset
70 // myNumPos for future use to be this position +1.
71 myNumPos = refPosition + 1;
72 }
73 // Increase the size for this reference id to hold at least myNumPos.
74 (myPosList[refID]).resize(myNumPos, false);
75 }
76
77 // It now holds this position, so set it to true.
78 myPosList[refID][refPosition] = true;
79}
80
81bool PosList::hasPosition(int refID, int refPosition)
82{
83 // Check for negative numbers, if so, just return false, not found.
84 if((refID < 0) || (refPosition < 0))
85 {
86 return(false);
87 }
88 bool found = false;
89 try
90 {
91 if((myPosList.at(refID)).at(refPosition))
92 {
93 found = true;
94 }
95 }
96 catch (std::out_of_range& oor)
97 {
98 // Nothing to do here, if it was out of range, then
99 // the position was not found (already set to false).
100 }
101 return(found);
102}
103
104
105void PosList::initVars()
106{
107 myPosList.clear();
108 myPosList.resize(myNumRefs, std::vector<bool>(myNumPos, false));
109}
bool hasPosition(int refID, int refPosition)
Return whether or not this list contains the specified reference ID and position (negative values wil...
Definition: PosList.cpp:81
virtual ~PosList()
Destructor.
Definition: PosList.cpp:36
PosList()
Constructor.
Definition: PosList.cpp:21
void addPosition(int refID, int refPosition)
Add the specified reference id/position (negative values will not be added).
Definition: PosList.cpp:42