hgcacheproxymodel/tsrc/unit/bmhelper.cpp
changeset 3 c863538fcbb6
parent 2 49c70dcc3f17
child 6 1cdcc61142d2
--- a/hgcacheproxymodel/tsrc/unit/bmhelper.cpp	Fri May 14 16:57:01 2010 +0300
+++ b/hgcacheproxymodel/tsrc/unit/bmhelper.cpp	Thu May 27 13:59:05 2010 +0300
@@ -13,7 +13,7 @@
 *
 * Description:
 *
-*  Version     : %version: 1 %
+*  Version     : %version: 5 %
 */
 #include "bmhelper.h"
 #include <QDebug>
@@ -30,10 +30,24 @@
 
 void BMHelper::release(int start, int end)
 {
+    if ((start <0 && end <0) || (start >=mBuffer.size() && end >=mBuffer.size() ) )
+        return;
+    
     if ( start<0)
         start = 0;
-    if (end>mBuffer.size() - 1)
-        end = mBuffer.size() - 1;
+    if (end>mBuffer.size())
+        end = mBuffer.size();
+    
+    if ( start > end){
+        int t = start;
+        start = end;
+        end = t;
+    }
+    
+    if ( start<0)
+        start = 0;
+    if (end>=mBuffer.size())
+        end = mBuffer.size()-1;
     
     for ( int i = start; i <= end; i++){
         if (mBuffer.value(i) == true){
@@ -47,10 +61,25 @@
 void BMHelper::request(int start, int end, HgRequestOrder order)
 {
     Q_UNUSED(order);
+    
+    if ((start <0 && end <0) || (start >=mBuffer.size() && end >=mBuffer.size() ) )
+        return;
+    
     if ( start<0)
         start = 0;
-    if (end>mBuffer.size() - 1)
-        end = mBuffer.size() - 1;
+    if (end>=mBuffer.size())
+        end = mBuffer.size()-1;
+    
+    if ( start > end){
+        int t = start;
+        start = end;
+        end = t;
+    }
+    if ( start<0)
+        start = 0;
+    if (end>=mBuffer.size())
+        end = mBuffer.size()-1;
+    
     
     for ( int i = start; i <= end; i++){
         if (mBuffer.value(i) == false){
@@ -64,40 +93,47 @@
 bool BMHelper::isIntergal(int bufferSize)
 {
     int c = mBuffer.count(true);
-    bool res = (bufferSize == c);
+    bool res = (c == mBuffer.count())||(bufferSize == c);
+    
+    
     if (res){ ///check integrity ( if all items from first true, to size are true;
         int f = mBuffer.indexOf(true);
         for ( int i =0; i < mBuffer.count(); i++){
-            if (mBuffer[i] != (i>=f && i < f+bufferSize) ){
+            if (mBuffer[i] != (i>=f && i < f+c) ){
                 res = false;
                 break;
             }
         }
     } else {
-        qWarning()<<QString("isIntergal mBuffer.count(true)=%1 bufferSize=%2").arg(c).arg(bufferSize);
+        QString arr;
+        QString item = "%1,";        
+        for ( int i =0; i < mBuffer.count(); i++){
+            if(mBuffer[i]){
+                arr+=item.arg(i);
+            }
+        }
+        qWarning()<<QString("isIntergal mBuffer.count(true)=%1 bufferSize=%2 visible:%3").arg(c).arg(bufferSize).arg(arr);
     }
     
     return res;
 }
+int BMHelper::totalSize()
+{
+    return mBuffer.count();
+}
 
-void BMHelper::itemCountChanged( int aIndex, bool aRemoved, int aNewTotalCount )
+void BMHelper::remove(int pos)
 {
-    Q_UNUSED(aRemoved);
-    
-    if ( aIndex < 0)
-        aIndex = 0;
-    if ( aIndex > mBuffer.count())
-        aIndex = mBuffer.count()-1;
-    
-    if ((mBuffer.count() - aNewTotalCount)>0){
-        while (mBuffer.count()!=aNewTotalCount){
-            if (aIndex > mBuffer.count() )
-                aIndex = mBuffer.count() -1;
-            mBuffer.removeAt(aIndex);
-        }
-    } else if ((mBuffer.count() - aNewTotalCount)<0){
-        while (mBuffer.count()!=aNewTotalCount){
-            mBuffer.insert(aIndex, false);
-        }
+    if ( pos <0 || pos > mBuffer.count()){
+        return;
     }
+    mBuffer.removeAt(pos);    
 }
+
+void BMHelper::insert(int pos)
+{
+    if ( pos <0 || pos > mBuffer.count()){
+        return;
+    }    
+    mBuffer.insert(pos, false);
+}