Commit ba3bb104 authored by Neil Kindlon's avatar Neil Kindlon
Browse files

Cahnged Closest's RecDistList class to not use map

parent e7656ff4
...@@ -8,63 +8,186 @@ ...@@ -8,63 +8,186 @@
#include "CloseSweep.h" #include "CloseSweep.h"
#include "ContextClosest.h" #include "ContextClosest.h"
RecDistList::RecDistList(int maxSize)
: _kVal(maxSize),
_empty(true),
_currNumIdxs(0),
_totalRecs(0) {
_allRecs.resize(_kVal);
for (int i=0; i < _kVal; i++) {
_allRecs[i] = new elemsType();
}
_distIndex = new indexType[_kVal];
clear();
}
RecDistList::~RecDistList() {
for (int i=0; i < _kVal; i++) {
delete _allRecs[i];
}
delete _distIndex;
_distIndex = NULL;
}
void RecDistList::clear() { void RecDistList::clear() {
for (distRecsType::iterator iter = _recs.begin(); iter != _recs.end(); iter++) { for (int i=0; i < _kVal; i++) {
delete iter->second; _allRecs[i]->clear();
_allRecs[i]->reserve(16);
}
for (int i=0; i < _kVal; i++) {
_distIndex[i].first = -1;
_distIndex[i].second = -1;
} }
_recs.clear(); _currNumIdxs = 0;
_empty = true;
_totalRecs = 0;
} }
bool RecDistList::addRec(int dist, const Record *record, chromDirType chromDir) { bool RecDistList::addRec(int dist, const Record *record, chromDirType chromDir) {
//if unique size is >= maxRecNum, that means the collection is int newPos = 0;
//full. In that case, if the new rec has greater distance than bool mustAppend = false;
// any in the collection, ignore it. If the distance isn't int useElemIdx = 0;
// greater, add it, removing the previously greater one
// if need be. if (dist > getMaxDist()) {
// If the collection isn't full, just the new. //dist is bigger than any currently contained.
if (_currNumIdxs == _kVal) {
if (uniqueSize() < _maxUniqueAllowed || exists(dist)) { //already full with smaller distances
insert(dist, record, chromDir);
return true;
} else {
//find previous greatest distance
revIterType rIter = _recs.rbegin();
int currMaxDist = rIter->first;
if (dist > currMaxDist) {
return false; return false;
} }
//Now we know dist is less than currMax. mustAppend = true;
//new dist is smaller. Erase old max. useElemIdx = _currNumIdxs;
delete rIter->second; newPos = _currNumIdxs;
}
//apparently you can't erase a reverse iterator, so we'll if (find(dist, newPos)) {
//make a normal one pointing at the end, then back it up. _allRecs[_distIndex[newPos].second]->push_back(elemPairType(chromDir, record));
iterType delIter = _recs.end(); _totalRecs++;
delIter--;
_recs.erase(delIter);
insert(dist, record, chromDir);
return true; return true;
} }
if (!mustAppend) {
//smaller than maxDist, and doesn't currently exist. must insert
//newPos now is the insertion point.
int startShiftPos = 0;
if (_currNumIdxs == _kVal) {
//already full. must remove oldest max.
//determine which vector it was using
//so we can re-use it.
startShiftPos = _kVal-1;
useElemIdx = _distIndex[startShiftPos].second;
} else {
//can add a new element
startShiftPos = _currNumIdxs;
useElemIdx = _currNumIdxs;
_currNumIdxs++;
}
for (int i=startShiftPos; i > newPos; i--) {
_distIndex[i].first = _distIndex[i-1].first;
_distIndex[i].second = _distIndex[i-1].second;
}
} else {
_currNumIdxs++;
}
_allRecs[useElemIdx]->clear();
_allRecs[useElemIdx]->reserve(16);
_allRecs[useElemIdx]->push_back(elemPairType(chromDir, record));
_distIndex[newPos].first = dist;
_distIndex[newPos].second = useElemIdx;
_empty = false;
_totalRecs++;
return true;
} }
void RecDistList::insert(int dist, const Record *record, chromDirType chromDir) { //bool RecDistList::addRec(int dist, const Record *record, chromDirType chromDir) {
elemsType *elems = NULL; // //if unique size is >= maxRecNum, that means the collection is
distRecsType::iterator iter = _recs.find(dist); // //full. In that case, if the new rec has greater distance than
if (iter == _recs.end()) { // // any in the collection, ignore it. If the distance isn't
//dist didn't exist before. Add it by // // greater, add it, removing the previously greater one
// creating new elems container, then // // if need be.
//putting this in the map. // // If the collection isn't full, just the new.
elems = new elemsType(); //
_recs[dist] = elems; // if (uniqueSize() < _kVal || exists(dist)) {
} else { // insert(dist, record, chromDir);
elems = iter->second; // return true;
// } else {
// //find previous greatest distance
// revIterType rIter = _recs.rbegin();
// int currMaxDist = rIter->first;
// if (dist > currMaxDist) {
// return false;
// }
// //Now we know dist is less than currMax.
// //new dist is smaller. Erase old max.
// delete rIter->second;
//
// //apparently you can't erase a reverse iterator, so we'll
// //make a normal one pointing at the end, then back it up.
// iterType delIter = _recs.end();
// delIter--;
// _recs.erase(delIter);
// insert(dist, record, chromDir);
// return true;
// }
//
//}
//void RecDistList::insert(int dist, const Record *record, chromDirType chromDir) {
// if ()
// elemsType *elems = NULL;
// distRecsType::iterator iter = _recs.find(dist);
// if (iter == _recs.end()) {
// //dist didn't exist before. Add it by
// // creating new elems container, then
// //putting this in the map.
// elems = new elemsType();
// _recs[dist] = elems;
// } else {
// elems = iter->second;
// }
// elems->push_back(pair<chromDirType, const Record *>(chromDir, record));
// _totalRecs++;
//}
//if true, pos will be the idx the distance is at.
//if false, pos will be the idx to insert at.
bool RecDistList::find(int dist, int &pos) const {
int lbound=0, ubound=_currNumIdxs-1, currVal =0;
pos = 0;
while(lbound <= ubound)
{
pos = (lbound + ubound) / 2;
currVal = _distIndex[pos].first;
if (currVal == dist) {
return true;
}
if (dist > currVal) {
lbound = pos + 1;
} else {
ubound = pos -1;
}
} }
elems->push_back(pair<chromDirType, const Record *>(chromDir, record)); pos = (ubound == -1 ) ? 0 : (lbound == _currNumIdxs ? _currNumIdxs : lbound);
_totalRecs++; return false;
} }
int RecDistList::getMaxLeftEndPos() const {
if (_empty) return -1;
int maxDist =_distIndex[_currNumIdxs-1].first;
const elemsType *elems = _allRecs[_distIndex[_currNumIdxs-1].second];
for (int i=0; i < (int)elems->size(); i++) {
if ((*elems)[i].first == LEFT) {
return maxDist;
}
}
return -1;
}
CloseSweep::CloseSweep(ContextClosest *context) CloseSweep::CloseSweep(ContextClosest *context)
: NewChromSweep(context), : NewChromSweep(context),
_context(context), _context(context),
...@@ -85,21 +208,6 @@ CloseSweep::CloseSweep(ContextClosest *context) ...@@ -85,21 +208,6 @@ CloseSweep::CloseSweep(ContextClosest *context)
} }
int RecDistList::getMaxLeftEndPos() const {
if (_recs.empty()) return -1;
int maxDist = _recs.rbegin()->first;
const elemsType *elems = _recs.rbegin()->second;
for (int i=0; i < (int)elems->size(); i++) {
if ((*elems)[i].first == LEFT) {
return maxDist;
}
}
return -1;
}
CloseSweep::~CloseSweep(void) { CloseSweep::~CloseSweep(void) {
for (int i=0; i < _numDBs; i++) { for (int i=0; i < _numDBs; i++) {
delete _minUpstreamRecs[i]; delete _minUpstreamRecs[i];
......
...@@ -34,39 +34,47 @@ public: ...@@ -34,39 +34,47 @@ public:
class RecDistList { class RecDistList {
public: public:
typedef enum { LEFT, OVERLAP, RIGHT } chromDirType; typedef enum { LEFT, OVERLAP, RIGHT } chromDirType;
RecDistList(int maxSize) : _maxUniqueAllowed(maxSize) {} RecDistList(int maxSize);
~RecDistList() { clear(); } ~RecDistList();
bool empty() const { return _recs.empty(); } bool empty() const { return _empty; }
void clear(); void clear();
int uniqueSize() const { return _recs.size(); } int uniqueSize() const { return _currNumIdxs; }
size_t totalSize() const { return _totalRecs; } size_t totalSize() const { return _totalRecs; }
bool addRec(int dist, const Record *, chromDirType chromDir); bool addRec(int dist, const Record *, chromDirType chromDir);
bool exists(int dist) const { return (_recs.find(dist) != _recs.end()); } bool exists(int dist) const {
int furtherestDistance() const { return _recs.rbegin()->first; } int dummyVal = 0;
return find(dist, dummyVal);
typedef vector<pair<chromDirType, const Record *> >elemsType; }
typedef map<int, elemsType *> distRecsType; typedef pair<chromDirType, const Record *> elemPairType;
typedef distRecsType::iterator iterType; typedef vector<elemPairType>elemsType;
typedef distRecsType::reverse_iterator revIterType; typedef pair<int, int> indexType;
typedef distRecsType::const_iterator constIterType;
typedef distRecsType::const_reverse_iterator constRevIterType; int getMaxDist() const { return _empty ? 0 : _distIndex[_currNumIdxs-1].first; }
typedef int constIterType; //used to be a map iter, trying not to change interface too much.
int getMaxDist() const { return _recs.empty() ? 0 : _recs.rbegin()->first; } constIterType begin() const { return 0; }
constIterType begin() const { return _recs.begin(); } constIterType end() const { return _currNumIdxs; }
constIterType end() const { return _recs.end(); } int currDist(constIterType iter) const { return _distIndex[iter].first; }
int currDist(constIterType iter) const { return iter->first; } size_t currNumElems(constIterType iter) const { return allElems(iter)->size(); }
size_t currNumElems(constIterType iter) const { return iter->second->size(); } const elemsType *allElems(constIterType iter) const { return _allRecs[_distIndex[iter].second]; }
const Record *firstElem(constIterType iter) const { return (iter->second->at(0)).second; }
const Record *lastElem(constIterType iter) const { return (iter->second->at(iter->second->size()-1)).second; }
const elemsType *allElems(constIterType iter) const { return iter->second; }
int getMaxLeftEndPos() const; int getMaxLeftEndPos() const;
private: private:
void insert(int dist, const Record *, chromDirType chromDir); void insert(int dist, const Record *, chromDirType chromDir);
distRecsType _recs;
int _maxUniqueAllowed;
//if true, pos will be the idx the distance is at.
//if false, pos will be the idx to insert at.
bool find(int dist, int &pos) const;
int _kVal; //max unique allowed
bool _empty;
int _currNumIdxs;
int _totalRecs; int _totalRecs;
vector<elemsType *> _allRecs;
indexType * _distIndex;
}; };
class CloseSweep : public NewChromSweep { class CloseSweep : public NewChromSweep {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment