Main Page | Namespace List | Class Hierarchy | Class List | File List | Namespace Members | Class 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 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       // Maybe throw an exception here since this shouldn't ever be called
00062 
00063     protected:
00064       DOCID_T did;    // document id
00065       COUNT_T tcount; // term count
00066     };
00067 
00068 
00069 
00071 
00079     class DocInfoList {
00080     public:
00081       DocInfoList() {}
00082       virtual ~DocInfoList() {}
00083 
00084     protected:
00085       // Helper functions for iterator, subclasses should override
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       // Single, internal iteration
00103       virtual void startIteration() const=0;
00105       virtual bool hasMore() const=0;
00107       virtual DocInfo *nextEntry() const=0;
00108 
00109       // C++ style forward input (readonly) iterator
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);  // list knows element class
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();   // get new element
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         // identical to prefix version
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);  // list knows element class
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;  // list associated with this iterator
00187         POS_T position;     // current position in list
00188         DocInfo* current;   // current element of list
00189       }; // end of nested iterator declaration
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;  // iterator at head of list
00204       mutable DocInfoList::iterator itend;    // iterator at end of list
00205       friend class iterator;
00206     };
00207   }
00208 }
00209 
00210 
00211 #endif

Generated on Tue Jun 15 11:02:53 2010 for Lemur by doxygen 1.3.4