CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_4_1_8_patch13/src/CondCore/ORA/src/MultiIndexDataTrie.cc

Go to the documentation of this file.
00001 #include "CondCore/ORA/interface/Exception.h"
00002 #include "MultiIndexDataTrie.h"
00003 //
00004 #include <sstream>
00005 // externals 
00006 #include "CoralBase/AttributeList.h"
00007 
00008 ora::MultiIndexDataTrie::MultiIndexDataTrie():
00009   m_children(),
00010   m_data(){
00011   
00012 }
00013 
00014 ora::MultiIndexDataTrie::~MultiIndexDataTrie(){
00015   clear();
00016 }
00017 
00018 size_t ora::MultiIndexDataTrie::push( const std::vector<int>& indexes,
00019                                       Record & data ){
00020   size_t s=0;
00021   MultiIndexDataTrie* trie = this;
00022   for( size_t i=0;i<indexes.size();i++){
00023     size_t ts = trie->m_children.size();
00024     MultiIndexDataTrie* nt = 0;
00025     if( ts == 0 || indexes[i] > (int)(ts-1) ){
00026       for(size_t j=0;j<indexes[i]-ts+1;j++){
00027         ++s;
00028         nt = new MultiIndexDataTrie;
00029         trie->m_children.push_back(nt);
00030       }
00031     } else {
00032       nt = trie->m_children[indexes[i]];
00033       if( !nt ){
00034         std::stringstream mess;
00035         mess << "Slot for index["<<i<<"] is empty.";
00036         throwException( mess.str(),"MultiIndexDataTrie::push" );
00037       }
00038     }
00039     trie = nt;
00040   }
00041   trie->m_data.swap(data);
00042   return s;
00043 }
00044 
00069 void ora::MultiIndexDataTrie::lookupAndClear( const std::vector<int>& indexes, Record & rec ) {
00070   MultiIndexDataTrie* branch = this;
00071   MultiIndexDataTrie* trie = 0;
00072   size_t i=0;
00073   for( ;i<indexes.size();i++){
00074     if( branch->m_children.size()==0 || indexes[i] > (int)(branch->m_children.size()-1)){
00075       std::stringstream mess;
00076       mess << "Index["<<i<<"] is out of bound.";
00077       throwException( mess.str(),"MultiIndexDataTrie::lookup" );
00078     }
00079     trie = branch;
00080     branch = branch->m_children[indexes[i]];
00081     if( !branch ){
00082       std::stringstream mess;
00083       mess << "Slot for index["<<i<<"] is empty.";
00084       throwException( mess.str(),"MultiIndexDataTrie::lookupAndClear" );      
00085     }
00086   }
00087   MultiIndexDataTrie* leaf = trie->m_children[indexes[i-1]];
00088   if(0==leaf->m_data.size()){
00089     throwException( "No Data for the specified index combination.",
00090                     "MultiIndexDataTrie::lookupAndClear" );
00091   }
00092   rec.swap(leaf->m_data);
00093   delete leaf;
00094   trie->m_children[indexes[i-1]] = 0;
00095 }
00096 
00097 size_t ora::MultiIndexDataTrie::size() const {
00098   return m_children.size();
00099 }
00100 
00101 void ora::MultiIndexDataTrie::clear(){
00102   for(std::vector<MultiIndexDataTrie*>::iterator iT = m_children.begin();
00103       iT != m_children.end(); iT++){
00104     if(*iT) delete *iT;
00105   }
00106   m_children.clear();
00107   Record tmp; tmp.swap(m_data);
00108 }
00109 
00110 size_t ora::MultiIndexDataTrie::branchSize( const std::vector<int>& indexes, size_t depth ) const{
00111   if( depth > indexes.size() ) depth = indexes.size();
00112   const MultiIndexDataTrie* trie = this;
00113   for( size_t i=0;i<depth;i++){
00114     if( trie->m_children.size()==0 || indexes[i] > (int)(trie->m_children.size()-1)){
00115       std::stringstream mess;
00116       mess << "Index["<<i<<"] is out of bound.";
00117       throwException( mess.str(),"MultiIndexDataTrie::lookup" );
00118     }
00119     trie = trie->m_children[indexes[i]];
00120     if( !trie ){
00121       std::stringstream mess;
00122       mess << "Slot for index["<<i<<"] is empty.";
00123       throwException( mess.str(),"MultiIndexDataTrie::branchSize" );      
00124     }
00125   }
00126   return trie->m_children.size();  
00127 }
00128 
00129 size_t ora::MultiIndexDataTrie::totalSize() const {
00130   size_t sz = 0;
00131   for(std::vector<MultiIndexDataTrie*>::const_iterator iT = m_children.begin();
00132       iT != m_children.end(); iT++){
00133     sz++;
00134     if(*iT) sz += (*iT)->totalSize();
00135   }
00136   return sz;
00137 }
00138 
00139