arraysaccessor.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 ARRAYSACCESSOR_H_OCT_17_2005
00067 #define ARRAYSACCESSOR_H_OCT_17_2005
00068 
00069 #include "array.h"
00070 
00071 
00072 template <typename Type>
00073 class ArraysAccessor
00074 {
00075  public:
00076   ArraysAccessor() : arrays_(new Array<const Array<Type>*>), 
00077                      indexes_(new Array<int>), 
00078                      itemIdx_(0), freeze_(false), reset_(false), noComb_(true){}
00079 
00080   ~ArraysAccessor() { delete arrays_; delete indexes_; }
00081 
00082 
00083   void appendArray(const Array<Type>* const & arr) 
00084   {
00085     assert(!freeze_);
00086     if (arrays_->size() == 0)
00087     {
00088       if (arr->size() == 0) noComb_ = true;
00089       else                  noComb_ = false;
00090     }
00091     else
00092     {
00093       if (arr->size() == 0) noComb_ = true;
00094     }
00095     arrays_->append(arr);
00096   }
00097 
00098   
00099   const Array<Type>* getArray(const int& idx) const { return (*arrays_)[idx]; }
00100 
00101   int getNumArrays() const { return arrays_->size();}
00102 
00103 
00104   void clear() 
00105   { 
00106     arrays_->clear(); 
00107     indexes_->clear(); 
00108     itemIdx_ = 0;
00109     freeze_ = false; 
00110     noComb_ = true;
00111   }
00112   
00113 
00114   void deleteArraysAndClear()
00115   {
00116     for (int i = 0; i < arrays_->size(); i++)
00117       delete (*arrays_)[i];
00118     clear();
00119   }
00120 
00121 
00122     // Start from first combination again.
00123   void reset()  { reset_ = true; }
00124 
00125   
00126     // returns false if there are no more elements
00127     // a contains the next combination of elements in the arrays that were added
00128   bool getNextCombination(Array<Type>& itemArr, Array<int>* idxArr=NULL)
00129   {
00130     if (noComb_) return false;
00131     if (!freeze_) prepareAccess();
00132     if (reset_) { reset_ = false; prepareAccess(); }
00133     if ((*indexes_)[0] < 0) return false;
00134     itemArr.clear();
00135     if (idxArr)  idxArr->clear();
00136     for (int i = 0; i < arrays_->size(); i++)
00137     {
00138       itemArr.append( (*((*arrays_)[i]))[ (*indexes_)[i] ] );
00139       if (idxArr) idxArr->append((*indexes_)[i]);
00140     }
00141 
00142     for (int i = arrays_->size()-1; i >= 0; i--)
00143     {
00144       (*indexes_)[i]++;
00145       if ((*indexes_)[i] < (*arrays_)[i]->size()) return true;
00146       (*indexes_)[i] = 0;
00147     }
00148     (*indexes_)[0] = -1;
00149     return true;    
00150   }
00151   
00152 
00153   int numItemsInCombination() const { return (*arrays_)[0]->size(); }
00154 
00155   
00156   bool hasNextCombination()
00157   {
00158     if (noComb_) return false;
00159     if (!freeze_) prepareAccess();
00160     if (reset_) { reset_ = false; prepareAccess(); }
00161     if ((*indexes_)[0] < 0) return false;
00162     itemIdx_ = 0;
00163     return true;
00164   }
00165 
00166 
00167   bool nextItemInCombination(Type& item, int&idx)
00168   {
00169     if (itemIdx_ >= indexes_->size()) 
00170     {
00171       for (int i = arrays_->size()-1; i >= 0; i--)
00172       {
00173         (*indexes_)[i]++;
00174         if ((*indexes_)[i] < (*arrays_)[i]->size()) return false;
00175         (*indexes_)[i] = 0;
00176       }
00177       (*indexes_)[0] = -1;
00178       return false;
00179     }
00180 
00181     
00182     idx = (*indexes_)[itemIdx_];
00183     item = (*((*arrays_)[itemIdx_]))[idx];
00184     itemIdx_++;
00185     return true;
00186   }
00187 
00188 
00189   bool nextItemInCombination(Type& item) 
00190   {
00191     int i; 
00192     return nextItemInCombination(item,i);
00193   }
00194 
00195 
00196   int numCombinations() const
00197   {
00198     if (arrays_->size() == 0) return 0;
00199     int n = 1;
00200     for (int i = 0; i < arrays_->size(); i++)
00201       n *= (*arrays_)[i]->size();
00202     return n;    
00203   }
00204 
00205   
00206  private:
00207 
00208   void prepareAccess()
00209   {
00210     freeze_ = true;
00211     if (noComb_ || arrays_->size() == 0) return;
00212     indexes_->growToSize(arrays_->size());
00213     for (int i = 0; i < arrays_->size(); i++) (*indexes_)[i] = 0;
00214     //arrays_->compress();
00215     //indexes_->compress();
00216   }
00217 
00218  private:
00219 
00220     // the contents of arrays_ are not owned by ArraysAccessor;
00221   Array<const Array<Type>*>* arrays_;
00222   Array<int>* indexes_;
00223   int itemIdx_;
00224   bool freeze_;
00225   bool reset_;
00226   bool noComb_;
00227 };
00228 
00229 
00230 #endif

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