sorting 1st steps
This commit is contained in:
@@ -0,0 +1,88 @@
|
||||
#ifndef lint
|
||||
static char rcsid[] = "@(#$Id: sortseq.cpp,v 1.1 2005-12-01 16:23:09 dockes Exp $ (C) 2005 J.F.Dockes";
|
||||
#endif
|
||||
#include <algorithm>
|
||||
|
||||
#include "debuglog.h"
|
||||
#include "sortseq.h"
|
||||
|
||||
using std::string;
|
||||
|
||||
class CompareDocs {
|
||||
RclSortSpec ss;
|
||||
public:
|
||||
CompareDocs(const RclSortSpec &sortspec) : ss(sortspec) {}
|
||||
|
||||
int operator()(const Rcl::Doc &x, const Rcl::Doc &y) {
|
||||
for (unsigned int i = 0; i < ss.crits.size(); i++) {
|
||||
switch (ss.crits[i]) {
|
||||
case RclSortSpec::RCLFLD_MTIME:
|
||||
{
|
||||
long xmtime = x.dmtime.empty() ? atol(x.fmtime.c_str()) :
|
||||
atol(x.dmtime.c_str());
|
||||
long ymtime = y.dmtime.empty() ? atol(y.fmtime.c_str()) :
|
||||
atol(y.dmtime.c_str());
|
||||
if (ss.dirs[i])
|
||||
return xmtime > ymtime;
|
||||
else
|
||||
return xmtime < ymtime;
|
||||
}
|
||||
continue;
|
||||
case RclSortSpec::RCLFLD_URL:
|
||||
if (ss.dirs[i])
|
||||
return x.url > y.url;
|
||||
else
|
||||
return x.url < y.url;
|
||||
continue;
|
||||
case RclSortSpec::RCLFLD_IPATH:
|
||||
if (ss.dirs[i])
|
||||
return x.ipath > y.ipath;
|
||||
else
|
||||
return x.ipath < y.ipath;
|
||||
continue;
|
||||
case RclSortSpec::RCLFLD_MIMETYPE:
|
||||
if (ss.dirs[i])
|
||||
return x.mimetype > y.mimetype;
|
||||
else
|
||||
return x.mimetype < y.mimetype;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
// Did all comparisons: must be equal
|
||||
return 0;
|
||||
}
|
||||
};
|
||||
|
||||
DocSeqSorted::DocSeqSorted(DocSequence &iseq, int cnt, RclSortSpec &sortspec)
|
||||
{
|
||||
LOGDEB1(("DocSeqSorted:: input cnt %d\n", cnt));
|
||||
|
||||
m_docs.resize(cnt);
|
||||
m_pcs.resize(cnt);
|
||||
int i;
|
||||
for (i = 0; i < cnt; i++) {
|
||||
if (!iseq.getDoc(i, m_docs[i], &m_pcs[i])) {
|
||||
LOGERR(("DocSeqSorted: getDoc failed for doc %d\n", i));
|
||||
break;
|
||||
}
|
||||
}
|
||||
m_count = i;
|
||||
LOGDEB1(("DocSeqSorted:: m_count %d\n", m_count));
|
||||
m_docs.resize(m_count);
|
||||
m_pcs.resize(m_count);
|
||||
m_title = string("Sorted ") + iseq.title();
|
||||
CompareDocs cmp(sortspec);
|
||||
sort(m_docs.begin(), m_docs.end(), cmp);
|
||||
}
|
||||
|
||||
bool DocSeqSorted::getDoc(int num, Rcl::Doc &doc, int *percent, string *)
|
||||
{
|
||||
LOGDEB1(("DocSeqSorted: fetching %d\n", num));
|
||||
|
||||
if (num >= m_count)
|
||||
return false;
|
||||
if (percent)
|
||||
*percent = m_pcs[num];
|
||||
doc = m_docs[num];
|
||||
return true;
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
#ifndef _SORTSEQ_H_INCLUDED_
|
||||
#define _SORTSEQ_H_INCLUDED_
|
||||
/* @(#$Id: sortseq.h,v 1.1 2005-12-01 16:23:09 dockes Exp $ (C) 2004 J.F.Dockes */
|
||||
|
||||
#include <vector>
|
||||
#include <string>
|
||||
|
||||
#include "docseq.h"
|
||||
|
||||
class RclSortSpec {
|
||||
public:
|
||||
enum Field {RCLFLD_URL, RCLFLD_IPATH, RCLFLD_MIMETYPE, RCLFLD_MTIME};
|
||||
void addCrit(Field fld, bool desc = false) {
|
||||
crits.push_back(fld);
|
||||
dirs.push_back(desc);
|
||||
}
|
||||
std::vector<Field> crits;
|
||||
std::vector<bool> dirs;
|
||||
};
|
||||
|
||||
/**
|
||||
* A sorted sequence is created from the first N documents of another one,
|
||||
* and sorts them according to the given criteria.
|
||||
*/
|
||||
class DocSeqSorted : public DocSequence {
|
||||
public:
|
||||
DocSeqSorted(DocSequence &iseq, int cnt, RclSortSpec &sortspec);
|
||||
virtual ~DocSeqSorted() {}
|
||||
virtual bool getDoc(int num, Rcl::Doc &doc, int *percent, string *sh = 0);
|
||||
virtual int getResCnt() {return m_count;}
|
||||
virtual std::string title() {return m_title;}
|
||||
private:
|
||||
std::string m_title;
|
||||
int m_count;
|
||||
std::vector<Rcl::Doc> m_docs;
|
||||
std::vector<int> m_pcs;
|
||||
};
|
||||
|
||||
#endif /* _SORTSEQ_H_INCLUDED_ */
|
||||
Reference in New Issue
Block a user