Home | History | Annotate | Download | only in slm
      1   0  yongsun /*
      2  82  yongsun  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
      3  82  yongsun  *
      4  82  yongsun  * Copyright (c) 2007 Sun Microsystems, Inc. All Rights Reserved.
      5  82  yongsun  *
      6  82  yongsun  * The contents of this file are subject to the terms of either the GNU Lesser
      7  82  yongsun  * General Public License Version 2.1 only ("LGPL") or the Common Development and
      8  82  yongsun  * Distribution License ("CDDL")(collectively, the "License"). You may not use this
      9  82  yongsun  * file except in compliance with the License. You can obtain a copy of the CDDL at
     10  82  yongsun  * http://www.opensource.org/licenses/cddl1.php and a copy of the LGPLv2.1 at
     11  82  yongsun  * http://www.opensource.org/licenses/lgpl-license.php. See the License for the
     12  82  yongsun  * specific language governing permissions and limitations under the License. When
     13  82  yongsun  * distributing the software, include this License Header Notice in each file and
     14  82  yongsun  * include the full text of the License in the License file as well as the
     15  82  yongsun  * following notice:
     16  82  yongsun  *
     17  82  yongsun  * NOTICE PURSUANT TO SECTION 9 OF THE COMMON DEVELOPMENT AND DISTRIBUTION LICENSE
     18  82  yongsun  * (CDDL)
     19  82  yongsun  * For Covered Software in this distribution, this License shall be governed by the
     20  82  yongsun  * laws of the State of California (excluding conflict-of-law provisions).
     21  82  yongsun  * Any litigation relating to this License shall be subject to the jurisdiction of
     22  82  yongsun  * the Federal Courts of the Northern District of California and the state courts
     23  82  yongsun  * of the State of California, with venue lying in Santa Clara County, California.
     24  82  yongsun  *
     25  82  yongsun  * Contributor(s):
     26  82  yongsun  *
     27  82  yongsun  * If you wish your version of this file to be governed by only the CDDL or only
     28  82  yongsun  * the LGPL Version 2.1, indicate your decision by adding "[Contributor]" elects to
     29  82  yongsun  * include this software in this distribution under the [CDDL or LGPL Version 2.1]
     30  82  yongsun  * license." If you don't indicate a single choice of license, a recipient has the
     31  82  yongsun  * option to distribute your version of this file under either the CDDL or the LGPL
     32  82  yongsun  * Version 2.1, or to extend the choice of license to its licensees as provided
     33  82  yongsun  * above. However, if you add LGPL Version 2.1 code and therefore, elected the LGPL
     34  82  yongsun  * Version 2 license, then the option applies only if the new code is made subject
     35  82  yongsun  * to such option by the copyright holder.
     36   0  yongsun  */
     37  82  yongsun 
     38   0  yongsun #ifndef _SIM_FILE_MERGE_H
     39   0  yongsun #define _SIM_FILE_MERGE_H
     40   0  yongsun 
     41   0  yongsun #include "../portability.h"
     42   0  yongsun 
     43   0  yongsun #include <stdio.h>
     44   0  yongsun #include <deque>
     45   0  yongsun #include <vector>
     46   0  yongsun #include <algorithm>
     47   0  yongsun 
     48   0  yongsun template<class unit_type>
     49   0  yongsun class TUnitAndParaInfo {
     50   0  yongsun public:
     51   0  yongsun     typedef unit_type TUnit;
     52   0  yongsun     TUnitAndParaInfo() : unit(), runOut(false) {}
     53   0  yongsun public:
     54   0  yongsun     TUnit unit;
     55   0  yongsun     bool runOut;
     56   0  yongsun };
     57   0  yongsun 
     58   0  yongsun template<class unit_type>
     59   0  yongsun class file_para
     60   0  yongsun {
     61   0  yongsun public:
     62   0  yongsun     typedef unit_type TUnit;
     63   0  yongsun     typedef TUnitAndParaInfo<TUnit> UnitAndParaInfo;
     64   0  yongsun     typedef std::deque<UnitAndParaInfo> TItemBuf;
     65   0  yongsun     typedef typename TItemBuf::iterator TIBIterator;
     66   0  yongsun     typedef typename TItemBuf::const_iterator TIBConstIterator;
     67   0  yongsun 
     68   0  yongsun     file_para(FILE* p_file, size_t start, size_t end)
     69   0  yongsun         : fp(p_file), cur_offset(start), last_offset(end), runOut(false), buf() {}
     70   0  yongsun 
     71   0  yongsun     UnitAndParaInfo& operator*()
     72   0  yongsun         {
     73   0  yongsun             if (buf.size() == 0) {
     74   0  yongsun                 for (int i=0; i < BUF_SIZE; ++i)
     75   0  yongsun                 {
     76   0  yongsun                     buf.push_back(UnitAndParaInfo());
     77   0  yongsun                     UnitAndParaInfo& e = buf.back();
     78   0  yongsun                     e.runOut = runOut = !(e.unit.read(fp, cur_offset, last_offset));
     79   0  yongsun                     if (runOut) break;
     80   0  yongsun                 }
     81   0  yongsun             }
     82   0  yongsun             return buf.front();
     83   0  yongsun         }
     84   0  yongsun 
     85   0  yongsun     file_para& operator++()
     86   0  yongsun         {
     87   0  yongsun             if (buf.size() == 0)
     88   0  yongsun                 this->operator*();
     89   0  yongsun             buf.pop_front();
     90   0  yongsun             return *this;
     91   0  yongsun         }
     92   0  yongsun 
     93   0  yongsun     bool operator< (file_para& another)
     94   0  yongsun         {
     95   0  yongsun             const UnitAndParaInfo& me = this->operator*();
     96   0  yongsun             const UnitAndParaInfo& you = *another;
     97   0  yongsun             if (me.runOut){
     98   0  yongsun                 if (you.runOut)
     99   0  yongsun                     return ((char*)&me < (char*)&you);
    100   0  yongsun                 else
    101   0  yongsun                     return true;
    102   0  yongsun             } else {
    103   0  yongsun                 if (you.runOut)
    104   0  yongsun                     return false;
    105   0  yongsun                 else {
    106   0  yongsun                     if (me.unit > you.unit) return true;
    107   0  yongsun                     if (me.unit == you.unit) return ((char*)&me < (char*)&you);
    108   0  yongsun                     return false;
    109   0  yongsun                 }
    110   0  yongsun             }
    111   0  yongsun         }
    112   0  yongsun 
    113   0  yongsun private:
    114   0  yongsun     static const int BUF_SIZE=1024;
    115   0  yongsun     FILE * fp;
    116   0  yongsun     bool runOut;
    117   0  yongsun     size_t cur_offset, last_offset;
    118   0  yongsun     TItemBuf buf;
    119   0  yongsun };
    120   0  yongsun 
    121   0  yongsun template <class PPara>
    122   0  yongsun class PtrCompare {
    123   0  yongsun public:
    124   0  yongsun     bool operator()(const PPara& p1, const PPara& p2)
    125   0  yongsun         { return (*p1 < *p2); }
    126   0  yongsun };
    127   0  yongsun 
    128   0  yongsun template<class unit_type>
    129   0  yongsun class CMultiWayFileMerger
    130   0  yongsun {
    131   0  yongsun public:
    132   0  yongsun     typedef file_para<unit_type> TPara;
    133   0  yongsun     typedef std::vector<TPara*> TParaVec;
    134   0  yongsun 
    135   0  yongsun     void addPara(FILE *fp, size_t first_offset, size_t last_offset)
    136   0  yongsun         {
    137   0  yongsun             paras.push_back(new TPara(fp, first_offset, last_offset));
    138   0  yongsun         }
    139   0  yongsun     void start()
    140   0  yongsun         {
    141   0  yongsun             std::make_heap(paras.begin(), paras.end(), PtrCompare<TPara*>());
    142   0  yongsun         }
    143   0  yongsun     TPara* getBest() //You then have to deal same items form different part
    144   0  yongsun         {
    145   0  yongsun             std::pop_heap(paras.begin(), paras.end(), PtrCompare<TPara*>());
    146   0  yongsun             return paras.back();
    147   0  yongsun         }
    148   0  yongsun     void next()
    149   0  yongsun         {
    150   0  yongsun             ++(*(paras.back()));
    151   0  yongsun             std::push_heap(paras.begin(), paras.end(), PtrCompare<TPara*>());
    152   0  yongsun         }
    153   0  yongsun     ~CMultiWayFileMerger() { for (int i=0; i < paras.size(); ++i) delete paras[i]; }
    154   0  yongsun private:
    155   0  yongsun     TParaVec paras;
    156   0  yongsun };
    157   0  yongsun 
    158   0  yongsun #endif
    159