00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013 #ifndef _DOCINFOLIST_HPP
00014 #define _DOCINFOLIST_HPP
00015
00016 #include "IndexTypes.hpp"
00017 #include <iterator>
00018
00019 namespace lemur
00020 {
00021 namespace api
00022 {
00023
00025
00034 class DocInfo {
00035 public:
00036 DocInfo() {}
00037 DocInfo(DOCID_T docID, COUNT_T termCount) :
00038 did(docID), tcount(termCount) {}
00039
00040 virtual ~DocInfo() {}
00041
00043 virtual DOCID_T docID() const {return did;}
00044
00046 virtual void docID(DOCID_T id) {did = id;}
00047
00049 virtual COUNT_T termCount() const {return tcount;}
00050
00052 virtual void termCount(COUNT_T c) {tcount = c;}
00053
00056 virtual const LOC_T* positions() const { return NULL; }
00057
00060 virtual void positions(const LOC_T* pos) {}
00061
00062
00063 protected:
00064 DOCID_T did;
00065 COUNT_T tcount;
00066 };
00067
00068
00069
00071
00079 class DocInfoList {
00080 public:
00081 DocInfoList() {}
00082 virtual ~DocInfoList() {}
00083
00084 protected:
00085
00087 virtual DocInfo* newElement() const { return new DocInfo(); }
00089 virtual DocInfo* getElement(DocInfo* elem, POS_T position) const =0;
00092 virtual void assignElement(DocInfo* to, DocInfo* from) const { *to = *from; }
00094 virtual POS_T beginPosition() const =0;
00096 virtual POS_T endPosition() const =0;
00098 virtual POS_T nextPosition(POS_T position) const =0;
00099
00100 public:
00101
00103 virtual void startIteration() const=0;
00105 virtual bool hasMore() const=0;
00107 virtual DocInfo *nextEntry() const=0;
00108
00109
00111 class iterator : std::iterator<std::input_iterator_tag, DocInfo> {
00112 public:
00113 iterator() : list(NULL), position(0), current(NULL) {}
00114 iterator(const iterator& other) {
00115 list = other.list;
00116 position = other.position;
00117 if ((list) && (other.current) ) {
00118 current = list->newElement();
00119 list->assignElement(current, other.current);
00120 } else {
00121 current = NULL;
00122 }
00123 }
00124 iterator(const DocInfoList* dil, POS_T pos) : list(dil), position(pos) {
00125 if (list) {
00126 if (position != list->endPosition()) {
00127 current = list->newElement();
00128 current = list->getElement(current, position);
00129 } else {
00130 current=NULL;
00131 }
00132 }
00133 }
00134
00135
00136 ~iterator() {
00137 delete(current);
00138 }
00139
00140 DocInfo& operator*() { return *current; }
00141 DocInfo* operator->() { return current; }
00142 iterator& operator++() {
00143 position = list->nextPosition(position);
00144 if (position != list->endPosition())
00145 current = list->getElement(current, position);
00146 return *this;
00147 }
00148
00149 iterator& operator++(int) {
00150 return operator++();
00151 }
00152 bool operator==(const iterator& other) const {
00153 return (list == other.list) && (position == other.position);
00154 }
00155 bool operator!=(const iterator& other) const {
00156 return (list != other.list) || (position != other.position);
00157 }
00158 iterator& operator=(const iterator& other) {
00159 list = other.list;
00160 position = other.position;
00161 if ((list) && (other.current)) {
00162 if (!current)
00163 current = list->newElement();
00164 list->assignElement(current, other.current);
00165 } else {
00166 delete(current);
00167 current = NULL;
00168 }
00169 return *this;
00170 }
00173 void seek(POS_T pos) {
00174 position = pos;
00175 if (position != list->endPosition()) {
00176 if (!current)
00177 current = list->newElement();
00178 current = list->getElement(current, position);
00179 } else {
00180 delete(current);
00181 current = NULL;
00182 }
00183 }
00184
00185 protected:
00186 const DocInfoList* list;
00187 POS_T position;
00188 DocInfo* current;
00189 };
00190
00191 iterator& begin() const {
00192 iterator it(this, beginPosition());
00193 itbegin = it;
00194 return itbegin;
00195 }
00196 iterator& end() const {
00197 iterator it(this, endPosition());
00198 itend = it;
00199 return itend;
00200 }
00201
00202 protected:
00203 mutable DocInfoList::iterator itbegin;
00204 mutable DocInfoList::iterator itend;
00205 friend class iterator;
00206 };
00207 }
00208 }
00209
00210
00211 #endif