Skip to content

Commit

Permalink
Add RankedItem template class.
Browse files Browse the repository at this point in the history
  • Loading branch information
adamv committed Nov 2, 2009
1 parent 1504fcb commit fae2b2d
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
47 changes: 47 additions & 0 deletions src/RankedItem.h
@@ -0,0 +1,47 @@
#ifndef __RANKEDITEM_H__
#define __RANKEDITEM_H__

#include <vector>

// T must define "operator <"
template <class T> class RankedItem {
public:
RankedItem() : data(NULL), rank(0) {};
RankedItem(const T* a, const std::vector<unsigned int>& hl):
data(a), hlChars(hl)
{
// Calculate rank (total distance between chars)
this->rank = 0;
if (hlChars.size() <= 1) return;

unsigned int prev = hlChars[0]+1;
for (std::vector<unsigned int>::const_iterator p = hlChars.begin()+1; p != hlChars.end(); ++p) {
this->rank += *p - prev;
prev = *p+1;
}
};

bool operator<(const RankedItem& other) const {
if (rank != other.rank) return rank < other.rank;
if (data && other.data) return data < other.data;
return false;
};

void swap(RankedItem& other){
const T* const temp_data = data;
data = other.data;
other.data = temp_data;

const unsigned int temp_rank = rank;
rank = other.rank;
other.rank = temp_rank;

hlChars.swap(other.hlChars);
};

const T* data;
std::vector<unsigned int> hlChars;
unsigned int rank;
};

#endif
4 changes: 4 additions & 0 deletions src/e.vcproj
Expand Up @@ -1453,6 +1453,10 @@
RelativePath="MultilineDataObject.h"
>
</File>
<File
RelativePath=".\RankedItem.h"
>
</File>
<File
RelativePath="RecursiveCriticalSection.h"
>
Expand Down

0 comments on commit fae2b2d

Please sign in to comment.