Main Page | Namespace List | Class Hierarchy | Class List | File List | Namespace Members | Class Members | File Members | Related Pages

FieldInfoList.hpp

Go to the documentation of this file.
00001 /*==========================================================================
00002  * Copyright (c) 2007 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  * 26 Sep 2007 - mjh - initial file start
00012  */
00013 
00014 
00015 #ifndef _FIELDINFOLIST_HPP
00016 #define _FIELDINFOLIST_HPP
00017 
00018 #include "IndexTypes.hpp"
00019 #include "lemur-platform.h"
00020 #include "Exception.hpp"
00021 
00022 namespace lemur
00023 {
00024   namespace api 
00025   {
00026     
00028 
00038     class FieldInfo {
00039     public:
00040       int begin;
00041       int end;
00042       int id;
00043       int ordinal;
00044       int parentOrdinal;
00045       INT64 number;
00046 
00047       inline FieldInfo() {}
00048 
00049       inline FieldInfo(int _begin, int _end, int _id, INT64 _number=0, int _ordinal=0, int _parentOrdinal=0) :
00050       begin(_begin), end(_end), id(_id),
00051       ordinal(_ordinal), parentOrdinal(_parentOrdinal), number(_number) {  }
00052 
00053       inline FieldInfo(const FieldInfo &c) : 
00054       begin(c.begin), end(c.end), id(c.id),
00055       ordinal(c.ordinal), parentOrdinal(c.parentOrdinal), number(c.number) {  }
00056 
00057 
00058     };
00059 
00060 
00062 
00069     class FieldInfoList {
00070     public:
00071       virtual ~FieldInfoList() {}
00072 
00073     protected:
00074       // Helper functions for iterator, subclasses should override
00076       virtual FieldInfo* newElement() const { return new FieldInfo(); }
00078       virtual FieldInfo* getElement(FieldInfo* elem, POS_T position) const =0;
00081       virtual void assignElement(FieldInfo* to, FieldInfo* from) const { *to = *from; }
00083       virtual POS_T beginPosition() const =0;
00085       virtual POS_T endPosition() const =0;
00087       virtual POS_T nextPosition(POS_T position) const =0;
00088 
00089     public:
00090       // Single, internal iteration
00092       virtual void startIteration()=0;
00093 
00095       virtual bool hasMore()const=0;
00096 
00098       virtual FieldInfo *nextEntry()const=0;
00099 
00101       virtual int size()=0;
00102 
00104       virtual FieldInfo* operator[] (int index)=0;
00105 
00106       // C++ style forward input (readonly) iterator
00108       class iterator : std::iterator<std::input_iterator_tag, FieldInfo> {
00109       public:
00110         iterator() : list(NULL), position(0), current(NULL) {}
00111         iterator(const iterator& other) {
00112           list = other.list;
00113           position = other.position;
00114           if ((list) && (other.current) ) {
00115             current = list->newElement();
00116             list->assignElement(current, other.current);  // list knows element class
00117           } else {
00118             current = NULL;
00119           }
00120         }
00121         iterator(const FieldInfoList* til, POS_T pos) : list(til), position(pos) {
00122           if (list) {
00123             if (position != list->endPosition()) {
00124               current = list->newElement();   // get new element
00125               current = list->getElement(current, position);
00126             } else {
00127               current = NULL;
00128             }
00129           }
00130         }
00131 
00132         ~iterator() {
00133           delete(current);
00134         }
00135 
00136         FieldInfo& operator*() { return *current; }
00137         FieldInfo* 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 FieldInfoList* list;  // list associated with this iterator
00183         POS_T position;     // current position in list
00184         FieldInfo* 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 FieldInfoList::iterator itbegin;  // iterator at head of list
00200       mutable FieldInfoList::iterator itend;    // iterator at end of list
00201       friend class iterator;
00202     };
00203   }
00204 }
00205 
00206 
00207 #endif

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