Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Related Pages  

DocInfoList.hpp

Go to the documentation of this file.
00001 /*==========================================================================
00002  * Copyright (c) 2001 Carnegie Mellon University.  All Rights Reserved.
00003  *
00004  * Use of the Lemur Toolkit for Language Modeling and Information Retrieval
00005  * is subject to the terms of the software license set forth in the LICENSE
00006  * file included with this software, and also available at
00007  * http://www.lemurproject.org/license.html
00008  *
00009  *==========================================================================
00010 */
00011 
00012 
00013 #ifndef _DOCINFOLIST_HPP
00014 #define _DOCINFOLIST_HPP
00015 
00016 #include "IndexTypes.hpp"
00017 #include <iterator>
00018 
00019 
00021 
00030 class DocInfo {
00031 public:
00032   DocInfo() {}
00033   DocInfo(DOCID_T docID, COUNT_T termCount) :
00034     did(docID), tcount(termCount) {}
00035 
00036   virtual ~DocInfo() {}
00037 
00039   virtual DOCID_T docID() const {return did;}
00040 
00042   virtual void docID(DOCID_T id) {did = id;}
00043 
00045   virtual COUNT_T termCount() const {return tcount;}
00046 
00048   virtual void termCount(COUNT_T c) {tcount = c;}
00049 
00052   virtual const LOC_T* positions() const { return NULL; }
00053 
00056   virtual void positions(const LOC_T* pos) {}
00057   // Maybe throw an exception here since this shouldn't ever be called
00058 
00059 protected:
00060   DOCID_T did;    // document id
00061   COUNT_T tcount; // term count
00062 };
00063 
00064 
00065 
00067 
00075 class DocInfoList {
00076 public:
00077   DocInfoList() {}
00078   virtual ~DocInfoList() {}
00079 
00080 protected:
00081   // Helper functions for iterator, subclasses should override
00083   virtual DocInfo* newElement() const { return new DocInfo(); }
00085   virtual DocInfo* getElement(DocInfo* elem, POS_T position) const =0;
00088   virtual void assignElement(DocInfo* to, DocInfo* from) const { *to = *from; }
00090   virtual POS_T beginPosition() const =0;
00092   virtual POS_T endPosition() const =0;
00094   virtual POS_T nextPosition(POS_T position) const =0;
00095 
00096 public:
00097   // Single, internal iteration
00099   virtual void startIteration() const=0;
00101   virtual bool hasMore() const=0;
00103   virtual DocInfo *nextEntry() const=0;
00104 
00105   // C++ style forward input (readonly) iterator
00107   class iterator : std::iterator<std::input_iterator_tag, DocInfo> {
00108   public:
00109     iterator() : list(NULL), position(NULL), current(NULL) {}
00110     iterator(const iterator& other) {
00111       list = other.list;
00112       position = other.position;
00113       if ((list) && (other.current) ) {
00114         current = list->newElement();
00115         list->assignElement(current, other.current);  // list knows element class
00116       } else {
00117         current = NULL;
00118       }  
00119     }
00120     iterator(const DocInfoList* dil, POS_T pos) : list(dil), position(pos) {
00121       if (list) {
00122         if (position != list->endPosition()) {
00123           current = list->newElement();   // get new element
00124           current = list->getElement(current, position);
00125         } else {
00126           current=NULL;
00127         }
00128       }
00129     }
00130 
00131 
00132     ~iterator() {
00133         delete(current);
00134     }
00135 
00136     DocInfo& operator*() { return *current; }
00137     DocInfo* operator->() { return current; }
00138     iterator& operator++() {
00139       position = list->nextPosition(position);
00140       if (position != list->endPosition())
00141         current = list->getElement(current, position);
00142       return *this;
00143     }
00144     // identical to prefix version
00145     iterator& operator++(int) {
00146       return operator++();
00147     }
00148     bool operator==(const iterator& other) const {
00149       return (list == other.list) && (position == other.position);
00150     }
00151     bool operator!=(const iterator& other) const {
00152       return (list != other.list) || (position != other.position);
00153     }
00154     iterator& operator=(const iterator& other) {
00155       list = other.list;
00156       position = other.position;
00157       if ((list) && (other.current)) {
00158         if (!current)
00159           current = list->newElement();
00160         list->assignElement(current, other.current);  // list knows element class
00161       } else {
00162         delete(current);
00163         current = NULL;
00164       }
00165       return *this;
00166     }
00169     void seek(POS_T pos) {
00170       position = pos;
00171       if (position != list->endPosition()) {
00172         if (!current)
00173           current = list->newElement();
00174         current = list->getElement(current, position);
00175       } else {
00176         delete(current);
00177         current = NULL;
00178       }
00179     }
00180 
00181   protected:
00182     const DocInfoList* list;  // list associated with this iterator
00183     POS_T position;     // current position in list
00184     DocInfo* current;   // current element of list
00185   }; // end of nested iterator declaration
00186 
00187   iterator& begin() const { 
00188     iterator it(this, beginPosition());
00189     itbegin = it;
00190     return itbegin;
00191   }
00192   iterator& end() const { 
00193     iterator it(this, endPosition());
00194     itend = it;
00195     return itend;
00196   }
00197 
00198 protected:
00199   mutable DocInfoList::iterator itbegin;  // iterator at head of list
00200   mutable DocInfoList::iterator itend;    // iterator at end of list
00201   friend class iterator;
00202 };
00203 
00204 
00205 #endif

Generated on Wed Nov 3 12:58:53 2004 for Lemur Toolkit by doxygen1.2.18