strfifolist.h

00001 /*
00002  * All of the documentation and software included in the
00003  * Alchemy Software is copyrighted by Stanley Kok, Parag
00004  * Singla, Matthew Richardson, Pedro Domingos, Marc
00005  * Sumner and Hoifung Poon.
00006  * 
00007  * Copyright [2004-07] Stanley Kok, Parag Singla, Matthew
00008  * Richardson, Pedro Domingos, Marc Sumner and Hoifung
00009  * Poon. All rights reserved.
00010  * 
00011  * Contact: Pedro Domingos, University of Washington
00012  * (pedrod@cs.washington.edu).
00013  * 
00014  * Redistribution and use in source and binary forms, with
00015  * or without modification, are permitted provided that
00016  * the following conditions are met:
00017  * 
00018  * 1. Redistributions of source code must retain the above
00019  * copyright notice, this list of conditions and the
00020  * following disclaimer.
00021  * 
00022  * 2. Redistributions in binary form must reproduce the
00023  * above copyright notice, this list of conditions and the
00024  * following disclaimer in the documentation and/or other
00025  * materials provided with the distribution.
00026  * 
00027  * 3. All advertising materials mentioning features or use
00028  * of this software must display the following
00029  * acknowledgment: "This product includes software
00030  * developed by Stanley Kok, Parag Singla, Matthew
00031  * Richardson, Pedro Domingos, Marc Sumner and Hoifung
00032  * Poon in the Department of Computer Science and
00033  * Engineering at the University of Washington".
00034  * 
00035  * 4. Your publications acknowledge the use or
00036  * contribution made by the Software to your research
00037  * using the following citation(s): 
00038  * Stanley Kok, Parag Singla, Matthew Richardson and
00039  * Pedro Domingos (2005). "The Alchemy System for
00040  * Statistical Relational AI", Technical Report,
00041  * Department of Computer Science and Engineering,
00042  * University of Washington, Seattle, WA.
00043  * http://www.cs.washington.edu/ai/alchemy.
00044  * 
00045  * 5. Neither the name of the University of Washington nor
00046  * the names of its contributors may be used to endorse or
00047  * promote products derived from this software without
00048  * specific prior written permission.
00049  * 
00050  * THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY OF WASHINGTON
00051  * AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
00052  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00053  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
00054  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY
00055  * OF WASHINGTON OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
00056  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00057  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00058  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00059  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
00060  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00061  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00062  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
00063  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00064  * 
00065  */
00066 #ifndef STRFIFOLIST_H_JUN_26_2005
00067 #define STRFIFOLIST_H_JUN_26_2005
00068 
00069 #include <list>
00070 #include <ostream>
00071 using namespace std;
00072 
00073 
00074   // Stores the tokens returned by follex. The first token is the latest one.
00075 class StrFifoList
00076 {
00077  public: 
00078   StrFifoList() { fifoList_ = new list<const char*>; }
00079 
00080   ~StrFifoList() 
00081   {
00082     list<const char*>::iterator it = fifoList_->begin();
00083     for (; it != fifoList_->end(); it++)  delete [] (*it);
00084     delete fifoList_; 
00085   }
00086 
00087   
00088   void deleteContentsAndClear()
00089   {
00090     list<const char*>::iterator it = fifoList_->begin();
00091     for (; it != fifoList_->end(); it++)  delete [] (*it);
00092     fifoList_->clear();
00093   }
00094 
00095 
00096     // Caller is responsible for deleting str.
00097   void add(const char* const & str)
00098   {
00099     //commented out: don't impose a max
00100     //if (fifoList_->size() == STRFIFOLIST_MAXSIZE) fifoList_->pop_back();
00101     char* s = new char[strlen(str)+1];
00102     strcpy(s,str);
00103     fifoList_->push_front(s);
00104   }
00105 
00106 
00107   void addLast(const char* const & str)
00108   {
00109     //commented out: don't impose a max
00110     //if (fifoList_->size() == STRFIFOLIST_MAXSIZE) fifoList_->pop_back();
00111     char* s = new char[strlen(str)+1];
00112     strcpy(s,str);
00113     fifoList_->push_back(s);
00114   }
00115 
00116 
00117     // Caller should not delete returned const char*
00118   const char* getItem(const int& idx)
00119   {
00120     if ((unsigned int)idx > fifoList_->size()-1) return NULL;
00121     list<const char*>::iterator it = fifoList_->begin();
00122     int i = 0;
00123     for (; it != fifoList_->end(); it++)
00124       if (i++ == idx) return (*it);
00125     return NULL;
00126   }
00127   
00128 
00129   unsigned int getSize() const { return fifoList_->size(); }
00130 
00131     // the caller should not modify or delete the returned pointer
00132   const char* back() const { return fifoList_->back(); }
00133 
00134     // caller is responsible to deleting returned pointer
00135   const char* removeLast() 
00136   {
00137     const char* last = fifoList_->back();
00138     fifoList_->pop_back();
00139     return last;
00140   }
00141 
00142 
00143   ostream& print(ostream& out) const 
00144   {
00145     list<const char*>::iterator it = fifoList_->begin();
00146     for (; it != fifoList_->end(); it++)
00147       out << (*it) << " ";
00148     return out;
00149   } 
00150   
00151  private:
00152   list<const char*>* fifoList_;
00153   
00154 };
00155 
00156 
00157 inline
00158 ostream& operator<<(ostream& out, const StrFifoList& l) { return l.print(out); }
00159 
00160 
00161 #endif

Generated on Tue Jan 16 05:30:05 2007 for Alchemy by  doxygen 1.5.1