Back to home page

EIC code displayed by LXR

 
 

    


Warning, /include/Geant4/tools/wroot/free_seg is written in an unsupported language. File is not indexed.

0001 // Copyright (C) 2010, Guy Barrand. All rights reserved.
0002 // See the file tools.license for terms.
0003 
0004 #ifndef tools_wroot_free_seg
0005 #define tools_wroot_free_seg
0006 
0007 #include "seek"
0008 #include "wbuf"
0009 #include "../forit"
0010 
0011 #include <ostream>
0012 
0013 namespace tools {
0014 namespace wroot {
0015 
0016 class free_seg {
0017 public:
0018   free_seg(std::ostream& a_out,seek a_first,seek a_last)
0019   :m_out(a_out),m_first(a_first),m_last(a_last){}
0020   virtual ~free_seg(){}
0021 public:
0022   free_seg(const free_seg& a_from)
0023   :m_out(a_from.m_out),m_first(a_from.m_first),m_last(a_from.m_last)
0024   {}
0025   free_seg& operator=(const free_seg& a_from){
0026     m_first = a_from.m_first;
0027     m_last = a_from.m_last;
0028     return *this;
0029   }
0030 public:
0031   std::ostream& out() const {return m_out;}
0032 
0033   seek first() const {return m_first;}
0034   seek last() const {return m_last;}
0035 
0036   void set_first(seek a_v) {m_first = a_v;}
0037   void set_last(seek a_v) {m_last = a_v;}
0038 
0039   unsigned int record_size() const {
0040     //GB if(fLast>RIO_START_BIG_FILE) {
0041     if((m_first>START_BIG_FILE())|| //GB
0042        (m_last>START_BIG_FILE()) ){
0043       return sizeof(short) +  2 * sizeof(seek);
0044     } else {
0045       return sizeof(short) +  2 * sizeof(seek32);
0046     }
0047   }
0048 
0049   bool fill_buffer(wbuf& a_wb) {
0050     short version = 1;
0051 
0052     //GB if(fLast>START_BIG_FILE()) version += big_file_version_tag();
0053     if((m_first>START_BIG_FILE())||
0054        (m_last>START_BIG_FILE())) version += big_file_version_tag();
0055 
0056     if(!a_wb.write(version)) return false;
0057 
0058     if(version>(short)big_file_version_tag()) {
0059       if(!a_wb.write(m_first)) return false;
0060       if(!a_wb.write(m_last)) return false;
0061     } else {
0062       if(m_first>START_BIG_FILE()) { //GB
0063         m_out << "tools::wroot::free_seg::fill_buffer :"
0064              << " attempt to write big Seek "
0065              << m_first << " on 32 bits."
0066              << std::endl;
0067         return false;
0068       }
0069       if(!a_wb.write((seek32)m_first)) return false;
0070       if(m_last>START_BIG_FILE()) { //GB
0071         m_out << "tools::wroot::free_seg::fill_buffer :"
0072              << " attempt to write big seek "
0073              << m_last << " on 32 bits."
0074              << std::endl;
0075         return false;
0076       }
0077       if(!a_wb.write((seek32)m_last)) return false;
0078     }
0079 
0080     return true;
0081   }
0082 
0083 protected:
0084   std::ostream& m_out;
0085   seek m_first;  //First free word of segment
0086   seek m_last;   //Last free word of segment
0087 };
0088 
0089 }}
0090 
0091 #include <list>
0092 
0093 namespace tools {
0094 namespace wroot {
0095 
0096 inline free_seg* find_after(const std::list<free_seg*>& a_list,free_seg* a_what) {
0097   tools_lforcit(free_seg*,a_list,it) {
0098     if((*it)==a_what) {
0099       it++;
0100       if(it==a_list.end()) return 0;
0101       return *it;
0102     }
0103   }
0104   return 0;
0105 }
0106 
0107 inline void remove(std::list<free_seg*>& a_list,free_seg* a_what) {
0108   //NOTE : it does not delete a_what.
0109   tools_lforit(free_seg*,a_list,it) {
0110     if((*it)==a_what) {
0111       a_list.erase(it);
0112       return;
0113     }
0114   }
0115 }
0116 
0117 inline void add_before(std::list<free_seg*>& a_list,free_seg* a_what,free_seg* a_new) {
0118   tools_lforit(free_seg*,a_list,it) {
0119     if((*it)==a_what) {
0120       a_list.insert(it,a_new);
0121       return;
0122     }
0123   }
0124 }
0125 
0126 inline free_seg* add_free(std::list<free_seg*>& a_list,seek a_first,seek a_last) {
0127   // Add a new free segment to the list of free segments
0128   // ===================================================
0129   //  If last just preceedes an existing free segment, then first becomes
0130   //     the new starting location of the free segment.
0131   //  if first just follows an existing free segment, then last becomes
0132   //     the new ending location of the free segment.
0133   //  if first just follows an existing free segment AND last just preceedes
0134   //     an existing free segment, these two segments are merged into
0135   //     one single segment.
0136   //
0137 
0138   free_seg* idcur = a_list.front();
0139 
0140   while (idcur) {
0141     seek curfirst = idcur->first();
0142     seek curlast  = idcur->last();
0143     if (curlast == (a_first-1)) {
0144       idcur->set_last(a_last);
0145       free_seg* idnext = find_after(a_list,idcur);
0146       if (idnext == 0) return idcur;
0147       if (idnext->first() > (a_last+1)) return idcur;
0148       idcur->set_last(idnext->last());
0149       remove(a_list,idnext); //idnext not deleted.
0150       delete idnext;
0151       return idcur;
0152     }
0153     if (curfirst == (a_last+1)) {
0154       idcur->set_first(a_first);
0155       return idcur;
0156     }
0157     if (a_first < curfirst) {
0158       free_seg* newfree = new free_seg(idcur->out(),a_first,a_last);
0159       add_before(a_list,idcur,newfree);
0160       return newfree;
0161     }
0162     idcur = find_after(a_list,idcur);
0163   }
0164 
0165   return 0;
0166 }
0167 
0168 
0169 }}
0170 
0171 #endif