Go to the documentation of this file.00001 #include "CondCore/ORA/interface/Exception.h"
00002 #include "MultiIndexDataTrie.h"
00003
00004 #include <sstream>
00005
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::lookupAndClear" );
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( indexes[i]+1 > (int)(trie->m_children.size())){
00115
00116 if( i+2>=indexes.size()) return 0;
00117
00118 std::stringstream mess;
00119 mess << "1 Index["<<i<<"] is out of bound.";
00120 throwException( mess.str(),"MultiIndexDataTrie::branchSize" );
00121 }
00122 trie = trie->m_children[indexes[i]];
00123 if( !trie ){
00124 std::stringstream mess;
00125 mess << "Slot for index["<<i<<"] is empty.";
00126 throwException( mess.str(),"MultiIndexDataTrie::branchSize" );
00127 }
00128 }
00129 return trie->m_children.size();
00130 }
00131
00132 size_t ora::MultiIndexDataTrie::totalSize() const {
00133 size_t sz = 0;
00134 for(std::vector<MultiIndexDataTrie*>::const_iterator iT = m_children.begin();
00135 iT != m_children.end(); iT++){
00136 sz++;
00137 if(*iT) sz += (*iT)->totalSize();
00138 }
00139 return sz;
00140 }
00141
00142