IndexBase.cpp
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #include "IndexBase.h"
00019 #include <iomanip>
00020
00021 Chunk SortedChunkList::pop()
00022 {
00023 Chunk newChunk = chunkList.begin()->second;
00024 chunkList.erase(chunkList.begin());
00025 return(newChunk);
00026 }
00027
00028
00029 bool SortedChunkList::insert(const Chunk& chunkToInsert)
00030 {
00031 std::pair<std::map<uint64_t, Chunk>::iterator, bool> insertRes;
00032
00033 insertRes =
00034 chunkList.insert(std::pair<uint64_t, Chunk>(chunkToInsert.chunk_beg,
00035 chunkToInsert));
00036
00037 if(!insertRes.second)
00038 {
00039
00040 std::cerr << "Failed to insert into the SortedChunkList.\n";
00041 std::cerr << "\tpreviously found chunk:\tbeg = " << std::hex
00042 << insertRes.first->second.chunk_beg
00043 << "\tend = "
00044 << insertRes.first->second.chunk_end
00045 << "\nnew chunk:\tbeg = " << std::hex
00046 << chunkToInsert.chunk_beg
00047 << "\tend = "
00048 << chunkToInsert.chunk_end
00049 << std::endl;
00050 }
00051
00052 return(insertRes.second);
00053 }
00054
00055 void SortedChunkList::clear()
00056 {
00057 chunkList.clear();
00058 }
00059
00060 bool SortedChunkList::empty()
00061 {
00062 return(chunkList.empty());
00063 }
00064
00065
00066
00067 bool SortedChunkList::mergeOverlapping()
00068 {
00069
00070 std::map<uint64_t, Chunk>::iterator currentPos = chunkList.begin();
00071 std::map<uint64_t, Chunk>::iterator nextPos = chunkList.begin();
00072 if(nextPos != chunkList.end())
00073 {
00074 ++nextPos;
00075 }
00076
00077
00078 while(nextPos != chunkList.end())
00079 {
00080
00081
00082
00083 if(nextPos->second.chunk_end < currentPos->second.chunk_end)
00084 {
00085 chunkList.erase(nextPos);
00086 nextPos = currentPos;
00087 ++nextPos;
00088 continue;
00089 }
00090
00091
00092
00093
00094 if((nextPos->second.chunk_beg >> 16) <=
00095 (currentPos->second.chunk_end >> 16))
00096 {
00097 currentPos->second.chunk_end = nextPos->second.chunk_end;
00098
00099
00100 chunkList.erase(nextPos);
00101 nextPos = currentPos;
00102 ++nextPos;
00103 continue;
00104 }
00105 else
00106 {
00107
00108 currentPos = nextPos;
00109 ++nextPos;
00110 }
00111 }
00112 return(true);
00113 }
00114
00115
00116 IndexBase::IndexBase()
00117 : n_ref(0)
00118 {
00119 myRefs.clear();
00120 }
00121
00122
00123
00124 IndexBase::~IndexBase()
00125 {
00126 }
00127
00128
00129
00130 void IndexBase::resetIndex()
00131 {
00132 n_ref = 0;
00133
00134 myRefs.clear();
00135 }
00136
00137
00138
00139 int32_t IndexBase::getNumRefs() const
00140 {
00141
00142 return(myRefs.size());
00143 }
00144
00145
00146
00147
00148
00149 int IndexBase::getBinsForRegion(uint32_t start, uint32_t end, uint16_t binList[MAX_NUM_BINS])
00150 {
00151 uint32_t binListIndex = 0, binNum;
00152 --end;
00153
00154
00155 if(start > MAX_POSITION)
00156 {
00157 start = MAX_POSITION;
00158 }
00159 if(end > MAX_POSITION)
00160 {
00161 end = MAX_POSITION;
00162 }
00163
00164 binList[binListIndex++] = 0;
00165 for (binNum = 1 + (start>>26); binNum <= 1 + (end>>26); ++binNum)
00166 binList[binListIndex++] = binNum;
00167 for (binNum = 9 + (start>>23); binNum <= 9 + (end>>23); ++binNum)
00168 binList[binListIndex++] = binNum;
00169 for (binNum = 73 + (start>>20); binNum <= 73 + (end>>20); ++binNum)
00170 binList[binListIndex++] = binNum;
00171 for (binNum = 585 + (start>>17); binNum <= 585 + (end>>17); ++binNum)
00172 binList[binListIndex++] = binNum;
00173 for (binNum = 4681 + (start>>14); binNum <= 4681 + (end>>14); ++binNum)
00174 binList[binListIndex++] = binNum;
00175
00176
00177 return binListIndex;
00178 }
00179
00180
00181
00182
00183 bool IndexBase::getMinOffsetFromLinearIndex(int32_t refID, uint32_t position,
00184 uint64_t& minOffset) const
00185 {
00186 int32_t linearIndex = position >> LINEAR_INDEX_SHIFT;
00187
00188 minOffset = 0;
00189
00190 if(refID > n_ref)
00191 {
00192
00193 return(false);
00194 }
00195
00196 int32_t linearOffsetSize = myRefs[refID].n_intv;
00197
00198
00199
00200
00201
00202 if((linearOffsetSize == 0) || (linearIndex >= linearOffsetSize))
00203
00204 {
00205 return(false);
00206 }
00207
00208
00209 minOffset = myRefs[refID].ioffsets[linearIndex];
00210
00211
00212
00213
00214
00215
00216
00217 while((minOffset == 0) && (--linearIndex >= 0))
00218 {
00219 minOffset = myRefs[refID].ioffsets[linearIndex];
00220 }
00221
00222
00223
00224
00225
00226
00227 linearIndex = 0;
00228 while((minOffset == 0) && (linearIndex < linearOffsetSize))
00229 {
00230 minOffset = myRefs[refID].ioffsets[linearIndex];
00231 linearIndex++;
00232 }
00233 if(minOffset == 0)
00234 {
00235
00236 return(false);
00237 }
00238 return(true);
00239 }