hashlist.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 HASHLIST_H_JUL_20_2005
00067 #define HASHLIST_H_JUL_20_2005
00068 
00069 #include <list>
00070 #include <ext/hash_map>
00071 using namespace __gnu_cxx;
00072 
00073 
00074   // A list that is backed up by a hash_map
00075 template <typename Type, class HashFn, class EqualFn> 
00076 class HashList
00077 {
00078  public:
00079   HashList() 
00080     : map_(new hash_map<Type, typename list<Type>::iterator, HashFn, EqualFn>),
00081       list_(new list<Type>) {}
00082   ~HashList() { delete map_; delete list_; }
00083 
00084 
00085   Type& front() { return list_->front(); }
00086   Type& back( ) { return list_->back();  }
00087 
00088   typename list<Type>::iterator begin() { return list_->begin(); }
00089   typename list<Type>::iterator end()   { return list_->end(); }
00090   typename list<Type>::reverse_iterator rbegin() { return list_->rbegin(); }
00091   typename list<Type>::reverse_iterator rend()   { return list_->rend(); }
00092 
00093     // the list and its contents should not be modified
00094   const list<Type>* getList() const { return list_; }
00095 
00096   bool contains(const Type& val) const  
00097   { return (map_->find(val) != map_->end()); }
00098 
00099 
00100   Type* find(const Type& val) const
00101   {
00102     typename 
00103       hash_map<Type, typename list<Type>::iterator, HashFn, EqualFn>::iterator  
00104       mit = map_->find(val);
00105     
00106     if (mit == map_->end()) return NULL;
00107     return (Type*)(&((*mit).first));
00108   }
00109 
00110 
00111   typename list<Type>::iterator findListIterator(const Type& val) const
00112   {
00113     typename 
00114       hash_map<Type, typename list<Type>::iterator, HashFn, EqualFn>::iterator  
00115       mit = map_->find(val);
00116     
00117     if (mit == map_->end()) return NULL;
00118     return (Type*)(&((*mit).first));
00119   }
00120 
00121 
00122   void clear() { map_->clear(); list_->clear(); }
00123 
00124 
00125   void deleteContentsAndClear()
00126   {    
00127     typename list<Type>::iterator it = list_->begin();
00128     for (; it != list_->end(); it++) delete *it;
00129     map_->clear();
00130     list_->clear();
00131     assert(map_->size() == list_->size());
00132   }
00133 
00134 
00135   bool empty() const { return list_->empty(); }
00136 
00137 
00138     // returns true if val is in HashList and is erased; otherwise returns false
00139   bool erase(const Type& val, const bool& deleteVal=false)
00140   {
00141     typename 
00142       hash_map<Type, typename list<Type>::iterator, HashFn, EqualFn>::iterator  
00143       mit = map_->find(val);
00144 
00145     if (mit == map_->end()) return false;
00146     typename list<Type>::iterator lit = (*mit).second;
00147     list_->erase(lit);
00148     map_->erase(mit);
00149     assert(map_->size() == list_->size());
00150     if (deleteVal) delete *lit;
00151     return true;
00152   }
00153 
00154 
00155   bool eraseAndDelete(const Type& val)  { return erase(val, true); }
00156 
00157 
00158     // returns true if the val is inserted, returns false otherwise
00159   bool insert(typename list<Type>::iterator loc, const Type& val)
00160   {
00161     if (contains(val)) return false;
00162     list_->insert(loc, val);
00163     (*map_)[val] = --loc;
00164     assert(map_->size() == list_->size());
00165     return true;
00166   }
00167   
00168 
00169   int maxSize() const 
00170   {
00171     if (map_->max_size() < list_->max_size()) return map_->max_size();
00172     return list_->max_size();
00173   }
00174 
00175 
00176   void popBack()
00177   {
00178     Type& val = list_->back();
00179     list_->pop_back();
00180     typename 
00181       hash_map<Type,typename list<Type>::iterator, HashFn, EqualFn>::iterator 
00182       mit = map_->find(val);
00183     assert(mit != map_->end());
00184     map_->erase(mit);
00185     assert(map_->size() == list_->size());
00186   }
00187 
00188 
00189   void popFront()
00190   {
00191     Type& val = list_->front();
00192     list_->pop_front();
00193     typename 
00194       hash_map<Type,typename list<Type>::iterator, HashFn, EqualFn>::iterator 
00195       mit = map_->find(val);
00196     assert(mit != map_->end());
00197     map_->erase(mit);
00198     assert(map_->size() == list_->size());
00199   }
00200 
00201 
00202   bool pushBack(const Type& val)
00203   {
00204     if (contains(val)) return false;
00205     list_->push_back(val);
00206     typename list<Type>::iterator lit = list_->end();
00207     (*map_)[val] = --lit;
00208     assert(map_->size() == list_->size());
00209     return true;
00210   }
00211 
00212 
00213   bool pushFront(const Type& val)
00214   {
00215     if (contains(val)) return false;
00216     list_->push_front(val);
00217     (*map_)[val] = list_->begin();
00218     assert(map_->size() == list_->size());
00219     return true;
00220   }
00221   
00222   
00223   void reverse() { list_->reverse(); }
00224 
00225   int size() { assert(map_->size() == list_->size()); return list_->size(); }
00226 
00227 
00228  private:
00229     // note that there are two copies of Type, one in map_ and one in list_
00230   hash_map<Type, typename list<Type>::iterator, HashFn, EqualFn>* map_;
00231   list<Type>* list_;
00232 };
00233 
00234 
00235 #endif

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