hgcacheproxymodel/tsrc/unit/bmhelper.cpp
branchGCC_SURGE
changeset 10 c348b9772569
parent 8 15f034b8a3b5
equal deleted inserted replaced
4:463f1934e292 10:c348b9772569
    11 *
    11 *
    12 * Contributors:
    12 * Contributors:
    13 *
    13 *
    14 * Description:
    14 * Description:
    15 *
    15 *
    16 *  Version     : %version: 1 %
    16 *  Version     : %version: 6 %
    17 */
    17 */
    18 #include "bmhelper.h"
    18 #include "bmhelper.h"
    19 #include <QDebug>
    19 #include <QDebug>
    20 
    20 
    21 BMHelper::BMHelper(int totalSize)
    21 BMHelper::BMHelper(int totalSize)
    28 {
    28 {
    29 }
    29 }
    30 
    30 
    31 void BMHelper::release(int start, int end)
    31 void BMHelper::release(int start, int end)
    32 {
    32 {
       
    33     if ((start <0 && end <0) || (start >=mBuffer.size() && end >=mBuffer.size() ) )
       
    34         return;
       
    35     
    33     if ( start<0)
    36     if ( start<0)
    34         start = 0;
    37         start = 0;
    35     if (end>mBuffer.size() - 1)
    38     if (end>mBuffer.size())
    36         end = mBuffer.size() - 1;
    39         end = mBuffer.size();
       
    40     
       
    41     if ( start > end){
       
    42         int t = start;
       
    43         start = end;
       
    44         end = t;
       
    45     }
       
    46     
       
    47     if ( start<0)
       
    48         start = 0;
       
    49     if (end>=mBuffer.size())
       
    50         end = mBuffer.size()-1;
    37     
    51     
    38     for ( int i = start; i <= end; i++){
    52     for ( int i = start; i <= end; i++){
    39         if (mBuffer.value(i) == true){
    53         if (mBuffer.value(i) == true){
    40             mBuffer.replace(i, false);
    54             mBuffer.replace(i, false);
    41         } else {
    55         } else {
    45 }
    59 }
    46 
    60 
    47 void BMHelper::request(int start, int end, HgRequestOrder order)
    61 void BMHelper::request(int start, int end, HgRequestOrder order)
    48 {
    62 {
    49     Q_UNUSED(order);
    63     Q_UNUSED(order);
       
    64     
       
    65     if ((start <0 && end <0) || (start >=mBuffer.size() && end >=mBuffer.size() ) )
       
    66         return;
       
    67     
    50     if ( start<0)
    68     if ( start<0)
    51         start = 0;
    69         start = 0;
    52     if (end>mBuffer.size() - 1)
    70     if (end>=mBuffer.size())
    53         end = mBuffer.size() - 1;
    71         end = mBuffer.size()-1;
       
    72     
       
    73     if ( start > end){
       
    74         int t = start;
       
    75         start = end;
       
    76         end = t;
       
    77     }
       
    78     if ( start<0)
       
    79         start = 0;
       
    80     if (end>=mBuffer.size())
       
    81         end = mBuffer.size()-1;
       
    82     
    54     
    83     
    55     for ( int i = start; i <= end; i++){
    84     for ( int i = start; i <= end; i++){
    56         if (mBuffer.value(i) == false){
    85         if (mBuffer.value(i) == false){
    57             mBuffer.replace(i, true);
    86             mBuffer.replace(i, true);
    58         } else {
    87         } else {
    62 }
    91 }
    63 
    92 
    64 bool BMHelper::isIntergal(int bufferSize)
    93 bool BMHelper::isIntergal(int bufferSize)
    65 {
    94 {
    66     int c = mBuffer.count(true);
    95     int c = mBuffer.count(true);
    67     bool res = (bufferSize == c);
    96     bool res = (c == mBuffer.count())||(bufferSize == c);
       
    97     
       
    98     
    68     if (res){ ///check integrity ( if all items from first true, to size are true;
    99     if (res){ ///check integrity ( if all items from first true, to size are true;
    69         int f = mBuffer.indexOf(true);
   100         int f = mBuffer.indexOf(true);
    70         for ( int i =0; i < mBuffer.count(); i++){
   101         for ( int i =0; i < mBuffer.count(); i++){
    71             if (mBuffer[i] != (i>=f && i < f+bufferSize) ){
   102             if (mBuffer[i] != (i>=f && i < f+c) ){
    72                 res = false;
   103                 res = false;
    73                 break;
   104                 break;
    74             }
   105             }
    75         }
   106         }
    76     } else {
   107     } else {
    77         qWarning()<<QString("isIntergal mBuffer.count(true)=%1 bufferSize=%2").arg(c).arg(bufferSize);
   108         QString arr;
       
   109         QString item = "%1,";        
       
   110         for ( int i =0; i < mBuffer.count(); i++){
       
   111             if(mBuffer[i]){
       
   112                 arr+=item.arg(i);
       
   113             }
       
   114         }
       
   115         qWarning()<<QString("isIntergal mBuffer.count(true)=%1 bufferSize=%2 visible:%3").arg(c).arg(bufferSize).arg(arr);
    78     }
   116     }
    79     
   117     
    80     return res;
   118     return res;
    81 }
   119 }
       
   120 int BMHelper::totalSize()
       
   121 {
       
   122     return mBuffer.count();
       
   123 }
    82 
   124 
    83 void BMHelper::itemCountChanged( int aIndex, bool aRemoved, int aNewTotalCount )
   125 void BMHelper::resizeCache(int newSize)
    84 {
   126 {
    85     Q_UNUSED(aRemoved);
   127     int diff = totalSize() - newSize;
    86     
   128     
    87     if ( aIndex < 0)
   129     while (diff != 0){
    88         aIndex = 0;
   130         if (diff >0){
    89     if ( aIndex > mBuffer.count())
   131             remove(mBuffer.count()-1);
    90         aIndex = mBuffer.count()-1;
   132             diff--;
    91     
   133         }else{
    92     if ((mBuffer.count() - aNewTotalCount)>0){
   134             insert(mBuffer.count());
    93         while (mBuffer.count()!=aNewTotalCount){
   135             diff++;
    94             if (aIndex > mBuffer.count() )
       
    95                 aIndex = mBuffer.count() -1;
       
    96             mBuffer.removeAt(aIndex);
       
    97         }
       
    98     } else if ((mBuffer.count() - aNewTotalCount)<0){
       
    99         while (mBuffer.count()!=aNewTotalCount){
       
   100             mBuffer.insert(aIndex, false);
       
   101         }
   136         }
   102     }
   137     }
       
   138     
   103 }
   139 }
       
   140 
       
   141 void BMHelper::remove(int pos)
       
   142 {
       
   143     if ( pos <0 || pos > mBuffer.count()){
       
   144         return;
       
   145     }
       
   146     mBuffer.removeAt(pos);    
       
   147 }
       
   148 
       
   149 void BMHelper::insert(int pos)
       
   150 {
       
   151     if ( pos <0 || pos > mBuffer.count()){
       
   152         return;
       
   153     }    
       
   154     mBuffer.insert(pos, false);
       
   155 }